intprops.h 21 KB

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