mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
std: fix bug in Pcg32 fill function (#13894)
The PCG32 fill function seems to have been copy-pasted from code using u64, so requesting `n` bytes where `(n & 7) > 4` bytes would cause the last few bytes to be all 0.
This commit is contained in:
parent
cdcb746cc2
commit
7d0d99aac0
1 changed files with 10 additions and 6 deletions
|
|
@ -51,7 +51,7 @@ fn seedTwo(self: *Pcg, init_s: u64, init_i: u64) void {
|
|||
|
||||
pub fn fill(self: *Pcg, buf: []u8) void {
|
||||
var i: usize = 0;
|
||||
const aligned_len = buf.len - (buf.len & 7);
|
||||
const aligned_len = buf.len - (buf.len & 3);
|
||||
|
||||
// Complete 4 byte segments.
|
||||
while (i < aligned_len) : (i += 4) {
|
||||
|
|
@ -108,11 +108,15 @@ test "pcg fill" {
|
|||
3247963454,
|
||||
};
|
||||
|
||||
for (seq) |s| {
|
||||
var buf0: [4]u8 = undefined;
|
||||
var buf1: [3]u8 = undefined;
|
||||
std.mem.writeIntLittle(u32, &buf0, s);
|
||||
var i: u32 = 0;
|
||||
while (i < seq.len) : (i += 2) {
|
||||
var buf0: [8]u8 = undefined;
|
||||
std.mem.writeIntLittle(u32, buf0[0..4], seq[i]);
|
||||
std.mem.writeIntLittle(u32, buf0[4..8], seq[i + 1]);
|
||||
|
||||
var buf1: [7]u8 = undefined;
|
||||
r.fill(&buf1);
|
||||
try std.testing.expect(std.mem.eql(u8, buf0[0..3], buf1[0..]));
|
||||
|
||||
try std.testing.expect(std.mem.eql(u8, buf0[0..7], buf1[0..]));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue