Skip to content

Commit c1634ba

Browse files
committed
Allow splitmix64 code to be avoided
Potentially allow platforms with known-good seed generation to avoid the code overhead of splitmix64. Not actually activating, as I don't believe we have any such platforms yet...
1 parent 4691a58 commit c1634ba

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

source/randLIB.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ static inline uint64_t rol(uint64_t n, int bits)
6969
return (n << bits) | (n >> (64 - bits));
7070
}
7171

72+
#ifndef RANDLIB_ASSUME_GOOD_SEED
7273
/* Lower-quality generator used only for initial seeding, if platform
7374
* isn't returning multiple seeds itself. Multiplies are rather heavy
7475
* for lower-end platforms, but this is initialisation only.
@@ -80,7 +81,8 @@ static uint64_t splitmix64(uint64_t *seed)
8081
z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB);
8182
return z ^ (z >> 31);
8283
}
83-
#endif
84+
#endif // RANDLIB_ASSUME_GOOD_SEED
85+
#endif // RANDOM_DEVICE
8486

8587
void randLIB_seed_random(void)
8688
{
@@ -102,6 +104,14 @@ void randLIB_seed_random(void)
102104
s = (uint64_t) arm_random_seed_get() << 32;
103105
state[1] ^= s | arm_random_seed_get();
104106

107+
#ifdef RANDLIB_ASSUME_GOOD_SEED
108+
/* Can avoid significant code overhead of splitmix64(), but we do still
109+
* have to check for the theoretically possible illegal case of all-zero.
110+
*/
111+
if (state[0] == 0 && state[1] == 0) {
112+
state[1] = 1;
113+
}
114+
#else
105115
/* This check serves to both to stir the state if the platform is returning
106116
* constant seeding values, and to avoid the illegal all-zero state.
107117
*/
@@ -110,7 +120,8 @@ void randLIB_seed_random(void)
110120
state[0] = splitmix64(&seed);
111121
state[1] = splitmix64(&seed);
112122
}
113-
#endif
123+
#endif // RANDLIB_ASSUME_GOOD_SEED
124+
#endif // RANDOM_DEVICE
114125
}
115126

116127
void randLIB_add_seed(uint64_t seed)

0 commit comments

Comments
 (0)