{-# LINE 1 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
module Data.Number.Flint.QSieve.FFI (
Qs (..)
, CQs()
, newQs
, withQs
, qsieve_knuth_schroeppel
, qsieve_primes_init
, qsieve_primes_increment
, qsieve_init_A
, qsieve_next_A
, qsieve_init_poly_first
, qsieve_init_poly_next
, qsieve_compute_C
, qsieve_do_sieving
, qsieve_do_sieving2
, qsieve_evaluate_candidate
, qsieve_evaluate_sieve
, qsieve_collect_relations
, qsieve_write_to_file
, qsieve_get_table_entry
, qsieve_add_to_hashtable
, qsieve_parse_relation
, qsieve_merge_relation
, qsieve_compare_relation
, qsieve_remove_duplicates
, qsieve_process_relation
, qsieve_factor
) where
import Control.Monad
import Foreign.C.String
import Foreign.C.Types
import Foreign.ForeignPtr
import Foreign.Ptr ( Ptr, FunPtr, plusPtr )
import Foreign.Storable
import Foreign.Marshal ( free )
import Data.Number.Flint.Flint
import Data.Number.Flint.Fmpz
import Data.Number.Flint.Fmpz.Factor
data Qs = Qs {-# UNPACK #-} !(ForeignPtr CQs)
type CQs = CFlint Qs
instance Storable CQs where
{-# INLINE sizeOf #-}
sizeOf :: CQs -> Int
sizeOf CQs
_ = (Int
552)
{-# LINE 67 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
{-# INLINE alignment #-}
alignment :: CQs -> Int
alignment CQs
_ = Int
8
{-# LINE 69 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
peek = undefined
poke :: Ptr CQs -> CQs -> IO ()
poke = forall a. HasCallStack => a
undefined
newQs :: Fmpz -> IO Qs
newQs Fmpz
n = do
ForeignPtr CQs
x <- forall a. Storable a => IO (ForeignPtr a)
mallocForeignPtr
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CQs
x forall a b. (a -> b) -> a -> b
$ \Ptr CQs
x ->
forall {a}. Fmpz -> (Ptr CFmpz -> IO a) -> IO (Fmpz, a)
withFmpz Fmpz
n forall a b. (a -> b) -> a -> b
$ \Ptr CFmpz
n -> do
Ptr CQs -> Ptr CFmpz -> IO ()
qsieve_init Ptr CQs
x Ptr CFmpz
n
forall a. FinalizerPtr a -> ForeignPtr a -> IO ()
addForeignPtrFinalizer FunPtr (Ptr CQs -> IO ())
p_qsieve_clear ForeignPtr CQs
x
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ ForeignPtr CQs -> Qs
Qs ForeignPtr CQs
x
{-# INLINE withQs #-}
withQs :: Qs -> (Ptr CQs -> IO a) -> IO (Qs, a)
withQs (Qs ForeignPtr CQs
x) Ptr CQs -> IO a
f = do
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CQs
x forall a b. (a -> b) -> a -> b
$ \Ptr CQs
px -> Ptr CQs -> IO a
f Ptr CQs
px forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ForeignPtr CQs -> Qs
Qs ForeignPtr CQs
x,)
data Hash = Hash {-# UNPACK #-} !(ForeignPtr CHash)
data CHash = CHash CMpLimb CMpLimb CMpLimb
instance Storable CHash where
{-# INLINE sizeOf #-}
sizeOf :: CHash -> Int
sizeOf CHash
_ = (Int
24)
{-# LINE 92 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
{-# INLINE alignment #-}
alignment :: CHash -> Int
alignment CHash
_ = Int
8
{-# LINE 94 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
peek ptr = CHash
<$> (\hsc_ptr -> peekByteOff hsc_ptr 0) ptr
{-# LINE 96 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
<*> (\hsc_ptr -> peekByteOff hsc_ptr 8) ptr
{-# LINE 97 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
<*> (\hsc_ptr -> peekByteOff hsc_ptr 16) ptr
{-# LINE 98 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
poke ptr (CHash prime next count) = do
(\hsc_ptr -> pokeByteOff hsc_ptr 0) ptr prime
{-# LINE 100 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
(\hsc_ptr -> pokeByteOff hsc_ptr 8) ptr next
{-# LINE 101 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
(\hsc_ptr -> pokeByteOff hsc_ptr 16) ptr count
{-# LINE 102 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
data Relation = Relation {-# UNPACK #-} !(ForeignPtr CRelation)
data CRelation = CRelation CMpLimb CLong CLong (Ptr CLong) (Ptr CFac) (Ptr CFmpz)
instance Storable CRelation where
{-# INLINE sizeOf #-}
sizeOf :: CRelation -> Int
sizeOf CRelation
_ = (Int
48)
{-# LINE 111 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
{-# INLINE alignment #-}
alignment :: CRelation -> Int
alignment CRelation
_ = Int
8
{-# LINE 113 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
peek ptr = CRelation
<$> (\hsc_ptr -> peekByteOff hsc_ptr 0) ptr
{-# LINE 115 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
<*> (\hsc_ptr -> peekByteOff hsc_ptr 8) ptr
{-# LINE 116 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
<*> (\hsc_ptr -> peekByteOff hsc_ptr 16) ptr
{-# LINE 117 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
<*> (\hsc_ptr -> peekByteOff hsc_ptr 24) ptr
{-# LINE 118 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
<*> (\hsc_ptr -> peekByteOff hsc_ptr 32) ptr
{-# LINE 119 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
<*> (\hsc_ptr -> peekByteOff hsc_ptr 40) ptr
{-# LINE 120 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
poke ptr (CRelation lp num_factors small_primes small factor y) = do
(\hsc_ptr -> pokeByteOff hsc_ptr 0) ptr lp
{-# LINE 122 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
(\hsc_ptr -> pokeByteOff hsc_ptr 8) ptr num_factors
{-# LINE 123 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
(\hsc_ptr -> pokeByteOff hsc_ptr 16) ptr small_primes
{-# LINE 124 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
(\hsc_ptr -> pokeByteOff hsc_ptr 24) ptr small
{-# LINE 125 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
(\hsc_ptr -> pokeByteOff hsc_ptr 32) ptr factor
{-# LINE 126 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
(\hsc_ptr -> pokeByteOff hsc_ptr 40) ptr y
{-# LINE 127 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
data Fac = Fac {-# UNPACK #-} !(ForeignPtr CFac)
data CFac = CFac CLong CLong
instance Storable CFac where
{-# INLINE sizeOf #-}
sizeOf :: CFac -> Int
sizeOf CFac
_ = (Int
16)
{-# LINE 136 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
{-# INLINE alignment #-}
alignment :: CFac -> Int
alignment CFac
_ = Int
8
{-# LINE 138 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
peek ptr = CFac
<$> (\hsc_ptr -> peekByteOff hsc_ptr 0) ptr
{-# LINE 140 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
<*> (\hsc_ptr -> peekByteOff hsc_ptr 8) ptr
{-# LINE 141 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
poke ptr (CFac ind exp) = do
(\hsc_ptr -> pokeByteOff hsc_ptr 0) ptr ind
{-# LINE 143 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
(\hsc_ptr -> pokeByteOff hsc_ptr 0) ptr exp
{-# LINE 144 "src/Data/Number/Flint/QSieve/FFI.hsc" #-}
foreign import ccall "flint/qsieve.h qsieve_init"
qsieve_init :: Ptr CQs -> Ptr CFmpz -> IO ()
foreign import ccall "flint/qsieve.h qsieve_clear"
qsieve_clear :: Ptr CQs -> IO ()
foreign import ccall "flint/qsieve.h &qsieve_clear"
p_qsieve_clear :: FunPtr (Ptr CQs -> IO ())
foreign import ccall "qsieve.h qsieve_knuth_schroeppel"
qsieve_knuth_schroeppel :: Ptr CQs -> IO CMpLimb
foreign import ccall "qsieve.h qsieve_primes_init"
qsieve_primes_init :: Ptr CQs -> IO CMpLimb
foreign import ccall "qsieve.h qsieve_primes_increment"
qsieve_primes_increment :: Ptr CQs -> CMpLimb -> IO CMpLimb
foreign import ccall "qsieve.h qsieve_init_A"
qsieve_init_A :: Ptr CQs -> IO ()
foreign import ccall "qsieve.h qsieve_next_A"
qsieve_next_A :: Ptr CQs -> IO ()
foreign import ccall "qsieve.h qsieve_init_poly_first"
qsieve_init_poly_first :: Ptr CQs -> IO ()
foreign import ccall "qsieve.h qsieve_init_poly_next"
qsieve_init_poly_next :: Ptr CQs -> IO ()
foreign import ccall "qsieve.h qsieve_compute_C"
qsieve_compute_C :: Ptr CQs -> IO ()
foreign import ccall "qsieve.h qsieve_do_sieving"
qsieve_do_sieving :: Ptr CQs -> CString -> IO ()
foreign import ccall "qsieve.h qsieve_do_sieving2"
qsieve_do_sieving2 :: Ptr CQs -> IO ()
foreign import ccall "qsieve.h qsieve_evaluate_candidate"
qsieve_evaluate_candidate :: Ptr CQs -> CLong -> CString -> IO CLong
foreign import ccall "qsieve.h qsieve_evaluate_sieve"
qsieve_evaluate_sieve :: Ptr CQs -> CString -> IO CLong
foreign import ccall "qsieve.h qsieve_collect_relations"
qsieve_collect_relations :: Ptr CQs -> CString -> IO CLong
foreign import ccall "qsieve.h qsieve_write_to_file"
qsieve_write_to_file :: Ptr CQs -> CMpLimb -> Ptr CFmpz -> IO ()
foreign import ccall "qsieve.h qsieve_get_table_entry"
qsieve_get_table_entry :: Ptr CQs -> CMpLimb -> IO (Ptr (Ptr CHash))
foreign import ccall "qsieve.h qsieve_add_to_hashtable"
qsieve_add_to_hashtable :: Ptr CQs -> CMpLimb -> IO ()
foreign import ccall "qsieve.h qsieve_parse_relation"
qsieve_parse_relation :: Ptr CQs -> CString -> IO (Ptr CRelation)
foreign import ccall "qsieve.h qsieve_merge_relation"
qsieve_merge_relation :: Ptr CQs -> Ptr CRelation -> Ptr CRelation -> IO (Ptr CRelation)
foreign import ccall "qsieve.h qsieve_compare_relation"
qsieve_compare_relation :: Ptr () -> Ptr () -> IO CInt
foreign import ccall "qsieve.h qsieve_remove_duplicates"
qsieve_remove_duplicates :: Ptr (Ptr CRelation) -> CLong -> IO CInt
foreign import ccall "qsieve.h qsieve_process_relation"
qsieve_process_relation :: Ptr CQs -> IO ()
foreign import ccall "qsieve.h qsieve_factor"
qsieve_factor :: Ptr CFmpzFactor -> Ptr CFmpz -> IO ()