]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: tools: add a new pointer hash function that also takes an argument
authorWilly Tarreau <w@1wt.eu>
Fri, 6 Mar 2026 18:14:06 +0000 (19:14 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 12 Mar 2026 17:06:37 +0000 (18:06 +0100)
The purpose here is to combine two pointers and a long argument instead
of having the caller perform the mixing. Also it's cleaner and more
efficient this was as the arg is mixed after the multiplications, and
modern processors are efficient at multiplying then adding.

include/haproxy/tools.h

index 7b876f49bdd68167a954f950408f5b7da2831937..2e266621d37c74e435b823d4e459759105fa7ad8 100644 (file)
@@ -1368,6 +1368,18 @@ static forceinline ullong _ptr2_hash(const void *p1, const void *p2)
        return x ^ y;
 }
 
+/* two-pointer plus arg version, low-level, use ptr2_hash_arg() instead */
+static forceinline ullong _ptr2_hash_arg(const void *p1, const void *p2, ulong arg)
+{
+       unsigned long long x = (unsigned long)p1;
+       unsigned long long y = (unsigned long)p2;
+
+       x *= 0xacd1be85U;
+       x += arg;
+       y *= 0x9d28e4e9U;
+       return x ^ y;
+}
+
 /* returns a hash on <bits> bits of pointer <p> that is suitable for being used
  * to compute statistic buckets, in that it's fast and reasonably distributed
  * thanks to mixing the bits via a multiplication by a prime number and using
@@ -1392,6 +1404,14 @@ static forceinline uint ptr2_hash(const void *p1, const void *p2, const int bits
        return _ptr_hash_reduce(_ptr2_hash(p1, p2), bits);
 }
 
+/* Same as above but works on two pointers and a long argument. It will return
+ * the same values if the second pointer is NULL.
+ */
+static forceinline uint ptr2_hash_arg(const void *p1, const void *p2, ulong arg, const int bits)
+{
+       return _ptr_hash_reduce(_ptr2_hash_arg(p1, p2, arg), bits);
+}
+
 
 /* Update array <fp> with the character transition <prev> to <curr>. If <prev>
  * is zero, it's assumed that <curr> is the first character. If <curr> is zero