util.h (442B)
1 #pragma once 2 3 #include <stdbool.h> 4 5 #include "../def.h" 6 7 /* If s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW, then s1*s2 <= USIZE_MAX 8 * (but not conversely). This lets us avoid division in overflow checks. */ 9 #define MUL_NO_OVERFLOW ((usize) 1 << (sizeof(usize) * 4)) 10 11 static inline bool 12 r_mul_will_overflow_(usize a, usize b) { 13 return (a >= MUL_NO_OVERFLOW || b >= MUL_NO_OVERFLOW) 14 && a > 0 && USIZE_MAX/a < b; 15 } 16 17 #undef MUL_NO_OVERFLOW