On ILP32, 64-bit result was shifted by value calculated for 32-bit long type and returned value was much outside hashmap capacity. As advised by Andrii Nakryiko, this patch uses different hashing variant for architectures with size_t shorter than long long.
Unified · Split
| @@ -11,14 +11,18 @@ · 10 unmodified lines | ||
| 11 | 11 | #include <stdbool.h> |
| 12 | 12 | #include <stddef.h> |
| 13 | 13 | #include <limits.h> |
| 14 | −#ifndef __WORDSIZE | |
| 15 | −#define __WORDSIZE (__SIZEOF_LONG__ * 8) | |
| 16 | −#endif | |
| 17 | 14 | |
| 18 | 15 | static inline size_t hash_bits(size_t h, int bits) |
| 19 | 16 | { |
| 20 | 17 | /* shuffle bits and return requested number of upper bits */ |
| 21 | − return (h * 11400714819323198485llu) >> (__WORDSIZE - bits); | |
| 18 | +#if (__SIZEOF_SIZE_T__ == __SIZEOF_LONG_LONG__) | |
| 19 | + /* LP64 case */ | |
| 20 | + return (h * 11400714819323198485llu) >> (__SIZEOF_LONG_LONG__ * 8 - bits); | |
| 21 | +#elif (__SIZEOF_SIZE_T__ <= __SIZEOF_LONG__) | |
| 22 | + return (h * 2654435769lu) >> (__SIZEOF_LONG__ * 8 - bits); | |
| 23 | +#else | |
| 24 | +# error "Unsupported size_t size" | |
| 25 | +#endif | |
| 22 | 26 | } |
| 23 | 27 | |
| 24 | 28 | typedef size_t (*hashmap_hash_fn)(const void *key, void *ctx); |