diff options
-rw-r--r-- | testsuite/tests/lib/integer/all.T | 9 | ||||
-rw-r--r-- | testsuite/tests/lib/integer/integerGcdExt.hs | 46 | ||||
-rw-r--r-- | testsuite/tests/lib/integer/integerGcdExt.stdout | 15 | ||||
-rw-r--r-- | testsuite/tests/lib/integer/integerGmpInternals.hs | 130 | ||||
-rw-r--r-- | testsuite/tests/lib/integer/integerGmpInternals.stdout | 43 | ||||
-rw-r--r-- | testsuite/tests/lib/integer/integerImportExport.hs | 118 | ||||
-rw-r--r-- | testsuite/tests/lib/integer/integerImportExport.stdout | 23 | ||||
-rw-r--r-- | testsuite/tests/lib/integer/integerPowMod.hs | 35 | ||||
-rw-r--r-- | testsuite/tests/lib/integer/integerPowMod.stdout | 3 |
9 files changed, 247 insertions, 175 deletions
diff --git a/testsuite/tests/lib/integer/all.T b/testsuite/tests/lib/integer/all.T index 97065a3341..33c5ea172d 100644 --- a/testsuite/tests/lib/integer/all.T +++ b/testsuite/tests/lib/integer/all.T @@ -1,9 +1,14 @@ test('integerBits', normal, compile_and_run, ['']) test('integerConversions', normal, compile_and_run, ['']) -# skip ghci as it doesn't support unboxed tuples -test('integerGmpInternals', [reqlib('integer-gmp'), omit_ways(['ghci'])], compile_and_run, ['']) test('plusMinusInteger', [omit_ways(['ghci'])], compile_and_run, ['']) test('integerConstantFolding', normal, makefile_test, ['integerConstantFolding']) test('fromToInteger', [], makefile_test, ['fromToInteger']) test('IntegerConversionRules', [], makefile_test, ['IntegerConversionRules']) test('gcdInteger', normal, compile_and_run, ['']) + +# skip ghci as it doesn't support unboxed tuples +test('integerImportExport', [reqlib('integer-gmp'), omit_ways(['ghci'])], compile_and_run, ['']) +test('integerGcdExt', [reqlib('integer-gmp'), omit_ways(['ghci'])], compile_and_run, ['']) + +test('integerPowMod', [reqlib('integer-gmp')], compile_and_run, ['']) +test('integerGmpInternals', [reqlib('integer-gmp')], compile_and_run, ['']) diff --git a/testsuite/tests/lib/integer/integerGcdExt.hs b/testsuite/tests/lib/integer/integerGcdExt.hs new file mode 100644 index 0000000000..d060c2d3b2 --- /dev/null +++ b/testsuite/tests/lib/integer/integerGcdExt.hs @@ -0,0 +1,46 @@ +{-# LANGUAGE UnboxedTuples #-} + +module Main (main) where + +import Data.List (group) +import Data.Bits +import Data.Word +import Control.Monad + +import GHC.Word +import GHC.Base +import qualified GHC.Integer.GMP.Internals as I + +gcdExtInteger :: Integer -> Integer -> (Integer, Integer) +gcdExtInteger a b = case I.gcdExtInteger a b of (# g, s #) -> (g, s) + +main :: IO () +main = do + print $ gcdExtInteger b e + print $ gcdExtInteger e b + print $ gcdExtInteger x y + print $ gcdExtInteger y x + print $ gcdExtInteger x (-y) + print $ gcdExtInteger (-x) y + print $ gcdExtInteger (-x) (-y) + + -- see #15350 + do + let a = 2 + b = 2^65 + 1 + print $ gcdExtInteger a b + print $ gcdExtInteger a (-b) + print $ gcdExtInteger (-a) b + print $ gcdExtInteger (-a) (-b) + print $ gcdExtInteger b a + print $ gcdExtInteger b (-a) + print $ gcdExtInteger (-b) a + print $ gcdExtInteger (-b) (-a) + + where + b = 2988348162058574136915891421498819466320163312926952423791023078876139 + e = 2351399303373464486466122544523690094744975233415544072992656881240319 + m = 10^(40::Int) + + x = 5328841272400314897981163497728751426 + y = 32052182750761975518649228050096851724 diff --git a/testsuite/tests/lib/integer/integerGcdExt.stdout b/testsuite/tests/lib/integer/integerGcdExt.stdout new file mode 100644 index 0000000000..199a0cb266 --- /dev/null +++ b/testsuite/tests/lib/integer/integerGcdExt.stdout @@ -0,0 +1,15 @@ +(1,-238164827888328100873319793437342927637138278785737103723156342382925) +(1,302679100340807588460107986194035692812415103244388831792688023418704) +(92889294,115110207004456909698806038261) +(92889294,-19137667681784054624628973533) +(92889294,115110207004456909698806038261) +(92889294,-115110207004456909698806038261) +(92889294,-115110207004456909698806038261) +(1,-18446744073709551616) +(1,-18446744073709551616) +(1,18446744073709551616) +(1,18446744073709551616) +(1,1) +(1,1) +(1,-1) +(1,-1) diff --git a/testsuite/tests/lib/integer/integerGmpInternals.hs b/testsuite/tests/lib/integer/integerGmpInternals.hs index 24780cc2ac..4d53560107 100644 --- a/testsuite/tests/lib/integer/integerGmpInternals.hs +++ b/testsuite/tests/lib/integer/integerGmpInternals.hs @@ -12,96 +12,15 @@ import GHC.Base import GHC.Integer.GMP.Internals (Integer(S#,Jp#,Jn#)) import qualified GHC.Integer.GMP.Internals as I --- NOTE: Some of the following operations were provided with --- integer-gmp-0.5.1, but were not ported to integer-gmp-1.0.0 (yet); --- so we use naive reference-implementations instead for the meantime --- in order to keep the reference-output untouched. - recipModInteger :: Integer -> Integer -> Integer recipModInteger = I.recipModInteger -gcdExtInteger :: Integer -> Integer -> (Integer, Integer) -gcdExtInteger a b = case I.gcdExtInteger a b of (# g, s #) -> (g, s) - -powModSecInteger :: Integer -> Integer -> Integer -> Integer -powModSecInteger = I.powModSecInteger - -powModInteger :: Integer -> Integer -> Integer -> Integer -powModInteger = I.powModInteger - -- FIXME: Lacks GMP2 version powInteger :: Integer -> Word -> Integer powInteger x e = x^e -exportInteger :: Integer -> MutableByteArray# RealWorld -> Word# -> Int# -> IO Word -exportInteger = I.exportIntegerToMutableByteArray - -exportIntegerAddr :: Integer -> Addr# -> Int# -> IO Word -exportIntegerAddr = I.exportIntegerToAddr - -importInteger :: ByteArray# -> Word# -> Word# -> Int# -> Integer -importInteger = I.importIntegerFromByteArray - -importIntegerAddr :: Addr# -> Word# -> Int# -> IO Integer -importIntegerAddr a l e = I.importIntegerFromAddr a l e - --- helpers -data MBA = MBA { unMBA :: !(MutableByteArray# RealWorld) } -data BA = BA { unBA :: !ByteArray# } - -newByteArray :: Word# -> IO MBA -newByteArray sz = IO $ \s -> case newPinnedByteArray# (word2Int# sz) s of (# s, arr #) -> (# s, MBA arr #) - -indexByteArray :: ByteArray# -> Word# -> Word8 -indexByteArray a# n# = W8# (indexWord8Array# a# (word2Int# n#)) - --- indexMutableByteArray :: MutableByteArray# RealWorld -> Word# -> IO Word8 --- indexMutableByteArray a# n# = IO $ \s -> case readWord8Array# a# (word2Int# n#) s of (# s', v #) -> (# s', W# v #) - -writeByteArray :: MutableByteArray# RealWorld -> Int# -> Word8 -> IO () -writeByteArray arr i (W8# w) = IO $ \s -> case writeWord8Array# arr i w s of s -> (# s, () #) - -lengthByteArray :: ByteArray# -> Word -lengthByteArray ba = W# (int2Word# (sizeofByteArray# ba)) - -unpackByteArray :: ByteArray# -> [Word8] -unpackByteArray ba | n == 0 = [] - | otherwise = [ indexByteArray ba i | W# i <- [0 .. n-1] ] - where - n = lengthByteArray ba - -freezeByteArray :: MutableByteArray# RealWorld -> IO BA -freezeByteArray arr = IO $ \s -> case unsafeFreezeByteArray# arr s of (# s, arr #) -> (# s, BA arr #) - ----------------------------------------------------------------------------- main :: IO () main = do - print $ powModInteger b e m - print $ powModInteger b e (m-1) - print $ powModSecInteger b e (m-1) - - putStrLn "\n# gcdExtInteger" - print $ gcdExtInteger b e - print $ gcdExtInteger e b - print $ gcdExtInteger x y - print $ gcdExtInteger y x - print $ gcdExtInteger x (-y) - print $ gcdExtInteger (-x) y - print $ gcdExtInteger (-x) (-y) - - -- see #15350 - do - let a = 2 - b = 2^65 + 1 - print $ gcdExtInteger a b - print $ gcdExtInteger a (-b) - print $ gcdExtInteger (-a) b - print $ gcdExtInteger (-a) (-b) - print $ gcdExtInteger b a - print $ gcdExtInteger b (-a) - print $ gcdExtInteger (-b) a - print $ gcdExtInteger (-b) (-a) - putStrLn "\n# powInteger" print $ powInteger 12345 0 print $ powInteger 12345 1 @@ -115,55 +34,6 @@ main = do print $ rle [ S# (I.testPrimeInteger k 25#) | k <- [ x .. x + 1000 ] ] print $ rle [ S# (I.testPrimeInteger k 25#) | k <- [ e .. e + 1000 ] ] - -- import/export primitives - print $ [ W# (I.sizeInBaseInteger x 2#) | x <- [b1024,b*e,b,e,m,x,y,-1,0,1] ] - print $ [ W# (I.sizeInBaseInteger x 256#) | x <- [b1024,b*e,b,e,m,x,y,-1,0,1] ] - - BA ba <- do - MBA mba <- newByteArray 128## - forM_ (zip [0..127] [0x01..]) $ \(I# i, w) -> do - writeByteArray mba i w - - let a = byteArrayContents# (unsafeCoerce# mba) - - print =<< importIntegerAddr a 0## 1# - print =<< importIntegerAddr a 0## 0# - - print =<< importIntegerAddr (plusAddr# a 22#) 1## 1# - print =<< importIntegerAddr (plusAddr# a 97#) 1## 0# - - print =<< importIntegerAddr a 23## 1# - print =<< importIntegerAddr a 23## 0# - - -- no-op - print =<< exportIntegerAddr 0 (plusAddr# a 0#) 1# - - -- write into array - print =<< exportIntegerAddr b (plusAddr# a 5#) 1# - print =<< exportIntegerAddr e (plusAddr# a 50#) 0# - - print =<< exportInteger m mba 85## 1# - print =<< exportInteger m mba 105## 0# - - print =<< importIntegerAddr (plusAddr# a 85#) 17## 1# - print =<< importIntegerAddr (plusAddr# a 105#) 17## 0# - - -- read back full array - print =<< importIntegerAddr a 128## 1# - print =<< importIntegerAddr a 128## 0# - - freezeByteArray mba - - print $ importInteger ba 0## 0## 1# - print $ importInteger ba 0## 0## 0# - - print $ importInteger ba 5## 29## 1# - print $ importInteger ba 50## 29## 0# - - print $ importInteger ba 0## 128## 1# - print $ importInteger ba 0## 128## 0# - - return () where b = 2988348162058574136915891421498819466320163312926952423791023078876139 e = 2351399303373464486466122544523690094744975233415544072992656881240319 diff --git a/testsuite/tests/lib/integer/integerGmpInternals.stdout b/testsuite/tests/lib/integer/integerGmpInternals.stdout index cff835bd27..4a3eac5055 100644 --- a/testsuite/tests/lib/integer/integerGmpInternals.stdout +++ b/testsuite/tests/lib/integer/integerGmpInternals.stdout @@ -1,23 +1,3 @@ -1527229998585248450016808958343740453059 -682382427572745901624116300491295556924 -682382427572745901624116300491295556924 - -# gcdExtInteger -(1,-238164827888328100873319793437342927637138278785737103723156342382925) -(1,302679100340807588460107986194035692812415103244388831792688023418704) -(92889294,115110207004456909698806038261) -(92889294,-19137667681784054624628973533) -(92889294,115110207004456909698806038261) -(92889294,-115110207004456909698806038261) -(92889294,-115110207004456909698806038261) -(1,-18446744073709551616) -(1,-18446744073709551616) -(1,18446744073709551616) -(1,18446744073709551616) -(1,1) -(1,1) -(1,-1) -(1,-1) # powInteger 1 @@ -31,26 +11,3 @@ [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199] [(25,0),(1,1),(261,0),(1,1),(107,0),(1,1),(49,0),(1,1),(27,0),(1,1),(137,0),(1,1),(49,0),(1,1),(53,0),(1,1),(29,0),(1,1),(39,0),(1,1),(89,0),(1,1),(37,0),(1,1),(21,0),(1,1),(65,0)] [(132,0),(1,1),(75,0),(1,1),(551,0),(1,1),(240,0)] -[1024,462,231,231,133,123,125,1,1,1] -[128,58,29,29,17,16,16,1,1,1] -0 -0 -23 -98 -96533667595335344311200144916688449305687896108635671 -2211224323355650230628428319497894791908413370238435841 -0 -29 -29 -17 -17 -10000000000000000000000000000000000000000 -10000000000000000000000000000000000000000 -707742318444110103305827088411305224215218021152567828572343353092273367732652472104598447612703966897013552405105205876531601836257828210094490315227838577315748169688646574531637174201439439064925789856330017827636213265611406915545853552494091915984057391978052737382104710796773315503272295152589111168 -90234380974657405463028074067522969606037220156164619283324346812591427336112251623694808896617626909786308083717091973493080700973825257532066851290431513505598597494043683887914929676998461181716680655771798101377425376141764430298850251302214681051036129537825969557396995822791626849336007688291942072833 -0 -0 -2988348162058574136915891421498819466320163312926952423791023078876139 -2351399303373464486466122544523690094744975233415544072992656881240319 -707742318444110103305827088411305224215218021152567828572343353092273367732652472104598447612703966897013552405105205876531601836257828210094490315227838577315748169688646574531637174201439439064925789856330017827636213265611406915545853552494091915984057391978052737382104710796773315503272295152589111168 -90234380974657405463028074067522969606037220156164619283324346812591427336112251623694808896617626909786308083717091973493080700973825257532066851290431513505598597494043683887914929676998461181716680655771798101377425376141764430298850251302214681051036129537825969557396995822791626849336007688291942072833 diff --git a/testsuite/tests/lib/integer/integerImportExport.hs b/testsuite/tests/lib/integer/integerImportExport.hs new file mode 100644 index 0000000000..bcd0531680 --- /dev/null +++ b/testsuite/tests/lib/integer/integerImportExport.hs @@ -0,0 +1,118 @@ +{-# LANGUAGE BangPatterns, CPP, MagicHash, UnboxedTuples #-} + +module Main (main) where + +import Data.List (group) +import Data.Bits +import Data.Word +import Control.Monad + +import GHC.Word +import GHC.Base +import GHC.Integer.GMP.Internals (Integer(S#,Jp#,Jn#)) +import qualified GHC.Integer.GMP.Internals as I + +exportInteger :: Integer -> MutableByteArray# RealWorld -> Word# -> Int# -> IO Word +exportInteger = I.exportIntegerToMutableByteArray + +exportIntegerAddr :: Integer -> Addr# -> Int# -> IO Word +exportIntegerAddr = I.exportIntegerToAddr + +importInteger :: ByteArray# -> Word# -> Word# -> Int# -> Integer +importInteger = I.importIntegerFromByteArray + +importIntegerAddr :: Addr# -> Word# -> Int# -> IO Integer +importIntegerAddr a l e = I.importIntegerFromAddr a l e + +-- helpers +data MBA = MBA { unMBA :: !(MutableByteArray# RealWorld) } +data BA = BA { unBA :: !ByteArray# } + +newByteArray :: Word# -> IO MBA +newByteArray sz = IO $ \s -> case newPinnedByteArray# (word2Int# sz) s of (# s, arr #) -> (# s, MBA arr #) + +indexByteArray :: ByteArray# -> Word# -> Word8 +indexByteArray a# n# = W8# (indexWord8Array# a# (word2Int# n#)) + +-- indexMutableByteArray :: MutableByteArray# RealWorld -> Word# -> IO Word8 +-- indexMutableByteArray a# n# = IO $ \s -> case readWord8Array# a# (word2Int# n#) s of (# s', v #) -> (# s', W# v #) + +writeByteArray :: MutableByteArray# RealWorld -> Int# -> Word8 -> IO () +writeByteArray arr i (W8# w) = IO $ \s -> case writeWord8Array# arr i w s of s -> (# s, () #) + +lengthByteArray :: ByteArray# -> Word +lengthByteArray ba = W# (int2Word# (sizeofByteArray# ba)) + +unpackByteArray :: ByteArray# -> [Word8] +unpackByteArray ba | n == 0 = [] + | otherwise = [ indexByteArray ba i | W# i <- [0 .. n-1] ] + where + n = lengthByteArray ba + +freezeByteArray :: MutableByteArray# RealWorld -> IO BA +freezeByteArray arr = IO $ \s -> case unsafeFreezeByteArray# arr s of (# s, arr #) -> (# s, BA arr #) + +---------------------------------------------------------------------------- +main :: IO () +main = do + -- import/export primitives + print $ [ W# (I.sizeInBaseInteger x 2#) | x <- [b1024,b*e,b,e,m,x,y,-1,0,1] ] + print $ [ W# (I.sizeInBaseInteger x 256#) | x <- [b1024,b*e,b,e,m,x,y,-1,0,1] ] + + BA ba <- do + MBA mba <- newByteArray 128## + forM_ (zip [0..127] [0x01..]) $ \(I# i, w) -> do + writeByteArray mba i w + + let a = byteArrayContents# (unsafeCoerce# mba) + + print =<< importIntegerAddr a 0## 1# + print =<< importIntegerAddr a 0## 0# + + print =<< importIntegerAddr (plusAddr# a 22#) 1## 1# + print =<< importIntegerAddr (plusAddr# a 97#) 1## 0# + + print =<< importIntegerAddr a 23## 1# + print =<< importIntegerAddr a 23## 0# + + -- no-op + print =<< exportIntegerAddr 0 (plusAddr# a 0#) 1# + + -- write into array + print =<< exportIntegerAddr b (plusAddr# a 5#) 1# + print =<< exportIntegerAddr e (plusAddr# a 50#) 0# + + print =<< exportInteger m mba 85## 1# + print =<< exportInteger m mba 105## 0# + + print =<< importIntegerAddr (plusAddr# a 85#) 17## 1# + print =<< importIntegerAddr (plusAddr# a 105#) 17## 0# + + -- read back full array + print =<< importIntegerAddr a 128## 1# + print =<< importIntegerAddr a 128## 0# + + freezeByteArray mba + + print $ importInteger ba 0## 0## 1# + print $ importInteger ba 0## 0## 0# + + print $ importInteger ba 5## 29## 1# + print $ importInteger ba 50## 29## 0# + + print $ importInteger ba 0## 128## 1# + print $ importInteger ba 0## 128## 0# + + return () + where + b = 2988348162058574136915891421498819466320163312926952423791023078876139 + e = 2351399303373464486466122544523690094744975233415544072992656881240319 + m = 10^(40::Int) + + x = 5328841272400314897981163497728751426 + y = 32052182750761975518649228050096851724 + + b1024 = roll (map fromIntegral (take 128 [0x80::Int .. ])) + + roll :: [Word8] -> Integer + roll = GHC.Base.foldr (\b a -> a `shiftL` 8 .|. fromIntegral b) 0 diff --git a/testsuite/tests/lib/integer/integerImportExport.stdout b/testsuite/tests/lib/integer/integerImportExport.stdout new file mode 100644 index 0000000000..950d72c90b --- /dev/null +++ b/testsuite/tests/lib/integer/integerImportExport.stdout @@ -0,0 +1,23 @@ +[1024,462,231,231,133,123,125,1,1,1] +[128,58,29,29,17,16,16,1,1,1] +0 +0 +23 +98 +96533667595335344311200144916688449305687896108635671 +2211224323355650230628428319497894791908413370238435841 +0 +29 +29 +17 +17 +10000000000000000000000000000000000000000 +10000000000000000000000000000000000000000 +707742318444110103305827088411305224215218021152567828572343353092273367732652472104598447612703966897013552405105205876531601836257828210094490315227838577315748169688646574531637174201439439064925789856330017827636213265611406915545853552494091915984057391978052737382104710796773315503272295152589111168 +90234380974657405463028074067522969606037220156164619283324346812591427336112251623694808896617626909786308083717091973493080700973825257532066851290431513505598597494043683887914929676998461181716680655771798101377425376141764430298850251302214681051036129537825969557396995822791626849336007688291942072833 +0 +0 +2988348162058574136915891421498819466320163312926952423791023078876139 +2351399303373464486466122544523690094744975233415544072992656881240319 +707742318444110103305827088411305224215218021152567828572343353092273367732652472104598447612703966897013552405105205876531601836257828210094490315227838577315748169688646574531637174201439439064925789856330017827636213265611406915545853552494091915984057391978052737382104710796773315503272295152589111168 +90234380974657405463028074067522969606037220156164619283324346812591427336112251623694808896617626909786308083717091973493080700973825257532066851290431513505598597494043683887914929676998461181716680655771798101377425376141764430298850251302214681051036129537825969557396995822791626849336007688291942072833 diff --git a/testsuite/tests/lib/integer/integerPowMod.hs b/testsuite/tests/lib/integer/integerPowMod.hs new file mode 100644 index 0000000000..97cdad65b2 --- /dev/null +++ b/testsuite/tests/lib/integer/integerPowMod.hs @@ -0,0 +1,35 @@ +module Main (main) where + +import Data.List (group) +import Data.Bits +import Data.Word +import Control.Monad + +import GHC.Word +import GHC.Base +import qualified GHC.Integer.GMP.Internals as I + +powModSecInteger :: Integer -> Integer -> Integer -> Integer +powModSecInteger = I.powModSecInteger + +powModInteger :: Integer -> Integer -> Integer -> Integer +powModInteger = I.powModInteger + +main :: IO () +main = do + print $ powModInteger b e m + print $ powModInteger b e (m-1) + print $ powModSecInteger b e (m-1) + + where + b = 2988348162058574136915891421498819466320163312926952423791023078876139 + e = 2351399303373464486466122544523690094744975233415544072992656881240319 + m = 10^(40::Int) + + x = 5328841272400314897981163497728751426 + y = 32052182750761975518649228050096851724 + + b1024 = roll (map fromIntegral (take 128 [0x80::Int .. ])) + + roll :: [Word8] -> Integer + roll = GHC.Base.foldr (\b a -> a `shiftL` 8 .|. fromIntegral b) 0 diff --git a/testsuite/tests/lib/integer/integerPowMod.stdout b/testsuite/tests/lib/integer/integerPowMod.stdout new file mode 100644 index 0000000000..bf3dc2e28b --- /dev/null +++ b/testsuite/tests/lib/integer/integerPowMod.stdout @@ -0,0 +1,3 @@ +1527229998585248450016808958343740453059 +682382427572745901624116300491295556924 +682382427572745901624116300491295556924 |