From bfa68b73572538d56038b3350ce46d2b3af19dba Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Fri, 5 Jun 2015 16:46:13 +0200 Subject: move foreign --- packages/base/src/Data/Packed/Foreign.hs | 99 ------------------------------ packages/base/src/Internal/Foreign.hs | 102 +++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 99 deletions(-) delete mode 100644 packages/base/src/Data/Packed/Foreign.hs create mode 100644 packages/base/src/Internal/Foreign.hs diff --git a/packages/base/src/Data/Packed/Foreign.hs b/packages/base/src/Data/Packed/Foreign.hs deleted file mode 100644 index 85b8bc7..0000000 --- a/packages/base/src/Data/Packed/Foreign.hs +++ /dev/null @@ -1,99 +0,0 @@ -{-# LANGUAGE MagicHash, UnboxedTuples #-} --- | FFI and hmatrix helpers. --- --- Sample usage, to upload a perspective matrix to a shader. --- --- @ glUniformMatrix4fv 0 1 (fromIntegral gl_TRUE) \`appMatrix\` perspective 0.01 100 (pi\/2) (4\/3) --- @ --- -{-# OPTIONS_HADDOCK hide #-} -module Data.Packed.Foreign - ( app - , appVector, appVectorLen - , appMatrix, appMatrixLen, appMatrixRaw, appMatrixRawLen - , unsafeMatrixToVector, unsafeMatrixToForeignPtr - ) where -import Data.Packed.Internal -import qualified Data.Vector.Storable as S -import Foreign (Ptr, ForeignPtr, Storable) -import GHC.Base (IO(..), realWorld#) - -{-# INLINE unsafeInlinePerformIO #-} --- | If we use unsafePerformIO, it may not get inlined, so in a function that returns IO (which are all safe uses of app* in this module), there would be --- unecessary calls to unsafePerformIO or its internals. -unsafeInlinePerformIO :: IO a -> a -unsafeInlinePerformIO (IO f) = case f realWorld# of - (# _, x #) -> x - -{-# INLINE app #-} --- | Only useful since it is left associated with a precedence of 1, unlike 'Prelude.$', which is right associative. --- e.g. --- --- @ --- someFunction --- \`appMatrixLen\` m --- \`appVectorLen\` v --- \`app\` other --- \`app\` arguments --- \`app\` go here --- @ --- --- One could also write: --- --- @ --- (someFunction --- \`appMatrixLen\` m --- \`appVectorLen\` v) --- other --- arguments --- (go here) --- @ --- -app :: (a -> b) -> a -> b -app f = f - -{-# INLINE appVector #-} -appVector :: Storable a => (Ptr a -> b) -> Vector a -> b -appVector f x = unsafeInlinePerformIO (S.unsafeWith x (return . f)) - -{-# INLINE appVectorLen #-} -appVectorLen :: Storable a => (CInt -> Ptr a -> b) -> Vector a -> b -appVectorLen f x = unsafeInlinePerformIO (S.unsafeWith x (return . f (fromIntegral (S.length x)))) - -{-# INLINE appMatrix #-} -appMatrix :: Element a => (Ptr a -> b) -> Matrix a -> b -appMatrix f x = unsafeInlinePerformIO (S.unsafeWith (flatten x) (return . f)) - -{-# INLINE appMatrixLen #-} -appMatrixLen :: Element a => (CInt -> CInt -> Ptr a -> b) -> Matrix a -> b -appMatrixLen f x = unsafeInlinePerformIO (S.unsafeWith (flatten x) (return . f r c)) - where - r = fromIntegral (rows x) - c = fromIntegral (cols x) - -{-# INLINE appMatrixRaw #-} -appMatrixRaw :: Storable a => (Ptr a -> b) -> Matrix a -> b -appMatrixRaw f x = unsafeInlinePerformIO (S.unsafeWith (xdat x) (return . f)) - -{-# INLINE appMatrixRawLen #-} -appMatrixRawLen :: Element a => (CInt -> CInt -> Ptr a -> b) -> Matrix a -> b -appMatrixRawLen f x = unsafeInlinePerformIO (S.unsafeWith (xdat x) (return . f r c)) - where - r = fromIntegral (rows x) - c = fromIntegral (cols x) - -infixl 1 `app` -infixl 1 `appVector` -infixl 1 `appMatrix` -infixl 1 `appMatrixRaw` - -{-# INLINE unsafeMatrixToVector #-} --- | This will disregard the order of the matrix, and simply return it as-is. --- If the order of the matrix is RowMajor, this function is identical to 'flatten'. -unsafeMatrixToVector :: Matrix a -> Vector a -unsafeMatrixToVector = xdat - -{-# INLINE unsafeMatrixToForeignPtr #-} -unsafeMatrixToForeignPtr :: Storable a => Matrix a -> (ForeignPtr a, Int) -unsafeMatrixToForeignPtr m = S.unsafeToForeignPtr0 (xdat m) - diff --git a/packages/base/src/Internal/Foreign.hs b/packages/base/src/Internal/Foreign.hs new file mode 100644 index 0000000..ea071a4 --- /dev/null +++ b/packages/base/src/Internal/Foreign.hs @@ -0,0 +1,102 @@ +{-# LANGUAGE MagicHash, UnboxedTuples #-} +-- | FFI and hmatrix helpers. +-- +-- Sample usage, to upload a perspective matrix to a shader. +-- +-- @ glUniformMatrix4fv 0 1 (fromIntegral gl_TRUE) \`appMatrix\` perspective 0.01 100 (pi\/2) (4\/3) +-- @ +-- + +module Internal.Foreign + ( app + , appVector, appVectorLen + , appMatrix, appMatrixLen, appMatrixRaw, appMatrixRawLen + , unsafeMatrixToVector, unsafeMatrixToForeignPtr + ) where + +import Foreign.C.Types(CInt) +import Internal.Vector +import Internal.Matrix +import qualified Data.Vector.Storable as S +import Foreign (Ptr, ForeignPtr, Storable) +import GHC.Base (IO(..), realWorld#) + +{-# INLINE unsafeInlinePerformIO #-} +-- | If we use unsafePerformIO, it may not get inlined, so in a function that returns IO (which are all safe uses of app* in this module), there would be +-- unecessary calls to unsafePerformIO or its internals. +unsafeInlinePerformIO :: IO a -> a +unsafeInlinePerformIO (IO f) = case f realWorld# of + (# _, x #) -> x + +{-# INLINE app #-} +-- | Only useful since it is left associated with a precedence of 1, unlike 'Prelude.$', which is right associative. +-- e.g. +-- +-- @ +-- someFunction +-- \`appMatrixLen\` m +-- \`appVectorLen\` v +-- \`app\` other +-- \`app\` arguments +-- \`app\` go here +-- @ +-- +-- One could also write: +-- +-- @ +-- (someFunction +-- \`appMatrixLen\` m +-- \`appVectorLen\` v) +-- other +-- arguments +-- (go here) +-- @ +-- +app :: (a -> b) -> a -> b +app f = f + +{-# INLINE appVector #-} +appVector :: Storable a => (Ptr a -> b) -> Vector a -> b +appVector f x = unsafeInlinePerformIO (S.unsafeWith x (return . f)) + +{-# INLINE appVectorLen #-} +appVectorLen :: Storable a => (CInt -> Ptr a -> b) -> Vector a -> b +appVectorLen f x = unsafeInlinePerformIO (S.unsafeWith x (return . f (fromIntegral (S.length x)))) + +{-# INLINE appMatrix #-} +appMatrix :: Element a => (Ptr a -> b) -> Matrix a -> b +appMatrix f x = unsafeInlinePerformIO (S.unsafeWith (flatten x) (return . f)) + +{-# INLINE appMatrixLen #-} +appMatrixLen :: Element a => (CInt -> CInt -> Ptr a -> b) -> Matrix a -> b +appMatrixLen f x = unsafeInlinePerformIO (S.unsafeWith (flatten x) (return . f r c)) + where + r = fromIntegral (rows x) + c = fromIntegral (cols x) + +{-# INLINE appMatrixRaw #-} +appMatrixRaw :: Storable a => (Ptr a -> b) -> Matrix a -> b +appMatrixRaw f x = unsafeInlinePerformIO (S.unsafeWith (xdat x) (return . f)) + +{-# INLINE appMatrixRawLen #-} +appMatrixRawLen :: Element a => (CInt -> CInt -> Ptr a -> b) -> Matrix a -> b +appMatrixRawLen f x = unsafeInlinePerformIO (S.unsafeWith (xdat x) (return . f r c)) + where + r = fromIntegral (rows x) + c = fromIntegral (cols x) + +infixl 1 `app` +infixl 1 `appVector` +infixl 1 `appMatrix` +infixl 1 `appMatrixRaw` + +{-# INLINE unsafeMatrixToVector #-} +-- | This will disregard the order of the matrix, and simply return it as-is. +-- If the order of the matrix is RowMajor, this function is identical to 'flatten'. +unsafeMatrixToVector :: Matrix a -> Vector a +unsafeMatrixToVector = xdat + +{-# INLINE unsafeMatrixToForeignPtr #-} +unsafeMatrixToForeignPtr :: Storable a => Matrix a -> (ForeignPtr a, Int) +unsafeMatrixToForeignPtr m = S.unsafeToForeignPtr0 (xdat m) + -- cgit v1.2.3