From e014df2b1a4b6fb0b633d736d8a63ee62309ce97 Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Thu, 19 Mar 2020 09:59:19 +0900 Subject: [PATCH] Fix rare panic in Sobol sampler. Due to the undefined behavior of shifting a number by its bit-width, the Sobol sampler would panic when sample index `1 << 15` was requested. This fixes it without introducing any additional checks or operations. --- sub_crates/sobol/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sub_crates/sobol/src/lib.rs b/sub_crates/sobol/src/lib.rs index 086f282..0b07290 100644 --- a/sub_crates/sobol/src/lib.rs +++ b/sub_crates/sobol/src/lib.rs @@ -92,7 +92,8 @@ fn sobol_u32(dimension: u32, index: u32) -> u32 { let j = index.trailing_zeros(); result ^= vecs[(i + j) as usize]; i += j + 1; - index >>= j + 1; + index >>= j; + index >>= 1; } (result as u32) << 16