From: Willy Tarreau Date: Tue, 25 Jul 2023 14:59:48 +0000 (+0200) Subject: MINOR: tools: make ptr_hash() support 0-bit outputs X-Git-Tag: v2.9-dev3~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ab6cb5dea035d180bc9461b4739d90dc9c1d2aaf;p=thirdparty%2Fhaproxy.git MINOR: tools: make ptr_hash() support 0-bit outputs 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. --- diff --git a/include/haproxy/tools.h b/include/haproxy/tools.h index fb9a019fd8..cea68c868d 100644 --- a/include/haproxy/tools.h +++ b/include/haproxy/tools.h @@ -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 is always a compile-time constant. + * It must be inlined so that 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;