1
0

intprops.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* intprops.h -- properties of integer types
  2. Copyright (C) 2001-2018 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. #define _GL_INT_CONVERT(e, v) (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) (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. */
  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. Padding bits are not supported; this is checked at compile-time below. */
  35. #define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
  36. /* The maximum and minimum values for the integer type T. */
  37. #define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
  38. #define TYPE_MAXIMUM(t) \
  39. ((t) (! TYPE_SIGNED (t) \
  40. ? (t) -1 \
  41. : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
  42. /* The maximum and minimum values for the type of the expression E,
  43. after integer promotion. E should not have side effects. */
  44. #define _GL_INT_MINIMUM(e) \
  45. (EXPR_SIGNED (e) \
  46. ? ~ _GL_SIGNED_INT_MAXIMUM (e) \
  47. : _GL_INT_CONVERT (e, 0))
  48. #define _GL_INT_MAXIMUM(e) \
  49. (EXPR_SIGNED (e) \
  50. ? _GL_SIGNED_INT_MAXIMUM (e) \
  51. : _GL_INT_NEGATE_CONVERT (e, 1))
  52. #define _GL_SIGNED_INT_MAXIMUM(e) \
  53. (((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH ((e) + 0) - 2)) - 1) * 2 + 1)
  54. /* Work around OpenVMS incompatibility with C99. */
  55. #if !defined LLONG_MAX && defined __INT64_MAX
  56. # define LLONG_MAX __INT64_MAX
  57. # define LLONG_MIN __INT64_MIN
  58. #endif
  59. /* This include file assumes that signed types are two's complement without
  60. padding bits; the above macros have undefined behavior otherwise.
  61. If this is a problem for you, please let us know how to fix it for your host.
  62. This assumption is tested by the intprops-tests module. */
  63. /* Does the __typeof__ keyword work? This could be done by
  64. 'configure', but for now it's easier to do it by hand. */
  65. #if (2 <= __GNUC__ \
  66. || (1210 <= __IBMC__ && defined __IBM__TYPEOF__) \
  67. || (0x5110 <= __SUNPRO_C && !__STDC__))
  68. # define _GL_HAVE___TYPEOF__ 1
  69. #else
  70. # define _GL_HAVE___TYPEOF__ 0
  71. #endif
  72. /* Return 1 if the integer type or expression T might be signed. Return 0
  73. if it is definitely unsigned. This macro does not evaluate its argument,
  74. and expands to an integer constant expression. */
  75. #if _GL_HAVE___TYPEOF__
  76. # define _GL_SIGNED_TYPE_OR_EXPR(t) TYPE_SIGNED (__typeof__ (t))
  77. #else
  78. # define _GL_SIGNED_TYPE_OR_EXPR(t) 1
  79. #endif
  80. /* Bound on length of the string representing an unsigned integer
  81. value representable in B bits. log10 (2.0) < 146/485. The
  82. smallest value of B where this bound is not tight is 2621. */
  83. #define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485)
  84. /* Bound on length of the string representing an integer type or expression T.
  85. Subtract 1 for the sign bit if T is signed, and then add 1 more for
  86. a minus sign if needed.
  87. Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 0 when its argument is
  88. signed, this macro may overestimate the true bound by one byte when
  89. applied to unsigned types of size 2, 4, 16, ... bytes. */
  90. #define INT_STRLEN_BOUND(t) \
  91. (INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \
  92. + _GL_SIGNED_TYPE_OR_EXPR (t))
  93. /* Bound on buffer size needed to represent an integer type or expression T,
  94. including the terminating null. */
  95. #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
  96. /* Range overflow checks.
  97. The INT_<op>_RANGE_OVERFLOW macros return 1 if the corresponding C
  98. operators might not yield numerically correct answers due to
  99. arithmetic overflow. They do not rely on undefined or
  100. implementation-defined behavior. Their implementations are simple
  101. and straightforward, but they are a bit harder to use than the
  102. INT_<op>_OVERFLOW macros described below.
  103. Example usage:
  104. long int i = ...;
  105. long int j = ...;
  106. if (INT_MULTIPLY_RANGE_OVERFLOW (i, j, LONG_MIN, LONG_MAX))
  107. printf ("multiply would overflow");
  108. else
  109. printf ("product is %ld", i * j);
  110. Restrictions on *_RANGE_OVERFLOW macros:
  111. These macros do not check for all possible numerical problems or
  112. undefined or unspecified behavior: they do not check for division
  113. by zero, for bad shift counts, or for shifting negative numbers.
  114. These macros may evaluate their arguments zero or multiple times,
  115. so the arguments should not have side effects. The arithmetic
  116. arguments (including the MIN and MAX arguments) must be of the same
  117. integer type after the usual arithmetic conversions, and the type
  118. must have minimum value MIN and maximum MAX. Unsigned types should
  119. use a zero MIN of the proper type.
  120. These macros are tuned for constant MIN and MAX. For commutative
  121. operations such as A + B, they are also tuned for constant B. */
  122. /* Return 1 if A + B would overflow in [MIN,MAX] arithmetic.
  123. See above for restrictions. */
  124. #define INT_ADD_RANGE_OVERFLOW(a, b, min, max) \
  125. ((b) < 0 \
  126. ? (a) < (min) - (b) \
  127. : (max) - (b) < (a))
  128. /* Return 1 if A - B would overflow in [MIN,MAX] arithmetic.
  129. See above for restrictions. */
  130. #define INT_SUBTRACT_RANGE_OVERFLOW(a, b, min, max) \
  131. ((b) < 0 \
  132. ? (max) + (b) < (a) \
  133. : (a) < (min) + (b))
  134. /* Return 1 if - A would overflow in [MIN,MAX] arithmetic.
  135. See above for restrictions. */
  136. #define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \
  137. ((min) < 0 \
  138. ? (a) < - (max) \
  139. : 0 < (a))
  140. /* Return 1 if A * B would overflow in [MIN,MAX] arithmetic.
  141. See above for restrictions. Avoid && and || as they tickle
  142. bugs in Sun C 5.11 2010/08/13 and other compilers; see
  143. <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>. */
  144. #define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \
  145. ((b) < 0 \
  146. ? ((a) < 0 \
  147. ? (a) < (max) / (b) \
  148. : (b) == -1 \
  149. ? 0 \
  150. : (min) / (b) < (a)) \
  151. : (b) == 0 \
  152. ? 0 \
  153. : ((a) < 0 \
  154. ? (a) < (min) / (b) \
  155. : (max) / (b) < (a)))
  156. /* Return 1 if A / B would overflow in [MIN,MAX] arithmetic.
  157. See above for restrictions. Do not check for division by zero. */
  158. #define INT_DIVIDE_RANGE_OVERFLOW(a, b, min, max) \
  159. ((min) < 0 && (b) == -1 && (a) < - (max))
  160. /* Return 1 if A % B would overflow in [MIN,MAX] arithmetic.
  161. See above for restrictions. Do not check for division by zero.
  162. Mathematically, % should never overflow, but on x86-like hosts
  163. INT_MIN % -1 traps, and the C standard permits this, so treat this
  164. as an overflow too. */
  165. #define INT_REMAINDER_RANGE_OVERFLOW(a, b, min, max) \
  166. INT_DIVIDE_RANGE_OVERFLOW (a, b, min, max)
  167. /* Return 1 if A << B would overflow in [MIN,MAX] arithmetic.
  168. See above for restrictions. Here, MIN and MAX are for A only, and B need
  169. not be of the same type as the other arguments. The C standard says that
  170. behavior is undefined for shifts unless 0 <= B < wordwidth, and that when
  171. A is negative then A << B has undefined behavior and A >> B has
  172. implementation-defined behavior, but do not check these other
  173. restrictions. */
  174. #define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \
  175. ((a) < 0 \
  176. ? (a) < (min) >> (b) \
  177. : (max) >> (b) < (a))
  178. /* True if __builtin_add_overflow (A, B, P) works when P is non-null. */
  179. #if 5 <= __GNUC__ && !defined __ICC
  180. # define _GL_HAS_BUILTIN_OVERFLOW 1
  181. #else
  182. # define _GL_HAS_BUILTIN_OVERFLOW 0
  183. #endif
  184. /* True if __builtin_add_overflow_p (A, B, C) works. */
  185. #define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
  186. /* The _GL*_OVERFLOW macros have the same restrictions as the
  187. *_RANGE_OVERFLOW macros, except that they do not assume that operands
  188. (e.g., A and B) have the same type as MIN and MAX. Instead, they assume
  189. that the result (e.g., A + B) has that type. */
  190. #if _GL_HAS_BUILTIN_OVERFLOW_P
  191. # define _GL_ADD_OVERFLOW(a, b, min, max) \
  192. __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
  193. # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
  194. __builtin_sub_overflow_p (a, b, (__typeof__ ((a) - (b))) 0)
  195. # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
  196. __builtin_mul_overflow_p (a, b, (__typeof__ ((a) * (b))) 0)
  197. #else
  198. # define _GL_ADD_OVERFLOW(a, b, min, max) \
  199. ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
  200. : (a) < 0 ? (b) <= (a) + (b) \
  201. : (b) < 0 ? (a) <= (a) + (b) \
  202. : (a) + (b) < (b))
  203. # define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \
  204. ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \
  205. : (a) < 0 ? 1 \
  206. : (b) < 0 ? (a) - (b) <= (a) \
  207. : (a) < (b))
  208. # define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \
  209. (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \
  210. || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))
  211. #endif
  212. #define _GL_DIVIDE_OVERFLOW(a, b, min, max) \
  213. ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
  214. : (a) < 0 ? (b) <= (a) + (b) - 1 \
  215. : (b) < 0 && (a) + (b) <= (a))
  216. #define _GL_REMAINDER_OVERFLOW(a, b, min, max) \
  217. ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \
  218. : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \
  219. : (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max))
  220. /* Return a nonzero value if A is a mathematical multiple of B, where
  221. A is unsigned, B is negative, and MAX is the maximum value of A's
  222. type. A's type must be the same as (A % B)'s type. Normally (A %
  223. -B == 0) suffices, but things get tricky if -B would overflow. */
  224. #define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \
  225. (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \
  226. ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \
  227. ? (a) \
  228. : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \
  229. : (a) % - (b)) \
  230. == 0)
  231. /* Check for integer overflow, and report low order bits of answer.
  232. The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators
  233. might not yield numerically correct answers due to arithmetic overflow.
  234. The INT_<op>_WRAPV macros also store the low-order bits of the answer.
  235. These macros work correctly on all known practical hosts, and do not rely
  236. on undefined behavior due to signed arithmetic overflow.
  237. Example usage, assuming A and B are long int:
  238. if (INT_MULTIPLY_OVERFLOW (a, b))
  239. printf ("result would overflow\n");
  240. else
  241. printf ("result is %ld (no overflow)\n", a * b);
  242. Example usage with WRAPV flavor:
  243. long int result;
  244. bool overflow = INT_MULTIPLY_WRAPV (a, b, &result);
  245. printf ("result is %ld (%s)\n", result,
  246. overflow ? "after overflow" : "no overflow");
  247. Restrictions on these macros:
  248. These macros do not check for all possible numerical problems or
  249. undefined or unspecified behavior: they do not check for division
  250. by zero, for bad shift counts, or for shifting negative numbers.
  251. These macros may evaluate their arguments zero or multiple times, so the
  252. arguments should not have side effects.
  253. The WRAPV macros are not constant expressions. They support only
  254. +, binary -, and *. The result type must be signed.
  255. These macros are tuned for their last argument being a constant.
  256. Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B,
  257. A % B, and A << B would overflow, respectively. */
  258. #define INT_ADD_OVERFLOW(a, b) \
  259. _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
  260. #define INT_SUBTRACT_OVERFLOW(a, b) \
  261. _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW)
  262. #if _GL_HAS_BUILTIN_OVERFLOW_P
  263. # define INT_NEGATE_OVERFLOW(a) INT_SUBTRACT_OVERFLOW (0, a)
  264. #else
  265. # define INT_NEGATE_OVERFLOW(a) \
  266. INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
  267. #endif
  268. #define INT_MULTIPLY_OVERFLOW(a, b) \
  269. _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)
  270. #define INT_DIVIDE_OVERFLOW(a, b) \
  271. _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW)
  272. #define INT_REMAINDER_OVERFLOW(a, b) \
  273. _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW)
  274. #define INT_LEFT_SHIFT_OVERFLOW(a, b) \
  275. INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
  276. _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))
  277. /* Return 1 if the expression A <op> B would overflow,
  278. where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test,
  279. assuming MIN and MAX are the minimum and maximum for the result type.
  280. Arguments should be free of side effects. */
  281. #define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \
  282. op_result_overflow (a, b, \
  283. _GL_INT_MINIMUM (0 * (b) + (a)), \
  284. _GL_INT_MAXIMUM (0 * (b) + (a)))
  285. /* Store the low-order bits of A + B, A - B, A * B, respectively, into *R.
  286. Return 1 if the result overflows. See above for restrictions. */
  287. #define INT_ADD_WRAPV(a, b, r) \
  288. _GL_INT_OP_WRAPV (a, b, r, +, __builtin_add_overflow, INT_ADD_OVERFLOW)
  289. #define INT_SUBTRACT_WRAPV(a, b, r) \
  290. _GL_INT_OP_WRAPV (a, b, r, -, __builtin_sub_overflow, INT_SUBTRACT_OVERFLOW)
  291. #define INT_MULTIPLY_WRAPV(a, b, r) \
  292. _GL_INT_OP_WRAPV (a, b, r, *, __builtin_mul_overflow, INT_MULTIPLY_OVERFLOW)
  293. /* Nonzero if this compiler has GCC bug 68193 or Clang bug 25390. See:
  294. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68193
  295. https://llvm.org/bugs/show_bug.cgi?id=25390
  296. For now, assume all versions of GCC-like compilers generate bogus
  297. warnings for _Generic. This matters only for older compilers that
  298. lack __builtin_add_overflow. */
  299. #if __GNUC__
  300. # define _GL__GENERIC_BOGUS 1
  301. #else
  302. # define _GL__GENERIC_BOGUS 0
  303. #endif
  304. /* Store the low-order bits of A <op> B into *R, where OP specifies
  305. the operation. BUILTIN is the builtin operation, and OVERFLOW the
  306. overflow predicate. Return 1 if the result overflows. See above
  307. for restrictions. */
  308. #if _GL_HAS_BUILTIN_OVERFLOW
  309. # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) builtin (a, b, r)
  310. #elif 201112 <= __STDC_VERSION__ && !_GL__GENERIC_BOGUS
  311. # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
  312. (_Generic \
  313. (*(r), \
  314. signed char: \
  315. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  316. signed char, SCHAR_MIN, SCHAR_MAX), \
  317. short int: \
  318. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  319. short int, SHRT_MIN, SHRT_MAX), \
  320. int: \
  321. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  322. int, INT_MIN, INT_MAX), \
  323. long int: \
  324. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  325. long int, LONG_MIN, LONG_MAX), \
  326. long long int: \
  327. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
  328. long long int, LLONG_MIN, LLONG_MAX)))
  329. #else
  330. # define _GL_INT_OP_WRAPV(a, b, r, op, builtin, overflow) \
  331. (sizeof *(r) == sizeof (signed char) \
  332. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  333. signed char, SCHAR_MIN, SCHAR_MAX) \
  334. : sizeof *(r) == sizeof (short int) \
  335. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  336. short int, SHRT_MIN, SHRT_MAX) \
  337. : sizeof *(r) == sizeof (int) \
  338. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned int, \
  339. int, INT_MIN, INT_MAX) \
  340. : _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow))
  341. # ifdef LLONG_MAX
  342. # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
  343. (sizeof *(r) == sizeof (long int) \
  344. ? _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  345. long int, LONG_MIN, LONG_MAX) \
  346. : _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long long int, \
  347. long long int, LLONG_MIN, LLONG_MAX))
  348. # else
  349. # define _GL_INT_OP_WRAPV_LONGISH(a, b, r, op, overflow) \
  350. _GL_INT_OP_CALC (a, b, r, op, overflow, unsigned long int, \
  351. long int, LONG_MIN, LONG_MAX)
  352. # endif
  353. #endif
  354. /* Store the low-order bits of A <op> B into *R, where the operation
  355. is given by OP. Use the unsigned type UT for calculation to avoid
  356. overflow problems. *R's type is T, with extrema TMIN and TMAX.
  357. T must be a signed integer type. Return 1 if the result overflows. */
  358. #define _GL_INT_OP_CALC(a, b, r, op, overflow, ut, t, tmin, tmax) \
  359. (sizeof ((a) op (b)) < sizeof (t) \
  360. ? _GL_INT_OP_CALC1 ((t) (a), (t) (b), r, op, overflow, ut, t, tmin, tmax) \
  361. : _GL_INT_OP_CALC1 (a, b, r, op, overflow, ut, t, tmin, tmax))
  362. #define _GL_INT_OP_CALC1(a, b, r, op, overflow, ut, t, tmin, tmax) \
  363. ((overflow (a, b) \
  364. || (EXPR_SIGNED ((a) op (b)) && ((a) op (b)) < (tmin)) \
  365. || (tmax) < ((a) op (b))) \
  366. ? (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 1) \
  367. : (*(r) = _GL_INT_OP_WRAPV_VIA_UNSIGNED (a, b, op, ut, t), 0))
  368. /* Return the low-order bits of A <op> B, where the operation is given
  369. by OP. Use the unsigned type UT for calculation to avoid undefined
  370. behavior on signed integer overflow, and convert the result to type T.
  371. UT is at least as wide as T and is no narrower than unsigned int,
  372. T is two's complement, and there is no padding or trap representations.
  373. Assume that converting UT to T yields the low-order bits, as is
  374. done in all known two's-complement C compilers. E.g., see:
  375. https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html
  376. According to the C standard, converting UT to T yields an
  377. implementation-defined result or signal for values outside T's
  378. range. However, code that works around this theoretical problem
  379. runs afoul of a compiler bug in Oracle Studio 12.3 x86. See:
  380. https://lists.gnu.org/r/bug-gnulib/2017-04/msg00049.html
  381. As the compiler bug is real, don't try to work around the
  382. theoretical problem. */
  383. #define _GL_INT_OP_WRAPV_VIA_UNSIGNED(a, b, op, ut, t) \
  384. ((t) ((ut) (a) op (ut) (b)))
  385. #endif /* _GL_INTPROPS_H */