zend_long.h 4.1 KB

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