]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
uint128: Add explicit cast function for 128bit -> 64bit conversion
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Tue, 31 Mar 2020 20:59:51 +0000 (14:59 -0600)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Tue, 31 Mar 2020 21:00:09 +0000 (15:00 -0600)
src/lib/util/retry.c
src/lib/util/uint128.h

index 0cbdd72a60e564ceee2fadcc04c4a8b52b1f1e37..b055b8998099a111c9d5b44c1e849d5c29dac4d2 100644 (file)
@@ -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));
        }
 
        /*
index dc53e27ac2d2fcfa7b1eed84cd18a9650bf5f211..f43f9126dbaa356d33f4005507fb57a8ef1619da 100644 (file)
@@ -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