xref: /PHP-7.2/Zend/zend_long.h (revision 7a7ec01a)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2018 Zend Technologies Ltd. (http://www.zend.com) |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 2.00 of the Zend license,     |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.zend.com/license/2_00.txt.                                |
11    | If you did not receive a copy of the Zend license and are unable to  |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@zend.com so we can mail you a copy immediately.              |
14    +----------------------------------------------------------------------+
15    | Authors: Anatol Belski <ab@php.net>                                  |
16    +----------------------------------------------------------------------+
17 */
18 
19 /* $Id$ */
20 
21 
22 #ifndef ZEND_LONG_H
23 #define ZEND_LONG_H
24 
25 #include "main/php_stdint.h"
26 
27 /* This is the heart of the whole int64 enablement in zval. */
28 #if defined(__x86_64__) || defined(__LP64__) || defined(_LP64) || defined(_WIN64)
29 # define ZEND_ENABLE_ZVAL_LONG64 1
30 #endif
31 
32 /* Integer types. */
33 #ifdef ZEND_ENABLE_ZVAL_LONG64
34 typedef int64_t zend_long;
35 typedef uint64_t zend_ulong;
36 typedef int64_t zend_off_t;
37 # define ZEND_LONG_MAX INT64_MAX
38 # define ZEND_LONG_MIN INT64_MIN
39 # define ZEND_ULONG_MAX UINT64_MAX
40 # define Z_L(i) INT64_C(i)
41 # define Z_UL(i) UINT64_C(i)
42 # define SIZEOF_ZEND_LONG 8
43 #else
44 typedef int32_t zend_long;
45 typedef uint32_t zend_ulong;
46 typedef int32_t zend_off_t;
47 # define ZEND_LONG_MAX INT32_MAX
48 # define ZEND_LONG_MIN INT32_MIN
49 # define ZEND_ULONG_MAX UINT32_MAX
50 # define Z_L(i) INT32_C(i)
51 # define Z_UL(i) UINT32_C(i)
52 # define SIZEOF_ZEND_LONG 4
53 #endif
54 
55 
56 /* Conversion macros. */
57 #define ZEND_LTOA_BUF_LEN 65
58 
59 #ifdef ZEND_ENABLE_ZVAL_LONG64
60 # define ZEND_LONG_FMT "%" PRId64
61 # define ZEND_ULONG_FMT "%" PRIu64
62 # define ZEND_XLONG_FMT "%" PRIx64
63 # define ZEND_LONG_FMT_SPEC PRId64
64 # define ZEND_ULONG_FMT_SPEC PRIu64
65 # ifdef ZEND_WIN32
66 #  define ZEND_LTOA(i, s, len) _i64toa_s((i), (s), (len), 10)
67 #  define ZEND_ATOL(i, s) i = _atoi64((s))
68 #  define ZEND_STRTOL(s0, s1, base) _strtoi64((s0), (s1), (base))
69 #  define ZEND_STRTOUL(s0, s1, base) _strtoui64((s0), (s1), (base))
70 #  define ZEND_STRTOL_PTR _strtoi64
71 #  define ZEND_STRTOUL_PTR _strtoui64
72 #  define ZEND_ABS _abs64
73 # else
74 #  define ZEND_LTOA(i, s, len) \
75 	do { \
76 		int st = snprintf((s), (len), ZEND_LONG_FMT, (i)); \
77 		(s)[st] = '\0'; \
78  	} while (0)
79 #  define ZEND_ATOL(i, s) (i) = atoll((s))
80 #  define ZEND_STRTOL(s0, s1, base) strtoll((s0), (s1), (base))
81 #  define ZEND_STRTOUL(s0, s1, base) strtoull((s0), (s1), (base))
82 #  define ZEND_STRTOL_PTR strtoll
83 #  define ZEND_STRTOUL_PTR strtoull
84 #  define ZEND_ABS imaxabs
85 # endif
86 #else
87 # define ZEND_STRTOL(s0, s1, base) strtol((s0), (s1), (base))
88 # define ZEND_STRTOUL(s0, s1, base) strtoul((s0), (s1), (base))
89 # define ZEND_LONG_FMT "%" PRId32
90 # define ZEND_ULONG_FMT "%" PRIu32
91 # define ZEND_XLONG_FMT "%" PRIx32
92 # define ZEND_LONG_FMT_SPEC PRId32
93 # define ZEND_ULONG_FMT_SPEC PRIu32
94 # ifdef ZEND_WIN32
95 #  define ZEND_LTOA(i, s, len) _ltoa_s((i), (s), (len), 10)
96 #  define ZEND_ATOL(i, s) i = atol((s))
97 # else
98 #  define ZEND_LTOA(i, s, len) \
99 	do { \
100 		int st = snprintf((s), (len), ZEND_LONG_FMT, (i)); \
101 		(s)[st] = '\0'; \
102  	} while (0)
103 #  define ZEND_ATOL(i, s) (i) = atol((s))
104 # endif
105 # define ZEND_STRTOL_PTR strtol
106 # define ZEND_STRTOUL_PTR strtoul
107 # define ZEND_ABS abs
108 #endif
109 
110 #if SIZEOF_ZEND_LONG == 4
111 # define MAX_LENGTH_OF_LONG 11
112 # define LONG_MIN_DIGITS "2147483648"
113 #elif SIZEOF_ZEND_LONG == 8
114 # define MAX_LENGTH_OF_LONG 20
115 # define LONG_MIN_DIGITS "9223372036854775808"
116 #else
117 # error "Unknown SIZEOF_ZEND_LONG"
118 #endif
119 
120 static const char long_min_digits[] = LONG_MIN_DIGITS;
121 
122 #ifdef _WIN64
123 # define ZEND_ADDR_FMT "0x%016I64x"
124 #elif SIZEOF_SIZE_T == 4
125 # define ZEND_ADDR_FMT "0x%08zx"
126 #elif SIZEOF_SIZE_T == 8
127 # define ZEND_ADDR_FMT "0x%016zx"
128 #else
129 # error "Unknown SIZEOF_SIZE_T"
130 #endif
131 
132 #endif /* ZEND_LONG_H */
133 
134 /*
135  * Local variables:
136  * tab-width: 4
137  * c-basic-offset: 4
138  * indent-tabs-mode: t
139  * End:
140  * vim600: sw=4 ts=4 fdm=marker
141  * vim<600: sw=4 ts=4
142  */
143