@@ -41,7 +41,6 @@ import Foreign.Marshal hiding (void)
4141import Foreign.Ptr (Ptr )
4242import Foreign.Storable
4343import System.IO.Unsafe
44- import Unsafe.Coerce
4544
4645import Data.Bits
4746
@@ -60,7 +59,7 @@ import ArrayFire.Arith
6059-- [1 1 1 1]
6160-- -1
6261bitNot
63- :: (AFType a , Bits a )
62+ :: forall a . (AFType a , Bits a , Integral a )
6463 => Array a
6564 -> Array a
6665bitNot arr = arr `bitXor` ones
@@ -72,148 +71,78 @@ bitNot arr = arr `bitXor` ones
7271 , fromIntegral d2
7372 , fromIntegral d3
7473 ]
75- (complement zeroBits)
74+ (fromIntegral ( complement ( zeroBits :: a )) )
7675
77- -- | Creates an 'Array' from a scalar value from given dimensions
78- --
79- -- >>> constant @Double [2,2] 2.0
80- -- ArrayFire Array
81- -- [2 2 1 1]
82- -- 2.0000 2.0000
83- -- 2.0000 2.0000
76+ -- | Creates a constant 'Array' filled with a 'Double' scalar.
77+ -- ArrayFire converts the value to the element type internally.
78+ -- Use 'constantComplex' for complex arrays, 'constantLong' / 'constantULong'
79+ -- for 64-bit integer arrays where the value exceeds 2^53.
8480constant
85- :: forall a . AFType a
86- => [Int ]
87- -- ^ Dimensions
88- -> a
89- -- ^ Scalar value
81+ :: forall a . AFType a
82+ => [Int ] -- ^ Dimensions
83+ -> Double -- ^ Scalar value
9084 -> Array a
9185{-# NOINLINE constant #-}
9286constant dims val =
93- case dtyp of
94- x | x == c64 ->
95- cast $ constantComplex dims (unsafeCoerce val :: Complex Double )
96- | x == c32 ->
97- cast $ constantComplex dims (unsafeCoerce val :: Complex Float )
98- | x == s64 ->
99- cast $ constantLong dims (unsafeCoerce val :: Int )
100- | x == u64 ->
101- cast $ constantULong dims (unsafeCoerce val :: Word64 )
102- | x == s32 ->
103- constant' dims (fromIntegral (unsafeCoerce val :: Int32 ) :: Double )
104- | x == s16 ->
105- constant' dims (fromIntegral (unsafeCoerce val :: Int16 ) :: Double )
106- | x == u32 ->
107- constant' dims (fromIntegral (unsafeCoerce val :: Word32 ) :: Double )
108- | x == u8 ->
109- constant' dims (fromIntegral (unsafeCoerce val :: Word8 ) :: Double )
110- | x == u16 ->
111- constant' dims (fromIntegral (unsafeCoerce val :: Word16 ) :: Double )
112- | x == f64 ->
113- constant' dims (unsafeCoerce val :: Double )
114- | x == b8 ->
115- constant' dims (fromIntegral (unsafeCoerce val :: CBool ) :: Double )
116- | x == f32 ->
117- constant' dims (realToFrac (unsafeCoerce val :: Float ))
118- | otherwise -> error " constant: Invalid array fire type"
87+ unsafePerformIO . mask_ $ do
88+ ptr <- calloca $ \ ptrPtr -> do
89+ withArray (fromIntegral <$> dims) $ \ dimArray -> do
90+ throwAFError =<< af_constant ptrPtr val n dimArray dtyp
91+ peek ptrPtr
92+ Array <$> newForeignPtr af_release_array_finalizer ptr
11993 where
94+ n = fromIntegral (length dims)
12095 dtyp = afType (Proxy @ a )
12196
122- -- Creates the array directly with the target dtype: @af_constant@ takes
123- -- the value as a C double for every non-complex, non-64-bit-integral
124- -- dtype. Routing through an f64 array and casting (as this used to do)
125- -- fails with AF_ERR_NO_DBL on OpenCL devices without fp64 support and
126- -- changes b8 semantics (the cast normalises non-zero values to 1).
127- constant'
128- :: [Int ]
129- -- ^ Dimensions
130- -> Double
131- -- ^ Scalar value
132- -> Array a
133- constant' dims' val' =
134- unsafePerformIO . mask_ $ do
135- ptr <- calloca $ \ ptrPtr -> do
136- withArray (fromIntegral <$> dims') $ \ dimArray -> do
137- throwAFError =<< af_constant ptrPtr val' n dimArray dtyp
138- peek ptrPtr
139- Array <$>
140- newForeignPtr
141- af_release_array_finalizer
142- ptr
143- where
144- n = fromIntegral (length dims')
145-
146- -- | Creates an 'Array (Complex Double)' from a scalar val'ue
147- --
148- -- @
149- -- >>> constantComplex [2,2] (2.0 :+ 2.0)
150- -- @
151- --
152- constantComplex
153- :: forall arr . (Real arr , AFType (Complex arr ))
154- => [Int ]
155- -- ^ Dimensions
156- -> Complex arr
157- -- ^ Scalar val'ue
158- -> Array (Complex arr )
159- constantComplex dims' ((realToFrac -> x) :+ (realToFrac -> y)) = unsafePerformIO . mask_ $ do
160- ptr <- calloca $ \ ptrPtr -> do
161- withArray (fromIntegral <$> dims') $ \ dimArray -> do
162- throwAFError =<< af_constant_complex ptrPtr x y n dimArray typ
163- peek ptrPtr
164- Array <$>
165- newForeignPtr
166- af_release_array_finalizer
167- ptr
168- where
169- n = fromIntegral (length dims')
170- typ = afType (Proxy @ (Complex arr ))
97+ -- | Creates a constant complex 'Array' from a 'Complex' scalar.
98+ constantComplex
99+ :: forall r . (Real r , AFType (Complex r ))
100+ => [Int ] -- ^ Dimensions
101+ -> Complex r -- ^ Scalar value
102+ -> Array (Complex r )
103+ {-# NOINLINE constantComplex #-}
104+ constantComplex dims ((realToFrac -> x) :+ (realToFrac -> y)) =
105+ unsafePerformIO . mask_ $ do
106+ ptr <- calloca $ \ ptrPtr -> do
107+ withArray (fromIntegral <$> dims) $ \ dimArray -> do
108+ throwAFError =<< af_constant_complex ptrPtr x y n dimArray typ
109+ peek ptrPtr
110+ Array <$> newForeignPtr af_release_array_finalizer ptr
111+ where
112+ n = fromIntegral (length dims)
113+ typ = afType (Proxy @ (Complex r ))
171114
172- -- | Creates an 'Array Int64' from a scalar val'ue
173- --
174- -- @
175- -- >>> constantLong [2,2] 2.0
176- -- @
177- --
178- constantLong
179- :: [Int ]
180- -- ^ Dimensions
181- -> Int
182- -- ^ Scalar val'ue
183- -> Array Int
184- constantLong dims' val' = unsafePerformIO . mask_ $ do
185- ptr <- calloca $ \ ptrPtr -> do
186- withArray (fromIntegral <$> dims') $ \ dimArray -> do
187- throwAFError =<< af_constant_long ptrPtr (fromIntegral val') n dimArray
188- peek ptrPtr
189- Array <$>
190- newForeignPtr
191- af_release_array_finalizer
192- ptr
193- where
194- n = fromIntegral (length dims')
115+ -- | Creates a constant 'Array' of 64-bit signed integers.
116+ -- Preserves the full integer value without 'Double' rounding.
117+ constantLong
118+ :: [Int ] -- ^ Dimensions
119+ -> Int -- ^ Scalar value
120+ -> Array Int
121+ {-# NOINLINE constantLong #-}
122+ constantLong dims val =
123+ unsafePerformIO . mask_ $ do
124+ ptr <- calloca $ \ ptrPtr -> do
125+ withArray (fromIntegral <$> dims) $ \ dimArray -> do
126+ throwAFError =<< af_constant_long ptrPtr (fromIntegral val) n dimArray
127+ peek ptrPtr
128+ Array <$> newForeignPtr af_release_array_finalizer ptr
129+ where n = fromIntegral (length dims)
195130
196- -- | Creates an 'Array Word64' from a scalar val'ue
197- --
198- -- @
199- -- >>> constantULong [2,2] 2.0
200- -- @
201- --
202- constantULong
203- :: [Int ]
204- -> Word64
205- -> Array Word64
206- constantULong dims' val' = unsafePerformIO . mask_ $ do
207- ptr <- calloca $ \ ptrPtr -> do
208- withArray (fromIntegral <$> dims') $ \ dimArray -> do
209- throwAFError =<< af_constant_ulong ptrPtr (fromIntegral val') n dimArray
210- peek ptrPtr
211- Array <$>
212- newForeignPtr
213- af_release_array_finalizer
214- ptr
215- where
216- n = fromIntegral (length dims')
131+ -- | Creates a constant 'Array' of 64-bit unsigned integers.
132+ -- Preserves the full integer value without 'Double' rounding.
133+ constantULong
134+ :: [Int ] -- ^ Dimensions
135+ -> Word64 -- ^ Scalar value
136+ -> Array Word64
137+ {-# NOINLINE constantULong #-}
138+ constantULong dims val =
139+ unsafePerformIO . mask_ $ do
140+ ptr <- calloca $ \ ptrPtr -> do
141+ withArray (fromIntegral <$> dims) $ \ dimArray -> do
142+ throwAFError =<< af_constant_ulong ptrPtr (fromIntegral val) n dimArray
143+ peek ptrPtr
144+ Array <$> newForeignPtr af_release_array_finalizer ptr
145+ where n = fromIntegral (length dims)
217146
218147-- | Creates a range of values in an Array
219148--
0 commit comments