summaryrefslogtreecommitdiff
path: root/libraries/integer-gmp2/src
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/integer-gmp2/src')
-rw-r--r--libraries/integer-gmp2/src/GHC/Integer/GMP/Internals.hs27
-rw-r--r--libraries/integer-gmp2/src/GHC/Integer/Type.hs17
2 files changed, 44 insertions, 0 deletions
diff --git a/libraries/integer-gmp2/src/GHC/Integer/GMP/Internals.hs b/libraries/integer-gmp2/src/GHC/Integer/GMP/Internals.hs
index 480866bec1..244dac7990 100644
--- a/libraries/integer-gmp2/src/GHC/Integer/GMP/Internals.hs
+++ b/libraries/integer-gmp2/src/GHC/Integer/GMP/Internals.hs
@@ -125,6 +125,10 @@ module GHC.Integer.GMP.Internals
, testPrimeBigNat
, testPrimeWord#
+ , nextPrimeInteger
+ , nextPrimeBigNat
+ , nextPrimeWord#
+
-- * Import/export functions
-- ** Compute size of serialisation
, sizeInBaseBigNat
@@ -323,3 +327,26 @@ foreign import ccall unsafe "integer_gmp_test_prime"
-- /Since: 1.0.0.0/
foreign import ccall unsafe "integer_gmp_test_prime1"
testPrimeWord# :: GmpLimb# -> Int# -> Int#
+
+
+-- | Compute next prime greater than @/n/@ probalistically.
+--
+-- According to the GMP documentation, the underlying function
+-- @mpz_nextprime()@ \"uses a probabilistic algorithm to identify
+-- primes. For practical purposes it's adequate, the chance of a
+-- composite passing will be extremely small.\"
+--
+-- /Since: 0.5.1.0/
+{-# NOINLINE nextPrimeInteger #-}
+nextPrimeInteger :: Integer -> Integer
+nextPrimeInteger (S# i#)
+ | isTrue# (i# ># 1#) = wordToInteger (nextPrimeWord# (int2Word# i#))
+ | True = S# 2#
+nextPrimeInteger (Jp# bn) = Jp# (nextPrimeBigNat bn)
+nextPrimeInteger (Jn# _) = S# 2#
+
+-- | Version of 'nextPrimeInteger' operating on 'Word#'s
+--
+-- /Since: 1.0.0.0/
+foreign import ccall unsafe "integer_gmp_next_prime1"
+ nextPrimeWord# :: GmpLimb# -> GmpLimb#
diff --git a/libraries/integer-gmp2/src/GHC/Integer/Type.hs b/libraries/integer-gmp2/src/GHC/Integer/Type.hs
index 48c5ed85bc..d771de1cda 100644
--- a/libraries/integer-gmp2/src/GHC/Integer/Type.hs
+++ b/libraries/integer-gmp2/src/GHC/Integer/Type.hs
@@ -1684,6 +1684,23 @@ isValidBigNat# (BN# ba#)
(# szq#, szr# #) = quotRemInt# sz# GMP_LIMB_BYTES#
+-- | Version of 'nextPrimeInteger' operating on 'BigNat's
+--
+-- /Since: 1.0.0.0/
+nextPrimeBigNat :: BigNat -> BigNat
+nextPrimeBigNat bn@(BN# ba#) = runS $ do
+ mbn@(MBN# mba#) <- newBigNat# n#
+ (W# c#) <- liftIO (nextPrime# mba# ba# n#)
+ case c# of
+ 0## -> unsafeFreezeBigNat# mbn
+ _ -> unsafeSnocFreezeBigNat# mbn c#
+ where
+ n# = sizeofBigNat# bn
+
+foreign import ccall unsafe "integer_gmp_next_prime"
+ nextPrime# :: MutableByteArray# RealWorld -> ByteArray# -> GmpSize#
+ -> IO GmpLimb
+
----------------------------------------------------------------------------
-- monadic combinators for low-level state threading