intprops.h 18 KB

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