intprops.h 21 KB

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