intprops.h 29 KB

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