]> pd.if.org Git - liblfds/blob - liblfds/liblfds7.1.0/liblfds710/inc/liblfds710/lfds710_prng.h
Initial import (all versions, including the new 7.1.0)
[liblfds] / liblfds / liblfds7.1.0 / liblfds710 / inc / liblfds710 / lfds710_prng.h
1 /***** defines *****/
2 #define LFDS710_PRNG_MAX  ( (lfds710_pal_uint_t) -1 )
3
4 /* TRD : the seed is from an on-line hardware RNG, using atmospheric noise
5          the URL below will generate another 16 random hex digits (e.g. a 64-bit number) and is
6          the RNG used to generate the number above (0x0a34655d34c092fe)
7
8          http://www.random.org/integers/?num=16&min=0&max=15&col=1&base=16&format=plain&rnd=new
9
10          the 32 bit seed is the upper half of the 64 bit seed
11
12          the "SplitMix" PRNG is from from Sebastiano vigna's site, CC0 license, http://xorshift.di.unimi.it/splitmix64.c
13          the 64-bit constants come directly from the source, the 32-bt constants are in fact the 32-bit murmurhash3 constants
14 */
15
16 #if( LFDS710_PAL_ALIGN_SINGLE_POINTER == 4 )
17   #define LFDS710_PRNG_SEED                            0x0a34655dUL
18   #define LFDS710_PRNG_SPLITMIX_MAGIC_RATIO            0x9E3779B9UL
19   #define LFDS710_PRNG_SPLITMIX_SHIFT_CONSTANT_ONE     16
20   #define LFDS710_PRNG_SPLITMIX_SHIFT_CONSTANT_TWO     13
21   #define LFDS710_PRNG_SPLITMIX_SHIFT_CONSTANT_THREE   16
22   #define LFDS710_PRNG_SPLITMIX_MULTIPLY_CONSTANT_ONE  0x85ebca6bUL
23   #define LFDS710_PRNG_SPLITMIX_MULTIPLY_CONSTANT_TWO  0xc2b2ae35UL
24 #endif
25
26 #if( LFDS710_PAL_ALIGN_SINGLE_POINTER == 8 )
27   #define LFDS710_PRNG_SEED                            0x0a34655d34c092feULL
28   #define LFDS710_PRNG_SPLITMIX_MAGIC_RATIO            0x9E3779B97F4A7C15ULL
29   #define LFDS710_PRNG_SPLITMIX_SHIFT_CONSTANT_ONE     30
30   #define LFDS710_PRNG_SPLITMIX_SHIFT_CONSTANT_TWO     27
31   #define LFDS710_PRNG_SPLITMIX_SHIFT_CONSTANT_THREE   31
32   #define LFDS710_PRNG_SPLITMIX_MULTIPLY_CONSTANT_ONE  0xBF58476D1CE4E5B9ULL
33   #define LFDS710_PRNG_SPLITMIX_MULTIPLY_CONSTANT_TWO  0x94D049BB133111EBULL
34 #endif
35
36 // TRD : struct lfds710_prng_state prng_state, lfds710_pal_uint_t random_value
37 #define LFDS710_PRNG_GENERATE( prng_state, random_value )                                                                  \
38 {                                                                                                                          \
39   LFDS710_PAL_ATOMIC_ADD( &(prng_state).entropy, LFDS710_PRNG_SPLITMIX_MAGIC_RATIO, (random_value), lfds710_pal_uint_t );  \
40   LFDS710_PRNG_ST_MIXING_FUNCTION( random_value );                                                                         \
41 }
42
43 // TRD : struct lfds710_prng_state prng_st_state, lfds710_pal_uint_t random_value
44 #define LFDS710_PRNG_ST_GENERATE( prng_st_state, random_value )                       \
45 {                                                                                     \
46   (random_value) = ( (prng_st_state).entropy += LFDS710_PRNG_SPLITMIX_MAGIC_RATIO );  \
47   LFDS710_PRNG_ST_MIXING_FUNCTION( random_value );                                    \
48 }
49
50 // TRD : lfds710_pal_uint_t random_value
51 #define LFDS710_PRNG_ST_MIXING_FUNCTION( random_value )                                                                                            \
52 {                                                                                                                                                  \
53   (random_value) = ((random_value) ^ ((random_value) >> LFDS710_PRNG_SPLITMIX_SHIFT_CONSTANT_ONE)) * LFDS710_PRNG_SPLITMIX_MULTIPLY_CONSTANT_ONE;  \
54   (random_value) = ((random_value) ^ ((random_value) >> LFDS710_PRNG_SPLITMIX_SHIFT_CONSTANT_TWO)) * LFDS710_PRNG_SPLITMIX_MULTIPLY_CONSTANT_TWO;  \
55   (random_value) = (random_value ^ (random_value >> LFDS710_PRNG_SPLITMIX_SHIFT_CONSTANT_THREE));                                                  \
56 }
57
58 /***** structs *****/
59 struct lfds710_prng_state
60 {
61   lfds710_pal_uint_t volatile LFDS710_PAL_ALIGN(LFDS710_PAL_ATOMIC_ISOLATION_IN_BYTES)
62     entropy;
63 };
64
65 struct lfds710_prng_st_state
66 {
67   lfds710_pal_uint_t
68     entropy;
69 };
70
71 /***** public prototypes *****/
72 void lfds710_prng_init_valid_on_current_logical_core( struct lfds710_prng_state *ps, lfds710_pal_uint_t seed );
73 void lfds710_prng_st_init( struct lfds710_prng_st_state *psts, lfds710_pal_uint_t seed );
74