commit 75f67e89d47355dd979e84db5cd439951bf7394a
parent 3709c4147b02303bb380caee5786b5c9c7b5e335
Author: Jared Tobin <jared@jtobin.io>
Date: Sat, 12 Jul 2025 16:40:17 -0230
lib: limb benchmarks
Diffstat:
5 files changed, 54 insertions(+), 26 deletions(-)
diff --git a/bench/Criterion/Limb.hs b/bench/Criterion/Limb.hs
@@ -0,0 +1,27 @@
+module Criterion.Limb (
+ benches
+ ) where
+
+import Criterion.Main
+import qualified Data.Word.Limb as Limb
+import Prelude hiding (recip)
+
+benches :: Benchmark
+benches = bgroup "limb arithmetic" [
+ quot_big
+ , quot_small
+ , recip
+ ]
+
+quot_big :: Benchmark
+quot_big = bench "wide quot (big)" $
+ nf (Limb.quot 4294967294 32 4294967293) 32
+
+quot_small :: Benchmark
+quot_small = bench "wide quot (small)" $
+ nf (Limb.quot 4294967294 32 2) 2
+
+recip :: Benchmark
+recip = bench "wide recip" $
+ nf Limb.recip 18446744073709551606
+
diff --git a/bench/Criterion/Wide.hs b/bench/Criterion/Wide.hs
@@ -8,30 +8,23 @@ import Prelude hiding (recip)
benches :: Benchmark
benches = bgroup "wide arithmetic" [
- div1by1_big
- , div1by1_small
- , recip
- , div2by1_bench
- , quotrem2by1_bench
+ quotrem_by1_bench
+ , shr_bench_small
+ , shr_bench_big
]
-div1by1_big :: Benchmark
-div1by1_big = bench "wide div1by1 (big)" $
- nf (Wide.div1by1 4294967294 32 4294967293) 32
+quotrem_by1_bench :: Benchmark
+quotrem_by1_bench = bench "wide quotrem_by1" $
+ nf (Wide.quotrem_by1 (Wide.wide 4294967294 32)) 18446744073709551606
-div1by1_small :: Benchmark
-div1by1_small = bench "wide div1by1 (small)" $
- nf (Wide.div1by1 4294967294 32 2) 2
+_quotrem_by1_bench :: Benchmark
+_quotrem_by1_bench = bench "wide _quotrem_by1" $
+ nf (Wide._quotrem_by1 (Wide.wide 4294967294 32)) 18446744073709551606
-recip :: Benchmark
-recip = bench "wide recip" $
- nf Wide.recip 18446744073709551606
-
-div2by1_bench :: Benchmark
-div2by1_bench = bench "wide div2by1" $
- nf (Wide.div2by1 (Wide.wide 4294967294 32)) 18446744073709551606
-
-quotrem2by1_bench :: Benchmark
-quotrem2by1_bench = bench "wide quotrem2by1" $
- nf (Wide.quotrem2by1 (Wide.wide 4294967294 32)) 18446744073709551606
+shr_bench_small :: Benchmark
+shr_bench_small = bench "wide shr (small)" $
+ nf (Wide.shr (Wide.wide maxBound maxBound)) 1
+shr_bench_big :: Benchmark
+shr_bench_big = bench "wide shr (big)" $
+ nf (Wide.shr (Wide.wide maxBound maxBound)) 127
diff --git a/bench/Main.hs b/bench/Main.hs
@@ -11,7 +11,8 @@ import qualified Data.Word.Extended as W
import Data.Word (Word64)
import Prelude hiding (or, and, div, mod)
import qualified Prelude (div)
-import qualified Criterion.Wide as W
+import qualified Criterion.Limb as Limb
+import qualified Criterion.Wide as Wide
add_sub :: Benchmark
add_sub = bgroup "addition & subtraction" [
@@ -51,7 +52,8 @@ main = defaultMain [
, multiplication
, division
, division_utils
- , W.benches
+ , Wide.benches
+ , Limb.benches
]
-- addition and subtraction ---------------------------------------------------
diff --git a/lib/Data/Word/Limb.hs b/lib/Data/Word/Limb.hs
@@ -19,6 +19,7 @@ module Data.Word.Limb (
-- * Boxed Wrappers
, quot
+ , recip
) where
import Data.Choice
@@ -148,3 +149,7 @@ quot# dividend dividend_bits divisor divisor_bits =
quot :: Word -> Word -> Word -> Word -> Word
quot (W# a) (W# b) (W# c) (W# d) = W# (quot# a b c d)
+recip :: Word -> Word
+recip (W# w) = case recip# w of
+ Reciprocal (# _, _, r #) -> W# r
+
diff --git a/ppad-fixed.cabal b/ppad-fixed.cabal
@@ -56,8 +56,9 @@ benchmark fixed-bench
hs-source-dirs: bench
main-is: Main.hs
other-modules:
- Criterion.Wide
- , Criterion.Choice
+ Criterion.Choice
+ , Criterion.Limb
+ , Criterion.Wide
ghc-options:
-rtsopts -O2 -Wall