Debian Codegraph @

include/lib/a5hash-5.25.h

raw
1 /**
2 * @file a5hash.h
3 *
4 * @version 5.25
5 *
6 * @brief The header file for the "a5hash" 64-bit hash function, "a5hash32"
7 * 32-bit hash function, "a5hash128" 128-bit hash function, and "a5rand"
8 * 64-bit PRNG.
9 *
10 * The source code is written in ISO C99, with full C++ compliance enabled
11 * conditionally and automatically when compiled with a C++ compiler.
12 *
13 * Description is available at https://github.com/avaneev/a5hash
14 *
15 * Email: [email protected] or [email protected]
16 *
17 * LICENSE:
18 *
19 * Copyright (c) 2025 Aleksey Vaneev
20 *
21 * Permission is hereby granted, free of charge, to any person obtaining a
22 * copy of this software and associated documentation files (the "Software"),
23 * to deal in the Software without restriction, including without limitation
24 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
25 * and/or sell copies of the Software, and to permit persons to whom the
26 * Software is furnished to do so, subject to the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be included in
29 * all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
36 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
37 * DEALINGS IN THE SOFTWARE.
38 */
39
40 #ifndef A5HASH_INCLUDED
41 #define A5HASH_INCLUDED
42
43 #define A5HASH_VER_STR "5.25" ///< A5HASH source code version string.
44
45 /**
46 * @def A5HASH_NS_CUSTOM
47 * @brief If this macro is defined externally, all symbols will be placed into
48 * the C++ namespace specified by this macro and will not be exported to the
49 * global namespace. WARNING: if the macro's value is empty, the symbols will
50 * be placed into the global namespace anyway.
51 */
52
53 /**
54 * @def A5HASH_U64_C( x )
55 * @brief Macro that defines a numeric value as unsigned 64-bit value.
56 *
57 * @param x Value.
58 */
59
60 /**
61 * @def A5HASH_NOEX
62 * @brief Macro that defines the "noexcept" function specifier for C++
63 * environment.
64 */
65
66 /**
67 * @def A5HASH_NULL
68 * @brief Macro that defines "nullptr" value, for C++ guidelines compliance.
69 */
70
71 /**
72 * @def A5HASH_NS
73 * @brief Macro that defines the actual implementation namespace in C++
74 * environment, with export of relevant symbols to the global namespace
75 * (if @ref A5HASH_NS_CUSTOM is undefined).
76 */
77
78 #if defined( __cplusplus )
79
80 #include <cstring>
81
82 #if __cplusplus >= 201103L
83
84 #include <cstdint>
85
86 #define A5HASH_U64_C( x ) UINT64_C( x )
87 #define A5HASH_NOEX noexcept
88 #define A5HASH_NULL nullptr
89
90 #else // __cplusplus >= 201103L
91
92 #include <stdint.h>
93
94 #define A5HASH_U64_C( x ) (uint64_t) x
95 #define A5HASH_NOEX throw()
96 #define A5HASH_NULL NULL
97
98 #endif // __cplusplus >= 201103L
99
100 #if defined( A5HASH_NS_CUSTOM )
101 #define A5HASH_NS A5HASH_NS_CUSTOM
102 #else // defined( A5HASH_NS_CUSTOM )
103 #define A5HASH_NS a5hash_impl
104 #endif // defined( A5HASH_NS_CUSTOM )
105
106 #else // defined( __cplusplus )
107
108 #include <string.h>
109 #include <stdint.h>
110
111 #define A5HASH_U64_C( x ) (uint64_t) x
112 #define A5HASH_NOEX
113 #define A5HASH_NULL NULL
114
115 #endif // defined( __cplusplus )
116
117 #if defined( _MSC_VER )
118 #include <intrin.h>
119 #endif // defined( _MSC_VER )
120
121 #define A5HASH_VAL10 A5HASH_U64_C( 0xAAAAAAAAAAAAAAAA ) ///< `10` bit-pairs.
122 #define A5HASH_VAL01 A5HASH_U64_C( 0x5555555555555555 ) ///< `01` bit-pairs.
123
124 /**
125 * @def A5HASH_ICC_GCC
126 * @brief Macro that denotes the use of ICC classic compiler with GCC-style
127 * built-in functions.
128 */
129
130 #if defined( __INTEL_COMPILER ) && __INTEL_COMPILER >= 1300 && \
131 !defined( _MSC_VER )
132
133 #define A5HASH_ICC_GCC
134
135 #endif // ICC check
136
137 /**
138 * @def A5HASH_GCC_BUILTINS
139 * @brief Macro that denotes availability of GCC-style built-in functions.
140 */
141
142 #if defined( __GNUC__ ) || defined( __clang__ ) || \
143 defined( __IBMC__ ) || defined( __IBMCPP__ ) || defined( A5HASH_ICC_GCC )
144
145 #define A5HASH_GCC_BUILTINS
146
147 #endif // GCC built-ins check
148
149 /**
150 * @def A5HASH_BMI2
151 * @brief Macro that denotes availability of the `mulx` intrinsic
152 * (MSVC-compatible compilers only).
153 */
154
155 #if defined( _MSC_VER )
156 #if defined( __BMI2__ ) || ( !defined( A5HASH_GCC_BUILTINS ) && \
157 defined( _M_AMD64 ) && defined( __AVX2__ ) && \
158 ( defined( __INTEL_COMPILER ) || _MSC_VER >= 1900 ))
159
160 #include <immintrin.h>
161 #define A5HASH_BMI2
162
163 #else // BMI2
164
165 #include <intrin.h>
166
167 #endif // BMI2
168 #endif // defined( _MSC_VER )
169
170 /**
171 * @def A5HASH_STATIC
172 * @brief Macro that defines a function as "static".
173 */
174
175 #if ( defined( __cplusplus ) && __cplusplus >= 201703L ) || \
176 ( defined( __STDC_VERSION__ ) && __STDC_VERSION__ >= 202311L )
177
178 #define A5HASH_STATIC [[maybe_unused]] static
179
180 #elif defined( A5HASH_GCC_BUILTINS )
181
182 #define A5HASH_STATIC static __attribute__((unused))
183
184 #else // defined( A5HASH_GCC_BUILTINS )
185
186 #define A5HASH_STATIC static
187
188 #endif // defined( A5HASH_GCC_BUILTINS )
189
190 /**
191 * @def A5HASH_INLINE
192 * @brief Macro that defines a function as inlinable at the compiler's
193 * discretion.
194 */
195
196 #define A5HASH_INLINE A5HASH_STATIC inline
197
198 /**
199 * @def A5HASH_INLINE_F
200 * @brief Macro to force code inlining.
201 */
202
203 #if defined( __LP64__ ) || defined( _LP64 ) || \
204 !( SIZE_MAX <= 0xFFFFFFFFU ) || ( defined( UINTPTR_MAX ) && \
205 !( UINTPTR_MAX <= 0xFFFFFFFFU )) || defined( __x86_64__ ) || \
206 defined( __aarch64__ ) || defined( _M_AMD64 ) || defined( _M_ARM64 )
207
208 #if defined( A5HASH_GCC_BUILTINS )
209
210 #define A5HASH_INLINE_F A5HASH_INLINE __attribute__((always_inline))
211
212 #elif defined( _MSC_VER )
213
214 #define A5HASH_INLINE_F A5HASH_STATIC __forceinline
215
216 #endif // defined( _MSC_VER )
217
218 #endif // 64-bit platform check
219
220 #if !defined( A5HASH_INLINE_F )
221 #define A5HASH_INLINE_F A5HASH_INLINE
222 #endif // !defined( A5HASH_INLINE_F )
223
224 #if defined( A5HASH_NS )
225
226 namespace A5HASH_NS {
227
228 using std :: memcpy;
229 using std :: size_t;
230
231 #if __cplusplus >= 201103L
232
233 using std :: uint32_t;
234 using std :: uint64_t;
235 using uint8_t = unsigned char; ///< For C++ type aliasing compliance.
236
237 #endif // __cplusplus >= 201103L
238
239 #endif // defined( A5HASH_NS )
240
241 /**
242 * @{
243 * @brief Load unsigned value of the specific bit size from memory.
244 *
245 * @param p Load address.
246 */
247
248 A5HASH_INLINE_F uint32_t a5hash_lu32( const uint8_t* const p ) A5HASH_NOEX
249 {
250 uint32_t v;
251 memcpy( &v, p, 4 );
252
253 return( v );
254 }
255
256 A5HASH_INLINE_F uint64_t a5hash_lu64( const uint8_t* const p ) A5HASH_NOEX
257 {
258 uint64_t v;
259 memcpy( &v, p, 8 );
260
261 return( v );
262 }
263
264 /** @} */
265
266 /**
267 * @brief 64-bit by 64-bit unsigned multiplication producing a 128-bit result.
268 *
269 * @param u Multiplier 1.
270 * @param v Multiplier 2.
271 * @param[out] rl The lower half of the 128-bit result.
272 * @param[out] rh The upper half of the 128-bit result.
273 */
274
275 A5HASH_INLINE_F void a5hash_umul128( const uint64_t u, const uint64_t v,
276 uint64_t* const rl, uint64_t* const rh ) A5HASH_NOEX
277 {
278 #if defined( A5HASH_BMI2 )
279
280 *rl = _mulx_u64( u, v, rh );
281
282 #elif defined( _MSC_VER ) && ( defined( _M_ARM64 ) || defined( _M_ARM64EC ) || \
283 ( defined( __INTEL_COMPILER ) && defined( _M_AMD64 )))
284
285 *rl = u * v;
286 *rh = __umulh( u, v );
287
288 #elif defined( _MSC_VER ) && ( defined( _M_AMD64 ) || defined( _M_IA64 ))
289
290 *rl = _umul128( u, v, rh );
291
292 #elif defined( __SIZEOF_INT128__ ) || \
293 ( defined( A5HASH_ICC_GCC ) && defined( __x86_64__ ))
294
295 __uint128_t r = u;
296 r *= v;
297
298 *rl = (uint64_t) r;
299 *rh = (uint64_t) ( r >> 64 );
300
301 #elif ( defined( __IBMC__ ) || defined( __IBMCPP__ )) && defined( __LP64__ )
302
303 *rl = u * v;
304 *rh = __mulhdu( u, v );
305
306 #else // defined( __IBMC__ )
307
308 // _umul128() code for 32-bit systems, adapted from Hacker's Delight,
309 // Henry S. Warren, Jr.
310
311 *rl = u * v;
312
313 const uint32_t u0 = (uint32_t) u;
314 const uint32_t v0 = (uint32_t) v;
315 const uint64_t w0 = (uint64_t) u0 * v0;
316 const uint32_t u1 = (uint32_t) ( u >> 32 );
317 const uint32_t v1 = (uint32_t) ( v >> 32 );
318 const uint64_t t = (uint64_t) u1 * v0 + (uint32_t) ( w0 >> 32 );
319 const uint64_t w1 = (uint64_t) u0 * v1 + (uint32_t) t;
320
321 *rh = (uint64_t) u1 * v1 + (uint32_t) ( w1 >> 32 ) +
322 (uint32_t) ( t >> 32 );
323
324 #endif // defined( __IBMC__ )
325 }
326
327 /**
328 * @brief A5HASH 64-bit hash function.
329 *
330 * Produces and returns a 64-bit hash value (digest) of the specified message,
331 * string, or binary data block. Designed for string/small key data hash-map
332 * and hash-table uses.
333 *
334 * @param Msg0 The message to produce a hash from. The alignment of this
335 * pointer is unimportant. It is valid to pass 0 when `MsgLen` equals 0.
336 * @param MsgLen Message length, in bytes, can be zero.
337 * @param UseSeed An optional value to use instead of the default seed (0).
338 * This value can have any number of significant bits and any statistical
339 * quality.
340 * @return 64-bit hash of the input data.
341 */
342
343 A5HASH_INLINE_F uint64_t a5hash( const void* const Msg0, size_t MsgLen,
344 const uint64_t UseSeed ) A5HASH_NOEX
345 {
346 const uint8_t* Msg = (const uint8_t*) Msg0;
347
348 uint64_t val01 = A5HASH_VAL01;
349 uint64_t val10 = A5HASH_VAL10;
350
351 // The seeds are initialized to mantissa bits of PI.
352
353 uint64_t Seed1 = A5HASH_U64_C( 0x243F6A8885A308D3 ) ^ MsgLen;
354 uint64_t Seed2 = A5HASH_U64_C( 0x452821E638D01377 ) ^ MsgLen;
355
356 a5hash_umul128( Seed2 ^ ( UseSeed & val10 ),
357 Seed1 ^ ( UseSeed & val01 ), &Seed1, &Seed2 );
358
359 if( MsgLen > 16 )
360 {
361 val01 ^= Seed1;
362 val10 ^= Seed2;
363
364 do
365 {
366 a5hash_umul128( (uint64_t) a5hash_lu32( Msg ) << 32 ^
367 a5hash_lu32( Msg + 4 ) ^ Seed1,
368 (uint64_t) a5hash_lu32( Msg + 8 ) << 32 ^
369 a5hash_lu32( Msg + 12 ) ^ Seed2, &Seed1, &Seed2 );
370
371 MsgLen -= 16;
372 Msg += 16;
373
374 Seed1 += val01;
375 Seed2 += val10;
376
377 } while( MsgLen > 16 );
378 }
379
380 if( MsgLen == 0 )
381 {
382 goto _fin;
383 }
384
385 if( MsgLen > 3 )
386 {
387 const uint8_t* Msg4;
388 size_t mo;
389
390 Msg4 = Msg + MsgLen - 4;
391 mo = MsgLen >> 3;
392
393 Seed1 ^= (uint64_t) a5hash_lu32( Msg ) << 32 | a5hash_lu32( Msg4 );
394
395 Seed2 ^= (uint64_t) a5hash_lu32( Msg + mo * 4 ) << 32 |
396 a5hash_lu32( Msg4 - mo * 4 );
397
398 _fin:
399 a5hash_umul128( Seed1, Seed2, &Seed1, &Seed2 );
400
401 a5hash_umul128( val01 ^ Seed1, Seed2, &Seed1, &Seed2 );
402
403 return( Seed1 ^ Seed2 );
404 }
405 else
406 {
407 Seed1 ^= Msg[ 0 ];
408
409 if( --MsgLen != 0 )
410 {
411 Seed1 ^= (uint64_t) Msg[ 1 ] << 8;
412
413 if( --MsgLen != 0 )
414 {
415 Seed1 ^= (uint64_t) Msg[ 2 ] << 16;
416 }
417 }
418
419 goto _fin;
420 }
421 }
422
423 /**
424 * @brief 32-bit by 32-bit unsigned multiplication producing a 64-bit result.
425 *
426 * @param u Multiplier 1.
427 * @param v Multiplier 2.
428 * @param[out] rl The lower half of the 64-bit result.
429 * @param[out] rh The upper half of the 64-bit result.
430 */
431
432 A5HASH_INLINE_F void a5hash_umul64( const uint32_t u, const uint32_t v,
433 uint32_t* const rl, uint32_t* const rh ) A5HASH_NOEX
434 {
435 const uint64_t r = (uint64_t) u * v;
436
437 *rl = (uint32_t) r;
438 *rh = (uint32_t) ( r >> 32 );
439 }
440
441 /**
442 * @brief A5HASH 32-bit hash function.
443 *
444 * Produces and returns a 32-bit hash value (digest) of the specified message,
445 * string, or binary data block. Designed for string/small key data hash-map
446 * and hash-table uses.
447 *
448 * This function works natively on 32-bit platforms and avoids the performance
449 * penalty of 64-bit arithmetic.
450 *
451 * @param Msg0 The message to produce a hash from. The alignment of this
452 * pointer is unimportant. It is valid to pass 0 when `MsgLen` equals 0.
453 * @param MsgLen Message length, in bytes, can be zero.
454 * @param UseSeed An optional value to use instead of the default seed (0).
455 * This value can have any number of significant bits and any statistical
456 * quality.
457 * @return 32-bit hash of the input data.
458 */
459
460 A5HASH_INLINE_F uint32_t a5hash32( const void* const Msg0, size_t MsgLen,
461 const uint32_t UseSeed ) A5HASH_NOEX
462 {
463 const uint8_t* Msg = (const uint8_t*) Msg0;
464
465 uint32_t val01 = (uint32_t) A5HASH_VAL01;
466 uint32_t val10 = (uint32_t) A5HASH_VAL10;
467
468 // The seeds are initialized to mantissa bits of PI.
469
470 uint32_t Seed1 = 0x243F6A88 ^ (uint32_t) MsgLen;
471 uint32_t Seed2 = 0x85A308D3 ^ (uint32_t) MsgLen;
472 uint32_t Seed3, Seed4;
473 uint32_t a, b, c, d;
474
475 #if SIZE_MAX <= 0xFFFFFFFFU
476
477 Seed3 = 0xFB0BD3EA;
478 Seed4 = 0x0F58FD47;
479
480 #else // SIZE_MAX <= 0xFFFFFFFFU
481
482 a5hash_umul64( (uint32_t) ( MsgLen >> 32 ) ^ 0x452821E6,
483 (uint32_t) ( MsgLen >> 32 ) ^ 0x38D01377, &Seed3, &Seed4 );
484
485 #endif // SIZE_MAX <= 0xFFFFFFFFU
486
487 a5hash_umul64( Seed2 ^ ( UseSeed & val10 ),
488 Seed1 ^ ( UseSeed & val01 ), &Seed1, &Seed2 );
489
490 if( MsgLen < 17 )
491 {
492 if( MsgLen > 3 )
493 {
494 const uint8_t* const Msg4 = Msg + MsgLen - 4;
495 size_t mo;
496
497 a = a5hash_lu32( Msg );
498 b = a5hash_lu32( Msg4 );
499
500 if( MsgLen < 9 )
501 {
502 goto _fin;
503 }
504
505 mo = MsgLen >> 3;
506
507 c = a5hash_lu32( Msg + mo * 4 );
508 d = a5hash_lu32( Msg4 - mo * 4 );
509 }
510 else
511 {
512 a = 0;
513 b = 0;
514
515 if( MsgLen != 0 )
516 {
517 a = Msg[ 0 ];
518
519 if( MsgLen != 1 )
520 {
521 a |= (uint32_t) Msg[ 1 ] << 8;
522
523 if( MsgLen != 2 )
524 {
525 a |= (uint32_t) Msg[ 2 ] << 16;
526 }
527 }
528 }
529
530 goto _fin;
531 }
532 }
533 else
534 {
535 val01 ^= Seed1;
536 val10 ^= Seed2;
537
538 do
539 {
540 const uint32_t s1 = Seed1;
541 const uint32_t s4 = Seed4;
542
543 a5hash_umul64( a5hash_lu32( Msg ) + Seed1,
544 a5hash_lu32( Msg + 4 ) + Seed2, &Seed1, &Seed2 );
545
546 a5hash_umul64( a5hash_lu32( Msg + 8 ) + Seed3,
547 a5hash_lu32( Msg + 12 ) + Seed4, &Seed3, &Seed4 );
548
549 MsgLen -= 16;
550 Msg += 16;
551
552 Seed1 += val01;
553 Seed2 += s4;
554 Seed3 += s1;
555 Seed4 += val10;
556
557 } while( MsgLen > 16 );
558
559 a = a5hash_lu32( Msg + MsgLen - 8 );
560 b = a5hash_lu32( Msg + MsgLen - 4 );
561
562 if( MsgLen < 9 )
563 {
564 goto _fin;
565 }
566
567 c = a5hash_lu32( Msg + MsgLen - 16 );
568 d = a5hash_lu32( Msg + MsgLen - 12 );
569 }
570
571 a5hash_umul64( c + Seed3, d + Seed4, &Seed3, &Seed4 );
572
573 _fin:
574 Seed1 ^= Seed3;
575 Seed2 ^= Seed4;
576
577 a5hash_umul64( a + Seed1, b + Seed2, &Seed1, &Seed2 );
578
579 a5hash_umul64( val01 ^ Seed1, Seed2, &a, &b );
580
581 return( a ^ b );
582 }
583
584 /**
585 * @brief A5HASH 128-bit hash function.
586 *
587 * Produces and returns a 128-bit hash value (digest) of the specified
588 * message, string, or binary data block. Designed for string/small key data
589 * hash-map and hash-table uses.
590 *
591 * @param Msg0 The message to produce a hash from. The alignment of this
592 * pointer is unimportant. It is valid to pass 0 when `MsgLen` equals 0.
593 * @param MsgLen Message length, in bytes, can be zero.
594 * @param UseSeed An optional value to use instead of the default seed (0).
595 * This value can have any number of significant bits and any statistical
596 * quality.
597 * @param[out] rh Pointer to 64-bit variable that receives upper 64 bits of
598 * 128-bit hash. The alignment of this pointer is unimportant. Can be 0.
599 * @return Lower 64 bits of 128-bit hash of the input data.
600 */
601
602 A5HASH_INLINE uint64_t a5hash128( const void* const Msg0, size_t MsgLen,
603 const uint64_t UseSeed, void* const rh ) A5HASH_NOEX
604 {
605 const uint8_t* Msg = (const uint8_t*) Msg0;
606
607 uint64_t val01 = A5HASH_VAL01;
608 uint64_t val10 = A5HASH_VAL10;
609
610 // The seeds are initialized to mantissa bits of PI.
611
612 uint64_t Seed1 = A5HASH_U64_C( 0x243F6A8885A308D3 ) ^ MsgLen;
613 uint64_t Seed2 = A5HASH_U64_C( 0x452821E638D01377 ) ^ MsgLen;
614 uint64_t Seed3 = A5HASH_U64_C( 0xA4093822299F31D0 );
615 uint64_t Seed4 = A5HASH_U64_C( 0xC0AC29B7C97C50DD );
616 uint64_t a, b, c, d;
617
618 a5hash_umul128( Seed2 ^ ( UseSeed & val10 ),
619 Seed1 ^ ( UseSeed & val01 ), &Seed1, &Seed2 );
620
621 if( MsgLen < 17 )
622 {
623 if( MsgLen > 3 )
624 {
625 const uint8_t* Msg4;
626 size_t mo;
627
628 Msg4 = Msg + MsgLen - 4;
629 mo = MsgLen >> 3;
630
631 a = (uint64_t) a5hash_lu32( Msg ) << 32 | a5hash_lu32( Msg4 );
632
633 b = (uint64_t) a5hash_lu32( Msg + mo * 4 ) << 32 |
634 a5hash_lu32( Msg4 - mo * 4 );
635
636 _fin16:
637 a5hash_umul128( a + Seed1, b + Seed2, &Seed1, &Seed2 );
638
639 a5hash_umul128( val01 ^ Seed1, Seed2, &a, &b );
640
641 a ^= b;
642
643 if( rh != A5HASH_NULL )
644 {
645 a5hash_umul128( Seed1 ^ Seed3, Seed2 ^ Seed4, &Seed3, &Seed4 );
646
647 Seed3 ^= Seed4;
648 memcpy( rh, &Seed3, 8 );
649 }
650
651 return( a );
652 }
653 else
654 {
655 a = 0;
656 b = 0;
657
658 if( MsgLen != 0 )
659 {
660 a = Msg[ 0 ];
661
662 if( --MsgLen != 0 )
663 {
664 a |= (uint64_t) Msg[ 1 ] << 8;
665
666 if( --MsgLen != 0 )
667 {
668 a |= (uint64_t) Msg[ 2 ] << 16;
669 }
670 }
671 }
672
673 goto _fin16;
674 }
675 }
676
677 if( MsgLen < 33 )
678 {
679 a = (uint64_t) a5hash_lu32( Msg ) << 32 | a5hash_lu32( Msg + 4 );
680 b = (uint64_t) a5hash_lu32( Msg + 8 ) << 32 | a5hash_lu32( Msg + 12 );
681 c = (uint64_t) a5hash_lu32( Msg + MsgLen - 16 ) << 32 |
682 a5hash_lu32( Msg + MsgLen - 12 );
683
684 d = (uint64_t) a5hash_lu32( Msg + MsgLen - 8 ) << 32 |
685 a5hash_lu32( Msg + MsgLen - 4 );
686
687 _fin_m:
688 a5hash_umul128( c + Seed3, d + Seed4, &Seed3, &Seed4 );
689
690 _fin:
691 Seed1 ^= Seed3;
692 Seed2 ^= Seed4;
693
694 a5hash_umul128( a + Seed1, b + Seed2, &Seed1, &Seed2 );
695
696 a5hash_umul128( val01 ^ Seed1, Seed2, &a, &b );
697
698 a ^= b;
699
700 if( rh != A5HASH_NULL )
701 {
702 a5hash_umul128( Seed1 ^ Seed3, Seed2 ^ Seed4, &Seed3, &Seed4 );
703
704 Seed3 ^= Seed4;
705 memcpy( rh, &Seed3, 8 );
706 }
707
708 return( a );
709 }
710 else
711 {
712 val01 ^= Seed1;
713 val10 ^= Seed2;
714
715 if( MsgLen > 64 )
716 {
717 uint64_t Seed5 = A5HASH_U64_C( 0x082EFA98EC4E6C89 );
718 uint64_t Seed6 = A5HASH_U64_C( 0x3F84D5B5B5470917 );
719 uint64_t Seed7 = A5HASH_U64_C( 0x13198A2E03707344 );
720 uint64_t Seed8 = A5HASH_U64_C( 0xBE5466CF34E90C6C );
721
722 do
723 {
724 const uint64_t s1 = Seed1;
725 const uint64_t s3 = Seed3;
726 const uint64_t s5 = Seed5;
727
728 a5hash_umul128( a5hash_lu64( Msg ) + Seed1,
729 a5hash_lu64( Msg + 32 ) + Seed2, &Seed1, &Seed2 );
730
731 Seed1 += val01;
732 Seed2 += Seed8;
733
734 a5hash_umul128( a5hash_lu64( Msg + 8 ) + Seed3,
735 a5hash_lu64( Msg + 40 ) + Seed4, &Seed3, &Seed4 );
736
737 Seed3 += s1;
738 Seed4 += val10;
739
740 a5hash_umul128( a5hash_lu64( Msg + 16 ) + Seed5,
741 a5hash_lu64( Msg + 48 ) + Seed6, &Seed5, &Seed6 );
742
743 a5hash_umul128( a5hash_lu64( Msg + 24 ) + Seed7,
744 a5hash_lu64( Msg + 56 ) + Seed8, &Seed7, &Seed8 );
745
746 MsgLen -= 64;
747 Msg += 64;
748
749 Seed5 += s3;
750 Seed6 += val10;
751 Seed7 += s5;
752 Seed8 += val10;
753
754 } while( MsgLen > 64 );
755
756 Seed1 ^= Seed5;
757 Seed2 ^= Seed6;
758 Seed3 ^= Seed7;
759 Seed4 ^= Seed8;
760
761 if( MsgLen > 32 )
762 {
763 goto _tail32;
764 }
765 }
766 else
767 {
768 uint64_t s1;
769
770 _tail32:
771 s1 = Seed1;
772
773 a5hash_umul128( a5hash_lu64( Msg ) + Seed1,
774 a5hash_lu64( Msg + 8 ) + Seed2, &Seed1, &Seed2 );
775
776 Seed1 += val01;
777 Seed2 += Seed4;
778
779 a5hash_umul128( a5hash_lu64( Msg + 16 ) + Seed3,
780 a5hash_lu64( Msg + 24 ) + Seed4, &Seed3, &Seed4 );
781
782 MsgLen -= 32;
783 Msg += 32;
784
785 Seed3 += s1;
786 Seed4 += val10;
787 }
788
789 a = a5hash_lu64( Msg + MsgLen - 16 );
790 b = a5hash_lu64( Msg + MsgLen - 8 );
791
792 if( MsgLen < 17 )
793 {
794 goto _fin;
795 }
796
797 c = a5hash_lu64( Msg + MsgLen - 32 );
798 d = a5hash_lu64( Msg + MsgLen - 24 );
799
800 goto _fin_m;
801 }
802 }
803
804 /**
805 * @brief A5RAND 64-bit pseudo-random number generator.
806 *
807 * A simple, reliable, self-starting, yet efficient PRNG with a 2^64 period.
808 * 0.50 cycles/byte performance. It self-starts in 4 iterations, which is the
809 * suggested "warm-up" period before using its output when seeds are
810 * initialized with an arbitrary value. If initialized with high-quality,
811 * uniformly random value (e.g., from the operating system's entropy or a
812 * hash function's output), the PRNG output is valid from the start.
813 *
814 * @param[in,out] Seed1 Seed value 1. Can be initialized to any value
815 * (even 0). Should not be used as the PRNG value.
816 * @param[in,out] Seed2 Seed value 2. Must be initialized to the same value as
817 * `Seed1`. Should not be used as the PRNG value.
818 * @return The next uniformly random 64-bit value.
819 */
820
821 A5HASH_INLINE_F uint64_t a5rand( uint64_t* const Seed1,
822 uint64_t* const Seed2 ) A5HASH_NOEX
823 {
824 uint64_t s1 = *Seed1;
825 uint64_t s2 = *Seed2;
826
827 a5hash_umul128( s1 + A5HASH_VAL01, s2 + A5HASH_VAL10, &s1, &s2 );
828
829 *Seed1 = s1;
830 *Seed2 = s2;
831
832 return( s1 ^ s2 );
833 }
834
835 #if defined( A5HASH_NS )
836
837 } // namespace A5HASH_NS
838
839 #if !defined( A5HASH_NS_CUSTOM )
840
841 namespace {
842
843 using A5HASH_NS :: a5hash_umul128;
844 using A5HASH_NS :: a5hash;
845 using A5HASH_NS :: a5hash32;
846 using A5HASH_NS :: a5hash128;
847 using A5HASH_NS :: a5rand;
848
849 } // namespace
850
851 #endif // !defined( A5HASH_NS_CUSTOM )
852
853 #endif // defined( A5HASH_NS )
854
855 // Defines for Doxygen.
856
857 #if !defined( A5HASH_NS_CUSTOM )
858 #define A5HASH_NS_CUSTOM
859 #endif // !defined( A5HASH_NS_CUSTOM )
860
861 #undef A5HASH_NS_CUSTOM
862 #undef A5HASH_U64_C
863 #undef A5HASH_NOEX
864 #undef A5HASH_NULL
865 #undef A5HASH_VAL10
866 #undef A5HASH_VAL01
867 #undef A5HASH_ICC_GCC
868 #undef A5HASH_GCC_BUILTINS
869 #undef A5HASH_BMI2
870 #undef A5HASH_STATIC
871 #undef A5HASH_INLINE
872 #undef A5HASH_INLINE_F
873
874 #endif // A5HASH_INCLUDED