From dcd92861d79cf2b225cd12effc970eec556a4c2f Mon Sep 17 00:00:00 2001 From: Tyge Date: Tue, 28 Apr 2020 22:57:41 +0200 Subject: Fixed error on GCC when using -O2 and -O3 optimization. Added volatile to hash function input. --- stc/cdefs.h | 5 +++-- stc/cmap.h | 9 +++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/stc/cdefs.h b/stc/cdefs.h index 54df947d..c1635fa5 100644 --- a/stc/cdefs.h +++ b/stc/cdefs.h @@ -64,7 +64,7 @@ /* One-byte-at-a-time hash based on Murmur's mix */ static inline uint32_t c_defaultHash(const void *data, size_t len) { - const uint8_t *key = (const uint8_t *) data; + const volatile uint8_t *key = (const uint8_t *) data; uint32_t x = UINT32_C(0xc613fc15); while (len--) { x ^= *key++; @@ -76,7 +76,8 @@ static inline uint32_t c_defaultHash(const void *data, size_t len) { /* https://nullprogram.com/blog/2018/07/31/: assume len positive multiple of 4 */ static inline uint32_t c_lowbias32Hash(const void *data, size_t len) { - const uint32_t *key = (const uint32_t *) data; uint32_t x = *key; + const volatile uint32_t *key = (const uint32_t *) data; + uint32_t x = *key; do { x ^= *key++ >> 16; x *= UINT32_C(0x7feb352d); diff --git a/stc/cmap.h b/stc/cmap.h index 7d94eb16..7ef28bec 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -28,6 +28,8 @@ #define cmap_init {cvector_init, 0, 90, 0} #define cmap_size(map) ((size_t) (map)._size) #define cmap_bucketCount(map) cvector_capacity((map)._table) +/* https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction */ +#define cmap_reduce(x, N) ((uint32_t) (((uint64_t) (x) * (N)) >> 32)) enum {cmapentry_HASH=0x7fff, cmapentry_USED=0x8000}; @@ -172,7 +174,7 @@ cmap_##tag##_setShrinkLimitFactor(CMap_##tag* self, double limit) { \ } \ \ static inline size_t \ -cmap_##tag##_bucket(CMap_##tag* self, cmap_##tag##_rawkey_t* const rawKeyPtr, uint32_t* hxPtr) { \ +cmap_##tag##_bucket(CMap_##tag* self, const cmap_##tag##_rawkey_t* rawKeyPtr, uint32_t* hxPtr) { \ uint32_t hash = keyHashRaw(rawKeyPtr, sizeof(cmap_##tag##_rawkey_t)), hx = (hash & cmapentry_HASH) | cmapentry_USED; \ size_t cap = cvector_capacity(self->_table); \ size_t idx = cmap_reduce(hash, cap); \ @@ -300,11 +302,6 @@ cmap_##tag##_next(cmap_##tag##_iter_t it) { \ return it; \ } -/* https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction */ -static inline uint32_t cmap_reduce(uint32_t x, uint32_t N) { - return ((uint64_t) x * (uint64_t) N) >> 32 ; -} - #else #define implement_CMap_10(tag, Key, Value, valueDestroy, keyDestroy, RawKey, \ keyHashRaw, keyEqualsRaw, keyGetRaw, keyInitRaw) -- cgit v1.2.3