forked from M-Labs/artiq
1
0
Fork 0

firmware: Explicitly use wrapping integer math in PRNGs

Patch by Hannah McLaughlin; apparently, the overflow actually
doesn't get checked/reported without `opt-level = 2` and
`lto = "thin"`.
This commit is contained in:
David Nadlinger 2022-02-03 23:57:17 +00:00
parent 8866ab301a
commit e45cb217be
2 changed files with 6 additions and 5 deletions

View File

@ -65,8 +65,8 @@ fn memory_test(total: &mut usize, wrong: &mut usize) -> bool {
}) })
} }
fn prng32(seed: &mut u32) -> u32 { *seed = 1664525 * *seed + 1013904223; *seed } fn prng32(seed: &mut u32) -> u32 { *seed = u32::wrapping_add(u32::wrapping_mul(1664525, *seed), 1013904223); *seed }
fn prng16(seed: &mut u16) -> u16 { *seed = 25173 * *seed + 13849; *seed } fn prng16(seed: &mut u16) -> u16 { *seed = u16::wrapping_add(u16::wrapping_mul(25173, *seed), 13849); *seed }
for _ in 0..4 { for _ in 0..4 {
// Test data bus // Test data bus

View File

@ -211,9 +211,10 @@ mod ddr {
// Generate pseudo-random sequence // Generate pseudo-random sequence
let mut prs = [0; DFII_NPHASES * DFII_PIX_DATA_SIZE]; let mut prs = [0; DFII_NPHASES * DFII_PIX_DATA_SIZE];
let mut prv = 42; let mut prv: u32 = 42;
for b in prs.iter_mut() { for b in prs.iter_mut() {
prv = 1664525 * prv + 1013904223;
prv = u32::wrapping_add(u32::wrapping_mul(1664525, prv), 1013904223);
*b = prv as u8; *b = prv as u8;
} }
@ -296,7 +297,7 @@ mod ddr {
let mut prs = [0; DFII_NPHASES * DFII_PIX_DATA_SIZE]; let mut prs = [0; DFII_NPHASES * DFII_PIX_DATA_SIZE];
let mut prv = 42; let mut prv = 42;
for b in prs.iter_mut() { for b in prs.iter_mut() {
prv = 1664525 * prv + 1013904223; prv = u32::wrapping_add(u32::wrapping_mul(1664525, prv), 1013904223);
*b = prv as u8; *b = prv as u8;
} }