commit dbd32a5726d25cdf168f210a66a0bf602980b683
parent e9343601b5ba22d3902953df39026fb8b4d1f8e1
Author: Jared Tobin <jared@jtobin.io>
Date: Sat, 10 Oct 2026 15:52:45 +0400
lib: select bech32 polymod generators by bit test
LLVM lowers the unrolled bit-test selection to conditional selects,
which run considerably faster than the equivalent mask arithmetic
(roughly 240ns vs 540ns over 71 words on an M4).
Diffstat:
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/lib/Data/ByteString/Bech32/Internal.hs b/lib/Data/ByteString/Bech32/Internal.hs
@@ -89,7 +89,9 @@ polymod = BS.foldl' alg 1 where
-- the generator 'g' if bit 'i' of 'b' is set, else zero
gen :: Word32 -> Int -> Word32 -> Word32
- gen b i g = negate ((b `B.shiftR` i) .&. 1) .&. g
+ gen b i g
+ | B.testBit b i = g
+ | otherwise = 0
{-# INLINE gen #-}
valid_hrp :: BS.ByteString -> Bool