From: Arran Cudbard-Bell Date: Tue, 31 Mar 2020 20:59:51 +0000 (-0600) Subject: uint128: Add explicit cast function for 128bit -> 64bit conversion X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b271e352f5edffbdfe5ff18bc17be9bb15371cf1;p=thirdparty%2Ffreeradius-server.git uint128: Add explicit cast function for 128bit -> 64bit conversion --- diff --git a/src/lib/util/retry.c b/src/lib/util/retry.c index 0cbdd72a60e..b055b899809 100644 --- a/src/lib/util/retry.c +++ b/src/lib/util/retry.c @@ -56,7 +56,7 @@ int fr_retry_init(fr_retry_t *r, fr_time_t now, fr_retry_config_t const *config) scale -= ((fr_time_delta_t) 1) << 31; /* scale it -2^31..+2^31 */ delay = uint128_mul64(scale, r->config->irt); - rt = (fr_time_delta_t) uint128_rshift(delay, 32); + rt = uint128_to_64(uint128_rshift(delay, 32)); r->rt = rt; r->next = now + rt; @@ -122,7 +122,7 @@ redo: scale += ((fr_time_delta_t) 1) << 33; /* multiple it by 2 * 2^32 */ delay = uint128_mul64(scale, r->rt); - rt = (fr_time_delta_t) uint128_rshift(delay, 32); + rt = uint128_to_64(uint128_rshift(delay, 32)); /* * Cap delay at MRT. @@ -136,7 +136,7 @@ redo: scale += ((fr_time_delta_t) 1) << 32; /* multiple it by 1 * 2^32 */ delay = uint128_mul64(scale, r->config->mrt); - rt = (fr_time_delta_t) uint128_rshift(delay, 32); + rt = uint128_to_64(uint128_rshift(delay, 32)); } /* diff --git a/src/lib/util/uint128.h b/src/lib/util/uint128.h index dc53e27ac2d..f43f9126dba 100644 --- a/src/lib/util/uint128.h +++ b/src/lib/util/uint128.h @@ -62,7 +62,7 @@ /** Create a 128 bit integer value with n bits high * */ -static uint128_t uint128_gen_mask(uint8_t bits) +static inline uint128_t uint128_gen_mask(uint8_t bits) { uint128_t ret; @@ -85,7 +85,7 @@ static uint128_t uint128_gen_mask(uint8_t bits) * * @author Jacob F. W */ -static uint128_t uint128_increment(uint128_t n) +static inline uint128_t uint128_increment(uint128_t n) { uint64_t t = (n.l + 1); @@ -99,7 +99,7 @@ static uint128_t uint128_increment(uint128_t n) * * @author Jacob F. W */ -static uint128_t uint128_decrement(uint128_t n) +static inline uint128_t uint128_decrement(uint128_t n) { uint64_t t = (n.l - 1); n.h -= ((t ^ n.l) & t) >> 63; @@ -112,7 +112,7 @@ static uint128_t uint128_decrement(uint128_t n) * * @author Jacob F. W */ -static uint128_t uint128_add(uint128_t a, uint128_t b) +static inline uint128_t uint128_add(uint128_t a, uint128_t b) { uint128_t ret; uint64_t tmp = (((a.l & b.l) & 1) + (a.l >> 1) + (b.l >> 1)) >> 63; @@ -125,7 +125,7 @@ static uint128_t uint128_add(uint128_t a, uint128_t b) * * @author Jacob F. W */ -static uint128_t uint128_sub(uint128_t a, uint128_t b) +static inline uint128_t uint128_sub(uint128_t a, uint128_t b) { uint128_t ret; uint64_t c; @@ -141,7 +141,7 @@ static uint128_t uint128_sub(uint128_t a, uint128_t b) * * @author Jacob F. W */ -static uint128_t uint128_mul64(uint64_t u, uint64_t v) +static inline uint128_t uint128_mul64(uint64_t u, uint64_t v) { uint128_t ret; uint64_t u1 = (u & 0xffffffff); @@ -174,7 +174,7 @@ static uint128_t uint128_mul64(uint64_t u, uint64_t v) * * @author Jacob F. W */ -static uint128_t uint128_mul(uint128_t n, uint128_t m) +static inline uint128_t uint128_mul(uint128_t n, uint128_t m) { uint128_t ret; @@ -188,7 +188,7 @@ static uint128_t uint128_mul(uint128_t n, uint128_t m) * * @note shift must be 127 bits or less. */ -static uint128_t uint128_lshift(uint128_t num, uint8_t bits) +static inline uint128_t uint128_lshift(uint128_t num, uint8_t bits) { rad_assert(bits < 128); @@ -207,7 +207,7 @@ static uint128_t uint128_lshift(uint128_t num, uint8_t bits) * * @note shift must be 127 bits or less. */ -static uint128_t uint128_rshift(uint128_t num, uint8_t bits) +static inline uint128_t uint128_rshift(uint128_t num, uint8_t bits) { rad_assert(bits < 128); @@ -225,7 +225,7 @@ static uint128_t uint128_rshift(uint128_t num, uint8_t bits) /** Perform bitwise & of two 128bit unsigned integers * */ -static uint128_t uint128_band(uint128_t a, uint128_t b) +static inline uint128_t uint128_band(uint128_t a, uint128_t b) { uint128_t ret; ret.l = a.l & b.l; @@ -236,7 +236,7 @@ static uint128_t uint128_band(uint128_t a, uint128_t b) /** Perform bitwise | of two 128bit unsigned integers * */ -static uint128_t uint128_bor(uint128_t a, uint128_t b) +static inline uint128_t uint128_bor(uint128_t a, uint128_t b) { uint128_t ret; ret.l = a.l | b.l; @@ -247,7 +247,7 @@ static uint128_t uint128_bor(uint128_t a, uint128_t b) /** Return whether the integers are equal * */ -static bool uint128_eq(uint128_t a, uint128_t b) +static inline bool uint128_eq(uint128_t a, uint128_t b) { return (a.h == b.h) && (a.l == b.l); } @@ -255,7 +255,7 @@ static bool uint128_eq(uint128_t a, uint128_t b) /** Return whether one integer is greater than the other * */ -static bool uint128_gt(uint128_t a, uint128_t b) +static inline bool uint128_gt(uint128_t a, uint128_t b) { if (a.h < b.h) return false; if (a.h > b.h) return true; @@ -265,12 +265,21 @@ static bool uint128_gt(uint128_t a, uint128_t b) /** Creates a new uint128_t from a uint64_t * */ -static uint128_t uint128_new(uint64_t h, uint64_t l) { +static inline uint128_t uint128_new(uint64_t h, uint64_t l) +{ uint128_t ret; ret.l = l; ret.h = h; return ret; } + +/** Returns the low bits of a 128bit integer + * + */ +static inline uint64_t uint128_to_64(uint128_t a) +{ + return ret.l; +} #else #define uint128_gen_mask(_bits) (((_bits) >= 128) ? ~(uint128_t)0x00 : (((uint128_t)1) << (_bits)) - 1) @@ -278,7 +287,7 @@ static uint128_t uint128_new(uint64_t h, uint64_t l) { #define uint128_decrement(_a) (*_a--) #define uint128_add(_a, _b) (_a + _b) #define uint128_sub(_a, _b) (_a - _b) -#define uint128_mul64(_a, _b) (((uint128_t)_a) * ((uint128_t)(_b))) +#define uint128_mul64(_a, _b) (((uint128_t)(_a)) * ((uint128_t)(_b))) #define uint128_mul(_a, _b) ((_a) * (_b)) #define uint128_lshift(_num, _bits) (_num << _bits) @@ -290,4 +299,6 @@ static uint128_t uint128_new(uint64_t h, uint64_t l) { #define uint128_gt(_a, _b) (_a > _b) #define uint128_new(_a, _b) ((uint128_t)_b | ((uint128_t)_a << 64)) + +#define uint128_to_64(_a) ((uint64_t)(_a)) #endif