@@ -41,6 +41,12 @@ struct api_lifecycle {
4141 void (* on_operation_failed )(void * api , void * caller , status_code cause );
4242};
4343
44+ struct hash_component {
45+ char * key ;
46+ uint64_t * hash ;
47+ uint64_t user_key ;
48+ };
49+
4450/**
4551 * Uses a pointer as an r-value for safe
4652 * comparison reasons.
@@ -104,43 +110,86 @@ static inline typed_pointer get_typed_pointer(void *address, pointer_type type)
104110 return pointer ;
105111}
106112
107- /**
108- * @brief Improved spreading: uses a 64-bit Mixer (MurmurHash3 style)
109- * to distribute entropy across the entire 64-bit range.
110- */
111113static inline status_code spread_64 (uint64_t in , uint64_t * out ) {
112114 if (rvalue (out ) == NULL ) {
113115 return EUNDEFINEDBUFFER ;
114116 }
115117
116- in = (in ^ (in >> 30 )) * 0xbf58476d1ce4e5b9ULL ;
117- in = (in ^ (in >> 27 )) * 0x94d049bb133111ebULL ;
118- in = in ^ (in >> 31 );
118+ in = in ^ ((in ^ (in >> 32 )) >> 32 );
119119 * out = in ;
120120
121121 return PASS ;
122122}
123123
124- static inline status_code hash_key (const char * key , uint64_t * hash ) {
125- if (rvalue ((void * ) key ) == NULL || rvalue (hash ) == NULL ) {
124+ static inline status_code hash_key (hash_component hasher ) {
125+ if (rvalue ((void * ) hasher .key ) == NULL ||
126+ rvalue (hasher .hash ) == NULL ) {
126127 return EUNDEFINEDBUFFER ;
127128 }
128129
129130 // Use a large prime constant to initialize (FNV-1a style)
130131 uint64_t h = 0xcbf29ce484222325ULL ;
131132
132133 // Process every character
133- for (const char * address = key ; * address != '\0' ; address ++ ) {
134+ // Algorithm for polynomial hashing using Horner's rule
135+ for (const char * address = hasher .key ; * address != '\0' ; address ++ ) {
136+ // use reverse Horner's Rule to compute the hash
134137 // 1. XOR the character into the hash
135- h ^= (uint8_t ) (* address );
136- // 2. Multiply by a large prime (this spreads the bits)
137- h *= 0x100000001b3ULL ;
138+ h ^= (* address );
139+ // 2. Multiply by 2^5 (aka 32) which is the same as left-shifting by
140+ // 5 bits using Cyclic Shift Hashcode
141+ h = (h << 5 ) | (h >> (64 - 5 ));
142+ // 4. Combine MSB Component with LSB Component
143+ h = (h >> 32 ) ^ h ;
138144 }
139145
140146 // Apply your spread_64 as a finalizer to ensure high entropy
141- return spread_64 (h , hash );
147+ return spread_64 (h ^ hasher . user_key , hasher . hash );
142148}
143149
150+ static inline uint64_t hash_compress (uint64_t hash ,
151+ uint64_t limit ) {
152+ // the equivalent of modulus operation
153+ // finds the remainder of an integer division operation
154+ return hash - (((uint64_t ) (hash /limit )) * limit );
155+ }
156+
157+ static inline uint64_t generate_next_odd (uint64_t n ) {
158+ return (n * 2 ) + 1 ;
159+ }
160+
161+ static inline status_code hashkey_compress64 (hash_component hasher ,
162+ uint64_t limit ) {
163+ if (rvalue ((void * ) hasher .key ) == NULL ||
164+ rvalue (hasher .hash ) == NULL ) {
165+ return EUNDEFINEDBUFFER ;
166+ }
167+
168+ status_code __code = hash_key (hasher );
169+ if (PASS != __code ) {
170+ return __code ;
171+ }
172+ * (hasher .hash ) = hash_compress (* (hasher .hash ), limit );
173+
174+ return PASS ;
175+ }
176+
177+ //static inline status_code is_prime(uint64_t n) {
178+ // return PASS;
179+ //}
180+ //
181+ //static inline uint64_t generate_next_prime(uint64_t n) {
182+ // uint64_t next_odd = n;
183+ //
184+ // while (1) {
185+ // next_odd = generate_next_odd(next_odd);
186+ // if (is_prime(next_odd)) {
187+ // return next_odd;
188+ // }
189+ // }
190+ // return n;
191+ //}
192+
144193#ifdef __cplusplus
145194};
146195#endif
0 commit comments