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