Skip to content

Commit a457907

Browse files
authored
Use Simd::wrapping_neg (#1414)
1 parent 1015e70 commit a457907

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

src/distributions/uniform.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -701,21 +701,15 @@ macro_rules! uniform_simd_int_impl {
701701
if !(low.simd_le(high).all()) {
702702
return Err(Error::EmptyRange);
703703
}
704-
let unsigned_max = Simd::splat(::core::$unsigned::MAX);
705704

706705
// NOTE: all `Simd` operations are inherently wrapping,
707706
// see https://doc.rust-lang.org/std/simd/struct.Simd.html
708707
let range: Simd<$unsigned, LANES> = ((high - low) + Simd::splat(1)).cast();
709-
// `% 0` will panic at runtime.
708+
709+
// We must avoid divide-by-zero by using 0 % 1 == 0.
710710
let not_full_range = range.simd_gt(Simd::splat(0));
711-
// replacing 0 with `unsigned_max` allows a faster `select`
712-
// with bitwise OR
713-
let modulo = not_full_range.select(range, unsigned_max);
714-
// wrapping addition
715-
// TODO: replace with `range.wrapping_neg() % module` when Simd supports this.
716-
let ints_to_reject = (Simd::splat(0) - range) % modulo;
717-
// When `range` is 0, `lo` of `v.wmul(range)` will always be
718-
// zero which means only one sample is needed.
711+
let modulo = not_full_range.select(range, Simd::splat(1));
712+
let ints_to_reject = range.wrapping_neg() % modulo;
719713

720714
Ok(UniformInt {
721715
low,

0 commit comments

Comments
 (0)