]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: tools: make ptr_hash() support 0-bit outputs
authorWilly Tarreau <w@1wt.eu>
Tue, 25 Jul 2023 14:59:48 +0000 (16:59 +0200)
committerWilly Tarreau <w@1wt.eu>
Sat, 12 Aug 2023 17:04:34 +0000 (19:04 +0200)
When dealing with macro-based size definitions, it is useful to be able
to hash pointers on zero bits so that the macro automatically returns a
constant 0. For now it only supports 1-32. Let's just add this special
case. It's automatically optimized out by the compiler since the function
is inlined.

include/haproxy/tools.h

index fb9a019fd8e6aaa696878b63c8724816d9c7f916..cea68c868d3180de1c2ec07d7e80268a1bfc37fe 100644 (file)
@@ -1089,12 +1089,16 @@ static inline uint statistical_prng_range(uint range)
  * fill ratio for 12 bits, ~1296 (~756 non-colliding) at 100% fill ratio for 11
  * bits, ~648 (~378 non-colliding) at 100% fill ratio for 10 bits, ~163 (95 non
  * colliding) at 100% fill ratio for 8 bits, hence 1-1/e and 1/e respectively.
- * It must be inlined so that <bits> is always a compile-time constant.
+ * It must be inlined so that <bits> is always a compile-time constant. It
+ * supports output sizes from 0 to 32 bits.
  */
 static forceinline uint ptr_hash(const void *p, const int bits)
 {
        unsigned long long x = (unsigned long)p;
 
+       if (!bits)
+               return 0;
+
        x *= 0xc1da9653U;
        if (sizeof(long) == 4)
                x ^= x >> 32;
@@ -1111,6 +1115,9 @@ static forceinline uint ptr2_hash(const void *p1, const void *p2, const int bits
        unsigned long long x = (unsigned long)p1;
        unsigned long long y = (unsigned long)p2;
 
+       if (!bits)
+               return 0;
+
        x *= 0xc1da9653U;
        y *= 0x96531cadU;
        x ^= y;