Skip to content

Commit 630d7aa

Browse files
dmjioclaude
andauthored
Remove unsafeCoerce from Data.constant (#73)
Replace runtime type-tag dispatch + unsafeCoerce with three direct FFI functions: constant (takes Double, ArrayFire converts internally), constantComplex (preserves imaginary part via af_constant_complex), constantLong / constantULong (64-bit integers without Double rounding). Add tests covering all four functions including precision boundaries (maxBound/minBound for Int and Word64). Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 6b8f649 commit 630d7aa

3 files changed

Lines changed: 138 additions & 193 deletions

File tree

‎src/ArrayFire/Data.hs‎

Lines changed: 63 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import Foreign.Marshal hiding (void)
4141
import Foreign.Ptr (Ptr)
4242
import Foreign.Storable
4343
import System.IO.Unsafe
44-
import Unsafe.Coerce
4544

4645
import Data.Bits
4746

@@ -60,7 +59,7 @@ import ArrayFire.Arith
6059
-- [1 1 1 1]
6160
-- -1
6261
bitNot
63-
:: (AFType a, Bits a)
62+
:: forall a. (AFType a, Bits a, Integral a)
6463
=> Array a
6564
-> Array a
6665
bitNot 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.
8480
constant
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 #-}
9286
constant 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
--

‎src/ArrayFire/Internal/Types.hsc‎

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -126,47 +126,20 @@ newtype Window = Window (ForeignPtr ())
126126
class Storable a => AFType a where
127127
afType :: Proxy a -> AFDtype
128128

129-
instance AFType Double where
130-
afType Proxy = f64
131-
132-
instance AFType Float where
133-
afType Proxy = f32
134-
135-
instance AFType (Complex Double) where
136-
afType Proxy = c64
137-
138-
instance AFType (Complex Float) where
139-
afType Proxy = c32
140-
141-
instance AFType CBool where
142-
afType Proxy = b8
143-
144-
instance AFType Int32 where
145-
afType Proxy = s32
146-
147-
instance AFType Word32 where
148-
afType Proxy = u32
149-
150-
instance AFType Word8 where
151-
afType Proxy = u8
152-
153-
instance AFType Int64 where
154-
afType Proxy = s64
155-
156-
instance AFType Int where
157-
afType Proxy = s64
158-
159-
instance AFType Int16 where
160-
afType Proxy = s16
161-
162-
instance AFType Word16 where
163-
afType Proxy = u16
164-
165-
instance AFType Word64 where
166-
afType Proxy = u64
167-
168-
instance AFType Word where
169-
afType Proxy = u64
129+
instance AFType Double where afType Proxy = f64
130+
instance AFType Float where afType Proxy = f32
131+
instance AFType (Complex Double) where afType Proxy = c64
132+
instance AFType (Complex Float) where afType Proxy = c32
133+
instance AFType CBool where afType Proxy = b8
134+
instance AFType Int32 where afType Proxy = s32
135+
instance AFType Word32 where afType Proxy = u32
136+
instance AFType Word8 where afType Proxy = u8
137+
instance AFType Int64 where afType Proxy = s64
138+
instance AFType Int where afType Proxy = s64
139+
instance AFType Int16 where afType Proxy = s16
140+
instance AFType Word16 where afType Proxy = u16
141+
instance AFType Word64 where afType Proxy = u64
142+
instance AFType Word where afType Proxy = u64
170143

171144
-- | Maps an ArrayFire element type to the scalar type returned by whole-array
172145
-- reductions (e.g. 'meanAll', 'det'). Real and integral element types yield

‎test/ArrayFire/DataSpec.hs‎

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,67 @@ import ArrayFire hiding (not)
1818
spec :: Spec
1919
spec =
2020
describe "Data tests" $ do
21-
it "Should create constant Array" $ do
22-
constant @Float [1] 1 `shouldBe` 1
23-
constant @Double [1] 1 `shouldBe` 1
24-
constant @Int16 [1] 1 `shouldBe` 1
25-
constant @Int32 [1] 1 `shouldBe` 1
26-
constant @Int64 [1] 1 `shouldBe` 1
27-
constant @Int [1] 1 `shouldBe` 1
28-
constant @Word16 [1] 1 `shouldBe` 1
29-
constant @Word32 [1] 1 `shouldBe` 1
30-
constant @Word64 [1] 1 `shouldBe` 1
31-
constant @Word [1] 1 `shouldBe` 1
32-
constant @CBool [1] 1 `shouldBe` 1
33-
constant @(Complex Double) [1] (1.0 :+ 1.0)
34-
`shouldBe`
35-
constant @(Complex Double) [1] (1.0 :+ 1.0)
36-
constant @(Complex Float) [1] (1.0 :+ 1.0)
37-
`shouldBe`
38-
constant @(Complex Float) [1] (1.0 :+ 1.0)
21+
describe "constant" $ do
22+
it "creates a scalar Float array" $
23+
constant @Float [1] 1 `shouldBe` scalar @Float 1
24+
it "creates a scalar Double array" $
25+
constant @Double [1] 2.5 `shouldBe` scalar @Double 2.5
26+
it "creates a scalar Int16 array" $
27+
constant @Int16 [1] 42 `shouldBe` scalar @Int16 42
28+
it "creates a scalar Int32 array" $
29+
constant @Int32 [1] (-7) `shouldBe` scalar @Int32 (-7)
30+
it "creates a scalar Word8 array" $
31+
constant @Word8 [1] 255 `shouldBe` scalar @Word8 255
32+
it "creates a scalar Word16 array" $
33+
constant @Word16 [1] 1000 `shouldBe` scalar @Word16 1000
34+
it "creates a scalar Word32 array" $
35+
constant @Word32 [1] 999 `shouldBe` scalar @Word32 999
36+
it "creates a CBool array" $
37+
constant @CBool [1] 1 `shouldBe` scalar @CBool 1
38+
it "creates a multi-element array with correct shape" $ do
39+
let a = constant @Double [3,3] 0
40+
getDims a `shouldBe` (3,3,1,1)
41+
it "all elements equal the scalar value" $
42+
constant @Float [4] 3.14 `shouldBe` vector @Float 4 [3.14, 3.14, 3.14, 3.14]
43+
44+
describe "constantComplex" $ do
45+
it "creates a Complex Double array preserving imaginary part" $
46+
constantComplex [1] (1.0 :+ 2.0)
47+
`shouldBe` scalar @(Complex Double) (1.0 :+ 2.0)
48+
it "creates a Complex Float array preserving imaginary part" $
49+
constantComplex [1] (3.0 :+ 4.0 :: Complex Float)
50+
`shouldBe` scalar @(Complex Float) (3.0 :+ 4.0)
51+
it "creates a zero complex array" $
52+
constantComplex [2] (0 :+ 0 :: Complex Double)
53+
`shouldBe` vector @(Complex Double) 2 [0, 0]
54+
it "handles purely real complex values" $
55+
constantComplex [1] (5.0 :+ 0.0 :: Complex Double)
56+
`shouldBe` scalar @(Complex Double) (5.0 :+ 0.0)
57+
it "handles purely imaginary complex values" $
58+
constantComplex [1] (0.0 :+ 7.0 :: Complex Double)
59+
`shouldBe` scalar @(Complex Double) (0.0 :+ 7.0)
60+
61+
describe "constantLong" $ do
62+
it "creates an Int array with value 1" $
63+
constantLong [1] 1 `shouldBe` scalar @Int 1
64+
it "creates an Int array with a negative value" $
65+
constantLong [1] (-42) `shouldBe` scalar @Int (-42)
66+
it "preserves maxBound :: Int without rounding" $
67+
constantLong [1] maxBound `shouldBe` scalar @Int maxBound
68+
it "preserves minBound :: Int without rounding" $
69+
constantLong [1] minBound `shouldBe` scalar @Int minBound
70+
it "creates a multi-element array" $
71+
constantLong [3] 7 `shouldBe` vector @Int 3 [7, 7, 7]
72+
73+
describe "constantULong" $ do
74+
it "creates a Word64 array with value 1" $
75+
constantULong [1] 1 `shouldBe` scalar @Word64 1
76+
it "creates a Word64 array with value 0" $
77+
constantULong [1] 0 `shouldBe` scalar @Word64 0
78+
it "preserves maxBound :: Word64 without rounding" $
79+
constantULong [1] maxBound `shouldBe` scalar @Word64 maxBound
80+
it "creates a multi-element array" $
81+
constantULong [3] 100 `shouldBe` vector @Word64 3 [100, 100, 100]
3982

4083
describe "arange" $ do
4184
it "generates a sequence along dim 0 for a 1D array" $ do

0 commit comments

Comments
 (0)