intprops.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /* intprops.h -- properties of integer types
  2. Copyright (C) 2001-2020 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. #if 5 <= __GNUC__ && !defined __ICC
  186. # define _GL_HAS_BUILTIN_ADD_OVERFLOW 1
  187. #elif defined __has_builtin
  188. # define _GL_HAS_BUILTIN_ADD_OVERFLOW __has_builtin (__builtin_add_overflow)
  189. #else
  190. # define _GL_HAS_BUILTIN_ADD_OVERFLOW 0
  191. #endif
  192. /* True if __builtin_mul_overflow (A, B, P) works when P is non-null. */
  193. #ifdef __clang__
  194. /* Work around Clang bug <https://bugs.llvm.org/show_bug.cgi?id=16404>. */
  195. # define _GL_HAS_BUILTIN_MUL_OVERFLOW 0
  196. #else
  197. # define _GL_HAS_BUILTIN_MUL_OVERFLOW _GL_HAS_BUILTIN_ADD_OVERFLOW
  198. #endif
  199. /* True if __builtin_add_overflow_p (A, B, C) works, and similarly for
  200. __builtin_sub_overflow_p and __builtin_mul_overflow_p. */
  201. #ifdef __clang__
  202. /* Clang 9 lacks __builtin_mul_overflow_p, and even if it did it would
  203. presumably run afoul of Clang bug 16404. */
  204. # define _GL_HAS_BUILTIN_OVERFLOW_P 0
  205. #else
  206. # define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
  207. #endif
  208. /* The _GL*_OVERFLOW macros have the same restrictions as the
  209. *_RANGE_OVERFLOW macros, except that they do not assume that operands
  210. (e.g., A and B) have the same type as MIN and MAX. Instead, they assume
  211. that the result (e.g., A + B) has that type. */
  212. #if _GL_HAS_BUILTIN_OVERFLOW_P
  213. # define _GL_ADD_OVERFLOW(a, b, min, max) \
  214. __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
  215. # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
  216. __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
  217. # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
  218. __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0)
  219. #else
  220. # define _GL_ADD_OVERFLOW(a, b, min, max) \
  221. ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
  222. : (a) < 0 ? (b) <= (a) + (b) \
  223. : (b) < 0 ? (a) <= (a) + (b) \
  224. : (a) + (b) < (b))
  225. # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
  226. ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \
  227. : (a) < 0 ? 1 \
  228. : (b) < 0 ? (a) - (b) <= (a) \
  229. : (a) < (b))
  230. # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
  231. (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \
  232. || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))
  233. #endif
  234. #define _GL_DIVIDE_OVERFLOW(a, b, min, max) \
  235. ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
  236. : (a) < 0 ? (b) <= (a) + (b) - 1 \
  237. : (b) < 0 && (a) + (b) <= (a))
  238. #define _GL_REMAINDER_OVERFLOW(a, b, min, max) \
  239. ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
  240. : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \
  241. : (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max))
  242. /* Return a nonzero value if A is a mathematical multiple of B, where
  243. A is unsigned, B is negative, and MAX is the maximum value of A's
  244. type. A's type must be the same as (A % B)'s type. Normally (A %
  245. -B == 0) suffices, but things get tricky if -B would overflow. */
  246. #define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \
  247. (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \
  248. ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \
  249. ? (a) \
  250. : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \
  251. : (a) % - (b)) \
  252. == 0)
  253. /* Check for integer overflow, and report low order bits of answer.
  254. The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators
  255. might not yield numerically correct answers due to arithmetic overflow.
  256. The INT_<op>_WRAPV macros compute the low-order bits of the sum,
  257. difference, and product of two C integers, and return 1 if these
  258. low-order bits are not numerically correct.
  259. These macros work correctly on all known practical hosts, and do not rely
  260. on undefined behavior due to signed arithmetic overflow.
  261. Example usage, assuming A and B are long int:
  262. if (INT_MULTIPLY_OVERFLOW (a, b))
  263. printf ("result would overflow\n");
  264. else
  265. printf ("result is %ld (no overflow)\n", a * b);
  266. Example usage with WRAPV flavor:
  267. long int result;
  268. bool overflow = INT_MULTIPLY_WRAPV (a, b, &result);
  269. printf ("result is %ld (%s)\n", result,
  270. overflow ? "after overflow" : "no overflow");
  271. Restrictions on these macros:
  272. These macros do not check for all possible numerical problems or
  273. undefined or unspecified behavior: they do not check for division
  274. by zero, for bad shift counts, or for shifting negative numbers.
  275. These macros may evaluate their arguments zero or multiple times, so the
  276. arguments should not have side effects.
  277. The WRAPV macros are not constant expressions. They support only
  278. +, binary -, and *. Because the WRAPV macros convert the result,
  279. they report overflow in different circumstances than the OVERFLOW
  280. macros do.
  281. These macros are tuned for their last input argument being a constant.
  282. Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B,
  283. A % B, and A << B would overflow, respectively. */
  284. #define INT_ADD_OVERFLOW(a, b) \
  285. _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
  286. #define INT_SUBTRACT_OVERFLOW(a, b) \
  287. _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
  288. #if _GL_HAS_BUILTIN_OVERFLOW_P
  289. # define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a)
  290. #else
  291. # define INT_NEGATE_OVERFLOW(a) \
  292. INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
  293. #endif
  294. #define INT_MULTIPLY_OVERFLOW(a, b) \
  295. _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)
  296. #define INT_DIVIDE_OVERFLOW(a, b) \
  297. _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW)
  298. #define INT_REMAINDER_OVERFLOW(a, b) \
  299. _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW)
  300. #define INT_LEFT_SHIFT_OVERFLOW(a, b) \
  301. INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
  302. _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
  303. /* Return 1 if the expression A <op> B would overflow,
  304. where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test,
  305. assuming MIN and MAX are the minimum and maximum for the result type.
  306. Arguments should be free of side effects. */
  307. #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \
  308. op_result_overflow (a, b, \
  309. _GL_INT_MINIMUM (_GL_INT_CONVERT (a, b)), \
  310. _GL_INT_MAXIMUM (_GL_INT_CONVERT (a, b)))
  311. /* Store the low-order bits of A + B, A - B, A * B, respectively, into *R.
  312. Return 1 if the result overflows. See above for restrictions. */
  313. #if _GL_HAS_BUILTIN_ADD_OVERFLOW
  314. # define INT_ADD_WRAPV(a, b, r) __builtin_add_overflow (a, b, r)
  315. # define INT_SUBTRACT_WRAPV(a, b, r) __builtin_sub_overflow (a, b, r)
  316. #else
  317. # define INT_ADD_WRAPV(a, b, r) \
  318. _GL_INT_OP_WRAPV (a, b, r, +, _GL_INT_ADD_RANGE_OVERFLOW)
  319. # define INT_SUBTRACT_WRAPV(a, b, r) \
  320. _GL_INT_OP_WRAPV (a, b, r, -, _GL_INT_SUBTRACT_RANGE_OVERFLOW)
  321. #endif
  322. #if _GL_HAS_BUILTIN_MUL_OVERFLOW
  323. # if (9 < __GNUC__ + (3 <= __GNUC_MINOR__) \
  324. || (__GNUC__ == 8 && 4 <= __GNUC_MINOR__))
  325. # define INT_MULTIPLY_WRAPV(a, b, r) __builtin_mul_overflow (a, b, r)
  326. # else
  327. /* Work around GCC bug 91450. */
  328. # define INT_MULTIPLY_WRAPV(a, b, r) \
  329. ((!_GL_SIGNED_TYPE_OR_EXPR (*(r)) && EXPR_SIGNED (a) && EXPR_SIGNED (b) \
  330. && _GL_INT_MULTIPLY_RANGE_OVERFLOW (a, b, 0, (__typeof__ (*(r))) -1)) \
  331. ? ((void) __builtin_mul_overflow (a, b, r), 1) \
  332. : __builtin_mul_overflow (a, b, r))
  333. # endif
  334. #else
  335. # define INT_MULTIPLY_WRAPV(a, b, r) \
  336. _GL_INT_OP_WRAPV (a, b, r, *, _GL_INT_MULTIPLY_RANGE_OVERFLOW)
  337. #endif
  338. /* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See:
  339. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193
  340. https://llvm.org/bugs/show_bug.cgi?id=25390
  341. For now, assume all versions of GCC-like compilers generate bogus
  342. warnings for _Generic. This matters only for compilers that
  343. lack relevant builtins. */
  344. #if __GNUC__ || defined __clang__
  345. # define _GL__GENERIC_BOGUS 1
  346. #else
  347. # define _GL__GENERIC_BOGUS 0
  348. #endif
  349. /* Store the low-order bits of A <op> B into *R, where OP specifies
  350. the operation and OVERFLOW the overflow predicate. Return 1 if the
  351. result overflows. See above for restrictions. */
  352. #if 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
  353. # define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \
  354. (_Generic \
  355. (*(r), \
  356. signed char: \
  357. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  358. signed char, SCHAR_MIN, SCHAR_MAX), \
  359. unsigned char: \
  360. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  361. unsigned char, 0, UCHAR_MAX), \
  362. short int: \
  363. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  364. short int, SHRT_MIN, SHRT_MAX), \
  365. unsigned short int: \
  366. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  367. unsigned short int, 0, USHRT_MAX), \
  368. int: \
  369. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  370. int, INT_MIN, INT_MAX), \
  371. unsigned int: \
  372. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  373. unsigned int, 0, UINT_MAX), \
  374. long int: \
  375. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  376. long int, LONG_MIN, LONG_MAX), \
  377. unsigned long int: \
  378. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  379. unsigned long int, 0, ULONG_MAX), \
  380. long long int: \
  381. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
  382. long long int, LLONG_MIN, LLONG_MAX), \
  383. unsigned long long int: \
  384. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
  385. unsigned long long int, 0, ULLONG_MAX)))
  386. #else
  387. /* Store the low-order bits of A <op> B into *R, where OP specifies
  388. the operation and OVERFLOW the overflow predicate. If *R is
  389. signed, its type is ST with bounds SMIN..SMAX; otherwise its type
  390. is UT with bounds U..UMAX. ST and UT are narrower than int.
  391. Return 1 if the result overflows. See above for restrictions. */
  392. # if _GL_HAVE___TYPEOF__
  393. # define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \
  394. (TYPE_SIGNED (__typeof__ (*(r))) \
  395. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, st, smin, smax) \
  396. : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, ut, 0, umax))
  397. # else
  398. # define _GL_INT_OP_WRAPV_SMALLISH(a,b,r,op,overflow,st,smin,smax,ut,umax) \
  399. (overflow (a, b, smin, smax) \
  400. ? (overflow (a, b, 0, umax) \
  401. ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 1) \
  402. : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) < 0) \
  403. : (overflow (a, b, 0, umax) \
  404. ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st)) >= 0 \
  405. : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a,b,op,unsigned,st), 0)))
  406. # endif
  407. # define _GL_INT_OP_WRAPV(a, b, r, op, overflow) \
  408. (sizeof *(r) == sizeof (signed char) \
  409. ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \
  410. signed char, SCHAR_MIN, SCHAR_MAX, \
  411. unsigned char, UCHAR_MAX) \
  412. : sizeof *(r) == sizeof (short int) \
  413. ? _GL_INT_OP_WRAPV_SMALLISH (a, b, r, op, overflow, \
  414. short int, SHRT_MIN, SHRT_MAX, \
  415. unsigned short int, USHRT_MAX) \
  416. : sizeof *(r) == sizeof (int) \
  417. ? (EXPR_SIGNED (*(r)) \
  418. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  419. int, INT_MIN, INT_MAX) \
  420. : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  421. unsigned int, 0, UINT_MAX)) \
  422. : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow))
  423. # ifdef LLONG_MAX
  424. # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
  425. (sizeof *(r) == sizeof (long int) \
  426. ? (EXPR_SIGNED (*(r)) \
  427. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  428. long int, LONG_MIN, LONG_MAX) \
  429. : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  430. unsigned long int, 0, ULONG_MAX)) \
  431. : (EXPR_SIGNED (*(r)) \
  432. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
  433. long long int, LLONG_MIN, LLONG_MAX) \
  434. : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
  435. unsigned long long int, 0, ULLONG_MAX)))
  436. # else
  437. # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
  438. (EXPR_SIGNED (*(r)) \
  439. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  440. long int, LONG_MIN, LONG_MAX) \
  441. : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  442. unsigned long int, 0, ULONG_MAX))
  443. # endif
  444. #endif
  445. /* Store the low-order bits of A <op> B into *R, where the operation
  446. is given by OP. Use the unsigned type UT for calculation to avoid
  447. overflow problems. *R's type is T, with extrema TMIN and TMAX.
  448. T must be a signed integer type. Return 1 if the result overflows. */
  449. #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
  450. (overflow (a, b, tmin, tmax) \
  451. ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \
  452. : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0))
  453. /* Return the low-order bits of A <op> B, where the operation is given
  454. by OP. Use the unsigned type UT for calculation to avoid undefined
  455. behavior on signed integer overflow, and convert the result to type T.
  456. UT is at least as wide as T and is no narrower than unsigned int,
  457. T is two's complement, and there is no padding or trap representations.
  458. Assume that converting UT to T yields the low-order bits, as is
  459. done in all known two's-complement C compilers. E.g., see:
  460. https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html
  461. According to the C standard, converting UT to T yields an
  462. implementation-defined result or signal for values outside T's
  463. range. However, code that works around this theoretical problem
  464. runs afoul of a compiler bug in Oracle Studio 12.3 x86. See:
  465. https://lists.gnu.org/r/bug-gnulib/2017-04/msg00049.html
  466. As the compiler bug is real, don't try to work around the
  467. theoretical problem. */
  468. #define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \
  469. ((t) ((ut) (a) op (ut) (b)))
  470. /* Return true if the numeric values A + B, A - B, A * B fall outside
  471. the range TMIN..TMAX. Arguments should be integer expressions
  472. without side effects. TMIN should be signed and nonpositive.
  473. TMAX should be positive, and should be signed unless TMIN is zero. */
  474. #define _GL_INT_ADD_RANGE_OVERFLOW(a, b, tmin, tmax) \
  475. ((b) < 0 \
  476. ? (((tmin) \
  477. ? ((EXPR_SIGNED (_GL_INT_CONVERT (a, (tmin) - (b))) || (b) < (tmin)) \
  478. && (a) < (tmin) - (b)) \
  479. : (a) <= -1 - (b)) \
  480. || ((EXPR_SIGNED (a) ? 0 <= (a) : (tmax) < (a)) && (tmax) < (a) + (b))) \
  481. : (a) < 0 \
  482. ? (((tmin) \
  483. ? ((EXPR_SIGNED (_GL_INT_CONVERT (b, (tmin) - (a))) || (a) < (tmin)) \
  484. && (b) < (tmin) - (a)) \
  485. : (b) <= -1 - (a)) \
  486. || ((EXPR_SIGNED (_GL_INT_CONVERT (a, b)) || (tmax) < (b)) \
  487. && (tmax) < (a) + (b))) \
  488. : (tmax) < (b) || (tmax) - (b) < (a))
  489. #define _GL_INT_SUBTRACT_RANGE_OVERFLOW(a, b, tmin, tmax) \
  490. (((a) < 0) == ((b) < 0) \
  491. ? ((a) < (b) \
  492. ? !(tmin) || -1 - (tmin) < (b) - (a) - 1 \
  493. : (tmax) < (a) - (b)) \
  494. : (a) < 0 \
  495. ? ((!EXPR_SIGNED (_GL_INT_CONVERT ((a) - (tmin), b)) && (a) - (tmin) < 0) \
  496. || (a) - (tmin) < (b)) \
  497. : ((! (EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
  498. && EXPR_SIGNED (_GL_INT_CONVERT ((tmax) + (b), a))) \
  499. && (tmax) <= -1 - (b)) \
  500. || (tmax) + (b) < (a)))
  501. #define _GL_INT_MULTIPLY_RANGE_OVERFLOW(a, b, tmin, tmax) \
  502. ((b) < 0 \
  503. ? ((a) < 0 \
  504. ? (EXPR_SIGNED (_GL_INT_CONVERT (tmax, b)) \
  505. ? (a) < (tmax) / (b) \
  506. : ((INT_NEGATE_OVERFLOW (b) \
  507. ? _GL_INT_CONVERT (b, tmax) >> (TYPE_WIDTH (+ (b)) - 1) \
  508. : (tmax) / -(b)) \
  509. <= -1 - (a))) \
  510. : INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (b, tmin)) && (b) == -1 \
  511. ? (EXPR_SIGNED (a) \
  512. ? 0 < (a) + (tmin) \
  513. : 0 < (a) && -1 - (tmin) < (a) - 1) \
  514. : (tmin) / (b) < (a)) \
  515. : (b) == 0 \
  516. ? 0 \
  517. : ((a) < 0 \
  518. ? (INT_NEGATE_OVERFLOW (_GL_INT_CONVERT (a, tmin)) && (a) == -1 \
  519. ? (EXPR_SIGNED (b) ? 0 < (b) + (tmin) : -1 - (tmin) < (b) - 1) \
  520. : (tmin) / (a) < (b)) \
  521. : (tmax) / (b) < (a)))
  522. #endif /* _GL_INTPROPS_H */