2 % (c) The AQUA Project, Glasgow University, 1994-1996
4 \section[PrelByteArr]{Module @PrelByteArr@}
6 Byte-arrays are flat arrays of non-pointers only.
9 {-# OPTIONS -fno-implicit-prelude #-}
11 module PrelByteArr where
13 import {-# SOURCE #-} PrelErr ( error )
16 import PrelList (foldl)
24 %*********************************************************
26 \subsection{The @Array@ types}
28 %*********************************************************
31 data Ix ix => ByteArray ix = ByteArray ix ix ByteArray#
32 data Ix ix => MutableByteArray s ix = MutableByteArray ix ix (MutableByteArray# s)
34 instance CCallable (ByteArray ix)
35 instance CCallable (MutableByteArray RealWorld ix)
36 -- Note the RealWorld! You can only ccall with MutableByteArray args
37 -- which are in the real world. When this was missed out, the result
38 -- was that a CCallOpId had a free tyvar, and since the compiler doesn't
39 -- expect that it didn't get zonked or substituted. Bad news.
41 instance Eq (MutableByteArray s ix) where
42 MutableByteArray _ _ arr1# == MutableByteArray _ _ arr2#
43 = sameMutableByteArray# arr1# arr2#
46 %*********************************************************
48 \subsection{Operations on mutable arrays}
50 %*********************************************************
52 Idle ADR question: What's the tradeoff here between flattening these
53 datatypes into @MutableArray ix ix (MutableArray# s elt)@ and using
54 it as is? As I see it, the former uses slightly less heap and
55 provides faster access to the individual parts of the bounds while the
56 code used has the benefit of providing a ready-made @(lo, hi)@ pair as
57 required by many array-related functions. Which wins? Is the
58 difference significant (probably not).
60 Idle AJG answer: When I looked at the outputted code (though it was 2
61 years ago) it seems like you often needed the tuple, and we build
62 it frequently. Now we've got the overloading specialiser things
63 might be different, though.
66 newCharArray, newIntArray, newWordArray, newAddrArray, newFloatArray, newDoubleArray
67 :: Ix ix => (ix,ix) -> ST s (MutableByteArray s ix)
69 {-# SPECIALIZE newCharArray :: IPr -> ST s (MutableByteArray s Int) #-}
70 {-# SPECIALIZE newIntArray :: IPr -> ST s (MutableByteArray s Int) #-}
71 {-# SPECIALIZE newWordArray :: IPr -> ST s (MutableByteArray s Int) #-}
72 {-# SPECIALIZE newAddrArray :: IPr -> ST s (MutableByteArray s Int) #-}
73 {-# SPECIALIZE newFloatArray :: IPr -> ST s (MutableByteArray s Int) #-}
74 {-# SPECIALIZE newDoubleArray :: IPr -> ST s (MutableByteArray s Int) #-}
76 newCharArray (l,u) = ST $ \ s# ->
77 case rangeSize (l,u) of { I# n# ->
78 case (newCharArray# n# s#) of { (# s2#, barr# #) ->
79 (# s2#, MutableByteArray l u barr# #) }}
81 newIntArray (l,u) = ST $ \ s# ->
82 case rangeSize (l,u) of { I# n# ->
83 case (newIntArray# n# s#) of { (# s2#, barr# #) ->
84 (# s2#, MutableByteArray l u barr# #) }}
86 newWordArray (l,u) = ST $ \ s# ->
87 case rangeSize (l,u) of { I# n# ->
88 case (newWordArray# n# s#) of { (# s2#, barr# #) ->
89 (# s2#, MutableByteArray l u barr# #) }}
91 newAddrArray (l,u) = ST $ \ s# ->
92 case rangeSize (l,u) of { I# n# ->
93 case (newAddrArray# n# s#) of { (# s2#, barr# #) ->
94 (# s2#, MutableByteArray l u barr# #) }}
96 newFloatArray (l,u) = ST $ \ s# ->
97 case rangeSize (l,u) of { I# n# ->
98 case (newFloatArray# n# s#) of { (# s2#, barr# #) ->
99 (# s2#, MutableByteArray l u barr# #) }}
101 newDoubleArray (l,u) = ST $ \ s# ->
102 case rangeSize (l,u) of { I# n# ->
103 case (newDoubleArray# n# s#) of { (# s2#, barr# #) ->
104 (# s2#, MutableByteArray l u barr# #) }}
107 readCharArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Char
108 readIntArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Int
109 readWordArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Word
110 readAddrArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Addr
111 readFloatArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Float
112 readDoubleArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Double
114 {-# SPECIALIZE readCharArray :: MutableByteArray s Int -> Int -> ST s Char #-}
115 {-# SPECIALIZE readIntArray :: MutableByteArray s Int -> Int -> ST s Int #-}
116 {-# SPECIALIZE readAddrArray :: MutableByteArray s Int -> Int -> ST s Addr #-}
117 --NO:{-# SPECIALIZE readFloatArray :: MutableByteArray s Int -> Int -> ST s Float #-}
118 {-# SPECIALIZE readDoubleArray :: MutableByteArray s Int -> Int -> ST s Double #-}
120 readCharArray (MutableByteArray l u barr#) n = ST $ \ s# ->
121 case (index (l,u) n) of { I# n# ->
122 case readCharArray# barr# n# s# of { (# s2#, r# #) ->
125 readIntArray (MutableByteArray l u barr#) n = ST $ \ s# ->
126 case (index (l,u) n) of { I# n# ->
127 case readIntArray# barr# n# s# of { (# s2#, r# #) ->
130 readWordArray (MutableByteArray l u barr#) n = ST $ \ s# ->
131 case (index (l,u) n) of { I# n# ->
132 case readWordArray# barr# n# s# of { (# s2#, r# #) ->
135 readAddrArray (MutableByteArray l u barr#) n = ST $ \ s# ->
136 case (index (l,u) n) of { I# n# ->
137 case readAddrArray# barr# n# s# of { (# s2#, r# #) ->
140 readFloatArray (MutableByteArray l u barr#) n = ST $ \ s# ->
141 case (index (l,u) n) of { I# n# ->
142 case readFloatArray# barr# n# s# of { (# s2#, r# #) ->
145 readDoubleArray (MutableByteArray l u barr#) n = ST $ \ s# ->
146 case (index (l,u) n) of { I# n# ->
147 case readDoubleArray# barr# n# s# of { (# s2#, r# #) ->
150 --Indexing of ordinary @Arrays@ is standard Haskell and isn't defined here.
151 indexCharArray :: Ix ix => ByteArray ix -> ix -> Char
152 indexIntArray :: Ix ix => ByteArray ix -> ix -> Int
153 indexWordArray :: Ix ix => ByteArray ix -> ix -> Word
154 indexAddrArray :: Ix ix => ByteArray ix -> ix -> Addr
155 indexFloatArray :: Ix ix => ByteArray ix -> ix -> Float
156 indexDoubleArray :: Ix ix => ByteArray ix -> ix -> Double
158 {-# SPECIALIZE indexCharArray :: ByteArray Int -> Int -> Char #-}
159 {-# SPECIALIZE indexIntArray :: ByteArray Int -> Int -> Int #-}
160 {-# SPECIALIZE indexAddrArray :: ByteArray Int -> Int -> Addr #-}
161 --NO:{-# SPECIALIZE indexFloatArray :: ByteArray Int -> Int -> Float #-}
162 {-# SPECIALIZE indexDoubleArray :: ByteArray Int -> Int -> Double #-}
164 indexCharArray (ByteArray l u barr#) n
165 = case (index (l,u) n) of { I# n# ->
166 case indexCharArray# barr# n# of { r# ->
169 indexIntArray (ByteArray l u barr#) n
170 = case (index (l,u) n) of { I# n# ->
171 case indexIntArray# barr# n# of { r# ->
174 indexWordArray (ByteArray l u barr#) n
175 = case (index (l,u) n) of { I# n# ->
176 case indexWordArray# barr# n# of { r# ->
179 indexAddrArray (ByteArray l u barr#) n
180 = case (index (l,u) n) of { I# n# ->
181 case indexAddrArray# barr# n# of { r# ->
184 indexFloatArray (ByteArray l u barr#) n
185 = case (index (l,u) n) of { I# n# ->
186 case indexFloatArray# barr# n# of { r# ->
189 indexDoubleArray (ByteArray l u barr#) n
190 = case (index (l,u) n) of { I# n# ->
191 case indexDoubleArray# barr# n# of { r# ->
194 writeCharArray :: Ix ix => MutableByteArray s ix -> ix -> Char -> ST s ()
195 writeIntArray :: Ix ix => MutableByteArray s ix -> ix -> Int -> ST s ()
196 writeWordArray :: Ix ix => MutableByteArray s ix -> ix -> Word -> ST s ()
197 writeAddrArray :: Ix ix => MutableByteArray s ix -> ix -> Addr -> ST s ()
198 writeFloatArray :: Ix ix => MutableByteArray s ix -> ix -> Float -> ST s ()
199 writeDoubleArray :: Ix ix => MutableByteArray s ix -> ix -> Double -> ST s ()
201 {-# SPECIALIZE writeCharArray :: MutableByteArray s Int -> Int -> Char -> ST s () #-}
202 {-# SPECIALIZE writeIntArray :: MutableByteArray s Int -> Int -> Int -> ST s () #-}
203 {-# SPECIALIZE writeAddrArray :: MutableByteArray s Int -> Int -> Addr -> ST s () #-}
204 --NO:{-# SPECIALIZE writeFloatArray :: MutableByteArray s Int -> Int -> Float -> ST s () #-}
205 {-# SPECIALIZE writeDoubleArray :: MutableByteArray s Int -> Int -> Double -> ST s () #-}
207 writeCharArray (MutableByteArray l u barr#) n (C# ele) = ST $ \ s# ->
208 case index (l,u) n of { I# n# ->
209 case writeCharArray# barr# n# ele s# of { s2# ->
212 writeIntArray (MutableByteArray l u barr#) n (I# ele) = ST $ \ s# ->
213 case index (l,u) n of { I# n# ->
214 case writeIntArray# barr# n# ele s# of { s2# ->
217 writeWordArray (MutableByteArray l u barr#) n (W# ele) = ST $ \ s# ->
218 case index (l,u) n of { I# n# ->
219 case writeWordArray# barr# n# ele s# of { s2# ->
222 writeAddrArray (MutableByteArray l u barr#) n (A# ele) = ST $ \ s# ->
223 case index (l,u) n of { I# n# ->
224 case writeAddrArray# barr# n# ele s# of { s2# ->
227 writeFloatArray (MutableByteArray l u barr#) n (F# ele) = ST $ \ s# ->
228 case index (l,u) n of { I# n# ->
229 case writeFloatArray# barr# n# ele s# of { s2# ->
232 writeDoubleArray (MutableByteArray l u barr#) n (D# ele) = ST $ \ s# ->
233 case index (l,u) n of { I# n# ->
234 case writeDoubleArray# barr# n# ele s# of { s2# ->