Created
February 23, 2025 10:49
-
-
Save ziap/70e49eafafb4be29b9644502fdbc603f to your computer and use it in GitHub Desktop.
PCG32 RSH-RR with vectorized 64-bit generation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Pcg32 = struct { | |
state: u64, | |
fn next_u32(self: *Pcg32) u32 { | |
const s = self.state; | |
self.state = s *% 0x5851f42d4c957f2d +% 0x14057b7ef767814f; | |
const xorshifted: u32 = @truncate((s ^ (s >> 18)) >> 27); | |
const rot: u5 = @intCast(s >> 59); | |
return (xorshifted >> rot) | (xorshifted << -% rot); | |
} | |
fn next_u64(self: *Pcg32) u64 { | |
const s0 = self.state; | |
const s1 = s0 *% 0x5851f42d4c957f2d +% 0x14057b7ef767814f; | |
self.state = s0 *% 0x685f98a2018fade9 +% 0x1a08ee1184ba6d32; | |
const s: @Vector(2, u64) = .{ s0, s1 }; | |
const mask: @Vector(2, u64) = comptime @splat(0xffffffff); | |
const xorshifted = ((s ^ (s >> @splat(18))) >> @splat(27)) & mask; | |
const rot: @Vector(2, u5) = @intCast(s >> @splat(59)); | |
const out = (xorshifted >> rot) | (xorshifted << -% rot); | |
return (out[0] << 32) | @as(u32, @truncate(out[1])); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment