diff options
Diffstat (limited to 'contrib/epee/include/int-util.h')
-rw-r--r-- | contrib/epee/include/int-util.h | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/contrib/epee/include/int-util.h b/contrib/epee/include/int-util.h index 939c018ea..dcd58a8c4 100644 --- a/contrib/epee/include/int-util.h +++ b/contrib/epee/include/int-util.h @@ -132,6 +132,23 @@ static inline uint32_t div128_32(uint64_t dividend_hi, uint64_t dividend_lo, uin // Long divisor with 2^64 base void div128_64(uint64_t dividend_hi, uint64_t dividend_lo, uint64_t divisor, uint64_t* quotient_hi, uint64_t *quotient_lo, uint64_t *remainder_hi, uint64_t *remainder_lo); +static inline void add64clamp(uint64_t *value, uint64_t add) +{ + static const uint64_t maxval = (uint64_t)-1; + if (*value > maxval - add) + *value = maxval; + else + *value += add; +} + +static inline void sub64clamp(uint64_t *value, uint64_t sub) +{ + if (*value < sub) + *value = 0; + else + *value -= sub; +} + #define IDENT16(x) ((uint16_t) (x)) #define IDENT32(x) ((uint32_t) (x)) #define IDENT64(x) ((uint64_t) (x)) |