zend_long.h revision bc5811f3
18ee2a4a9SAnatol Belski/*
28ee2a4a9SAnatol Belski   +----------------------------------------------------------------------+
38ee2a4a9SAnatol Belski   | Zend Engine                                                          |
48ee2a4a9SAnatol Belski   +----------------------------------------------------------------------+
59e29f841SSammy Kaye Powers   | Copyright (c) 1998-2017 Zend Technologies Ltd. (http://www.zend.com) |
68ee2a4a9SAnatol Belski   +----------------------------------------------------------------------+
78ee2a4a9SAnatol Belski   | This source file is subject to version 2.00 of the Zend license,     |
8b7a7b1a6SStanislav Malyshev   | that is bundled with this package in the file LICENSE, and is        |
98ee2a4a9SAnatol Belski   | available through the world-wide-web at the following url:           |
108ee2a4a9SAnatol Belski   | http://www.zend.com/license/2_00.txt.                                |
118ee2a4a9SAnatol Belski   | If you did not receive a copy of the Zend license and are unable to  |
128ee2a4a9SAnatol Belski   | obtain it through the world-wide-web, please send a note to          |
138ee2a4a9SAnatol Belski   | license@zend.com so we can mail you a copy immediately.              |
148ee2a4a9SAnatol Belski   +----------------------------------------------------------------------+
158ee2a4a9SAnatol Belski   | Authors: Anatol Belski <ab@php.net>                                  |
168ee2a4a9SAnatol Belski   +----------------------------------------------------------------------+
178ee2a4a9SAnatol Belski*/
188ee2a4a9SAnatol Belski
198ee2a4a9SAnatol Belski/* $Id$ */
208ee2a4a9SAnatol Belski
218ee2a4a9SAnatol Belski
224d997f63SAnatol Belski#ifndef ZEND_LONG_H
234d997f63SAnatol Belski#define ZEND_LONG_H
248ee2a4a9SAnatol Belski
25c3e3c98eSAnatol Belski#include "main/php_stdint.h"
268ee2a4a9SAnatol Belski
27baee5922SAnatol Belski/* This is the heart of the whole int64 enablement in zval. */
28f3dde293SNikita Popov#if defined(__x86_64__) || defined(__LP64__) || defined(_LP64) || defined(_WIN64)
295d062b61SAnatol Belski# define ZEND_ENABLE_ZVAL_LONG64 1
308ee2a4a9SAnatol Belski#endif
318ee2a4a9SAnatol Belski
32202e8db1SAnatol Belski/* Integer types. */
335d062b61SAnatol Belski#ifdef ZEND_ENABLE_ZVAL_LONG64
345d030000SAnatol Belskitypedef int64_t zend_long;
355d030000SAnatol Belskitypedef uint64_t zend_ulong;
365d030000SAnatol Belskitypedef int64_t zend_off_t;
375d030000SAnatol Belski# define ZEND_LONG_MAX INT64_MAX
385d030000SAnatol Belski# define ZEND_LONG_MIN INT64_MIN
395d030000SAnatol Belski# define ZEND_ULONG_MAX UINT64_MAX
4079c9dc75SAnatol Belski# define Z_L(i) INT64_C(i)
4179c9dc75SAnatol Belski# define Z_UL(i) UINT64_C(i)
4228b7a033SAnatol Belski# define SIZEOF_ZEND_LONG 8
438ee2a4a9SAnatol Belski#else
44c429c74bSAnatol Belskitypedef int32_t zend_long;
45c429c74bSAnatol Belskitypedef uint32_t zend_ulong;
46c429c74bSAnatol Belskitypedef int32_t zend_off_t;
47c429c74bSAnatol Belski# define ZEND_LONG_MAX INT32_MAX
48c429c74bSAnatol Belski# define ZEND_LONG_MIN INT32_MIN
49c429c74bSAnatol Belski# define ZEND_ULONG_MAX UINT32_MAX
50964e1174SNikita Popov# define Z_L(i) INT32_C(i)
51964e1174SNikita Popov# define Z_UL(i) UINT32_C(i)
525d030000SAnatol Belski# define SIZEOF_ZEND_LONG 4
538ee2a4a9SAnatol Belski#endif
548ee2a4a9SAnatol Belski
558ee2a4a9SAnatol Belski
56202e8db1SAnatol Belski/* Conversion macros. */
574d997f63SAnatol Belski#define ZEND_LTOA_BUF_LEN 65
588ee2a4a9SAnatol Belski
595d062b61SAnatol Belski#ifdef ZEND_ENABLE_ZVAL_LONG64
60c429c74bSAnatol Belski# define ZEND_LONG_FMT "%" PRId64
61c429c74bSAnatol Belski# define ZEND_ULONG_FMT "%" PRIu64
6216160386SDmitry Stogov# define ZEND_XLONG_FMT "%" PRIx64
63c429c74bSAnatol Belski# define ZEND_LONG_FMT_SPEC PRId64
64c429c74bSAnatol Belski# define ZEND_ULONG_FMT_SPEC PRIu64
65ce2cd892SKalle Sommer Nielsen# ifdef ZEND_WIN32
664d997f63SAnatol Belski#  define ZEND_LTOA(i, s, len) _i64toa_s((i), (s), (len), 10)
674d997f63SAnatol Belski#  define ZEND_ATOL(i, s) i = _atoi64((s))
684d997f63SAnatol Belski#  define ZEND_STRTOL(s0, s1, base) _strtoi64((s0), (s1), (base))
694d997f63SAnatol Belski#  define ZEND_STRTOUL(s0, s1, base) _strtoui64((s0), (s1), (base))
704d997f63SAnatol Belski#  define ZEND_STRTOL_PTR _strtoi64
714d997f63SAnatol Belski#  define ZEND_STRTOUL_PTR _strtoui64
728ee2a4a9SAnatol Belski#  define ZEND_ABS _abs64
738ee2a4a9SAnatol Belski# else
744d997f63SAnatol Belski#  define ZEND_LTOA(i, s, len) \
758ee2a4a9SAnatol Belski	do { \
76c429c74bSAnatol Belski		int st = snprintf((s), (len), ZEND_LONG_FMT, (i)); \
778ee2a4a9SAnatol Belski		(s)[st] = '\0'; \
788ee2a4a9SAnatol Belski 	} while (0)
794d997f63SAnatol Belski#  define ZEND_ATOL(i, s) (i) = atoll((s))
804d997f63SAnatol Belski#  define ZEND_STRTOL(s0, s1, base) strtoll((s0), (s1), (base))
814d997f63SAnatol Belski#  define ZEND_STRTOUL(s0, s1, base) strtoull((s0), (s1), (base))
824d997f63SAnatol Belski#  define ZEND_STRTOL_PTR strtoll
834d997f63SAnatol Belski#  define ZEND_STRTOUL_PTR strtoull
84d6930c9aSAnatol Belski#  define ZEND_ABS imaxabs
858ee2a4a9SAnatol Belski# endif
868ee2a4a9SAnatol Belski#else
874d997f63SAnatol Belski# define ZEND_STRTOL(s0, s1, base) strtol((s0), (s1), (base))
884d997f63SAnatol Belski# define ZEND_STRTOUL(s0, s1, base) strtoul((s0), (s1), (base))
89c429c74bSAnatol Belski# define ZEND_LONG_FMT "%" PRId32
90c429c74bSAnatol Belski# define ZEND_ULONG_FMT "%" PRIu32
9116160386SDmitry Stogov# define ZEND_XLONG_FMT "%" PRIx32
92c429c74bSAnatol Belski# define ZEND_LONG_FMT_SPEC PRId32
93c429c74bSAnatol Belski# define ZEND_ULONG_FMT_SPEC PRIu32
94ce2cd892SKalle Sommer Nielsen# ifdef ZEND_WIN32
954d997f63SAnatol Belski#  define ZEND_LTOA(i, s, len) _ltoa_s((i), (s), (len), 10)
964d997f63SAnatol Belski#  define ZEND_ATOL(i, s) i = atol((s))
978ee2a4a9SAnatol Belski# else
984d997f63SAnatol Belski#  define ZEND_LTOA(i, s, len) \
998ee2a4a9SAnatol Belski	do { \
100c429c74bSAnatol Belski		int st = snprintf((s), (len), ZEND_LONG_FMT, (i)); \
1018ee2a4a9SAnatol Belski		(s)[st] = '\0'; \
1028ee2a4a9SAnatol Belski 	} while (0)
1034d997f63SAnatol Belski#  define ZEND_ATOL(i, s) (i) = atol((s))
1048ee2a4a9SAnatol Belski# endif
1054d997f63SAnatol Belski# define ZEND_STRTOL_PTR strtol
1064d997f63SAnatol Belski# define ZEND_STRTOUL_PTR strtoul
1078ee2a4a9SAnatol Belski# define ZEND_ABS abs
1088ee2a4a9SAnatol Belski#endif
1098ee2a4a9SAnatol Belski
11061d37e05SDmitry Stogov#if SIZEOF_ZEND_LONG == 4
11161d37e05SDmitry Stogov# define MAX_LENGTH_OF_LONG 11
11261d37e05SDmitry Stogov# define LONG_MIN_DIGITS "2147483648"
11361d37e05SDmitry Stogov#elif SIZEOF_ZEND_LONG == 8
11461d37e05SDmitry Stogov# define MAX_LENGTH_OF_LONG 20
11561d37e05SDmitry Stogov# define LONG_MIN_DIGITS "9223372036854775808"
11661d37e05SDmitry Stogov#else
11761d37e05SDmitry Stogov# error "Unknown SIZEOF_ZEND_LONG"
11861d37e05SDmitry Stogov#endif
11961d37e05SDmitry Stogov
12061d37e05SDmitry Stogovstatic const char long_min_digits[] = LONG_MIN_DIGITS;
1218ee2a4a9SAnatol Belski
12216160386SDmitry Stogov#ifdef _WIN64
12316160386SDmitry Stogov# define ZEND_ADDR_FMT "0x%016I64x"
12416160386SDmitry Stogov#elif SIZEOF_SIZE_T == 4
12516160386SDmitry Stogov# define ZEND_ADDR_FMT "0x%08zx"
12616160386SDmitry Stogov#elif SIZEOF_SIZE_T == 8
12716160386SDmitry Stogov# define ZEND_ADDR_FMT "0x%016zx"
12816160386SDmitry Stogov#else
12916160386SDmitry Stogov# error "Unknown SIZEOF_SIZE_T"
13016160386SDmitry Stogov#endif
13116160386SDmitry Stogov
1324d997f63SAnatol Belski#endif /* ZEND_LONG_H */
1338ee2a4a9SAnatol Belski
1348ee2a4a9SAnatol Belski/*
1358ee2a4a9SAnatol Belski * Local variables:
1368ee2a4a9SAnatol Belski * tab-width: 4
1378ee2a4a9SAnatol Belski * c-basic-offset: 4
1388ee2a4a9SAnatol Belski * indent-tabs-mode: t
1398ee2a4a9SAnatol Belski * End:
140bc5811f3SAnatol Belski * vim600: sw=4 ts=4 fdm=marker
141bc5811f3SAnatol Belski * vim<600: sw=4 ts=4
1428ee2a4a9SAnatol Belski */
143