|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * int.h |
| 4 | + * Routines to perform integer math, while checking for overflows. |
| 5 | + * |
| 6 | + * The routines in this file are intended to be well defined C, without |
| 7 | + * relying on compiler flags like -fwrapv. |
| 8 | + * |
| 9 | + * To reduce the overhead of these routines try to use compiler intrinsics |
| 10 | + * where available. That's not that important for the 16, 32 bit cases, but |
| 11 | + * the 64 bit cases can be considerably faster with intrinsics. In case no |
| 12 | + * intrinsics are available 128 bit math is used where available. |
| 13 | + * |
| 14 | + * Copyright (c) 2017, PostgreSQL Global Development Group |
| 15 | + * |
| 16 | + * src/include/common/int.h |
| 17 | + * |
| 18 | + *------------------------------------------------------------------------- |
| 19 | + */ |
| 20 | +#ifndefCOMMON_INT_H |
| 21 | +#defineCOMMON_INT_H |
| 22 | + |
| 23 | +/* |
| 24 | + * If a + b overflows, return true, otherwise store the result of a + b into |
| 25 | + * *result. The content of *result is implementation defined in case of |
| 26 | + * overflow. |
| 27 | + */ |
| 28 | +staticinlinebool |
| 29 | +pg_add_s16_overflow(int16a,int16b,int16*result) |
| 30 | +{ |
| 31 | +#if defined(HAVE__BUILTIN_OP_OVERFLOW) |
| 32 | +return__builtin_add_overflow(a,b,result); |
| 33 | +#else |
| 34 | +int32res= (int32)a+ (int32)b; |
| 35 | + |
| 36 | +if (res>PG_INT16_MAX||res<PG_INT16_MIN) |
| 37 | +return true; |
| 38 | +*result= (int16)res; |
| 39 | +return false; |
| 40 | +#endif |
| 41 | +} |
| 42 | + |
| 43 | +/* |
| 44 | + * If a - b overflows, return true, otherwise store the result of a + b into |
| 45 | + * *result. The content of *result is implementation defined in case of |
| 46 | + * overflow. |
| 47 | + */ |
| 48 | +staticinlinebool |
| 49 | +pg_sub_s16_overflow(int16a,int16b,int16*result) |
| 50 | +{ |
| 51 | +#if defined(HAVE__BUILTIN_OP_OVERFLOW) |
| 52 | +return__builtin_sub_overflow(a,b,result); |
| 53 | +#else |
| 54 | +int32res= (int32)a- (int32)b; |
| 55 | + |
| 56 | +if (res>PG_INT16_MAX||res<PG_INT16_MIN) |
| 57 | +return true; |
| 58 | +*result= (int16)res; |
| 59 | +return false; |
| 60 | +#endif |
| 61 | +} |
| 62 | + |
| 63 | +/* |
| 64 | + * If a * b overflows, return true, otherwise store the result of a + b into |
| 65 | + * *result. The content of *result is implementation defined in case of |
| 66 | + * overflow. |
| 67 | + */ |
| 68 | +staticinlinebool |
| 69 | +pg_mul_s16_overflow(int16a,int16b,int16*result) |
| 70 | +{ |
| 71 | +#if defined(HAVE__BUILTIN_OP_OVERFLOW) |
| 72 | +return__builtin_mul_overflow(a,b,result); |
| 73 | +#else |
| 74 | +int32res= (int32)a* (int32)b; |
| 75 | + |
| 76 | +if (res>PG_INT16_MAX||res<PG_INT16_MIN) |
| 77 | +return true; |
| 78 | +*result= (int16)res; |
| 79 | +return false; |
| 80 | +#endif |
| 81 | +} |
| 82 | + |
| 83 | +/* |
| 84 | + * If a + b overflows, return true, otherwise store the result of a + b into |
| 85 | + * *result. The content of *result is implementation defined in case of |
| 86 | + * overflow. |
| 87 | + */ |
| 88 | +staticinlinebool |
| 89 | +pg_add_s32_overflow(int32a,int32b,int32*result) |
| 90 | +{ |
| 91 | +#if defined(HAVE__BUILTIN_OP_OVERFLOW) |
| 92 | +return__builtin_add_overflow(a,b,result); |
| 93 | +#else |
| 94 | +int64res= (int64)a+ (int64)b; |
| 95 | + |
| 96 | +if (res>PG_INT32_MAX||res<PG_INT32_MIN) |
| 97 | +return true; |
| 98 | +*result= (int32)res; |
| 99 | +return false; |
| 100 | +#endif |
| 101 | +} |
| 102 | + |
| 103 | +/* |
| 104 | + * If a - b overflows, return true, otherwise store the result of a + b into |
| 105 | + * *result. The content of *result is implementation defined in case of |
| 106 | + * overflow. |
| 107 | + */ |
| 108 | +staticinlinebool |
| 109 | +pg_sub_s32_overflow(int32a,int32b,int32*result) |
| 110 | +{ |
| 111 | +#if defined(HAVE__BUILTIN_OP_OVERFLOW) |
| 112 | +return__builtin_sub_overflow(a,b,result); |
| 113 | +#else |
| 114 | +int64res= (int64)a- (int64)b; |
| 115 | + |
| 116 | +if (res>PG_INT32_MAX||res<PG_INT32_MIN) |
| 117 | +return true; |
| 118 | +*result= (int32)res; |
| 119 | +return false; |
| 120 | +#endif |
| 121 | +} |
| 122 | + |
| 123 | +/* |
| 124 | + * If a * b overflows, return true, otherwise store the result of a + b into |
| 125 | + * *result. The content of *result is implementation defined in case of |
| 126 | + * overflow. |
| 127 | + */ |
| 128 | +staticinlinebool |
| 129 | +pg_mul_s32_overflow(int32a,int32b,int32*result) |
| 130 | +{ |
| 131 | +#if defined(HAVE__BUILTIN_OP_OVERFLOW) |
| 132 | +return__builtin_mul_overflow(a,b,result); |
| 133 | +#else |
| 134 | +int64res= (int64)a* (int64)b; |
| 135 | + |
| 136 | +if (res>PG_INT32_MAX||res<PG_INT32_MIN) |
| 137 | +return true; |
| 138 | +*result= (int32)res; |
| 139 | +return false; |
| 140 | +#endif |
| 141 | +} |
| 142 | + |
| 143 | +/* |
| 144 | + * If a + b overflows, return true, otherwise store the result of a + b into |
| 145 | + * *result. The content of *result is implementation defined in case of |
| 146 | + * overflow. |
| 147 | + */ |
| 148 | +staticinlinebool |
| 149 | +pg_add_s64_overflow(int64a,int64b,int64*result) |
| 150 | +{ |
| 151 | +#if defined(HAVE__BUILTIN_OP_OVERFLOW) |
| 152 | +return__builtin_add_overflow(a,b,result); |
| 153 | +#elif defined(HAVE_INT128) |
| 154 | +int128res= (int128)a+ (int128)b; |
| 155 | + |
| 156 | +if (res>PG_INT64_MAX||res<PG_INT64_MIN) |
| 157 | +return true; |
| 158 | +*result= (int64)res; |
| 159 | +return false; |
| 160 | +#else |
| 161 | +if ((a>0&&b>0&&a>PG_INT64_MAX-b)|| |
| 162 | +(a<0&&b<0&&a<PG_INT64_MIN-b)) |
| 163 | +return true; |
| 164 | +*result=a+b; |
| 165 | +return false; |
| 166 | +#endif |
| 167 | +} |
| 168 | + |
| 169 | +/* |
| 170 | + * If a - b overflows, return true, otherwise store the result of a + b into |
| 171 | + * *result. The content of *result is implementation defined in case of |
| 172 | + * overflow. |
| 173 | + */ |
| 174 | +staticinlinebool |
| 175 | +pg_sub_s64_overflow(int64a,int64b,int64*result) |
| 176 | +{ |
| 177 | +#if defined(HAVE__BUILTIN_OP_OVERFLOW) |
| 178 | +return__builtin_sub_overflow(a,b,result); |
| 179 | +#elif defined(HAVE_INT128) |
| 180 | +int128res= (int128)a- (int128)b; |
| 181 | + |
| 182 | +if (res>PG_INT64_MAX||res<PG_INT64_MIN) |
| 183 | +return true; |
| 184 | +*result= (int64)res; |
| 185 | +return false; |
| 186 | +#else |
| 187 | +if ((a<0&&b>0&&a<PG_INT64_MIN+b)|| |
| 188 | +(a>0&&b<0&&a>PG_INT64_MAX+b)) |
| 189 | +return true; |
| 190 | +*result=a-b; |
| 191 | +return false; |
| 192 | +#endif |
| 193 | +} |
| 194 | + |
| 195 | +/* |
| 196 | + * If a * b overflows, return true, otherwise store the result of a + b into |
| 197 | + * *result. The content of *result is implementation defined in case of |
| 198 | + * overflow. |
| 199 | + */ |
| 200 | +staticinlinebool |
| 201 | +pg_mul_s64_overflow(int64a,int64b,int64*result) |
| 202 | +{ |
| 203 | +#if defined(HAVE__BUILTIN_OP_OVERFLOW) |
| 204 | +return__builtin_mul_overflow(a,b,result); |
| 205 | +#elif defined(HAVE_INT128) |
| 206 | +int128res= (int128)a* (int128)b; |
| 207 | + |
| 208 | +if (res>PG_INT64_MAX||res<PG_INT64_MIN) |
| 209 | +return true; |
| 210 | +*result= (int64)res; |
| 211 | +return false; |
| 212 | +#else |
| 213 | +/* |
| 214 | + * Overflow can only happen if at least one value is outside the range |
| 215 | + * sqrt(min)..sqrt(max) so check that first as the division can be quite a |
| 216 | + * bit more expensive than the multiplication. |
| 217 | + * |
| 218 | + * Multiplying by 0 or 1 can't overflow of course and checking for 0 |
| 219 | + * separately avoids any risk of dividing by 0. Be careful about dividing |
| 220 | + * INT_MIN by -1 also, note reversing the a and b to ensure we're always |
| 221 | + * dividing it by a positive value. |
| 222 | + * |
| 223 | + */ |
| 224 | +if ((a>PG_INT32_MAX||a<PG_INT32_MIN|| |
| 225 | +b>PG_INT32_MAX||b<PG_INT32_MIN)&& |
| 226 | +a!=0&&a!=1&&b!=0&&b!=1&& |
| 227 | +((a>0&&b>0&&a>PG_INT64_MAX /b)|| |
| 228 | + (a>0&&b<0&&b<PG_INT64_MIN /a)|| |
| 229 | + (a<0&&b>0&&a<PG_INT64_MIN /b)|| |
| 230 | + (a<0&&b<0&&a<PG_INT64_MAX /b))) |
| 231 | +{ |
| 232 | +return true; |
| 233 | +} |
| 234 | +*result=a*b; |
| 235 | +return false; |
| 236 | +#endif |
| 237 | +} |
| 238 | + |
| 239 | +#endif/* COMMON_INT_H */ |