intprops.h revision 1.4
1/* $NetBSD: intprops.h,v 1.4 2016/01/08 21:35:41 christos Exp $ */ 2 3/* intprops.h -- properties of integer types 4 5 Copyright (C) 2001-2005, 2009-2015 Free Software Foundation, Inc. 6 7 This program is free software: you can redistribute it and/or modify 8 it under the terms of the GNU Lesser General Public License as published by 9 the Free Software Foundation; either version 2.1 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU Lesser General Public License for more details. 16 17 You should have received a copy of the GNU Lesser General Public License 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 20/* Written by Paul Eggert. */ 21 22#ifndef _GL_INTPROPS_H 23#define _GL_INTPROPS_H 24 25#include <limits.h> 26 27/* Return an integer value, converted to the same type as the integer 28 expression E after integer type promotion. V is the unconverted value. */ 29#define _GL_INT_CONVERT(e, v) (0 * (e) + (v)) 30 31/* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see 32 <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00406.html>. */ 33#define _GL_INT_NEGATE_CONVERT(e, v) (0 * (e) - (v)) 34 35/* The extra casts in the following macros work around compiler bugs, 36 e.g., in Cray C 5.0.3.0. */ 37 38/* True if the arithmetic type T is an integer type. bool counts as 39 an integer. */ 40#define TYPE_IS_INTEGER(t) ((t) 1.5 == 1) 41 42/* True if negative values of the signed integer type T use two's 43 complement, ones' complement, or signed magnitude representation, 44 respectively. Much GNU code assumes two's complement, but some 45 people like to be portable to all possible C hosts. */ 46#define TYPE_TWOS_COMPLEMENT(t) ((t) ~ (t) 0 == (t) -1) 47#define TYPE_ONES_COMPLEMENT(t) ((t) ~ (t) 0 == 0) 48#define TYPE_SIGNED_MAGNITUDE(t) ((t) ~ (t) 0 < (t) -1) 49 50/* True if the signed integer expression E uses two's complement. */ 51#define _GL_INT_TWOS_COMPLEMENT(e) (~ _GL_INT_CONVERT (e, 0) == -1) 52 53/* True if the arithmetic type T is signed. */ 54#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) 55 56/* Return 1 if the integer expression E, after integer promotion, has 57 a signed type. */ 58#define _GL_INT_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0) 59 60 61/* Minimum and maximum values for integer types and expressions. These 62 macros have undefined behavior if T is signed and has padding bits. 63 If this is a problem for you, please let us know how to fix it for 64 your host. */ 65 66/* The maximum and minimum values for the integer type T. */ 67#define TYPE_MINIMUM(t) \ 68 ((t) (! TYPE_SIGNED (t) \ 69 ? (t) 0 \ 70 : TYPE_SIGNED_MAGNITUDE (t) \ 71 ? ~ (t) 0 \ 72 : ~ TYPE_MAXIMUM (t))) 73#define TYPE_MAXIMUM(t) \ 74 ((t) (! TYPE_SIGNED (t) \ 75 ? (t) -1 \ 76 : ((((t) 1 << (sizeof (t) * CHAR_BIT - 2)) - 1) * 2 + 1))) 77 78/* The maximum and minimum values for the type of the expression E, 79 after integer promotion. E should not have side effects. */ 80#define _GL_INT_MINIMUM(e) \ 81 (_GL_INT_SIGNED (e) \ 82 ? - _GL_INT_TWOS_COMPLEMENT (e) - _GL_SIGNED_INT_MAXIMUM (e) \ 83 : _GL_INT_CONVERT (e, 0)) 84#define _GL_INT_MAXIMUM(e) \ 85 (_GL_INT_SIGNED (e) \ 86 ? _GL_SIGNED_INT_MAXIMUM (e) \ 87 : _GL_INT_NEGATE_CONVERT (e, 1)) 88#define _GL_SIGNED_INT_MAXIMUM(e) \ 89 (((_GL_INT_CONVERT (e, 1) << (sizeof ((e) + 0) * CHAR_BIT - 2)) - 1) * 2 + 1) 90 91 92/* Return 1 if the __typeof__ keyword works. This could be done by 93 'configure', but for now it's easier to do it by hand. */ 94#if (2 <= __GNUC__ || defined __IBM__TYPEOF__ \ 95 || (0x5110 <= __SUNPRO_C && !__STDC__)) 96# define _GL_HAVE___TYPEOF__ 1 97#else 98# define _GL_HAVE___TYPEOF__ 0 99#endif 100 101/* Return 1 if the integer type or expression T might be signed. Return 0 102 if it is definitely unsigned. This macro does not evaluate its argument, 103 and expands to an integer constant expression. */ 104#if _GL_HAVE___TYPEOF__ 105# define _GL_SIGNED_TYPE_OR_EXPR(t) TYPE_SIGNED (__typeof__ (t)) 106#else 107# define _GL_SIGNED_TYPE_OR_EXPR(t) 1 108#endif 109 110/* Bound on length of the string representing an unsigned integer 111 value representable in B bits. log10 (2.0) < 146/485. The 112 smallest value of B where this bound is not tight is 2621. */ 113#define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485) 114 115/* Bound on length of the string representing an integer type or expression T. 116 Subtract 1 for the sign bit if T is signed, and then add 1 more for 117 a minus sign if needed. 118 119 Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 0 when its argument is 120 signed, this macro may overestimate the true bound by one byte when 121 applied to unsigned types of size 2, 4, 16, ... bytes. */ 122#define INT_STRLEN_BOUND(t) \ 123 (INT_BITS_STRLEN_BOUND (sizeof (t) * CHAR_BIT \ 124 - _GL_SIGNED_TYPE_OR_EXPR (t)) \ 125 + _GL_SIGNED_TYPE_OR_EXPR (t)) 126 127/* Bound on buffer size needed to represent an integer type or expression T, 128 including the terminating null. */ 129#define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1) 130 131 132/* Range overflow checks. 133 134 The INT_<op>_RANGE_OVERFLOW macros return 1 if the corresponding C 135 operators might not yield numerically correct answers due to 136 arithmetic overflow. They do not rely on undefined or 137 implementation-defined behavior. Their implementations are simple 138 and straightforward, but they are a bit harder to use than the 139 INT_<op>_OVERFLOW macros described below. 140 141 Example usage: 142 143 long int i = ...; 144 long int j = ...; 145 if (INT_MULTIPLY_RANGE_OVERFLOW (i, j, LONG_MIN, LONG_MAX)) 146 printf ("multiply would overflow"); 147 else 148 printf ("product is %ld", i * j); 149 150 Restrictions on *_RANGE_OVERFLOW macros: 151 152 These macros do not check for all possible numerical problems or 153 undefined or unspecified behavior: they do not check for division 154 by zero, for bad shift counts, or for shifting negative numbers. 155 156 These macros may evaluate their arguments zero or multiple times, 157 so the arguments should not have side effects. The arithmetic 158 arguments (including the MIN and MAX arguments) must be of the same 159 integer type after the usual arithmetic conversions, and the type 160 must have minimum value MIN and maximum MAX. Unsigned types should 161 use a zero MIN of the proper type. 162 163 These macros are tuned for constant MIN and MAX. For commutative 164 operations such as A + B, they are also tuned for constant B. */ 165 166/* Return 1 if A + B would overflow in [MIN,MAX] arithmetic. 167 See above for restrictions. */ 168#define INT_ADD_RANGE_OVERFLOW(a, b, min, max) \ 169 ((b) < 0 \ 170 ? (a) < (min) - (b) \ 171 : (max) - (b) < (a)) 172 173/* Return 1 if A - B would overflow in [MIN,MAX] arithmetic. 174 See above for restrictions. */ 175#define INT_SUBTRACT_RANGE_OVERFLOW(a, b, min, max) \ 176 ((b) < 0 \ 177 ? (max) + (b) < (a) \ 178 : (a) < (min) + (b)) 179 180/* Return 1 if - A would overflow in [MIN,MAX] arithmetic. 181 See above for restrictions. */ 182#define INT_NEGATE_RANGE_OVERFLOW(a, min, max) \ 183 ((min) < 0 \ 184 ? (a) < - (max) \ 185 : 0 < (a)) 186 187/* Return 1 if A * B would overflow in [MIN,MAX] arithmetic. 188 See above for restrictions. Avoid && and || as they tickle 189 bugs in Sun C 5.11 2010/08/13 and other compilers; see 190 <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00401.html>. */ 191#define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max) \ 192 ((b) < 0 \ 193 ? ((a) < 0 \ 194 ? (a) < (max) / (b) \ 195 : (b) == -1 \ 196 ? 0 \ 197 : (min) / (b) < (a)) \ 198 : (b) == 0 \ 199 ? 0 \ 200 : ((a) < 0 \ 201 ? (a) < (min) / (b) \ 202 : (max) / (b) < (a))) 203 204/* Return 1 if A / B would overflow in [MIN,MAX] arithmetic. 205 See above for restrictions. Do not check for division by zero. */ 206#define INT_DIVIDE_RANGE_OVERFLOW(a, b, min, max) \ 207 ((min) < 0 && (b) == -1 && (a) < - (max)) 208 209/* Return 1 if A % B would overflow in [MIN,MAX] arithmetic. 210 See above for restrictions. Do not check for division by zero. 211 Mathematically, % should never overflow, but on x86-like hosts 212 INT_MIN % -1 traps, and the C standard permits this, so treat this 213 as an overflow too. */ 214#define INT_REMAINDER_RANGE_OVERFLOW(a, b, min, max) \ 215 INT_DIVIDE_RANGE_OVERFLOW (a, b, min, max) 216 217/* Return 1 if A << B would overflow in [MIN,MAX] arithmetic. 218 See above for restrictions. Here, MIN and MAX are for A only, and B need 219 not be of the same type as the other arguments. The C standard says that 220 behavior is undefined for shifts unless 0 <= B < wordwidth, and that when 221 A is negative then A << B has undefined behavior and A >> B has 222 implementation-defined behavior, but do not check these other 223 restrictions. */ 224#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max) \ 225 ((a) < 0 \ 226 ? (a) < (min) >> (b) \ 227 : (max) >> (b) < (a)) 228 229 230/* The _GL*_OVERFLOW macros have the same restrictions as the 231 *_RANGE_OVERFLOW macros, except that they do not assume that operands 232 (e.g., A and B) have the same type as MIN and MAX. Instead, they assume 233 that the result (e.g., A + B) has that type. */ 234#define _GL_ADD_OVERFLOW(a, b, min, max) \ 235 ((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \ 236 : (a) < 0 ? (b) <= (a) + (b) \ 237 : (b) < 0 ? (a) <= (a) + (b) \ 238 : (a) + (b) < (b)) 239#define _GL_SUBTRACT_OVERFLOW(a, b, min, max) \ 240 ((min) < 0 ? INT_SUBTRACT_RANGE_OVERFLOW (a, b, min, max) \ 241 : (a) < 0 ? 1 \ 242 : (b) < 0 ? (a) - (b) <= (a) \ 243 : (a) < (b)) 244#define _GL_MULTIPLY_OVERFLOW(a, b, min, max) \ 245 (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a)))) \ 246 || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max)) 247#define _GL_DIVIDE_OVERFLOW(a, b, min, max) \ 248 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \ 249 : (a) < 0 ? (b) <= (a) + (b) - 1 \ 250 : (b) < 0 && (a) + (b) <= (a)) 251#define _GL_REMAINDER_OVERFLOW(a, b, min, max) \ 252 ((min) < 0 ? (b) == _GL_INT_NEGATE_CONVERT (min, 1) && (a) < - (max) \ 253 : (a) < 0 ? (a) % (b) != ((max) - (b) + 1) % (b) \ 254 : (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max)) 255 256/* Return a nonzero value if A is a mathematical multiple of B, where 257 A is unsigned, B is negative, and MAX is the maximum value of A's 258 type. A's type must be the same as (A % B)'s type. Normally (A % 259 -B == 0) suffices, but things get tricky if -B would overflow. */ 260#define _GL_UNSIGNED_NEG_MULTIPLE(a, b, max) \ 261 (((b) < -_GL_SIGNED_INT_MAXIMUM (b) \ 262 ? (_GL_SIGNED_INT_MAXIMUM (b) == (max) \ 263 ? (a) \ 264 : (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \ 265 : (a) % - (b)) \ 266 == 0) 267 268 269/* Integer overflow checks. 270 271 The INT_<op>_OVERFLOW macros return 1 if the corresponding C operators 272 might not yield numerically correct answers due to arithmetic overflow. 273 They work correctly on all known practical hosts, and do not rely 274 on undefined behavior due to signed arithmetic overflow. 275 276 Example usage: 277 278 long int i = ...; 279 long int j = ...; 280 if (INT_MULTIPLY_OVERFLOW (i, j)) 281 printf ("multiply would overflow"); 282 else 283 printf ("product is %ld", i * j); 284 285 These macros do not check for all possible numerical problems or 286 undefined or unspecified behavior: they do not check for division 287 by zero, for bad shift counts, or for shifting negative numbers. 288 289 These macros may evaluate their arguments zero or multiple times, so the 290 arguments should not have side effects. 291 292 These macros are tuned for their last argument being a constant. 293 294 Return 1 if the integer expressions A * B, A - B, -A, A * B, A / B, 295 A % B, and A << B would overflow, respectively. */ 296 297#define INT_ADD_OVERFLOW(a, b) \ 298 _GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW) 299#define INT_SUBTRACT_OVERFLOW(a, b) \ 300 _GL_BINARY_OP_OVERFLOW (a, b, _GL_SUBTRACT_OVERFLOW) 301#define INT_NEGATE_OVERFLOW(a) \ 302 INT_NEGATE_RANGE_OVERFLOW (a, _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a)) 303#define INT_MULTIPLY_OVERFLOW(a, b) \ 304 _GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW) 305#define INT_DIVIDE_OVERFLOW(a, b) \ 306 _GL_BINARY_OP_OVERFLOW (a, b, _GL_DIVIDE_OVERFLOW) 307#define INT_REMAINDER_OVERFLOW(a, b) \ 308 _GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW) 309#define INT_LEFT_SHIFT_OVERFLOW(a, b) \ 310 INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \ 311 _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a)) 312 313/* Return 1 if the expression A <op> B would overflow, 314 where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test, 315 assuming MIN and MAX are the minimum and maximum for the result type. 316 Arguments should be free of side effects. */ 317#define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \ 318 op_result_overflow (a, b, \ 319 _GL_INT_MINIMUM (0 * (b) + (a)), \ 320 _GL_INT_MAXIMUM (0 * (b) + (a))) 321 322#endif /* _GL_INTPROPS_H */ 323