comparison nuklear_ui/nuklear.h @ 2683:596786e43c24

Avoid unaligned access in nk_murmur_hash
author Michael Pavone <pavone@retrodev.com>
date Wed, 26 Mar 2025 22:48:34 -0700
parents 86dfcf3f418a
children
comparison
equal deleted inserted replaced
2682:143cb5762ec9 2683:596786e43c24
5558 NK_API nk_hash 5558 NK_API nk_hash
5559 nk_murmur_hash(const void * key, int len, nk_hash seed) 5559 nk_murmur_hash(const void * key, int len, nk_hash seed)
5560 { 5560 {
5561 /* 32-Bit MurmurHash3: https://code.google.com/p/smhasher/wiki/MurmurHash3*/ 5561 /* 32-Bit MurmurHash3: https://code.google.com/p/smhasher/wiki/MurmurHash3*/
5562 #define NK_ROTL(x,r) ((x) << (r) | ((x) >> (32 - r))) 5562 #define NK_ROTL(x,r) ((x) << (r) | ((x) >> (32 - r)))
5563 union {const nk_uint *i; const nk_byte *b;} conv = {0};
5564 const nk_byte *data = (const nk_byte*)key; 5563 const nk_byte *data = (const nk_byte*)key;
5565 const int nblocks = len/4; 5564 const int nblocks = len/4;
5566 nk_uint h1 = seed; 5565 nk_uint h1 = seed;
5567 const nk_uint c1 = 0xcc9e2d51; 5566 const nk_uint c1 = 0xcc9e2d51;
5568 const nk_uint c2 = 0x1b873593; 5567 const nk_uint c2 = 0x1b873593;
5571 nk_uint k1; 5570 nk_uint k1;
5572 int i; 5571 int i;
5573 5572
5574 /* body */ 5573 /* body */
5575 if (!key) return 0; 5574 if (!key) return 0;
5576 conv.b = (data + nblocks*4); 5575 for (i = 0; i < nblocks*4; i+=4) {
5577 blocks = (const nk_uint*)conv.i; 5576 k1 = data[i] | data[i+1] << 8u | data[i+2] << 16u | data[i+3] << 24u;
5578 for (i = -nblocks; i; ++i) {
5579 k1 = blocks[i];
5580 k1 *= c1; 5577 k1 *= c1;
5581 k1 = NK_ROTL(k1,15); 5578 k1 = NK_ROTL(k1,15);
5582 k1 *= c2; 5579 k1 *= c2;
5583 5580
5584 h1 ^= k1; 5581 h1 ^= k1;