{-# LANGUAGE CPP                 #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications    #-}
-- |
-- Module      : Data.Array.Accelerate.Numeric.Sum.LLVM.Native
-- Copyright   : [2017..2020] Trevor L. McDonell
-- License     : BSD3
--
-- Maintainer  : Trevor L. McDonell <trevor.mcdonell@gmail.com>
-- Stability   : experimental
-- Portability : non-portable (GHC extensions)
--

module Data.Array.Accelerate.Numeric.Sum.LLVM.Native (

  fadd, fsub, fmul,

) where

import Data.Array.Accelerate                                        as A
import Data.Array.Accelerate.Sugar.Elt
import Data.Array.Accelerate.Type

#ifdef ACCELERATE_LLVM_NATIVE_BACKEND
import Data.Array.Accelerate.LLVM.CodeGen.Sugar
import Data.Array.Accelerate.LLVM.Native.Foreign                    as A
import qualified Data.Array.Accelerate.Numeric.Sum.LLVM.Prim        as Prim
#endif

#ifdef ACCELERATE_LLVM_NATIVE_BACKEND
wrap2 :: (Elt a, Elt b, Elt c)
      => String                                       -- name of the operation
      -> IRFun1 Native () (EltR (a, b) -> EltR c)     -- foreign implementation
      -> (Exp a -> Exp b -> Exp c)                    -- fallback implementation
      -> Exp a
      -> Exp b
      -> Exp c
wrap2 :: String
-> IRFun1 Native () (EltR (a, b) -> EltR c)
-> (Exp a -> Exp b -> Exp c)
-> Exp a
-> Exp b
-> Exp c
wrap2 String
str IRFun1 Native () (EltR (a, b) -> EltR c)
f Exp a -> Exp b -> Exp c
g = (Exp (Plain (Exp a), Plain (Exp b)) -> Exp c)
-> Exp a -> Exp b -> Exp c
forall (f :: * -> *) a b c.
Lift f (f a, f b) =>
(f (Plain (f a), Plain (f b)) -> f c) -> f a -> f b -> f c
A.curry (ForeignExp (EltR (a, b) -> EltR c)
-> (Exp (a, b) -> Exp c) -> Exp (a, b) -> Exp c
forall x y (asm :: * -> *).
(Elt x, Elt y, Foreign asm) =>
asm (EltR x -> EltR y) -> (Exp x -> Exp y) -> Exp x -> Exp y
foreignExp (String
-> IRFun1 Native () ((((), EltR a), EltR b) -> EltR c)
-> ForeignExp ((((), EltR a), EltR b) -> EltR c)
forall x y.
String -> IRFun1 Native () (x -> y) -> ForeignExp (x -> y)
ForeignExp String
str IRFun1 Native () ((((), EltR a), EltR b) -> EltR c)
IRFun1 Native () (EltR (a, b) -> EltR c)
f) ((Exp a -> Exp b -> Exp c)
-> Exp (Plain (Exp a), Plain (Exp b)) -> Exp c
forall (f :: * -> *) a b c.
Unlift f (f a, f b) =>
(f a -> f b -> f c) -> f (Plain (f a), Plain (f b)) -> f c
A.uncurry Exp a -> Exp b -> Exp c
g))
#endif

fadd :: forall a. (Elt a, IsFloating a) => (Exp a -> Exp a -> Exp a) -> Exp a -> Exp a -> Exp a
#ifdef ACCELERATE_LLVM_NATIVE_BACKEND
fadd :: (Exp a -> Exp a -> Exp a) -> Exp a -> Exp a -> Exp a
fadd = String
-> IRFun1 Native () (EltR (a, a) -> EltR a)
-> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Exp a
-> Exp a
forall a b c.
(Elt a, Elt b, Elt c) =>
String
-> IRFun1 Native () (EltR (a, b) -> EltR c)
-> (Exp a -> Exp b -> Exp c)
-> Exp a
-> Exp b
-> Exp c
wrap2 String
"fadd" (FloatingType a
-> IROpenFun1 Native () () ((((), EltR a), EltR a) -> EltR a)
forall a arch env aenv.
FloatingType a
-> IROpenFun1 arch env aenv ((((), EltR a), EltR a) -> EltR a)
Prim.fadd (IsFloating a => FloatingType a
forall a. IsFloating a => FloatingType a
floatingType @a))
#else
fadd = id
#endif

fsub :: forall a. (Elt a, IsFloating a) => (Exp a -> Exp a -> Exp a) -> Exp a -> Exp a -> Exp a
#ifdef ACCELERATE_LLVM_NATIVE_BACKEND
fsub :: (Exp a -> Exp a -> Exp a) -> Exp a -> Exp a -> Exp a
fsub = String
-> IRFun1 Native () (EltR (a, a) -> EltR a)
-> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Exp a
-> Exp a
forall a b c.
(Elt a, Elt b, Elt c) =>
String
-> IRFun1 Native () (EltR (a, b) -> EltR c)
-> (Exp a -> Exp b -> Exp c)
-> Exp a
-> Exp b
-> Exp c
wrap2 String
"fsub" (FloatingType a
-> IROpenFun1 Native () () ((((), EltR a), EltR a) -> EltR a)
forall a arch env aenv.
FloatingType a
-> IROpenFun1 arch env aenv ((((), EltR a), EltR a) -> EltR a)
Prim.fsub (IsFloating a => FloatingType a
forall a. IsFloating a => FloatingType a
floatingType @a))
#else
fsub = id
#endif

fmul :: forall a. (Elt a, IsFloating a) => (Exp a -> Exp a -> Exp a) -> Exp a -> Exp a -> Exp a
#ifdef ACCELERATE_LLVM_NATIVE_BACKEND
fmul :: (Exp a -> Exp a -> Exp a) -> Exp a -> Exp a -> Exp a
fmul = String
-> IRFun1 Native () (EltR (a, a) -> EltR a)
-> (Exp a -> Exp a -> Exp a)
-> Exp a
-> Exp a
-> Exp a
forall a b c.
(Elt a, Elt b, Elt c) =>
String
-> IRFun1 Native () (EltR (a, b) -> EltR c)
-> (Exp a -> Exp b -> Exp c)
-> Exp a
-> Exp b
-> Exp c
wrap2 String
"fmul" (FloatingType a
-> IROpenFun1 Native () () ((((), EltR a), EltR a) -> EltR a)
forall a arch env aenv.
FloatingType a
-> IROpenFun1 arch env aenv ((((), EltR a), EltR a) -> EltR a)
Prim.fmul (IsFloating a => FloatingType a
forall a. IsFloating a => FloatingType a
floatingType @a))
#else
fmul = id
#endif