18ee2a4a9SAnatol Belski/*
28ee2a4a9SAnatol Belski   +----------------------------------------------------------------------+
38ee2a4a9SAnatol Belski   | Zend Engine                                                          |
48ee2a4a9SAnatol Belski   +----------------------------------------------------------------------+
5a6519d05SXinchen Hui   | Copyright (c) 1998-2018 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
194d997f63SAnatol Belski#ifndef ZEND_LONG_H
204d997f63SAnatol Belski#define ZEND_LONG_H
218ee2a4a9SAnatol Belski
22c3e3c98eSAnatol Belski#include "main/php_stdint.h"
238ee2a4a9SAnatol Belski
24baee5922SAnatol Belski/* This is the heart of the whole int64 enablement in zval. */
25f3dde293SNikita Popov#if defined(__x86_64__) || defined(__LP64__) || defined(_LP64) || defined(_WIN64)
265d062b61SAnatol Belski# define ZEND_ENABLE_ZVAL_LONG64 1
278ee2a4a9SAnatol Belski#endif
288ee2a4a9SAnatol Belski
29202e8db1SAnatol Belski/* Integer types. */
305d062b61SAnatol Belski#ifdef ZEND_ENABLE_ZVAL_LONG64
315d030000SAnatol Belskitypedef int64_t zend_long;
325d030000SAnatol Belskitypedef uint64_t zend_ulong;
335d030000SAnatol Belskitypedef int64_t zend_off_t;
345d030000SAnatol Belski# define ZEND_LONG_MAX INT64_MAX
355d030000SAnatol Belski# define ZEND_LONG_MIN INT64_MIN
365d030000SAnatol Belski# define ZEND_ULONG_MAX UINT64_MAX
3779c9dc75SAnatol Belski# define Z_L(i) INT64_C(i)
3879c9dc75SAnatol Belski# define Z_UL(i) UINT64_C(i)
3928b7a033SAnatol Belski# define SIZEOF_ZEND_LONG 8
408ee2a4a9SAnatol Belski#else
41c429c74bSAnatol Belskitypedef int32_t zend_long;
42c429c74bSAnatol Belskitypedef uint32_t zend_ulong;
43c429c74bSAnatol Belskitypedef int32_t zend_off_t;
44c429c74bSAnatol Belski# define ZEND_LONG_MAX INT32_MAX
45c429c74bSAnatol Belski# define ZEND_LONG_MIN INT32_MIN
46c429c74bSAnatol Belski# define ZEND_ULONG_MAX UINT32_MAX
47964e1174SNikita Popov# define Z_L(i) INT32_C(i)
48964e1174SNikita Popov# define Z_UL(i) UINT32_C(i)
495d030000SAnatol Belski# define SIZEOF_ZEND_LONG 4
508ee2a4a9SAnatol Belski#endif
518ee2a4a9SAnatol Belski
528ee2a4a9SAnatol Belski
53202e8db1SAnatol Belski/* Conversion macros. */
544d997f63SAnatol Belski#define ZEND_LTOA_BUF_LEN 65
558ee2a4a9SAnatol Belski
565d062b61SAnatol Belski#ifdef ZEND_ENABLE_ZVAL_LONG64
57c429c74bSAnatol Belski# define ZEND_LONG_FMT "%" PRId64
58c429c74bSAnatol Belski# define ZEND_ULONG_FMT "%" PRIu64
5916160386SDmitry Stogov# define ZEND_XLONG_FMT "%" PRIx64
60c429c74bSAnatol Belski# define ZEND_LONG_FMT_SPEC PRId64
61c429c74bSAnatol Belski# define ZEND_ULONG_FMT_SPEC PRIu64
62ce2cd892SKalle Sommer Nielsen# ifdef ZEND_WIN32
634d997f63SAnatol Belski#  define ZEND_LTOA(i, s, len) _i64toa_s((i), (s), (len), 10)
644d997f63SAnatol Belski#  define ZEND_ATOL(i, s) i = _atoi64((s))
654d997f63SAnatol Belski#  define ZEND_STRTOL(s0, s1, base) _strtoi64((s0), (s1), (base))
664d997f63SAnatol Belski#  define ZEND_STRTOUL(s0, s1, base) _strtoui64((s0), (s1), (base))
674d997f63SAnatol Belski#  define ZEND_STRTOL_PTR _strtoi64
684d997f63SAnatol Belski#  define ZEND_STRTOUL_PTR _strtoui64
698ee2a4a9SAnatol Belski#  define ZEND_ABS _abs64
708ee2a4a9SAnatol Belski# else
714d997f63SAnatol Belski#  define ZEND_LTOA(i, s, len) \
728ee2a4a9SAnatol Belski	do { \
73c429c74bSAnatol Belski		int st = snprintf((s), (len), ZEND_LONG_FMT, (i)); \
748ee2a4a9SAnatol Belski		(s)[st] = '\0'; \
758ee2a4a9SAnatol Belski 	} while (0)
764d997f63SAnatol Belski#  define ZEND_ATOL(i, s) (i) = atoll((s))
774d997f63SAnatol Belski#  define ZEND_STRTOL(s0, s1, base) strtoll((s0), (s1), (base))
784d997f63SAnatol Belski#  define ZEND_STRTOUL(s0, s1, base) strtoull((s0), (s1), (base))
794d997f63SAnatol Belski#  define ZEND_STRTOL_PTR strtoll
804d997f63SAnatol Belski#  define ZEND_STRTOUL_PTR strtoull
81d6930c9aSAnatol Belski#  define ZEND_ABS imaxabs
828ee2a4a9SAnatol Belski# endif
838ee2a4a9SAnatol Belski#else
844d997f63SAnatol Belski# define ZEND_STRTOL(s0, s1, base) strtol((s0), (s1), (base))
854d997f63SAnatol Belski# define ZEND_STRTOUL(s0, s1, base) strtoul((s0), (s1), (base))
86c429c74bSAnatol Belski# define ZEND_LONG_FMT "%" PRId32
87c429c74bSAnatol Belski# define ZEND_ULONG_FMT "%" PRIu32
8816160386SDmitry Stogov# define ZEND_XLONG_FMT "%" PRIx32
89c429c74bSAnatol Belski# define ZEND_LONG_FMT_SPEC PRId32
90c429c74bSAnatol Belski# define ZEND_ULONG_FMT_SPEC PRIu32
91ce2cd892SKalle Sommer Nielsen# ifdef ZEND_WIN32
924d997f63SAnatol Belski#  define ZEND_LTOA(i, s, len) _ltoa_s((i), (s), (len), 10)
934d997f63SAnatol Belski#  define ZEND_ATOL(i, s) i = atol((s))
948ee2a4a9SAnatol Belski# else
954d997f63SAnatol Belski#  define ZEND_LTOA(i, s, len) \
968ee2a4a9SAnatol Belski	do { \
97c429c74bSAnatol Belski		int st = snprintf((s), (len), ZEND_LONG_FMT, (i)); \
988ee2a4a9SAnatol Belski		(s)[st] = '\0'; \
998ee2a4a9SAnatol Belski 	} while (0)
1004d997f63SAnatol Belski#  define ZEND_ATOL(i, s) (i) = atol((s))
1018ee2a4a9SAnatol Belski# endif
1024d997f63SAnatol Belski# define ZEND_STRTOL_PTR strtol
1034d997f63SAnatol Belski# define ZEND_STRTOUL_PTR strtoul
1048ee2a4a9SAnatol Belski# define ZEND_ABS abs
1058ee2a4a9SAnatol Belski#endif
1068ee2a4a9SAnatol Belski
10761d37e05SDmitry Stogov#if SIZEOF_ZEND_LONG == 4
10861d37e05SDmitry Stogov# define MAX_LENGTH_OF_LONG 11
10961d37e05SDmitry Stogov# define LONG_MIN_DIGITS "2147483648"
11061d37e05SDmitry Stogov#elif SIZEOF_ZEND_LONG == 8
11161d37e05SDmitry Stogov# define MAX_LENGTH_OF_LONG 20
11261d37e05SDmitry Stogov# define LONG_MIN_DIGITS "9223372036854775808"
11361d37e05SDmitry Stogov#else
11461d37e05SDmitry Stogov# error "Unknown SIZEOF_ZEND_LONG"
11561d37e05SDmitry Stogov#endif
11661d37e05SDmitry Stogov
11761d37e05SDmitry Stogovstatic const char long_min_digits[] = LONG_MIN_DIGITS;
1188ee2a4a9SAnatol Belski
11916160386SDmitry Stogov#ifdef _WIN64
12016160386SDmitry Stogov# define ZEND_ADDR_FMT "0x%016I64x"
12116160386SDmitry Stogov#elif SIZEOF_SIZE_T == 4
12216160386SDmitry Stogov# define ZEND_ADDR_FMT "0x%08zx"
12316160386SDmitry Stogov#elif SIZEOF_SIZE_T == 8
12416160386SDmitry Stogov# define ZEND_ADDR_FMT "0x%016zx"
12516160386SDmitry Stogov#else
12616160386SDmitry Stogov# error "Unknown SIZEOF_SIZE_T"
12716160386SDmitry Stogov#endif
12816160386SDmitry Stogov
1294d997f63SAnatol Belski#endif /* ZEND_LONG_H */
1308ee2a4a9SAnatol Belski
1318ee2a4a9SAnatol Belski/*
1328ee2a4a9SAnatol Belski * Local variables:
1338ee2a4a9SAnatol Belski * tab-width: 4
1348ee2a4a9SAnatol Belski * c-basic-offset: 4
1358ee2a4a9SAnatol Belski * indent-tabs-mode: t
1368ee2a4a9SAnatol Belski * End:
137bc5811f3SAnatol Belski * vim600: sw=4 ts=4 fdm=marker
138bc5811f3SAnatol Belski * vim<600: sw=4 ts=4
1398ee2a4a9SAnatol Belski */
140