Optimize default RNG for the common case

This commit is contained in:
Feoramund
2024-06-20 11:27:51 -04:00
parent 28e33d86de
commit 1dfc89567e
+16 -9
View File
@@ -86,16 +86,23 @@ default_random_generator_proc :: proc(data: rawptr, mode: Random_Generator_Mode,
init(r, 0) init(r, 0)
} }
pos := i8(0) switch len(p) {
val := u64(0) case size_of(u64):
for &v in p { // Fast path for a 64-bit destination.
if pos == 0 { (transmute(^u64)raw_data(p))^ = read_u64(r)
val = read_u64(r) case:
pos = 7 // All other cases.
pos := i8(0)
val := u64(0)
for &v in p {
if pos == 0 {
val = read_u64(r)
pos = 7
}
v = byte(val)
val >>= 8
pos -= 1
} }
v = byte(val)
val >>= 8
pos -= 1
} }
case .Reset: case .Reset: