intprops.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /* intprops.h -- properties of integer types
  2. Copyright (C) 2001-2021 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 General Public License as published
  5. by the Free Software Foundation; either version 3 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 General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* Written by Paul Eggert. */
  14. #ifndef _GL_INTPROPS_H
  15. #define _GL_INTPROPS_H
  16. #include <limits.h>
  17. /* Return a value with the common real type of E and V and the value of V.
  18. Do not evaluate E. */
  19. #define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))
  20. /* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see
  21. <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00406.html>. */
  22. #define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v))
  23. /* The extra casts in the following macros work around compiler bugs,
  24. e.g., in Cray C 5.0.3.0. */
  25. /* True if the arithmetic type T is an integer type. bool counts as
  26. an integer. */
  27. #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1)
  28. /* True if the real type T is signed. */
  29. #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
  30. /* Return 1 if the real expression E, after promotion, has a
  31. signed or floating type. Do not evaluate E. */
  32. #define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
  33. /* Minimum and maximum values for integer types and expressions. */
  34. /* The width in bits of the integer type or expression T.
  35. Do not evaluate T. T must not be a bit-field expression.
  36. Padding bits are not supported; this is checked at compile-time below. */
  37. #define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
  38. /* The maximum and minimum values for the integer type T. */
  39. #define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
  40. #define TYPE_MAXIMUM(t) \
  41. ((t) (! TYPE_SIGNED (t) \
  42. ? (t) -1 \
  43. : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
  44. /* The maximum and minimum values for the type of the expression E,
  45. after integer promotion. E is not evaluated. */
  46. #define _GL_INT_MINIMUM(e) \
  47. (EXPR_SIGNED (e) \
  48. ? ~ _GL_SIGNED_INT_MAXIMUM (e) \
  49. : _GL_INT_CONVERT (e, 0))
  50. #define _GL_INT_MAXIMUM(e) \
  51. (EXPR_SIGNED (e) \
  52. ? _GL_SIGNED_INT_MAXIMUM (e) \
  53. : _GL_INT_NEGATE_CONVERT (e, 1))
  54. #define _GL_SIGNED_INT_MAXIMUM(e) \
  55. (((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH (+ (e)) - 2)) - 1) * 2 + 1)
  56. /* Work around OpenVMS incompatibility with C99. */
  57. #if !defined LLONG_MAX && defined __INT64_MAX
  58. # define LLONG_MAX __INT64_MAX
  59. # define LLONG_MIN __INT64_MIN
  60. #endif
  61. /* This include file assumes that signed types are two's complement without
  62. padding bits; the above macros have undefined behavior otherwise.
  63. If this is a problem for you, please let us know how to fix it for your host.
  64. This assumption is tested by the intprops-tests module. */
  65. /* Does the __typeof__ keyword work? This could be done by
  66. 'configure', but for now it's easier to do it by hand. */
  67. #if (2 <= __GNUC__ \
  68. || (4 <= __clang_major__) \
  69. || (1210 <= __IBMC__ && defined __IBM__TYPEOF__) \
  70. || (0x5110 <= __SUNPRO_C && !__STDC__))
  71. # define _GL_HAVE___TYPEOF__ 1
  72. #else
  73. # define _GL_HAVE___TYPEOF__ 0
  74. #endif
  75. /* Return 1 if the integer type or expression T might be signed. Return 0
  76. if it is definitely unsigned. T must not be a bit-field expression.
  77. This macro does not evaluate its argument, and expands to an
  78. integer constant expression. */
  79. #if _GL_HAVE___TYPEOF__
  80. # define _GL_SIGNED_TYPE_OR_EXPR(t) TYPE_SIGNED (__typeof__ (t))
  81. #else
  82. # define _GL_SIGNED_TYPE_OR_EXPR(t) 1
  83. #endif
  84. /* Bound on length of the string representing an unsigned integer
  85. value representable in B bits. log10 (2.0) < 146/485. The
  86. smallest value of B where this bound is not tight is 2621. */
  87. #define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485)
  88. /* Bound on length of the string representing an integer type or expression T.
  89. T must not be a bit-field expression.
  90. Subtract 1 for the sign bit if T is signed, and then add 1 more for
  91. a minus sign if needed.
  92. Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 1 when its argument is
  93. unsigned, this macro may overestimate the true bound by one byte when
  94. applied to unsigned types of size 2, 4, 16, ... bytes. */
  95. #define INT_STRLEN_BOUND(t) \
  96. (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \
  97. + _GL_SIGNED_TYPE_OR_EXPR (t))
  98. /* Bound on buffer size needed to represent an integer type or expression T,
  99. including the terminating null. T must not be a bit-field expression. */
  100. #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
  101. /* Range overflow checks.
  102. The INT_<op>_RANGE_OVERFLOW macros return 1 if the corresponding C
  103. operators might not yield numerically correct answers due to
  104. arithmetic overflow. They do not rely on undefined or
  105. implementation-defined behavior. Their implementations are simple
  106. and straightforward, but they are a bit harder to use than the
  107. INT_<op>_OVERFLOW macros described below.
  108. Example usage:
  109. long int i = ...;
  110. long int j = ...;
  111. if (INT_MULTIPLY_RANGE_OVERFLOW (i, j, LONG_MIN, LONG_MAX))
  112. printf ("multiply would overflow");
  113. else
  114. printf ("product is %ld", i * j);
  115. Restrictions on *_RANGE_OVERFLOW macros:
  116. These macros do not check for all possible numerical problems or
  117. undefined or unspecified behavior: they do not check for division
  118. by zero, for bad shift counts, or for shifting negative numbers.
  119. These macros may evaluate their arguments zero or multiple times,
  120. so the arguments should not have side effects. The arithmetic
  121. arguments (including the MIN and MAX arguments) must be of the same
  122. integer type after the usual arithmetic conversions, and the type
  123. must have minimum value MIN and maximum MAX. Unsigned types should
  124. use a zero MIN of the proper type.
  125. These macros are tuned for constant MIN and MAX. For commutative
  126. operations such as A + B, they are also tuned for constant B. */
  127. /* Return 1 if A + B would overflow in [MIN,MAX] arithmetic.
  128. See above for restrictions. */
  129. #define INT_ADD_RANGE_OVERFLOW(a, b, min, max) \
  130. ((b) < 0 \
  131. ? (a) < (min) - (b) \
  132. : (max) - (b) < (a))
  133. /* Return 1 if A - B would overflow in [MIN,MAX] arithmetic.
  134. See above for restrictions. */
  135. #define INT_SUBTRACT_RANGE_OVERFLOW(a, b, min, max) \
  136. ((b) < 0 \
  137. ? (max) + (b) < (a) \
  138. : (a) < (min) + (b))
  139. /* Return 1 if - A would overflow in [MIN,MAX] arithmetic.
  140. See above for restrictions. */
  141. #define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \
  142. ((min) < 0 \
  143. ? (a) < - (max) \
  144. : 0 < (a))
  145. /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic.
  146. See above for restrictions. Avoid && and || as they tickle
  147. bugs in Sun C 5.11 2010/08/13 and other compilers; see
  148. <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>. */
  149. #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \
  150. ((b) < 0 \
  151. ? ((a) < 0 \
  152. ? (a) < (max) / (b) \
  153. : (b) == -1 \
  154. ? 0 \
  155. : (min) / (b) < (a)) \
  156. : (b) == 0 \
  157. ? 0 \
  158. : ((a) < 0 \
  159. ? (a) < (min) / (b) \
  160. : (max) / (b) < (a)))
  161. /* Return 1 if A / B would overflow in [MIN,MAX] arithmetic.
  162. See above for restrictions. Do not check for division by zero. */
  163. #define INT_DIVIDE_RANGE_OVERFLOW(a, b, min, max) \
  164. ((min) < 0 && (b) == -1 && (a) < - (max))
  165. /* Return 1 if A % B would overflow in [MIN,MAX] arithmetic.
  166. See above for restrictions. Do not check for division by zero.
  167. Mathematically, % should never overflow, but on x86-like hosts
  168. INT_MIN % -1 traps, and the C standard permits this, so treat this
  169. as an overflow too. */
  170. #define INT_REMAINDER_RANGE_OVERFLOW(a, b, min, max) \
  171. INT_DIVIDE_RANGE_OVERFLOW (a, b, min, max)
  172. /* Return 1 if A << B would overflow in [MIN,MAX] arithmetic.
  173. See above for restrictions. Here, MIN and MAX are for A only, and B need
  174. not be of the same type as the other arguments. The C standard says that
  175. behavior is undefined for shifts unless 0 <= B < wordwidth, and that when
  176. A is negative then A << B has undefined behavior and A >> B has
  177. implementation-defined behavior, but do not check these other
  178. restrictions. */
  179. #define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \
  180. ((a) < 0 \
  181. ? (a) < (min) >> (b) \
  182. : (max) >> (b) < (a))
  183. /* True if __builtin_add_overflow (A, B, P) and __builtin_sub_overflow
  184. (A, B, P) work when P is non-null. */
  185. /* __builtin_{add,sub}_overflow exists but is not reliable in GCC 5.x and 6.x,
  186. see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98269>. */
  187. #if 7 <= __GNUC__ && !defined __ICC
  188. # define _GL_HAS_BUILTIN_ADD_OVERFLOW 1
  189. #elif defined __has_builtin
  190. # define _GL_HAS_BUILTIN_ADD_OVERFLOW __has_builtin (__builtin_add_overflow)
  191. #else
  192. # define _GL_HAS_BUILTIN_ADD_OVERFLOW 0
  193. #endif
  194. /* True if __builtin_mul_overflow (A, B, P) works when P is non-null. */
  195. #ifdef __clang__
  196. /* Work around Clang bug <https://bugs.llvm.org/show_bug.cgi?id=16404>. */
  197. # define _GL_HAS_BUILTIN_MUL_OVERFLOW 0
  198. #else
  199. # define _GL_HAS_BUILTIN_MUL_OVERFLOW _GL_HAS_BUILTIN_ADD_OVERFLOW
  200. #endif
  201. /* True if __builtin_add_overflow_p (A, B, C) works, and similarly for
  202. __builtin_sub_overflow_p and __builtin_mul_overflow_p. */
  203. #if defined __clang__ || defined __ICC
  204. /* Clang 11 lacks __builtin_mul_overflow_p, and even if it did it
  205. would presumably run afoul of Clang bug 16404. ICC 2021.1's
  206. __builtin_add_overflow_p etc. are not treated as integral constant
  207. expressions even when all arguments are. */
  208. # define _GL_HAS_BUILTIN_OVERFLOW_P 0
  209. #elif defined __has_builtin
  210. # define _GL_HAS_BUILTIN_OVERFLOW_P __has_builtin (__builtin_mul_overflow_p)
  211. #else
  212. # define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
  213. #endif
  214. /* The _GL*_OVERFLOW macros have the same restrictions as the
  215. *_RANGE_OVERFLOW macros, except that they do not assume that operands
  216. (e.g., A and B) have the same type as MIN and MAX. Instead, they assume
  217. that the result (e.g., A + B) has that type. */
  218. #if _GL_HAS_BUILTIN_OVERFLOW_P
  219. # define _GL_ADD_OVERFLOW(a, b, min, max) \
  220. __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
  221. # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
  222. __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
  223. # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
  224. __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0)
  225. #else
  226. # define _GL_ADD_OVERFLOW(a, b, min, max) \
  227. ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
  228. : (a) < 0 ? (b) <= (a) + (b) \
  229. : (b) < 0 ? (a) <= (a) + (b) \
  230. : (a) + (b) < (b))
  231. # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
  232. ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \
  233. : (a) < 0 ? 1 \
  234. : (b) < 0 ? (a) - (b) <= (a) \
  235. : (a) < (b))
  236. # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
  237. (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \
  238. || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))
  239. #endif
  240. #define _GL_DIVIDE_OVERFLOW(a, b, min, max) \
  241. ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
  242. : (a) < 0 ? (b) <= (a) + (b) - 1 \
  243. : (b) < 0 && (a) + (b) <= (a))
  244. #define _GL_REMAINDER_OVERFLOW(a, b, min, max) \
  245. ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
  246. : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \
  247. : (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max))
  248. /* Return a nonzero value if A is a mathematical multiple of B, where
  249. A is unsigned, B is negative, and MAX is the maximum value of A's
  250. type. A's type must be the same as (A % B)'s type. Normally (A %
  251. -B == 0) suffices, but things get tricky if -B would overflow. */
  252. #define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \
  253. (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \
  254. ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \
  255. ? (a) \
  256. : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \
  257. : (a) % - (b)) \
  258. == 0)
  259. /* Check for integer overflow, and report low order bits of answer.
  260. The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators
  261. might not yield numerically correct answers due to arithmetic overflow.
  262. The INT_<op>_WRAPV macros compute the low-order bits of the sum,
  263. difference, and product of two C integers, and return 1 if these
  264. low-order bits are not numerically correct.
  265. These macros work correctly on all known practical hosts, and do not rely
  266. on undefined behavior due to signed arithmetic overflow.
  267. Example usage, assuming A and B are long int:
  268. if (INT_MULTIPLY_OVERFLOW (a, b))
  269. printf ("result would overflow\n");
  270. else
  271. printf ("result is %ld (no overflow)\n", a * b);
  272. Example usage with WRAPV flavor:
  273. long int result;
  274. bool overflow = INT_MULTIPLY_WRAPV (a, b, &result);
  275. printf ("result is %ld (%s)\n", result,
  276. overflow ? "after overflow" : "no overflow");
  277. Restrictions on these macros:
  278. These macros do not check for all possible numerical problems or
  279. undefined or unspecified behavior: they do not check for division
  280. by zero, for bad shift counts, or for shifting negative numbers.
  281. These macros may evaluate their arguments zero or multiple times, so the
  282. arguments should not have side effects.
  283. The WRAPV macros are not constant expressions. They support only
  284. +, binary -, and *. Because the WRAPV macros convert the result,
  285. they report overflow in different circumstances than the OVERFLOW
  286. macros do.
  287. These macros are tuned for their last input argument being a constant.
  288. Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B,
  289. A % B, and A << B would overflow, respectively. */
  290. #define INT_ADD_OVERFLOW(a, b) \
  291. _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
  292. #define INT_SUBTRACT_OVERFLOW(a, b) \
  293. _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
  294. #if _GL_HAS_BUILTIN_OVERFLOW_P
  295. # define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a)
  296. #else
  297. # define INT_NEGATE_OVERFLOW(a) \
  298. INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
  299. #endif
  300. #define INT_MULTIPLY_OVERFLOW(a, b) \
  301. _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)
  302. #define INT_DIVIDE_OVERFLOW(a, b) \
  303. _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW)
  304. #define INT_REMAINDER_OVERFLOW(a, b) \
  305. _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW)
  306. #define INT_LEFT_SHIFT_OVERFLOW(a, b) \
  307. INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
  308. _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
  309. /* Return 1 if the expression A <op> B would overflow,
  310. where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test,
  311. assuming MIN and MAX are the minimum and maximum for the result type.
  312. Arguments should be free of side effects. */
  313. #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \
  314. op_result_overflow (a, b, \
  315. _GL_INT_MINIMUM (_GL_INT_CONVERT (a, b)), \
  316. _GL_INT_MAXIMUM (_GL_INT_CONVERT (a, b)))
  317. /* Store the low-order bits of A + B, A - B, A * B, respectively, into *R.
  318. Return 1 if the result overflows. See above for restrictions. */
  319. #if _GL_HAS_BUILTIN_ADD_OVERFLOW
  320. # define INT_ADD_WRAPV(a, b, r) __builtin_add_overflow (a, b, r)
  321. # define INT_SUBTRACT_WRAPV(a, b, r) __builtin_sub_overflow (a, b, r)
  322. #else
  323. # define INT_ADD_WRAPV(a, b, r) \
  324. _GL_INT_OP_WRAPV (a, b, r, +, _GL_INT_ADD_RANGE_OVERFLOW)
  325. # define INT_SUBTRACT_WRAPV(a, b, r) \
  326. _GL_INT_OP_WRAPV (a, b, r, -, _GL_INT_SUBTRACT_RANGE_OVERFLOW)
  327. #endif
  328. #if _GL_HAS_BUILTIN_MUL_OVERFLOW
  329. # if ((9 < __GNUC__ + (3 <= __GNUC_MINOR__) \
  330. || (__GNUC__ == 8 && 4 <= __GNUC_MINOR__)) \
  331. && !defined __ICC)
  332. # define INT_MULTIPLY_WRAPV(a, b, r) __builtin_mul_overflow (a, b, r)
  333. # else
  334. /* Work around GCC bug 91450. */
  335. # define INT_MULTIPLY_WRAPV(a, b, r) \
  336. ((!_GL_SIGNED_TYPE_OR_EXPR (*(r)) && EXPR_SIGNED (a) && EXPR_SIGNED (b) \
  337. && _GL_INT_MULTIPLY_RANGE_OVERFLOW (a, b, 0, (__typeof__ (*(r))) -1)) \
  338. ? ((void) __builtin_mul_overflow (a, b, r), 1) \
  339. : __builtin_mul_overflow (a, b, r))
  340. # endif
  341. #else
  342. # define INT_MULTIPLY_WRAPV(a, b, r) \
  343. _GL_INT_OP_WRAPV (a, b, r, *, _GL_INT_MULTIPLY_RANGE_OVERFLOW)
  344. #endif
  345. /* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See:
  346. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193
  347. https://llvm.org/bugs/show_bug.cgi?id=25390
  348. For now, assume all versions of GCC-like compilers generate bogus
  349. warnings for _Generic. This matters only for compilers that
  350. lack relevant builtins. */
  351. #if __GNUC__ || defined __clang__
  352. # define _GL__GENERIC_BOGUS 1
  353. #else
  354. # define _GL__GENERIC_BOGUS 0
  355. #endif
  356. /* Store the low-order bits of A <op> B into *R, where OP specifies
  357. the operation and OVERFLOW the overflow predicate. Return 1 if the
  358. result overflows. See above for restrictions. */
  359. #if 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
  360. # define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \
  361. (_Generic \
  362. (*(r), \
  363. signed char: \
  364. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  365. signed char, SCHAR_MIN, SCHAR_MAX), \
  366. unsigned char: \
  367. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  368. unsigned char, 0, UCHAR_MAX), \
  369. short int: \
  370. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  371. short int, SHRT_MIN, SHRT_MAX), \
  372. unsigned short int: \
  373. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  374. unsigned short int, 0, USHRT_MAX), \
  375. int: \
  376. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  377. int, INT_MIN, INT_MAX), \
  378. unsigned int: \
  379. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  380. unsigned int, 0, UINT_MAX), \
  381. long int: \
  382. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  383. long int, LONG_MIN, LONG_MAX), \
  384. unsigned long int: \
  385. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  386. unsigned long int, 0, ULONG_MAX), \
  387. long long int: \
  388. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
  389. long long int, LLONG_MIN, LLONG_MAX), \
  390. unsigned long long int: \
  391. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
  392. unsigned long long int, 0, ULLONG_MAX)))
  393. #else
  394. /* Store the low-order bits of A <op> B into *R, where OP specifies
  395. the operation and OVERFLOW the overflow predicate. If *R is
  396. signed, its type is ST with bounds SMIN..SMAX; otherwise its type
  397. is UT with bounds U..UMAX. ST and UT are narrower than int.
  398. Return 1 if the result overflows. See above for restrictions. */
  399. # if _GL_HAVE___TYPEOF__
  400. # define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \
  401. (TYPE_SIGNED (__typeof__ (*(r))) \
  402. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, st, smin, smax) \
  403. : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, ut, 0, umax))
  404. # else
  405. # define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \
  406. (overflow (a, b, smin, smax) \
  407. ? (overflow (a, b, 0, umax) \
  408. ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 1) \
  409. : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) < 0) \
  410. : (overflow (a, b, 0, umax) \
  411. ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) >= 0 \
  412. : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 0)))
  413. # endif
  414. # define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \
  415. (sizeof *(r) == sizeof (signed char) \
  416. ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \
  417. signed char, SCHAR_MIN, SCHAR_MAX, \
  418. unsigned char, UCHAR_MAX) \
  419. : sizeof *(r) == sizeof (short int) \
  420. ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \
  421. short int, SHRT_MIN, SHRT_MAX, \
  422. unsigned short int, USHRT_MAX) \
  423. : sizeof *(r) == sizeof (int) \
  424. ? (EXPR_SIGNED (*(r)) \
  425. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  426. int, INT_MIN, INT_MAX) \
  427. : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  428. unsigned int, 0, UINT_MAX)) \
  429. : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow))
  430. # ifdef LLONG_MAX
  431. # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
  432. (sizeof *(r) == sizeof (long int) \
  433. ? (EXPR_SIGNED (*(r)) \
  434. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  435. long int, LONG_MIN, LONG_MAX) \
  436. : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  437. unsigned long int, 0, ULONG_MAX)) \
  438. : (EXPR_SIGNED (*(r)) \
  439. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
  440. long long int, LLONG_MIN, LLONG_MAX) \
  441. : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
  442. unsigned long long int, 0, ULLONG_MAX)))
  443. # else
  444. # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
  445. (EXPR_SIGNED (*(r)) \
  446. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  447. long int, LONG_MIN, LONG_MAX) \
  448. : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  449. unsigned long int, 0, ULONG_MAX))
  450. # endif
  451. #endif
  452. /* Store the low-order bits of A <op> B into *R, where the operation
  453. is given by OP. Use the unsigned type UT for calculation to avoid
  454. overflow problems. *R's type is T, with extrema TMIN and TMAX.
  455. T must be a signed integer type. Return 1 if the result overflows. */
  456. #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
  457. (overflow (a, b, tmin, tmax) \
  458. ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \
  459. : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0))
  460. /* Return the low-order bits of A <op> B, where the operation is given
  461. by OP. Use the unsigned type UT for calculation to avoid undefined
  462. behavior on signed integer overflow, and convert the result to type T.
  463. UT is at least as wide as T and is no narrower than unsigned int,
  464. T is two's complement, and there is no padding or trap representations.
  465. Assume that converting UT to T yields the low-order bits, as is
  466. done in all known two's-complement C compilers. E.g., see:
  467. https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html
  468. According to the C standard, converting UT to T yields an
  469. implementation-defined result or signal for values outside T's
  470. range. However, code that works around this theoretical problem
  471. runs afoul of a compiler bug in Oracle Studio 12.3 x86. See:
  472. https://lists.gnu.org/r/bug-gnulib/2017-04/msg00049.html
  473. As the compiler bug is real, don't try to work around the
  474. theoretical problem. */
  475. #define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \
  476. ((t) ((ut) (a) op (ut) (b)))
  477. /* Return true if the numeric values A + B, A - B, A * B fall outside
  478. the range TMIN..TMAX. Arguments should be integer expressions
  479. without side effects. TMIN should be signed and nonpositive.
  480. TMAX should be positive, and should be signed unless TMIN is zero. */
  481. #define _GL_INT_ADD_RANGE_OVERFLOW(a, b, tmin, tmax) \
  482. ((b) < 0 \
  483. ? (((tmin) \
  484. ? ((EXPR_SIGNED (_GL_INT_CONVERT (a, (tmin) - (b))) || (b) < (tmin)) \
  485. && (a) < (tmin) - (b)) \
  486. : (a) <= -1 - (b)) \
  487. || ((EXPR_SIGNED (a) ? 0 <= (a) : (tmax) < (a)) && (tmax) < (a) + (b))) \
  488. : (a) < 0 \
  489. ? (((tmin) \
  490. ? ((EXPR_SIGNED (_GL_INT_CONVERT (b, (tmin) - (a))) || (a) < (tmin)) \
  491. && (b) < (tmin) - (a)) \
  492. : (b) <= -1 - (a)) \
  493. || ((EXPR_SIGNED (_GL_INT_CONVERT (a, b)) || (tmax) < (b)) \
  494. && (tmax) < (a) + (b))) \
  495. : (tmax) < (b) || (tmax) - (b) < (a))
  496. #define _GL_INT_SUBTRACT_RANGE_OVERFLOW(a, b, tmin, tmax) \
  497. (((a) < 0) == ((b) < 0) \
  498. ? ((a) < (b) \
  499. ? !(tmin) || -1 - (tmin) < (b) - (a) - 1 \
  500. : (tmax) < (a) - (b)) \
  501. : (a) < 0 \
  502. ? ((!EXPR_SIGNED (_GL_INT_CONVERT ((a) - (tmin), b)) && (a) - (tmin) < 0) \
  503. || (a) - (tmin) < (b)) \
  504. : ((! (EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
  505. && EXPR_SIGNED (_GL_INT_CONVERT ((tmax) + (b), a))) \
  506. && (tmax) <= -1 - (b)) \
  507. || (tmax) + (b) < (a)))
  508. #define _GL_INT_MULTIPLY_RANGE_OVERFLOW(a, b, tmin, tmax) \
  509. ((b) < 0 \
  510. ? ((a) < 0 \
  511. ? (EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
  512. ? (a) < (tmax) / (b) \
  513. : ((INT_NEGATE_OVERFLOW (b) \
  514. ? _GL_INT_CONVERT (b, tmax) >> (TYPE_WIDTH (+ (b)) - 1) \
  515. : (tmax) / -(b)) \
  516. <= -1 - (a))) \
  517. : INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (b, tmin)) && (b) == -1 \
  518. ? (EXPR_SIGNED (a) \
  519. ? 0 < (a) + (tmin) \
  520. : 0 < (a) && -1 - (tmin) < (a) - 1) \
  521. : (tmin) / (b) < (a)) \
  522. : (b) == 0 \
  523. ? 0 \
  524. : ((a) < 0 \
  525. ? (INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (a, tmin)) && (a) == -1 \
  526. ? (EXPR_SIGNED (b) ? 0 < (b) + (tmin) : -1 - (tmin) < (b) - 1) \
  527. : (tmin) / (a) < (b)) \
  528. : (tmax) / (b) < (a)))
  529. /* The following macros compute A + B, A - B, and A * B, respectively.
  530. If no overflow occurs, they set *R to the result and return 1;
  531. otherwise, they return 0 and may modify *R.
  532. Example usage:
  533. long int result;
  534. if (INT_ADD_OK (a, b, &result))
  535. printf ("result is %ld\n", result);
  536. else
  537. printf ("overflow\n");
  538. A, B, and *R should be integers; they need not be the same type,
  539. and they need not be all signed or all unsigned.
  540. These macros work correctly on all known practical hosts, and do not rely
  541. on undefined behavior due to signed arithmetic overflow.
  542. These macros are not constant expressions.
  543. These macros may evaluate their arguments zero or multiple times, so the
  544. arguments should not have side effects.
  545. These macros are tuned for B being a constant. */
  546. #define INT_ADD_OK(a, b, r) ! INT_ADD_WRAPV (a, b, r)
  547. #define INT_SUBTRACT_OK(a, b, r) ! INT_SUBTRACT_WRAPV (a, b, r)
  548. #define INT_MULTIPLY_OK(a, b, r) ! INT_MULTIPLY_WRAPV (a, b, r)
  549. #endif /* _GL_INTPROPS_H */