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