fixed

Pure Haskell large fixed-width integers.
git clone git://git.ppad.tech/fixed.git
Log | Files | Refs | README | LICENSE

commit 01591df69446d8e729c500276f46d2c961868809
parent 1391b6091f4f29f1cb4347e9b7cdf3ef9978051d
Author: Jared Tobin <jared@jtobin.io>
Date:   Thu, 23 Jan 2025 11:24:50 +0400

lib: misc bench and test additions

Diffstat:
Mbench/Main.hs | 18+++++++++++++++++-
Mbench/Weight.hs | 16++++++++++++----
Mppad-fixed.cabal | 1+
Mtest/Main.hs | 17+++++++++++++++++
4 files changed, 47 insertions(+), 5 deletions(-)

diff --git a/bench/Main.hs b/bench/Main.hs @@ -7,7 +7,8 @@ import Data.Bits ((.|.), (.&.), (.^.)) import qualified Data.Word.Extended as W import Control.DeepSeq import Criterion.Main -import Prelude hiding (or, and) +import Prelude hiding (or, and, div) +import qualified Prelude (div) instance NFData W.Word256 instance NFData W.Word512 @@ -102,6 +103,19 @@ mul128 = bench "mul128" $ nf (W.mul w0) w1 where !w0 = W.to_word256 0x7fffffffffffffffffffffffffffffed !w1 = W.to_word256 0x7ffffffffffffffbffffffffffffffed +div_baseline :: Benchmark +div_baseline = bench "div (baseline)" $ nf (Prelude.div w0) w1 where + w0, w1 :: Integer + !w0 = 0x41cf50c7d0d65afabcf5ba37750dba71c7db29ec9f20a216d3ef013a59b9188a + !w1 = 0x066bd4c3c10e30260cb6e7832af25f15527b089b258a1fef13b6eec3ce73bf06 + +div :: Benchmark +div = bench "div" $ nf (W.div w0) w1 where + !w0 = W.to_word256 + 0x41cf50c7d0d65afabcf5ba37750dba71c7db29ec9f20a216d3ef013a59b9188a + !w1 = W.to_word256 + 0x066bd4c3c10e30260cb6e7832af25f15527b089b258a1fef13b6eec3ce73bf06 + main :: IO () main = defaultMain [ or_baseline @@ -118,5 +132,7 @@ main = defaultMain [ , mul , mul128_baseline , mul128 + , div_baseline + , div ] diff --git a/bench/Weight.hs b/bench/Weight.hs @@ -16,10 +16,16 @@ i0 = 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed i1 = 0x7fffffffffffffffffffffffffffffffffffffffffffffbfffffffffffffffed w0, w1 :: W.Word256 -w0 = W.to_word256 - 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed -w1 = W.to_word256 - 0x7fffffffffffffffffffffffffffffffffffffffffffffbfffffffffffffffed +w0 = W.to_word256 i0 +w1 = W.to_word256 i1 + +i2, i3 :: Integer +i2 = 0x41cf50c7d0d65afabcf5ba37750dba71c7db29ec9f20a216d3ef013a59b9188a +i3 = 0x066bd4c3c10e30260cb6e7832af25f15527b089b258a1fef13b6eec3ce73bf06 + +w2, w3 :: W.Word256 +w2 = W.to_word256 i2 +w3 = W.to_word256 i3 main :: IO () main = W.mainWith $ do @@ -31,4 +37,6 @@ main = W.mainWith $ do W.func "mul" (W.mul_512 w0) w1 W.func "mul128 (baseline)" ((-) i0) i1 W.func "mul128" (W.mul w0) w1 + W.func "div (baseline)" (Prelude.div i2) i3 + W.func "div" (W.div w2) w3 diff --git a/ppad-fixed.cabal b/ppad-fixed.cabal @@ -41,6 +41,7 @@ test-suite fixed-tests base , bytestring , ppad-fixed + , primitive , tasty , tasty-hunit , tasty-quickcheck diff --git a/test/Main.hs b/test/Main.hs @@ -3,8 +3,10 @@ module Main where +import Control.Monad.ST import Data.Bits ((.|.), (.&.), (.>>.), (.^.)) import qualified Data.Bits as B +import qualified Data.Primitive.PrimArray as PA import Data.Word (Word64) import Data.Word.Extended import Prelude hiding (and, or, div) @@ -181,6 +183,20 @@ quotrem_2by1_case0 = do !o = quotrem_2by1 8 4 d (recip_2by1 d) H.assertEqual mempty (P 8 2052) o +quotrem_by1_case0 :: H.Assertion +quotrem_by1_case0 = do + let (q, r) = runST $ do + quo <- PA.newPrimArray 4 + let !u = PA.primArrayFromList [8, 4] + !d = B.complement 0xFF :: Word64 + re <- quotrem_by1 quo u d + qu <- PA.unsafeFreezePrimArray quo + pure (qu, re) + let pec_array = PA.primArrayFromList [4, 0, 0, 0] + pec_rem = 1032 + H.assertEqual mempty pec_rem r + H.assertEqual mempty pec_array q + -- main ----------------------------------------------------------------------- comparison :: TestTree @@ -249,6 +265,7 @@ main = defaultMain $ , H.testCase "recip_2by1 matches case0" recip_2by1_case0 , H.testCase "recip_2by1 matches case1" recip_2by1_case1 , H.testCase "quotrem_2by1 matches case0" quotrem_2by1_case0 + , H.testCase "quotrem_by1 matches case0" quotrem_by1_case0 ] ]