|
| 1 | +#include <assert.h> |
| 2 | +#include <limits.h> |
| 3 | +#include <stdint.h> |
| 4 | + |
| 5 | +#define A_VALUE_X_SO_THAT_X_TIMES_X_DOES_NOT_OVERFLOW(T) \ |
| 6 | + (((T)(1)) << (sizeof(T) * CHAR_BIT) / 2 - 1) |
| 7 | + |
| 8 | +void check_int(void) |
| 9 | +{ |
| 10 | + int result; |
| 11 | + assert(!__builtin_smul_overflow(1, 1, &result)); |
| 12 | + assert(result == 1); |
| 13 | + int const lt_isqrt_of_int_max = |
| 14 | + A_VALUE_X_SO_THAT_X_TIMES_X_DOES_NOT_OVERFLOW(int); |
| 15 | + assert(!__builtin_smul_overflow( |
| 16 | + lt_isqrt_of_int_max, lt_isqrt_of_int_max, &result)); |
| 17 | + assert(result == lt_isqrt_of_int_max * lt_isqrt_of_int_max); |
| 18 | + assert(__builtin_smul_overflow( |
| 19 | + lt_isqrt_of_int_max << 1, lt_isqrt_of_int_max << 1, &result)); |
| 20 | + assert(0 && "reachability"); |
| 21 | +} |
| 22 | + |
| 23 | +void check_long(void) |
| 24 | +{ |
| 25 | + long result; |
| 26 | + assert(!__builtin_smull_overflow(1l, 1l, &result)); |
| 27 | + assert(result == 1l); |
| 28 | + long const lt_isqrt_of_long_max = |
| 29 | + A_VALUE_X_SO_THAT_X_TIMES_X_DOES_NOT_OVERFLOW(long); |
| 30 | + assert(!__builtin_smull_overflow( |
| 31 | + lt_isqrt_of_long_max, lt_isqrt_of_long_max, &result)); |
| 32 | + assert(result == lt_isqrt_of_long_max * lt_isqrt_of_long_max); |
| 33 | + assert(__builtin_smull_overflow( |
| 34 | + lt_isqrt_of_long_max << 1, lt_isqrt_of_long_max << 1, &result)); |
| 35 | + assert(0 && "reachability"); |
| 36 | +} |
| 37 | + |
| 38 | +void check_long_long(void) |
| 39 | +{ |
| 40 | + long long result; |
| 41 | + assert(!__builtin_smulll_overflow(1ll, 1ll, &result)); |
| 42 | + assert(result == 1ll); |
| 43 | + long long const lt_isqrt_of_long_long_max = |
| 44 | + A_VALUE_X_SO_THAT_X_TIMES_X_DOES_NOT_OVERFLOW(long long); |
| 45 | + assert(!__builtin_smulll_overflow( |
| 46 | + lt_isqrt_of_long_long_max, lt_isqrt_of_long_long_max, &result)); |
| 47 | + assert(result == lt_isqrt_of_long_long_max * lt_isqrt_of_long_long_max); |
| 48 | + assert(__builtin_smulll_overflow( |
| 49 | + lt_isqrt_of_long_long_max << 1, lt_isqrt_of_long_long_max << 1, &result)); |
| 50 | + assert(0 && "reachability"); |
| 51 | +} |
| 52 | + |
| 53 | +void check_unsigned(void) |
| 54 | +{ |
| 55 | + unsigned result; |
| 56 | + assert(!__builtin_umul_overflow(1u, 1u, &result)); |
| 57 | + assert(result == 1u); |
| 58 | + unsigned const lt_isqrt_of_unsigned_max = |
| 59 | + A_VALUE_X_SO_THAT_X_TIMES_X_DOES_NOT_OVERFLOW(unsigned); |
| 60 | + assert(!__builtin_umul_overflow( |
| 61 | + lt_isqrt_of_unsigned_max, lt_isqrt_of_unsigned_max, &result)); |
| 62 | + assert(result == lt_isqrt_of_unsigned_max * lt_isqrt_of_unsigned_max); |
| 63 | + assert(__builtin_umul_overflow( |
| 64 | + lt_isqrt_of_unsigned_max << 1, lt_isqrt_of_unsigned_max << 1, &result)); |
| 65 | + assert(0 && "reachability"); |
| 66 | +} |
| 67 | + |
| 68 | +void check_unsigned_long(void) |
| 69 | +{ |
| 70 | + unsigned long result; |
| 71 | + assert(!__builtin_umull_overflow(1ul, 1ul, &result)); |
| 72 | + assert(result == 1ul); |
| 73 | + unsigned long const lt_isqrt_of_unsigned_long_max = |
| 74 | + A_VALUE_X_SO_THAT_X_TIMES_X_DOES_NOT_OVERFLOW(unsigned long); |
| 75 | + assert(!__builtin_umull_overflow( |
| 76 | + lt_isqrt_of_unsigned_long_max, lt_isqrt_of_unsigned_long_max, &result)); |
| 77 | + assert( |
| 78 | + result == lt_isqrt_of_unsigned_long_max * lt_isqrt_of_unsigned_long_max); |
| 79 | + assert(__builtin_umull_overflow( |
| 80 | + lt_isqrt_of_unsigned_long_max << 1, |
| 81 | + lt_isqrt_of_unsigned_long_max << 1, |
| 82 | + &result)); |
| 83 | + assert(0 && "reachability"); |
| 84 | +} |
| 85 | + |
| 86 | +void check_unsigned_long_long(void) |
| 87 | +{ |
| 88 | + unsigned long long result; |
| 89 | + assert(!__builtin_umulll_overflow(1ull, 1ull, &result)); |
| 90 | + assert(result == 1ull); |
| 91 | + unsigned long long const lt_isqrt_of_unsigned_long_long_max = |
| 92 | + A_VALUE_X_SO_THAT_X_TIMES_X_DOES_NOT_OVERFLOW(unsigned long long); |
| 93 | + assert(!__builtin_umulll_overflow( |
| 94 | + lt_isqrt_of_unsigned_long_long_max, |
| 95 | + lt_isqrt_of_unsigned_long_long_max, |
| 96 | + &result)); |
| 97 | + assert( |
| 98 | + result == |
| 99 | + lt_isqrt_of_unsigned_long_long_max * lt_isqrt_of_unsigned_long_long_max); |
| 100 | + assert(__builtin_umulll_overflow( |
| 101 | + lt_isqrt_of_unsigned_long_long_max << 1, |
| 102 | + lt_isqrt_of_unsigned_long_long_max << 1, |
| 103 | + &result)); |
| 104 | + assert(0 && "reachability"); |
| 105 | +} |
| 106 | + |
| 107 | +int main(void) |
| 108 | +{ |
| 109 | + check_int(); |
| 110 | + check_long(); |
| 111 | + check_long_long(); |
| 112 | + check_unsigned(); |
| 113 | + check_unsigned_long(); |
| 114 | + check_unsigned_long_long(); |
| 115 | + return 0; |
| 116 | +} |
0 commit comments