diff options
author | jwerle <joseph.werle@gmail.com> | 2024-12-31 13:33:14 -0500 |
---|---|---|
committer | jwerle <joseph.werle@gmail.com> | 2024-12-31 13:33:17 -0500 |
commit | 48e2090841ae570756403fc37eec4e65554719f4 (patch) | |
tree | 304f27a4360b3c0e6780197923d2e7372be2fd32 /murmurhash.c | |
parent | ffaacccda0f647a99806160fe90db4b012219603 (diff) |
feat(murmurhash.c): support big-endian env
- opt-in with '-DMURMURHASH_WANTS_HTOLE32' to enable 'htole32' impl
- opt-in with '-DMURMURHASH_HAS_HTOLE32' to indicate 'htole32' exists
Diffstat (limited to 'murmurhash.c')
-rw-r--r-- | murmurhash.c | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/murmurhash.c b/murmurhash.c index 0c631c4..5a4588e 100644 --- a/murmurhash.c +++ b/murmurhash.c @@ -1,7 +1,7 @@ /** * `murmurhash.h' - murmurhash * - * copyright (c) 2014-2022 joseph werle <joseph.werle@gmail.com> + * copyright (c) 2014-2025 joseph werle <joseph.werle@gmail.com> */ #include <stdlib.h> @@ -9,6 +9,23 @@ #include <stdint.h> #include "murmurhash.h" +#if MURMURHASH_WANTS_HTOLE32 +#define MURMURHASH_HAS_HTOLE32 1 +#ifndef htole32 +static uint32_t htole32 (uint32_t value) { +#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + value = ( + ((value & 0xFF000000) >> 24) | + ((value & 0x00FF0000) >> 8) | + ((value & 0x0000FF00) << 8) | + ((value & 0x000000FF) << 24) + ); +#endif + return value; +} +#endif +#endif + uint32_t murmurhash (const char *key, uint32_t len, uint32_t seed) { uint32_t c1 = 0xcc9e2d51; @@ -33,7 +50,11 @@ murmurhash (const char *key, uint32_t len, uint32_t seed) { // for each 4 byte chunk of `key' for (i = -l; i != 0; ++i) { // next 4 byte chunk of `key' + #if MURMURHASH_HAS_HTOLE32 + k = htole32(chunks[i]); + #else k = chunks[i]; + #endif // encode next 4 byte chunk of `key' k *= c1; |