xref: /PHP-7.0/Zend/zend_long.h (revision 478f119a)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2017 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_LONG_FMT_SPEC PRId64
63 # define ZEND_ULONG_FMT_SPEC PRIu64
64 # ifdef ZEND_WIN32
65 #  define ZEND_LTOA(i, s, len) _i64toa_s((i), (s), (len), 10)
66 #  define ZEND_ATOL(i, s) i = _atoi64((s))
67 #  define ZEND_STRTOL(s0, s1, base) _strtoi64((s0), (s1), (base))
68 #  define ZEND_STRTOUL(s0, s1, base) _strtoui64((s0), (s1), (base))
69 #  define ZEND_STRTOL_PTR _strtoi64
70 #  define ZEND_STRTOUL_PTR _strtoui64
71 #  define ZEND_ABS _abs64
72 # else
73 #  define ZEND_LTOA(i, s, len) \
74 	do { \
75 		int st = snprintf((s), (len), ZEND_LONG_FMT, (i)); \
76 		(s)[st] = '\0'; \
77  	} while (0)
78 #  define ZEND_ATOL(i, s) (i) = atoll((s))
79 #  define ZEND_STRTOL(s0, s1, base) strtoll((s0), (s1), (base))
80 #  define ZEND_STRTOUL(s0, s1, base) strtoull((s0), (s1), (base))
81 #  define ZEND_STRTOL_PTR strtoll
82 #  define ZEND_STRTOUL_PTR strtoull
83 #  define ZEND_ABS imaxabs
84 # endif
85 #else
86 # define ZEND_STRTOL(s0, s1, base) strtol((s0), (s1), (base))
87 # define ZEND_STRTOUL(s0, s1, base) strtoul((s0), (s1), (base))
88 # define ZEND_LONG_FMT "%" PRId32
89 # define ZEND_ULONG_FMT "%" PRIu32
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(i, s) i = 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(i, s) (i) = 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 #endif /* ZEND_LONG_H */
121 
122 /*
123  * Local variables:
124  * tab-width: 4
125  * c-basic-offset: 4
126  * indent-tabs-mode: t
127  * End:
128  */
129