intprops.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* intprops.h -- properties of integer types
  2. Copyright (C) 2001-2024 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify it
  4. under the terms of the GNU Lesser General Public License as published
  5. by the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #ifndef _GL_INTPROPS_H
  14. #define _GL_INTPROPS_H
  15. #include "intprops-internal.h"
  16. /* The extra casts in the following macros work around compiler bugs,
  17. e.g., in Cray C 5.0.3.0. */
  18. /* True if the arithmetic type T is an integer type. bool counts as
  19. an integer. */
  20. #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1)
  21. /* True if the real type T is signed. */
  22. #define TYPE_SIGNED(t) _GL_TYPE_SIGNED (t)
  23. /* Return 1 if the real expression E, after promotion, has a
  24. signed or floating type. Do not evaluate E. */
  25. #define EXPR_SIGNED(e) _GL_EXPR_SIGNED (e)
  26. /* Minimum and maximum values for integer types and expressions. */
  27. /* The width in bits of the integer type or expression T.
  28. Do not evaluate T. T must not be a bit-field expression.
  29. Padding bits are not supported; this is checked at compile-time below. */
  30. #define TYPE_WIDTH(t) _GL_TYPE_WIDTH (t)
  31. /* The maximum and minimum values for the integer type T. */
  32. #define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
  33. #define TYPE_MAXIMUM(t) \
  34. ((t) (! TYPE_SIGNED (t) \
  35. ? (t) -1 \
  36. : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
  37. /* Bound on length of the string representing an unsigned integer
  38. value representable in B bits. log10 (2.0) < 146/485. The
  39. smallest value of B where this bound is not tight is 2621. */
  40. #define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485)
  41. /* Bound on length of the string representing an integer type or expression T.
  42. T must not be a bit-field expression.
  43. Subtract 1 for the sign bit if T is signed, and then add 1 more for
  44. a minus sign if needed.
  45. Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 1 when its argument is
  46. unsigned, this macro may overestimate the true bound by one byte when
  47. applied to unsigned types of size 2, 4, 16, ... bytes. */
  48. #define INT_STRLEN_BOUND(t) \
  49. (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \
  50. + _GL_SIGNED_TYPE_OR_EXPR (t))
  51. /* Bound on buffer size needed to represent an integer type or expression T,
  52. including the terminating null. T must not be a bit-field expression. */
  53. #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
  54. /* Range overflow checks.
  55. The INT_<op>_RANGE_OVERFLOW macros return 1 if the corresponding C
  56. operators overflow arithmetically when given the same arguments.
  57. These macros do not rely on undefined or implementation-defined behavior.
  58. Although their implementations are simple and straightforward,
  59. they are harder to use and may be less efficient than the
  60. INT_<op>_WRAPV, INT_<op>_OK, and INT_<op>_OVERFLOW macros described below.
  61. Example usage:
  62. long int i = ...;
  63. long int j = ...;
  64. if (INT_MULTIPLY_RANGE_OVERFLOW (i, j, LONG_MIN, LONG_MAX))
  65. printf ("multiply would overflow");
  66. else
  67. printf ("product is %ld", i * j);
  68. Restrictions on *_RANGE_OVERFLOW macros:
  69. These macros do not check for all possible numerical problems or
  70. undefined or unspecified behavior: they do not check for division
  71. by zero, for bad shift counts, or for shifting negative numbers.
  72. These macros may evaluate their arguments zero or multiple times,
  73. so the arguments should not have side effects. The arithmetic
  74. arguments (including the MIN and MAX arguments) must be of the same
  75. integer type after the usual arithmetic conversions, and the type
  76. must have minimum value MIN and maximum MAX. Unsigned types should
  77. use a zero MIN of the proper type.
  78. Because all arguments are subject to integer promotions, these
  79. macros typically do not work on types narrower than 'int'.
  80. These macros are tuned for constant MIN and MAX. For commutative
  81. operations such as A + B, they are also tuned for constant B. */
  82. /* Return 1 if A + B would overflow in [MIN,MAX] arithmetic.
  83. See above for restrictions. */
  84. #define INT_ADD_RANGE_OVERFLOW(a, b, min, max) \
  85. ((b) < 0 \
  86. ? (a) < (min) - (b) \
  87. : (max) - (b) < (a))
  88. /* Return 1 if A - B would overflow in [MIN,MAX] arithmetic.
  89. See above for restrictions. */
  90. #define INT_SUBTRACT_RANGE_OVERFLOW(a, b, min, max) \
  91. ((b) < 0 \
  92. ? (max) + (b) < (a) \
  93. : (a) < (min) + (b))
  94. /* Return 1 if - A would overflow in [MIN,MAX] arithmetic.
  95. See above for restrictions. */
  96. #define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \
  97. _GL_INT_NEGATE_RANGE_OVERFLOW (a, min, max)
  98. /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic.
  99. See above for restrictions. Avoid && and || as they tickle
  100. bugs in Sun C 5.11 2010/08/13 and other compilers; see
  101. <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>. */
  102. #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \
  103. ((b) < 0 \
  104. ? ((a) < 0 \
  105. ? (a) < (max) / (b) \
  106. : (b) == -1 \
  107. ? 0 \
  108. : (min) / (b) < (a)) \
  109. : (b) == 0 \
  110. ? 0 \
  111. : ((a) < 0 \
  112. ? (a) < (min) / (b) \
  113. : (max) / (b) < (a)))
  114. /* Return 1 if A / B would overflow in [MIN,MAX] arithmetic.
  115. See above for restrictions. Do not check for division by zero. */
  116. #define INT_DIVIDE_RANGE_OVERFLOW(a, b, min, max) \
  117. ((min) < 0 && (b) == -1 && (a) < - (max))
  118. /* Return 1 if A % B would overflow in [MIN,MAX] arithmetic.
  119. See above for restrictions. Do not check for division by zero.
  120. Mathematically, % should never overflow, but on x86-like hosts
  121. INT_MIN % -1 traps, and the C standard permits this, so treat this
  122. as an overflow too. */
  123. #define INT_REMAINDER_RANGE_OVERFLOW(a, b, min, max) \
  124. INT_DIVIDE_RANGE_OVERFLOW (a, b, min, max)
  125. /* Return 1 if A << B would overflow in [MIN,MAX] arithmetic.
  126. See above for restrictions. Here, MIN and MAX are for A only, and B need
  127. not be of the same type as the other arguments. The C standard says that
  128. behavior is undefined for shifts unless 0 <= B < wordwidth, and that when
  129. A is negative then A << B has undefined behavior and A >> B has
  130. implementation-defined behavior, but do not check these other
  131. restrictions. */
  132. #define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \
  133. ((a) < 0 \
  134. ? (a) < (min) >> (b) \
  135. : (max) >> (b) < (a))
  136. /* The _GL*_OVERFLOW macros have the same restrictions as the
  137. *_RANGE_OVERFLOW macros, except that they do not assume that operands
  138. (e.g., A and B) have the same type as MIN and MAX. Instead, they assume
  139. that the result (e.g., A + B) has that type. */
  140. #if _GL_HAS_BUILTIN_OVERFLOW_P
  141. # define _GL_ADD_OVERFLOW(a, b, min, max) \
  142. __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
  143. # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
  144. __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
  145. # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
  146. __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0)
  147. #else
  148. # define _GL_ADD_OVERFLOW(a, b, min, max) \
  149. ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
  150. : (a) < 0 ? (b) <= (a) + (b) \
  151. : (b) < 0 ? (a) <= (a) + (b) \
  152. : (a) + (b) < (b))
  153. # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
  154. ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \
  155. : (a) < 0 ? 1 \
  156. : (b) < 0 ? (a) - (b) <= (a) \
  157. : (a) < (b))
  158. # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
  159. (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \
  160. || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))
  161. #endif
  162. #define _GL_DIVIDE_OVERFLOW(a, b, min, max) \
  163. ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
  164. : (a) < 0 ? (b) <= (a) + (b) - 1 \
  165. : (b) < 0 && (a) + (b) <= (a))
  166. #define _GL_REMAINDER_OVERFLOW(a, b, min, max) \
  167. ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
  168. : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \
  169. : (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max))
  170. /* Return a nonzero value if A is a mathematical multiple of B, where
  171. A is unsigned, B is negative, and MAX is the maximum value of A's
  172. type. A's type must be the same as (A % B)'s type. Normally (A %
  173. -B == 0) suffices, but things get tricky if -B would overflow. */
  174. #define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \
  175. (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \
  176. ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \
  177. ? (a) \
  178. : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \
  179. : (a) % - (b)) \
  180. == 0)
  181. /* Check for integer overflow, and report low order bits of answer.
  182. The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators
  183. might not yield numerically correct answers due to arithmetic overflow.
  184. The INT_<op>_WRAPV macros compute the low-order bits of the sum,
  185. difference, and product of two C integers, and return 1 if these
  186. low-order bits are not numerically correct.
  187. These macros work correctly on all known practical hosts, and do not rely
  188. on undefined behavior due to signed arithmetic overflow.
  189. Example usage, assuming A and B are long int:
  190. if (INT_MULTIPLY_OVERFLOW (a, b))
  191. printf ("result would overflow\n");
  192. else
  193. printf ("result is %ld (no overflow)\n", a * b);
  194. Example usage with WRAPV flavor:
  195. long int result;
  196. bool overflow = INT_MULTIPLY_WRAPV (a, b, &result);
  197. printf ("result is %ld (%s)\n", result,
  198. overflow ? "after overflow" : "no overflow");
  199. Restrictions on these macros:
  200. These macros do not check for all possible numerical problems or
  201. undefined or unspecified behavior: they do not check for division
  202. by zero, for bad shift counts, or for shifting negative numbers.
  203. These macros may evaluate their arguments zero or multiple times, so the
  204. arguments should not have side effects.
  205. The WRAPV macros are not constant expressions. They support only
  206. +, binary -, and *.
  207. Because the WRAPV macros convert the result, they report overflow
  208. in different circumstances than the OVERFLOW macros do. For
  209. example, in the typical case with 16-bit 'short' and 32-bit 'int',
  210. if A, B and *R are all of type 'short' then INT_ADD_OVERFLOW (A, B)
  211. returns false because the addition cannot overflow after A and B
  212. are converted to 'int', whereas INT_ADD_WRAPV (A, B, R) returns
  213. true or false depending on whether the sum fits into 'short'.
  214. These macros are tuned for their last input argument being a constant.
  215. A, B, and *R should be integers; they need not be the same type,
  216. and they need not be all signed or all unsigned.
  217. However, none of the integer types should be bit-precise,
  218. and *R's type should not be char, bool, or an enumeration type.
  219. Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B,
  220. A % B, and A << B would overflow, respectively. */
  221. #define INT_ADD_OVERFLOW(a, b) \
  222. _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
  223. #define INT_SUBTRACT_OVERFLOW(a, b) \
  224. _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
  225. #define INT_NEGATE_OVERFLOW(a) _GL_INT_NEGATE_OVERFLOW (a)
  226. #define INT_MULTIPLY_OVERFLOW(a, b) \
  227. _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)
  228. #define INT_DIVIDE_OVERFLOW(a, b) \
  229. _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW)
  230. #define INT_REMAINDER_OVERFLOW(a, b) \
  231. _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW)
  232. #define INT_LEFT_SHIFT_OVERFLOW(a, b) \
  233. INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
  234. _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
  235. /* Return 1 if the expression A <op> B would overflow,
  236. where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test,
  237. assuming MIN and MAX are the minimum and maximum for the result type.
  238. Arguments should be free of side effects. */
  239. #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \
  240. op_result_overflow (a, b, \
  241. _GL_INT_MINIMUM (_GL_INT_CONVERT (a, b)), \
  242. _GL_INT_MAXIMUM (_GL_INT_CONVERT (a, b)))
  243. /* Store the low-order bits of A + B, A - B, A * B, respectively, into *R.
  244. Return 1 if the result overflows. See above for restrictions. */
  245. #define INT_ADD_WRAPV(a, b, r) _GL_INT_ADD_WRAPV (a, b, r)
  246. #define INT_SUBTRACT_WRAPV(a, b, r) _GL_INT_SUBTRACT_WRAPV (a, b, r)
  247. #define INT_MULTIPLY_WRAPV(a, b, r) _GL_INT_MULTIPLY_WRAPV (a, b, r)
  248. /* The following macros compute A + B, A - B, and A * B, respectively.
  249. If no overflow occurs, they set *R to the result and return 1;
  250. otherwise, they return 0 and may modify *R.
  251. Example usage:
  252. long int result;
  253. if (INT_ADD_OK (a, b, &result))
  254. printf ("result is %ld\n", result);
  255. else
  256. printf ("overflow\n");
  257. A, B, and *R should be integers; they need not be the same type,
  258. and they need not be all signed or all unsigned.
  259. However, none of the integer types should be bit-precise,
  260. and *R's type should not be char, bool, or an enumeration type.
  261. These macros work correctly on all known practical hosts, and do not rely
  262. on undefined behavior due to signed arithmetic overflow.
  263. These macros are not constant expressions.
  264. These macros may evaluate their arguments zero or multiple times, so the
  265. arguments should not have side effects.
  266. These macros are tuned for B being a constant. */
  267. #define INT_ADD_OK(a, b, r) (! INT_ADD_WRAPV (a, b, r))
  268. #define INT_SUBTRACT_OK(a, b, r) (! INT_SUBTRACT_WRAPV (a, b, r))
  269. #define INT_MULTIPLY_OK(a, b, r) (! INT_MULTIPLY_WRAPV (a, b, r))
  270. #endif /* _GL_INTPROPS_H */