intprops.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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 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. /* 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 harder to use and may be less
  107. efficient than the INT_<op>_WRAPV, INT_<op>_OK, and
  108. INT_<op>_OVERFLOW macros described below.
  109. Example usage:
  110. long int i = ...;
  111. long int j = ...;
  112. if (INT_MULTIPLY_RANGE_OVERFLOW (i, j, LONG_MIN, LONG_MAX))
  113. printf ("multiply would overflow");
  114. else
  115. printf ("product is %ld", i * j);
  116. Restrictions on *_RANGE_OVERFLOW macros:
  117. These macros do not check for all possible numerical problems or
  118. undefined or unspecified behavior: they do not check for division
  119. by zero, for bad shift counts, or for shifting negative numbers.
  120. These macros may evaluate their arguments zero or multiple times,
  121. so the arguments should not have side effects. The arithmetic
  122. arguments (including the MIN and MAX arguments) must be of the same
  123. integer type after the usual arithmetic conversions, and the type
  124. must have minimum value MIN and maximum MAX. Unsigned types should
  125. use a zero MIN of the proper type.
  126. Because all arguments are subject to integer promotions, these
  127. macros typically do not work on types narrower than 'int'.
  128. These macros are tuned for constant MIN and MAX. For commutative
  129. operations such as A + B, they are also tuned for constant B. */
  130. /* Return 1 if A + B would overflow in [MIN,MAX] arithmetic.
  131. See above for restrictions. */
  132. #define INT_ADD_RANGE_OVERFLOW(a, b, min, max) \
  133. ((b) < 0 \
  134. ? (a) < (min) - (b) \
  135. : (max) - (b) < (a))
  136. /* Return 1 if A - B would overflow in [MIN,MAX] arithmetic.
  137. See above for restrictions. */
  138. #define INT_SUBTRACT_RANGE_OVERFLOW(a, b, min, max) \
  139. ((b) < 0 \
  140. ? (max) + (b) < (a) \
  141. : (a) < (min) + (b))
  142. /* Return 1 if - A would overflow in [MIN,MAX] arithmetic.
  143. See above for restrictions. */
  144. #define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \
  145. ((min) < 0 \
  146. ? (a) < - (max) \
  147. : 0 < (a))
  148. /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic.
  149. See above for restrictions. Avoid && and || as they tickle
  150. bugs in Sun C 5.11 2010/08/13 and other compilers; see
  151. <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>. */
  152. #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \
  153. ((b) < 0 \
  154. ? ((a) < 0 \
  155. ? (a) < (max) / (b) \
  156. : (b) == -1 \
  157. ? 0 \
  158. : (min) / (b) < (a)) \
  159. : (b) == 0 \
  160. ? 0 \
  161. : ((a) < 0 \
  162. ? (a) < (min) / (b) \
  163. : (max) / (b) < (a)))
  164. /* Return 1 if A / B would overflow in [MIN,MAX] arithmetic.
  165. See above for restrictions. Do not check for division by zero. */
  166. #define INT_DIVIDE_RANGE_OVERFLOW(a, b, min, max) \
  167. ((min) < 0 && (b) == -1 && (a) < - (max))
  168. /* Return 1 if A % B would overflow in [MIN,MAX] arithmetic.
  169. See above for restrictions. Do not check for division by zero.
  170. Mathematically, % should never overflow, but on x86-like hosts
  171. INT_MIN % -1 traps, and the C standard permits this, so treat this
  172. as an overflow too. */
  173. #define INT_REMAINDER_RANGE_OVERFLOW(a, b, min, max) \
  174. INT_DIVIDE_RANGE_OVERFLOW (a, b, min, max)
  175. /* Return 1 if A << B would overflow in [MIN,MAX] arithmetic.
  176. See above for restrictions. Here, MIN and MAX are for A only, and B need
  177. not be of the same type as the other arguments. The C standard says that
  178. behavior is undefined for shifts unless 0 <= B < wordwidth, and that when
  179. A is negative then A << B has undefined behavior and A >> B has
  180. implementation-defined behavior, but do not check these other
  181. restrictions. */
  182. #define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \
  183. ((a) < 0 \
  184. ? (a) < (min) >> (b) \
  185. : (max) >> (b) < (a))
  186. /* True if __builtin_add_overflow (A, B, P) and __builtin_sub_overflow
  187. (A, B, P) work when P is non-null. */
  188. /* __builtin_{add,sub}_overflow exists but is not reliable in GCC 5.x and 6.x,
  189. see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98269>. */
  190. #if 7 <= __GNUC__ && !defined __ICC
  191. # define _GL_HAS_BUILTIN_ADD_OVERFLOW 1
  192. #elif defined __has_builtin
  193. # define _GL_HAS_BUILTIN_ADD_OVERFLOW __has_builtin (__builtin_add_overflow)
  194. #else
  195. # define _GL_HAS_BUILTIN_ADD_OVERFLOW 0
  196. #endif
  197. /* True if __builtin_mul_overflow (A, B, P) works when P is non-null. */
  198. #ifdef __clang__
  199. /* Work around Clang bug <https://bugs.llvm.org/show_bug.cgi?id=16404>. */
  200. # define _GL_HAS_BUILTIN_MUL_OVERFLOW 0
  201. #else
  202. # define _GL_HAS_BUILTIN_MUL_OVERFLOW _GL_HAS_BUILTIN_ADD_OVERFLOW
  203. #endif
  204. /* True if __builtin_add_overflow_p (A, B, C) works, and similarly for
  205. __builtin_sub_overflow_p and __builtin_mul_overflow_p. */
  206. #if defined __clang__ || defined __ICC
  207. /* Clang 11 lacks __builtin_mul_overflow_p, and even if it did it
  208. would presumably run afoul of Clang bug 16404. ICC 2021.1's
  209. __builtin_add_overflow_p etc. are not treated as integral constant
  210. expressions even when all arguments are. */
  211. # define _GL_HAS_BUILTIN_OVERFLOW_P 0
  212. #elif defined __has_builtin
  213. # define _GL_HAS_BUILTIN_OVERFLOW_P __has_builtin (__builtin_mul_overflow_p)
  214. #else
  215. # define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
  216. #endif
  217. /* The _GL*_OVERFLOW macros have the same restrictions as the
  218. *_RANGE_OVERFLOW macros, except that they do not assume that operands
  219. (e.g., A and B) have the same type as MIN and MAX. Instead, they assume
  220. that the result (e.g., A + B) has that type. */
  221. #if _GL_HAS_BUILTIN_OVERFLOW_P
  222. # define _GL_ADD_OVERFLOW(a, b, min, max) \
  223. __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
  224. # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
  225. __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
  226. # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
  227. __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0)
  228. #else
  229. # define _GL_ADD_OVERFLOW(a, b, min, max) \
  230. ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
  231. : (a) < 0 ? (b) <= (a) + (b) \
  232. : (b) < 0 ? (a) <= (a) + (b) \
  233. : (a) + (b) < (b))
  234. # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
  235. ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \
  236. : (a) < 0 ? 1 \
  237. : (b) < 0 ? (a) - (b) <= (a) \
  238. : (a) < (b))
  239. # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
  240. (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \
  241. || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))
  242. #endif
  243. #define _GL_DIVIDE_OVERFLOW(a, b, min, max) \
  244. ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
  245. : (a) < 0 ? (b) <= (a) + (b) - 1 \
  246. : (b) < 0 && (a) + (b) <= (a))
  247. #define _GL_REMAINDER_OVERFLOW(a, b, min, max) \
  248. ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
  249. : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \
  250. : (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max))
  251. /* Return a nonzero value if A is a mathematical multiple of B, where
  252. A is unsigned, B is negative, and MAX is the maximum value of A's
  253. type. A's type must be the same as (A % B)'s type. Normally (A %
  254. -B == 0) suffices, but things get tricky if -B would overflow. */
  255. #define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \
  256. (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \
  257. ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \
  258. ? (a) \
  259. : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \
  260. : (a) % - (b)) \
  261. == 0)
  262. /* Check for integer overflow, and report low order bits of answer.
  263. The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators
  264. might not yield numerically correct answers due to arithmetic overflow.
  265. The INT_<op>_WRAPV macros compute the low-order bits of the sum,
  266. difference, and product of two C integers, and return 1 if these
  267. low-order bits are not numerically correct.
  268. These macros work correctly on all known practical hosts, and do not rely
  269. on undefined behavior due to signed arithmetic overflow.
  270. Example usage, assuming A and B are long int:
  271. if (INT_MULTIPLY_OVERFLOW (a, b))
  272. printf ("result would overflow\n");
  273. else
  274. printf ("result is %ld (no overflow)\n", a * b);
  275. Example usage with WRAPV flavor:
  276. long int result;
  277. bool overflow = INT_MULTIPLY_WRAPV (a, b, &result);
  278. printf ("result is %ld (%s)\n", result,
  279. overflow ? "after overflow" : "no overflow");
  280. Restrictions on these macros:
  281. These macros do not check for all possible numerical problems or
  282. undefined or unspecified behavior: they do not check for division
  283. by zero, for bad shift counts, or for shifting negative numbers.
  284. These macros may evaluate their arguments zero or multiple times, so the
  285. arguments should not have side effects.
  286. The WRAPV macros are not constant expressions. They support only
  287. +, binary -, and *.
  288. Because the WRAPV macros convert the result, they report overflow
  289. in different circumstances than the OVERFLOW macros do. For
  290. example, in the typical case with 16-bit 'short' and 32-bit 'int',
  291. if A, B and R are all of type 'short' then INT_ADD_OVERFLOW (A, B)
  292. returns false because the addition cannot overflow after A and B
  293. are converted to 'int', whereas INT_ADD_WRAPV (A, B, &R) returns
  294. true or false depending on whether the sum fits into 'short'.
  295. These macros are tuned for their last input argument being a constant.
  296. Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B,
  297. A % B, and A << B would overflow, respectively. */
  298. #define INT_ADD_OVERFLOW(a, b) \
  299. _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
  300. #define INT_SUBTRACT_OVERFLOW(a, b) \
  301. _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
  302. #if _GL_HAS_BUILTIN_OVERFLOW_P
  303. # define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a)
  304. #else
  305. # define INT_NEGATE_OVERFLOW(a) \
  306. INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
  307. #endif
  308. #define INT_MULTIPLY_OVERFLOW(a, b) \
  309. _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)
  310. #define INT_DIVIDE_OVERFLOW(a, b) \
  311. _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW)
  312. #define INT_REMAINDER_OVERFLOW(a, b) \
  313. _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW)
  314. #define INT_LEFT_SHIFT_OVERFLOW(a, b) \
  315. INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
  316. _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
  317. /* Return 1 if the expression A <op> B would overflow,
  318. where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test,
  319. assuming MIN and MAX are the minimum and maximum for the result type.
  320. Arguments should be free of side effects. */
  321. #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \
  322. op_result_overflow (a, b, \
  323. _GL_INT_MINIMUM (_GL_INT_CONVERT (a, b)), \
  324. _GL_INT_MAXIMUM (_GL_INT_CONVERT (a, b)))
  325. /* Store the low-order bits of A + B, A - B, A * B, respectively, into *R.
  326. Return 1 if the result overflows. See above for restrictions. */
  327. #if _GL_HAS_BUILTIN_ADD_OVERFLOW
  328. # define INT_ADD_WRAPV(a, b, r) __builtin_add_overflow (a, b, r)
  329. # define INT_SUBTRACT_WRAPV(a, b, r) __builtin_sub_overflow (a, b, r)
  330. #else
  331. # define INT_ADD_WRAPV(a, b, r) \
  332. _GL_INT_OP_WRAPV (a, b, r, +, _GL_INT_ADD_RANGE_OVERFLOW)
  333. # define INT_SUBTRACT_WRAPV(a, b, r) \
  334. _GL_INT_OP_WRAPV (a, b, r, -, _GL_INT_SUBTRACT_RANGE_OVERFLOW)
  335. #endif
  336. #if _GL_HAS_BUILTIN_MUL_OVERFLOW
  337. # if ((9 < __GNUC__ + (3 <= __GNUC_MINOR__) \
  338. || (__GNUC__ == 8 && 4 <= __GNUC_MINOR__)) \
  339. && !defined __ICC)
  340. # define INT_MULTIPLY_WRAPV(a, b, r) __builtin_mul_overflow (a, b, r)
  341. # else
  342. /* Work around GCC bug 91450. */
  343. # define INT_MULTIPLY_WRAPV(a, b, r) \
  344. ((!_GL_SIGNED_TYPE_OR_EXPR (*(r)) && EXPR_SIGNED (a) && EXPR_SIGNED (b) \
  345. && _GL_INT_MULTIPLY_RANGE_OVERFLOW (a, b, 0, (__typeof__ (*(r))) -1)) \
  346. ? ((void) __builtin_mul_overflow (a, b, r), 1) \
  347. : __builtin_mul_overflow (a, b, r))
  348. # endif
  349. #else
  350. # define INT_MULTIPLY_WRAPV(a, b, r) \
  351. _GL_INT_OP_WRAPV (a, b, r, *, _GL_INT_MULTIPLY_RANGE_OVERFLOW)
  352. #endif
  353. /* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See:
  354. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193
  355. https://llvm.org/bugs/show_bug.cgi?id=25390
  356. For now, assume all versions of GCC-like compilers generate bogus
  357. warnings for _Generic. This matters only for compilers that
  358. lack relevant builtins. */
  359. #if __GNUC__ || defined __clang__
  360. # define _GL__GENERIC_BOGUS 1
  361. #else
  362. # define _GL__GENERIC_BOGUS 0
  363. #endif
  364. /* Store the low-order bits of A <op> B into *R, where OP specifies
  365. the operation and OVERFLOW the overflow predicate. Return 1 if the
  366. result overflows. See above for restrictions. */
  367. #if 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
  368. # define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \
  369. (_Generic \
  370. (*(r), \
  371. signed char: \
  372. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  373. signed char, SCHAR_MIN, SCHAR_MAX), \
  374. unsigned char: \
  375. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  376. unsigned char, 0, UCHAR_MAX), \
  377. short int: \
  378. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  379. short int, SHRT_MIN, SHRT_MAX), \
  380. unsigned short int: \
  381. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  382. unsigned short int, 0, USHRT_MAX), \
  383. int: \
  384. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  385. int, INT_MIN, INT_MAX), \
  386. unsigned int: \
  387. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  388. unsigned int, 0, UINT_MAX), \
  389. long int: \
  390. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  391. long int, LONG_MIN, LONG_MAX), \
  392. unsigned long int: \
  393. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  394. unsigned long int, 0, ULONG_MAX), \
  395. long long int: \
  396. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
  397. long long int, LLONG_MIN, LLONG_MAX), \
  398. unsigned long long int: \
  399. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
  400. unsigned long long int, 0, ULLONG_MAX)))
  401. #else
  402. /* Store the low-order bits of A <op> B into *R, where OP specifies
  403. the operation and OVERFLOW the overflow predicate. If *R is
  404. signed, its type is ST with bounds SMIN..SMAX; otherwise its type
  405. is UT with bounds U..UMAX. ST and UT are narrower than int.
  406. Return 1 if the result overflows. See above for restrictions. */
  407. # if _GL_HAVE___TYPEOF__
  408. # define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \
  409. (TYPE_SIGNED (__typeof__ (*(r))) \
  410. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, st, smin, smax) \
  411. : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, ut, 0, umax))
  412. # else
  413. # define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \
  414. (overflow (a, b, smin, smax) \
  415. ? (overflow (a, b, 0, umax) \
  416. ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 1) \
  417. : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) < 0) \
  418. : (overflow (a, b, 0, umax) \
  419. ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) >= 0 \
  420. : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 0)))
  421. # endif
  422. # define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \
  423. (sizeof *(r) == sizeof (signed char) \
  424. ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \
  425. signed char, SCHAR_MIN, SCHAR_MAX, \
  426. unsigned char, UCHAR_MAX) \
  427. : sizeof *(r) == sizeof (short int) \
  428. ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \
  429. short int, SHRT_MIN, SHRT_MAX, \
  430. unsigned short int, USHRT_MAX) \
  431. : sizeof *(r) == sizeof (int) \
  432. ? (EXPR_SIGNED (*(r)) \
  433. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  434. int, INT_MIN, INT_MAX) \
  435. : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  436. unsigned int, 0, UINT_MAX)) \
  437. : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow))
  438. # ifdef LLONG_MAX
  439. # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
  440. (sizeof *(r) == sizeof (long int) \
  441. ? (EXPR_SIGNED (*(r)) \
  442. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  443. long int, LONG_MIN, LONG_MAX) \
  444. : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  445. unsigned long int, 0, ULONG_MAX)) \
  446. : (EXPR_SIGNED (*(r)) \
  447. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
  448. long long int, LLONG_MIN, LLONG_MAX) \
  449. : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
  450. unsigned long long int, 0, ULLONG_MAX)))
  451. # else
  452. # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
  453. (EXPR_SIGNED (*(r)) \
  454. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  455. long int, LONG_MIN, LONG_MAX) \
  456. : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  457. unsigned long int, 0, ULONG_MAX))
  458. # endif
  459. #endif
  460. /* Store the low-order bits of A <op> B into *R, where the operation
  461. is given by OP. Use the unsigned type UT for calculation to avoid
  462. overflow problems. *R's type is T, with extrema TMIN and TMAX.
  463. T must be a signed integer type. Return 1 if the result overflows. */
  464. #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
  465. (overflow (a, b, tmin, tmax) \
  466. ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \
  467. : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0))
  468. /* Return the low-order bits of A <op> B, where the operation is given
  469. by OP. Use the unsigned type UT for calculation to avoid undefined
  470. behavior on signed integer overflow, and convert the result to type T.
  471. UT is at least as wide as T and is no narrower than unsigned int,
  472. T is two's complement, and there is no padding or trap representations.
  473. Assume that converting UT to T yields the low-order bits, as is
  474. done in all known two's-complement C compilers. E.g., see:
  475. https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html
  476. According to the C standard, converting UT to T yields an
  477. implementation-defined result or signal for values outside T's
  478. range. However, code that works around this theoretical problem
  479. runs afoul of a compiler bug in Oracle Studio 12.3 x86. See:
  480. https://lists.gnu.org/r/bug-gnulib/2017-04/msg00049.html
  481. As the compiler bug is real, don't try to work around the
  482. theoretical problem. */
  483. #define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \
  484. ((t) ((ut) (a) op (ut) (b)))
  485. /* Return true if the numeric values A + B, A - B, A * B fall outside
  486. the range TMIN..TMAX. Arguments should be integer expressions
  487. without side effects. TMIN should be signed and nonpositive.
  488. TMAX should be positive, and should be signed unless TMIN is zero. */
  489. #define _GL_INT_ADD_RANGE_OVERFLOW(a, b, tmin, tmax) \
  490. ((b) < 0 \
  491. ? (((tmin) \
  492. ? ((EXPR_SIGNED (_GL_INT_CONVERT (a, (tmin) - (b))) || (b) < (tmin)) \
  493. && (a) < (tmin) - (b)) \
  494. : (a) <= -1 - (b)) \
  495. || ((EXPR_SIGNED (a) ? 0 <= (a) : (tmax) < (a)) && (tmax) < (a) + (b))) \
  496. : (a) < 0 \
  497. ? (((tmin) \
  498. ? ((EXPR_SIGNED (_GL_INT_CONVERT (b, (tmin) - (a))) || (a) < (tmin)) \
  499. && (b) < (tmin) - (a)) \
  500. : (b) <= -1 - (a)) \
  501. || ((EXPR_SIGNED (_GL_INT_CONVERT (a, b)) || (tmax) < (b)) \
  502. && (tmax) < (a) + (b))) \
  503. : (tmax) < (b) || (tmax) - (b) < (a))
  504. #define _GL_INT_SUBTRACT_RANGE_OVERFLOW(a, b, tmin, tmax) \
  505. (((a) < 0) == ((b) < 0) \
  506. ? ((a) < (b) \
  507. ? !(tmin) || -1 - (tmin) < (b) - (a) - 1 \
  508. : (tmax) < (a) - (b)) \
  509. : (a) < 0 \
  510. ? ((!EXPR_SIGNED (_GL_INT_CONVERT ((a) - (tmin), b)) && (a) - (tmin) < 0) \
  511. || (a) - (tmin) < (b)) \
  512. : ((! (EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
  513. && EXPR_SIGNED (_GL_INT_CONVERT ((tmax) + (b), a))) \
  514. && (tmax) <= -1 - (b)) \
  515. || (tmax) + (b) < (a)))
  516. #define _GL_INT_MULTIPLY_RANGE_OVERFLOW(a, b, tmin, tmax) \
  517. ((b) < 0 \
  518. ? ((a) < 0 \
  519. ? (EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
  520. ? (a) < (tmax) / (b) \
  521. : ((INT_NEGATE_OVERFLOW (b) \
  522. ? _GL_INT_CONVERT (b, tmax) >> (TYPE_WIDTH (+ (b)) - 1) \
  523. : (tmax) / -(b)) \
  524. <= -1 - (a))) \
  525. : INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (b, tmin)) && (b) == -1 \
  526. ? (EXPR_SIGNED (a) \
  527. ? 0 < (a) + (tmin) \
  528. : 0 < (a) && -1 - (tmin) < (a) - 1) \
  529. : (tmin) / (b) < (a)) \
  530. : (b) == 0 \
  531. ? 0 \
  532. : ((a) < 0 \
  533. ? (INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (a, tmin)) && (a) == -1 \
  534. ? (EXPR_SIGNED (b) ? 0 < (b) + (tmin) : -1 - (tmin) < (b) - 1) \
  535. : (tmin) / (a) < (b)) \
  536. : (tmax) / (b) < (a)))
  537. /* The following macros compute A + B, A - B, and A * B, respectively.
  538. If no overflow occurs, they set *R to the result and return 1;
  539. otherwise, they return 0 and may modify *R.
  540. Example usage:
  541. long int result;
  542. if (INT_ADD_OK (a, b, &result))
  543. printf ("result is %ld\n", result);
  544. else
  545. printf ("overflow\n");
  546. A, B, and *R should be integers; they need not be the same type,
  547. and they need not be all signed or all unsigned.
  548. These macros work correctly on all known practical hosts, and do not rely
  549. on undefined behavior due to signed arithmetic overflow.
  550. These macros are not constant expressions.
  551. These macros may evaluate their arguments zero or multiple times, so the
  552. arguments should not have side effects.
  553. These macros are tuned for B being a constant. */
  554. #define INT_ADD_OK(a, b, r) ! INT_ADD_WRAPV (a, b, r)
  555. #define INT_SUBTRACT_OK(a, b, r) ! INT_SUBTRACT_WRAPV (a, b, r)
  556. #define INT_MULTIPLY_OK(a, b, r) ! INT_MULTIPLY_WRAPV (a, b, r)
  557. #endif /* _GL_INTPROPS_H */