From 64fcebf4e53dc0e5782eaee3753ba3a789e5761a Mon Sep 17 00:00:00 2001 From: a dinosaur Date: Sun, 1 Sep 2024 05:11:48 +1000 Subject: [PATCH] simpler range mask calculation --- Sources/Voxelotl/Random/RandomRange.swift | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/Sources/Voxelotl/Random/RandomRange.swift b/Sources/Voxelotl/Random/RandomRange.swift index 1352213..de89e48 100644 --- a/Sources/Voxelotl/Random/RandomRange.swift +++ b/Sources/Voxelotl/Random/RandomRange.swift @@ -65,21 +65,9 @@ public extension RandomProvider where Output: UnsignedInteger { } mutating func nextModless(in bound: Output) -> Output { - func pow2MaskFrom(range num: Output) -> Output { - if num & (num - 1) == 0 { - return num - 1 - } - var result: Output = 1 - for _ in 0..= num { - return result - 1 - } - result <<= 1 - } - return .max - } + let shift = bound.leadingZeroBitCount + let mask = Output.max &>> shift - let mask = pow2MaskFrom(range: bound) var result: Output repeat { result = next() & mask