summaryrefslogtreecommitdiff
path: root/testsuite
diff options
context:
space:
mode:
authorSylvain Henry <sylvain@haskus.fr>2020-09-30 17:43:10 +0200
committerMarge Bot <ben+marge-bot@smart-cactus.org>2020-10-02 13:52:38 -0400
commit12c06927a03a2fdb516f7008c57d68568b02b576 (patch)
treec01860d5708f449e96722e4fa45d5dd992e03d28 /testsuite
parent3c9beab75aaa5fbbb11132c99e2af114f322152f (diff)
downloadhaskell-12c06927a03a2fdb516f7008c57d68568b02b576.tar.gz
Bignum: implement integerRecipMod (#18427)
Diffstat (limited to 'testsuite')
-rw-r--r--testsuite/tests/lib/integer/all.T3
-rw-r--r--testsuite/tests/lib/integer/integerGmpInternals.hs4
-rw-r--r--testsuite/tests/lib/integer/integerGmpInternals.stdout1
-rw-r--r--testsuite/tests/lib/integer/integerRecipMod.hs39
-rw-r--r--testsuite/tests/lib/integer/integerRecipMod.stdout5
5 files changed, 46 insertions, 6 deletions
diff --git a/testsuite/tests/lib/integer/all.T b/testsuite/tests/lib/integer/all.T
index 0c997f7ebf..43bcb0f3e4 100644
--- a/testsuite/tests/lib/integer/all.T
+++ b/testsuite/tests/lib/integer/all.T
@@ -8,7 +8,8 @@ test('IntegerConversionRules', [], makefile_test, ['IntegerConversionRules'])
test('gcdInteger', normal, compile_and_run, [''])
test('gcdeInteger', normal, compile_and_run, [''])
test('integerPowMod', [], compile_and_run, [''])
-test('integerGcdExt', [omit_ways(['ghci'])], compile_and_run, [''])
+test('integerGcdExt', [], compile_and_run, [''])
+test('integerRecipMod', [], compile_and_run, [''])
# skip ghci as it doesn't support unboxed tuples
test('integerImportExport', [omit_ways(['ghci'])], compile_and_run, [''])
diff --git a/testsuite/tests/lib/integer/integerGmpInternals.hs b/testsuite/tests/lib/integer/integerGmpInternals.hs
index 53a776e13d..982e4dcaba 100644
--- a/testsuite/tests/lib/integer/integerGmpInternals.hs
+++ b/testsuite/tests/lib/integer/integerGmpInternals.hs
@@ -12,9 +12,6 @@ import GHC.Base
import GHC.Num.Integer
import qualified GHC.Num.Integer as I
-recipModInteger :: Integer -> Integer -> Integer
-recipModInteger = I.recipModInteger
-
-- FIXME: Lacks GMP2 version
powInteger :: Integer -> Word -> Integer
powInteger x e = x^e
@@ -25,7 +22,6 @@ main = do
print $ powInteger 12345 0
print $ powInteger 12345 1
print $ powInteger 12345 30
- print $ [ (x,i) | x <- [-7..71], let i = recipModInteger x (2*3*11*11*17*17), i /= 0 ]
putStrLn "\n# nextPrimeInteger"
print $ I.nextPrimeInteger b
diff --git a/testsuite/tests/lib/integer/integerGmpInternals.stdout b/testsuite/tests/lib/integer/integerGmpInternals.stdout
index 4a3eac5055..1ffeabb78b 100644
--- a/testsuite/tests/lib/integer/integerGmpInternals.stdout
+++ b/testsuite/tests/lib/integer/integerGmpInternals.stdout
@@ -3,7 +3,6 @@
1
12345
555562377826831043419246079513769804614412256811161773362797946971665712715296306339052301636736176350153982639312744140625
-[(-7,149867),(-5,167851),(-1,209813),(1,1),(5,41963),(7,59947),(13,177535),(19,143557),(23,182447),(25,134281),(29,7235),(31,33841),(35,95915),(37,113413),(41,61409),(43,24397),(47,174101),(49,158431),(53,193979),(59,188477),(61,185737),(65,35507),(67,118999),(71,186173)]
# nextPrimeInteger
2988348162058574136915891421498819466320163312926952423791023078876343
diff --git a/testsuite/tests/lib/integer/integerRecipMod.hs b/testsuite/tests/lib/integer/integerRecipMod.hs
new file mode 100644
index 0000000000..e5de646790
--- /dev/null
+++ b/testsuite/tests/lib/integer/integerRecipMod.hs
@@ -0,0 +1,39 @@
+{-# LANGUAGE BangPatterns, CPP, MagicHash, UnboxedTuples, TupleSections #-}
+
+module Main (main) where
+
+import Data.List (group)
+import Data.Bits
+import Data.Word
+import Data.Maybe
+import Control.Monad
+
+import GHC.Word
+import GHC.Base
+import GHC.Num.Integer
+import qualified GHC.Num.Integer as I
+
+recipModInteger :: Integer -> Integer -> Maybe Integer
+recipModInteger x m = case I.integerRecipMod# x m of
+ (# y | #) -> Just y
+ (# | () #) -> Nothing
+
+main :: IO ()
+main = do
+ let
+ f x = case recipModInteger x (2*3*11*11*17*17) of
+ y -> fmap (x,) y
+
+ g x = case recipModInteger x (-2*3*11*11*17*17) of
+ y -> fmap (x,) y
+
+ -- positive modulo
+ print $ mapMaybe f [-7..71]
+
+ -- negative modulo
+ print $ mapMaybe g [-7..71]
+
+ -- modulo == 1, -1 or 0
+ print (recipModInteger 77 1)
+ print (recipModInteger 77 (-1))
+ print (recipModInteger 77 0)
diff --git a/testsuite/tests/lib/integer/integerRecipMod.stdout b/testsuite/tests/lib/integer/integerRecipMod.stdout
new file mode 100644
index 0000000000..205af95fb9
--- /dev/null
+++ b/testsuite/tests/lib/integer/integerRecipMod.stdout
@@ -0,0 +1,5 @@
+[(-7,149867),(-5,167851),(-1,209813),(1,1),(5,41963),(7,59947),(13,177535),(19,143557),(23,182447),(25,134281),(29,7235),(31,33841),(35,95915),(37,113413),(41,61409),(43,24397),(47,174101),(49,158431),(53,193979),(59,188477),(61,185737),(65,35507),(67,118999),(71,186173)]
+[(-7,149867),(-5,167851),(-1,209813),(1,1),(5,41963),(7,59947),(13,177535),(19,143557),(23,182447),(25,134281),(29,7235),(31,33841),(35,95915),(37,113413),(41,61409),(43,24397),(47,174101),(49,158431),(53,193979),(59,188477),(61,185737),(65,35507),(67,118999),(71,186173)]
+Nothing
+Nothing
+Nothing