Add lots of Prim instances
[darcs-mirrors/vector.git] / Data / Vector / Primitive / Prim.hs
1 {-# LANGUAGE MagicHash, UnboxedTuples, CPP #-}
2
3 -- |
4 -- Module : Data.Vector.Primitive.Prim
5 -- Copyright : (c) Roman Leshchinskiy 2008
6 -- License : BSD-style
7 --
8 -- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
9 -- Stability : experimental
10 -- Portability : non-portable
11 --
12 -- Primitives for manipulating unboxed arrays of primitive types.
13 --
14
15 module Data.Vector.Primitive.Prim (
16 Prim(..)
17 ) where
18
19 import GHC.Base (
20 Int(..), Char(..),
21 )
22 import GHC.Float (
23 Float(..), Double(..)
24 )
25 import GHC.Word (
26 Word(..), Word8(..), Word16(..), Word32(..), Word64(..)
27 )
28 import GHC.Int (
29 Int8(..), Int16(..), Int32(..), Int64(..)
30 )
31
32 import GHC.Prim
33 import Data.Array.Base (
34 wORD_SCALE, fLOAT_SCALE, dOUBLE_SCALE
35 )
36
37 -- | Class of types that can be stored in primitive arrays
38 class Prim a where
39 -- | Yield the size in bytes of a 'ByteArray#' which can store @n@ elements
40 size# :: a -- ^ Dummy type parameter, never evaluated
41 -> Int# -- ^ Number of elements
42 -> Int#
43
44 -- | Indexing
45 at# :: ByteArray# -> Int# -> a
46
47 -- | Yield the element at the given position
48 read# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, a #)
49
50 -- | Store the given element at the given position
51 write# :: MutableByteArray# s -> Int# -> a -> State# s -> State# s
52
53 -- FIXME: use Template Haskell as soon as it properly supports unboxed types
54 -- and especially tuples
55 #define derivePrim(ty, ctr, scale, idx, rd, wr) \
56 instance Prim ty where { \
57 size# _ = scale \
58 ; at# arr# i# = ctr (idx arr# i#) \
59 ; read# arr# i# s# = case rd arr# i# s# of \
60 { (# s1#, x# #) -> (# s1#, ctr x# #) } \
61 ; write# arr# i# (ctr x#) s# = wr arr# i# x# s# }
62
63 derivePrim(Word, W#, wORD_SCALE,
64 indexWordArray#, readWordArray#, writeWordArray#)
65 derivePrim(Word8, W8#, (\n# -> n#),
66 indexWord8Array#, readWord8Array#, writeWord8Array#)
67 derivePrim(Word16, W16#, (*# 2#),
68 indexWord16Array#, readWord16Array#, writeWord16Array#)
69 derivePrim(Word32, W32#, (*# 4#),
70 indexWord32Array#, readWord32Array#, writeWord32Array#)
71 derivePrim(Word64, W64#, (*# 8#),
72 indexWord64Array#, readWord64Array#, writeWord64Array#)
73 derivePrim(Int, I#, wORD_SCALE,
74 indexIntArray#, readIntArray#, writeIntArray#)
75 derivePrim(Int8, I8#, (\n# -> n#),
76 indexInt8Array#, readInt8Array#, writeInt8Array#)
77 derivePrim(Int16, I16#, (*# 2#),
78 indexInt16Array#, readInt16Array#, writeInt16Array#)
79 derivePrim(Int32, I32#, (*# 4#),
80 indexInt32Array#, readInt32Array#, writeInt32Array#)
81 derivePrim(Int64, I64#, (*# 8#),
82 indexInt64Array#, readInt64Array#, writeInt64Array#)
83 derivePrim(Float, F#, fLOAT_SCALE,
84 indexFloatArray#, readFloatArray#, writeFloatArray#)
85 derivePrim(Double, D#, dOUBLE_SCALE,
86 indexDoubleArray#, readDoubleArray#, writeDoubleArray#)
87 derivePrim(Char, C#, (*# 4#),
88 indexWideCharArray#, readWideCharArray#, writeWideCharArray#)
89