xref: /PHP-7.4/Zend/zend_long.h (revision 92ac598a)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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 #ifndef ZEND_LONG_H
20 #define ZEND_LONG_H
21 
22 #include "main/php_stdint.h"
23 
24 /* This is the heart of the whole int64 enablement in zval. */
25 #if defined(__x86_64__) || defined(__LP64__) || defined(_LP64) || defined(_WIN64)
26 # define ZEND_ENABLE_ZVAL_LONG64 1
27 #endif
28 
29 /* Integer types. */
30 #ifdef ZEND_ENABLE_ZVAL_LONG64
31 typedef int64_t zend_long;
32 typedef uint64_t zend_ulong;
33 typedef int64_t zend_off_t;
34 # define ZEND_LONG_MAX INT64_MAX
35 # define ZEND_LONG_MIN INT64_MIN
36 # define ZEND_ULONG_MAX UINT64_MAX
37 # define Z_L(i) INT64_C(i)
38 # define Z_UL(i) UINT64_C(i)
39 # define SIZEOF_ZEND_LONG 8
40 #else
41 typedef int32_t zend_long;
42 typedef uint32_t zend_ulong;
43 typedef int32_t zend_off_t;
44 # define ZEND_LONG_MAX INT32_MAX
45 # define ZEND_LONG_MIN INT32_MIN
46 # define ZEND_ULONG_MAX UINT32_MAX
47 # define Z_L(i) INT32_C(i)
48 # define Z_UL(i) UINT32_C(i)
49 # define SIZEOF_ZEND_LONG 4
50 #endif
51 
52 
53 /* Conversion macros. */
54 #define ZEND_LTOA_BUF_LEN 65
55 
56 #ifdef ZEND_ENABLE_ZVAL_LONG64
57 # define ZEND_LONG_FMT "%" PRId64
58 # define ZEND_ULONG_FMT "%" PRIu64
59 # define ZEND_XLONG_FMT "%" PRIx64
60 # define ZEND_LONG_FMT_SPEC PRId64
61 # define ZEND_ULONG_FMT_SPEC PRIu64
62 # ifdef ZEND_WIN32
63 #  define ZEND_LTOA(i, s, len) _i64toa_s((i), (s), (len), 10)
64 #  define ZEND_ATOL(i, s) i = _atoi64((s))
65 #  define ZEND_STRTOL(s0, s1, base) _strtoi64((s0), (s1), (base))
66 #  define ZEND_STRTOUL(s0, s1, base) _strtoui64((s0), (s1), (base))
67 #  define ZEND_STRTOL_PTR _strtoi64
68 #  define ZEND_STRTOUL_PTR _strtoui64
69 #  define ZEND_ABS _abs64
70 # else
71 #  define ZEND_LTOA(i, s, len) \
72 	do { \
73 		int st = snprintf((s), (len), ZEND_LONG_FMT, (i)); \
74 		(s)[st] = '\0'; \
75  	} while (0)
76 #  define ZEND_ATOL(i, s) (i) = atoll((s))
77 #  define ZEND_STRTOL(s0, s1, base) strtoll((s0), (s1), (base))
78 #  define ZEND_STRTOUL(s0, s1, base) strtoull((s0), (s1), (base))
79 #  define ZEND_STRTOL_PTR strtoll
80 #  define ZEND_STRTOUL_PTR strtoull
81 #  define ZEND_ABS imaxabs
82 # endif
83 #else
84 # define ZEND_STRTOL(s0, s1, base) strtol((s0), (s1), (base))
85 # define ZEND_STRTOUL(s0, s1, base) strtoul((s0), (s1), (base))
86 # define ZEND_LONG_FMT "%" PRId32
87 # define ZEND_ULONG_FMT "%" PRIu32
88 # define ZEND_XLONG_FMT "%" PRIx32
89 # define ZEND_LONG_FMT_SPEC PRId32
90 # define ZEND_ULONG_FMT_SPEC PRIu32
91 # ifdef ZEND_WIN32
92 #  define ZEND_LTOA(i, s, len) _ltoa_s((i), (s), (len), 10)
93 #  define ZEND_ATOL(i, s) i = atol((s))
94 # else
95 #  define ZEND_LTOA(i, s, len) \
96 	do { \
97 		int st = snprintf((s), (len), ZEND_LONG_FMT, (i)); \
98 		(s)[st] = '\0'; \
99  	} while (0)
100 #  define ZEND_ATOL(i, s) (i) = atol((s))
101 # endif
102 # define ZEND_STRTOL_PTR strtol
103 # define ZEND_STRTOUL_PTR strtoul
104 # define ZEND_ABS abs
105 #endif
106 
107 #if SIZEOF_ZEND_LONG == 4
108 # define MAX_LENGTH_OF_LONG 11
109 # define LONG_MIN_DIGITS "2147483648"
110 #elif SIZEOF_ZEND_LONG == 8
111 # define MAX_LENGTH_OF_LONG 20
112 # define LONG_MIN_DIGITS "9223372036854775808"
113 #else
114 # error "Unknown SIZEOF_ZEND_LONG"
115 #endif
116 
117 static const char long_min_digits[] = LONG_MIN_DIGITS;
118 
119 #ifdef _WIN64
120 # define ZEND_ADDR_FMT "0x%016I64x"
121 #elif SIZEOF_SIZE_T == 4
122 # define ZEND_ADDR_FMT "0x%08zx"
123 #elif SIZEOF_SIZE_T == 8
124 # define ZEND_ADDR_FMT "0x%016zx"
125 #else
126 # error "Unknown SIZEOF_SIZE_T"
127 #endif
128 
129 #endif /* ZEND_LONG_H */
130