{-# OPTIONS_GHC -optc-DACF_INLINES_C #-}
{-# LINE 1 "src/Data/Number/Flint/Acb/Acf/FFI.hsc" #-}
{-|
module      :  Data.Number.Flint.Acb.Acf.FFI
copyright   :  (c) 2022 Hartmut Monien
license     :  GNU GPL, version 2 or above (see LICENSE)
maintainer  :  hmonien@uni-bonn.de
-}
module Data.Number.Flint.Acb.Acf.FFI (
  -- * Complex floating-point numbers
    Acf (..)
  , CAcf (..)
  , newAcf
  , withAcf
  , withNewAcf
  -- * Memory management
  , acf_init
  , acf_clear
  , acf_swap
  , acf_allocated_bytes
  -- * Basic manipulation
  , acf_real_ptr
  , acf_imag_ptr
  , acf_set
  , acf_equal
  -- * Arithmetic
  , acf_add
  , acf_sub
  , acf_mul
  -- * Approximate arithmetic
  , acf_approx_inv
  , acf_approx_div
  , acf_approx_sqrt
  , acf_approx_dot
) where

-- Complex floating-point numbers ----------------------------------------------

import Foreign.Ptr
import Foreign.ForeignPtr
import Foreign.Storable
import Foreign.Marshal.Alloc
import Foreign.C.Types
import Foreign.C.String

import Data.Number.Flint.Flint
import Data.Number.Flint.Fmpz
import Data.Number.Flint.Fmpq

import Data.Number.Flint.Arb.Types
import Data.Number.Flint.Acb.Types




-- acf_t -----------------------------------------------------------------------

-- | Createst a new `CAcf` structure encapsulated in `Acf`.
newAcf :: IO Acf
newAcf = do
  ForeignPtr CAcf
p <- IO (ForeignPtr CAcf)
forall a. Storable a => IO (ForeignPtr a)
mallocForeignPtr
  ForeignPtr CAcf -> (Ptr CAcf -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CAcf
p Ptr CAcf -> IO ()
acf_init
  FinalizerPtr CAcf -> ForeignPtr CAcf -> IO ()
forall a. FinalizerPtr a -> ForeignPtr a -> IO ()
addForeignPtrFinalizer FinalizerPtr CAcf
p_acf_clear ForeignPtr CAcf
p
  Acf -> IO Acf
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Acf -> IO Acf) -> Acf -> IO Acf
forall a b. (a -> b) -> a -> b
$ ForeignPtr CAcf -> Acf
Acf ForeignPtr CAcf
p
  
-- | Access to the C pointer in `Acf` structure.
{-# INLINE withAcf #-}
withAcf :: Acf -> (Ptr CAcf -> IO a) -> IO (Acf, a)
withAcf (Acf ForeignPtr CAcf
p) Ptr CAcf -> IO a
f = ForeignPtr CAcf -> (Ptr CAcf -> IO (Acf, a)) -> IO (Acf, a)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CAcf
p ((Ptr CAcf -> IO (Acf, a)) -> IO (Acf, a))
-> (Ptr CAcf -> IO (Acf, a)) -> IO (Acf, a)
forall a b. (a -> b) -> a -> b
$ (a -> (Acf, a)) -> IO a -> IO (Acf, a)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ForeignPtr CAcf -> Acf
Acf ForeignPtr CAcf
p,) (IO a -> IO (Acf, a))
-> (Ptr CAcf -> IO a) -> Ptr CAcf -> IO (Acf, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr CAcf -> IO a
f

withNewAcf :: (Ptr CAcf -> IO a) -> IO (Acf, a)
withNewAcf Ptr CAcf -> IO a
f = do
  Acf
x <- IO Acf
newAcf
  Acf -> (Ptr CAcf -> IO a) -> IO (Acf, a)
forall {a}. Acf -> (Ptr CAcf -> IO a) -> IO (Acf, a)
withAcf Acf
x ((Ptr CAcf -> IO a) -> IO (Acf, a))
-> (Ptr CAcf -> IO a) -> IO (Acf, a)
forall a b. (a -> b) -> a -> b
$ \Ptr CAcf
x -> Ptr CAcf -> IO a
f Ptr CAcf
x
  
instance Storable CAcf where
  {-# INLINE sizeOf #-}
  sizeOf :: CAcf -> Int
sizeOf CAcf
_ = (Int
64)
{-# LINE 74 "src/Data/Number/Flint/Acb/Acf/FFI.hsc" #-}
  {-# INLINE alignment #-}
  alignment :: CAcf -> Int
alignment CAcf
_ = Int
8
{-# LINE 76 "src/Data/Number/Flint/Acb/Acf/FFI.hsc" #-}
  peek = error "CAcf.peek: Not defined"
  poke :: Ptr CAcf -> CAcf -> IO ()
poke = [Char] -> Ptr CAcf -> CAcf -> IO ()
forall a. HasCallStack => [Char] -> a
error [Char]
"CAcf.poke: Not defined"

-- Memory management -----------------------------------------------------------

-- | /acf_init/ /x/ 
--
-- Initializes the variable /x/ for use, and sets its value to zero.
foreign import ccall "acf.h acf_init"
  acf_init :: Ptr CAcf -> IO ()

-- | /acf_clear/ /x/ 
--
-- Clears the variable /x/, freeing or recycling its allocated memory.
foreign import ccall "acf.h acf_clear"
  acf_clear :: Ptr CAcf -> IO ()

foreign import ccall "acf.h &acf_clear"
  p_acf_clear :: FunPtr (Ptr CAcf -> IO ())

-- | /acf_swap/ /z/ /x/ 
--
-- Swaps /z/ and /x/ efficiently.
foreign import ccall "acf.h acf_swap"
  acf_swap :: Ptr CAcf -> Ptr CAcf -> IO ()

-- | /acf_allocated_bytes/ /x/ 
--
-- Returns the total number of bytes heap-allocated internally by this
-- object. The count excludes the size of the structure itself. Add
-- @sizeof(acf_struct)@ to get the size of the object as a whole.
foreign import ccall "acf.h acf_allocated_bytes"
  acf_allocated_bytes :: Ptr CAcf -> IO CLong

-- Basic manipulation ----------------------------------------------------------

-- | /acf_real_ptr/ /z/ 
foreign import ccall "acf.h acf_real_ptr"
  acf_real_ptr :: Ptr CAcf -> IO (Ptr CArf)
  
-- | /acf_imag_ptr/ /z/ 
--
-- Returns a pointer to the real or imaginary part of /z/.
foreign import ccall "acf.h acf_imag_ptr"
  acf_imag_ptr :: Ptr CAcf -> IO (Ptr CArf)

-- | /acf_set/ /z/ /x/ 
--
-- Sets /z/ to the value /x/.
foreign import ccall "acf.h acf_set"
  acf_set :: Ptr CAcf -> Ptr CAcf -> IO ()

-- | /acf_equal/ /x/ /y/ 
--
-- Returns whether /x/ and /y/ are equal.
foreign import ccall "acf.h acf_equal"
  acf_equal :: Ptr CAcf -> Ptr CAcf -> IO CInt

-- Arithmetic ------------------------------------------------------------------

-- | /acf_add/ /res/ /x/ /y/ /prec/ /rnd/ 
--
foreign import ccall "acf.h acf_add"
  acf_add :: Ptr CAcf -> Ptr CAcf -> Ptr CAcf -> CLong -> ArfRnd -> IO CInt

-- | /acf_sub/ /res/ /x/ /y/ /prec/ /rnd/ 
--
foreign import ccall "acf.h acf_sub"
  acf_sub :: Ptr CAcf -> Ptr CAcf -> Ptr CAcf -> CLong -> ArfRnd -> IO CInt

-- | /acf_mul/ /res/ /x/ /y/ /prec/ /rnd/ 
--
-- Sets /res/ to the sum, difference or product of /x/ or /y/, correctly
-- rounding the real and imaginary parts in direction /rnd/. The return
-- flag has the least significant bit set if the real part is inexact, and
-- the second least significant bit set if the imaginary part is inexact.
foreign import ccall "acf.h acf_mul"
  acf_mul :: Ptr CAcf -> Ptr CAcf -> Ptr CAcf -> CLong -> ArfRnd -> IO CInt

-- Approximate arithmetic ------------------------------------------------------

-- The following operations are /not/ correctly rounded. The @rnd@
-- parameter specifies the final direction of rounding, but intermediate
-- roundings are implementation-defined.
--
-- | /acf_approx_inv/ /res/ /x/ /prec/ /rnd/ 
foreign import ccall "acf.h acf_approx_inv"
  acf_approx_inv :: Ptr CAcf -> Ptr CAcf -> CLong -> ArfRnd -> IO ()
-- | /acf_approx_div/ /res/ /x/ /y/ /prec/ /rnd/ 
foreign import ccall "acf.h acf_approx_div"
  acf_approx_div :: Ptr CAcf -> Ptr CAcf -> Ptr CAcf -> CLong -> ArfRnd -> IO ()
-- | /acf_approx_sqrt/ /res/ /x/ /prec/ /rnd/ 
--
-- Computes an approximate inverse, quotient or square root.
foreign import ccall "acf.h acf_approx_sqrt"
  acf_approx_sqrt :: Ptr CAcf -> Ptr CAcf -> CLong -> ArfRnd -> IO ()

-- | /acf_approx_dot/ /res/ /initial/ /subtract/ /x/ /xstep/ /y/ /ystep/ /len/ /prec/ /rnd/ 
--
-- Computes an approximate dot product, with the same meaning of the
-- parameters as @arb_dot@.
foreign import ccall "acf.h acf_approx_dot"
  acf_approx_dot :: Ptr CAcf -> Ptr CAcf -> CInt -> Ptr CAcf -> CLong -> Ptr CAcf -> CLong -> CLong -> CLong -> ArfRnd -> IO ()