From e1f7e51af3769ea07274575734a15f2a417fb8d6 Mon Sep 17 00:00:00 2001 From: cptr0 Date: Sat, 22 Nov 2025 18:55:26 +0100 Subject: [PATCH 1/3] Add 32-bit Jenkins Lookup3 hash --- hash/hash_lookup3.c | 103 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 hash/hash_lookup3.c diff --git a/hash/hash_lookup3.c b/hash/hash_lookup3.c new file mode 100644 index 0000000000..07c6a38310 --- /dev/null +++ b/hash/hash_lookup3.c @@ -0,0 +1,103 @@ +/** + * @addtogroup hash Hash algorithms + * @{ + * @file hash_lookup3.c + * @author [cptr0](https://github.com/cptr0) + * @brief 32-bit [Jenkins hash](https://en.wikipedia.org/wiki/Jenkins_hash_function) algorithm + */ + +#include +#include +#include +#include + +// Rotate x left by k bits +#define rot(x,k) (((x) << (k)) | ((x) >> (32-(k)))) + +/** + * @brief 32-bit Jenkins Lookup3 algorithm implementation + * + * @param s NULL terminated ASCII string to hash + * @return 32-bit hash result + */ + +uint32_t lookup3_32(const char *key, uint32_t seed) { + uint32_t a, b, c; + a = b = 0x9e3779b9; + c = seed; + + size_t i = 0; + + size_t length = strlen(key); + + while (length - i >= 12) { + a += (uint32_t)key[i] | ((uint32_t)key[i+1] << 8) | + ((uint32_t)key[i+2] << 16) | ((uint32_t)key[i+3] << 24); + b += (uint32_t)key[i+4] | ((uint32_t)key[i+5] << 8) | + ((uint32_t)key[i+6] << 16) | ((uint32_t)key[i+7] << 24); + c += (uint32_t)key[i+8] | ((uint32_t)key[i+9] << 8) | + ((uint32_t)key[i+10] << 16) | ((uint32_t)key[i+11] << 24); + + a -= c; a ^= rot(c,4); c += b; + b -= a; b ^= rot(a,6); a += c; + c -= b; c ^= rot(b,8); b += a; + a -= c; a ^= rot(c,16); c += b; + b -= a; b ^= rot(a,19); a += c; + c -= b; c ^= rot(b,4); b += a; + + i += 12; + } + + uint32_t remainingA = 0, remainingB = 0, remainingC = 0; + switch (length - i) { + case 11: remainingC += ((uint32_t)key[i+10]) << 16; + case 10: remainingC += ((uint32_t)key[i+9]) << 8; + case 9: remainingC += (uint32_t)key[i+8]; + case 8: remainingB += ((uint32_t)key[i+7]) << 24; + case 7: remainingB += ((uint32_t)key[i+6]) << 16; + case 6: remainingB += ((uint32_t)key[i+5]) << 8; + case 5: remainingB += (uint32_t)key[i+4]; + case 4: remainingA += ((uint32_t)key[i+3]) << 24; + case 3: remainingA += ((uint32_t)key[i+2]) << 16; + case 2: remainingA += ((uint32_t)key[i+1]) << 8; + case 1: remainingA += (uint32_t)key[i]; + a += remainingA; + b += remainingB; + c += remainingC; + } + + c ^= b; c -= rot(b,14); + a ^= c; a -= rot(c,11); + b ^= a; b -= rot(a,25); + c ^= b; c -= rot(b,16); + a ^= c; a -= rot(c,4); + b ^= a; b -= rot(a,14); + c ^= b; c -= rot(b,24); + + return c; +} +/** + * @brief Test function for lookup3_32 function + * + * @returns None + */ +void test_lookup3_32() +{ + assert(lookup3_32("Hello, Jenkins!", 0) == 2484708164); + assert(lookup3_32("Hello Jenkinss", 0) == 850494015); + assert(lookup3_32("Hello, Jenkins", 0) == 3393305742); + assert(lookup3_32("hello, jenkins!", 0) == 3521406038); + + printf("%s", "Test passed.\n"); +} + +/** + * @brief Main function + * + * @returns 0 on successful program exit + */ +int main() +{ + test_lookup3_32(); + return 0; +} From a236a2cb4ce154f1905fa58f6fcc7c32e96a2af8 Mon Sep 17 00:00:00 2001 From: cptr0 Date: Sat, 22 Nov 2025 19:09:54 +0100 Subject: [PATCH 2/3] Minor tweaks Made the coed blocks consistent. --- hash/hash_lookup3.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/hash/hash_lookup3.c b/hash/hash_lookup3.c index 07c6a38310..f9e2832031 100644 --- a/hash/hash_lookup3.c +++ b/hash/hash_lookup3.c @@ -21,7 +21,8 @@ * @return 32-bit hash result */ -uint32_t lookup3_32(const char *key, uint32_t seed) { +uint32_t lookup3_32(const char *key, uint32_t seed) +{ uint32_t a, b, c; a = b = 0x9e3779b9; c = seed; @@ -30,7 +31,8 @@ uint32_t lookup3_32(const char *key, uint32_t seed) { size_t length = strlen(key); - while (length - i >= 12) { + while (length - i >= 12) + { a += (uint32_t)key[i] | ((uint32_t)key[i+1] << 8) | ((uint32_t)key[i+2] << 16) | ((uint32_t)key[i+3] << 24); b += (uint32_t)key[i+4] | ((uint32_t)key[i+5] << 8) | @@ -49,7 +51,8 @@ uint32_t lookup3_32(const char *key, uint32_t seed) { } uint32_t remainingA = 0, remainingB = 0, remainingC = 0; - switch (length - i) { + switch (length - i) + { case 11: remainingC += ((uint32_t)key[i+10]) << 16; case 10: remainingC += ((uint32_t)key[i+9]) << 8; case 9: remainingC += (uint32_t)key[i+8]; From 7d25227c416d8932c8296740986c878edc0924ae Mon Sep 17 00:00:00 2001 From: cptr0 Date: Sat, 22 Nov 2025 19:27:50 +0100 Subject: [PATCH 3/3] Add Lookup3 algorithm to README --- hash/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/hash/README.md b/hash/README.md index 90942636cf..731d2c617e 100644 --- a/hash/README.md +++ b/hash/README.md @@ -6,3 +6,4 @@ * adler_32 (32 bit) * crc32 (32 bit) * BLAKE2b +* Lookup3 (32 bit)