f299f1f73ab60d918e38771398ad24a0b198468d
[ghc-hetmet.git] / ghc / lib / std / PrelByteArr.lhs
1 % -----------------------------------------------------------------------------
2 % $Id: PrelByteArr.lhs,v 1.7 2000/06/30 13:39:35 simonmar Exp $
3 %
4 % (c) The University of Glasgow, 1994-2000
5 %
6
7 \section[PrelByteArr]{Module @PrelByteArr@}
8
9 Byte-arrays are flat arrays of non-pointers only.
10
11 \begin{code}
12 {-# OPTIONS -fno-implicit-prelude #-}
13
14 module PrelByteArr where
15
16 import {-# SOURCE #-} PrelErr ( error )
17 import PrelArr
18 import PrelFloat
19 import PrelList (foldl)
20 import PrelST
21 import PrelBase
22 import PrelAddr
23 import PrelGHC
24
25 \end{code}
26
27 %*********************************************************
28 %*                                                      *
29 \subsection{The @Array@ types}
30 %*                                                      *
31 %*********************************************************
32
33 \begin{code}
34 data Ix ix => ByteArray ix              = ByteArray        ix ix ByteArray#
35 data Ix ix => MutableByteArray s ix     = MutableByteArray ix ix (MutableByteArray# s)
36
37 instance CCallable (ByteArray ix)
38 instance CCallable (MutableByteArray RealWorld ix)
39         -- Note the RealWorld!  You can only ccall with MutableByteArray args
40         -- which are in the real world.  When this was missed out, the result
41         -- was that a CCallOpId had a free tyvar, and since the compiler doesn't
42         -- expect that it didn't get zonked or substituted.  Bad news.
43
44 instance Eq (MutableByteArray s ix) where
45         MutableByteArray _ _ arr1# == MutableByteArray _ _ arr2#
46                 = sameMutableByteArray# arr1# arr2#
47 \end{code}
48
49 %*********************************************************
50 %*                                                      *
51 \subsection{Operations on mutable arrays}
52 %*                                                      *
53 %*********************************************************
54
55 Idle ADR question: What's the tradeoff here between flattening these
56 datatypes into @MutableArray ix ix (MutableArray# s elt)@ and using
57 it as is?  As I see it, the former uses slightly less heap and
58 provides faster access to the individual parts of the bounds while the
59 code used has the benefit of providing a ready-made @(lo, hi)@ pair as
60 required by many array-related functions.  Which wins? Is the
61 difference significant (probably not).
62
63 Idle AJG answer: When I looked at the outputted code (though it was 2
64 years ago) it seems like you often needed the tuple, and we build
65 it frequently. Now we've got the overloading specialiser things
66 might be different, though.
67
68 \begin{code}
69 newCharArray, newIntArray, newWordArray, newAddrArray, newFloatArray, newDoubleArray
70          :: Ix ix => (ix,ix) -> ST s (MutableByteArray s ix) 
71
72 {-# SPECIALIZE newCharArray   :: IPr -> ST s (MutableByteArray s Int) #-}
73 {-# SPECIALIZE newIntArray    :: IPr -> ST s (MutableByteArray s Int) #-}
74 {-# SPECIALIZE newWordArray   :: IPr -> ST s (MutableByteArray s Int) #-}
75 {-# SPECIALIZE newAddrArray   :: IPr -> ST s (MutableByteArray s Int) #-}
76 {-# SPECIALIZE newFloatArray  :: IPr -> ST s (MutableByteArray s Int) #-}
77 {-# SPECIALIZE newDoubleArray :: IPr -> ST s (MutableByteArray s Int) #-}
78
79 newCharArray (l,u) = ST $ \ s# ->
80     case rangeSize (l,u)          of { I# n# ->
81     case (newCharArray# n# s#)    of { (# s2#, barr# #) ->
82     (# s2#, MutableByteArray l u barr# #) }}
83
84 newIntArray (l,u) = ST $ \ s# ->
85     case rangeSize (l,u)          of { I# n# ->
86     case (newIntArray# n# s#)     of { (# s2#, barr# #) ->
87     (# s2#, MutableByteArray l u barr# #) }}
88
89 newWordArray (l,u) = ST $ \ s# ->
90     case rangeSize (l,u)          of { I# n# ->
91     case (newWordArray# n# s#)    of { (# s2#, barr# #) ->
92     (# s2#, MutableByteArray l u barr# #) }}
93
94 newAddrArray (l,u) = ST $ \ s# ->
95     case rangeSize (l,u)          of { I# n# ->
96     case (newAddrArray# n# s#)    of { (# s2#, barr# #) ->
97     (# s2#, MutableByteArray l u barr# #) }}
98
99 newFloatArray (l,u) = ST $ \ s# ->
100     case rangeSize (l,u)          of { I# n# ->
101     case (newFloatArray# n# s#)   of { (# s2#, barr# #) ->
102     (# s2#, MutableByteArray l u barr# #) }}
103
104 newDoubleArray (l,u) = ST $ \ s# ->
105     case rangeSize (l,u)          of { I# n# ->
106     case (newDoubleArray# n# s#)  of { (# s2#, barr# #) ->
107     (# s2#, MutableByteArray l u barr# #) }}
108
109
110 readCharArray   :: Ix ix => MutableByteArray s ix -> ix -> ST s Char 
111 readIntArray    :: Ix ix => MutableByteArray s ix -> ix -> ST s Int
112 readWordArray   :: Ix ix => MutableByteArray s ix -> ix -> ST s Word
113 readAddrArray   :: Ix ix => MutableByteArray s ix -> ix -> ST s Addr
114 readFloatArray  :: Ix ix => MutableByteArray s ix -> ix -> ST s Float
115 readDoubleArray :: Ix ix => MutableByteArray s ix -> ix -> ST s Double
116
117 {-# SPECIALIZE readCharArray   :: MutableByteArray s Int -> Int -> ST s Char #-}
118 {-# SPECIALIZE readIntArray    :: MutableByteArray s Int -> Int -> ST s Int #-}
119 {-# SPECIALIZE readAddrArray   :: MutableByteArray s Int -> Int -> ST s Addr #-}
120 --NO:{-# SPECIALIZE readFloatArray  :: MutableByteArray s Int -> Int -> ST s Float #-}
121 {-# SPECIALIZE readDoubleArray :: MutableByteArray s Int -> Int -> ST s Double #-}
122
123 readCharArray (MutableByteArray l u barr#) n = ST $ \ s# ->
124     case (index (l,u) n)                of { I# n# ->
125     case readCharArray# barr# n# s#     of { (# s2#, r# #) ->
126     (# s2#, C# r# #) }}
127
128 readIntArray (MutableByteArray l u barr#) n = ST $ \ s# ->
129     case (index (l,u) n)                of { I# n# ->
130     case readIntArray# barr# n# s#      of { (# s2#, r# #) ->
131     (# s2#, I# r# #) }}
132
133 readWordArray (MutableByteArray l u barr#) n = ST $ \ s# ->
134     case (index (l,u) n)                of { I# n# ->
135     case readWordArray# barr# n# s#     of { (# s2#, r# #) ->
136     (# s2#, W# r# #) }}
137
138 readAddrArray (MutableByteArray l u barr#) n = ST $ \ s# ->
139     case (index (l,u) n)                of { I# n# ->
140     case readAddrArray# barr# n# s#     of { (# s2#, r# #) ->
141     (# s2#, A# r# #) }}
142
143 readFloatArray (MutableByteArray l u barr#) n = ST $ \ s# ->
144     case (index (l,u) n)                of { I# n# ->
145     case readFloatArray# barr# n# s#    of { (# s2#, r# #) ->
146     (# s2#, F# r# #) }}
147
148 readDoubleArray (MutableByteArray l u barr#) n = ST $ \ s# ->
149     case (index (l,u) n)                of { I# n# ->
150     case readDoubleArray# barr# n# s#   of { (# s2#, r# #) ->
151     (# s2#, D# r# #) }}
152
153 --Indexing of ordinary @Arrays@ is standard Haskell and isn't defined here.
154 indexCharArray   :: Ix ix => ByteArray ix -> ix -> Char 
155 indexIntArray    :: Ix ix => ByteArray ix -> ix -> Int
156 indexWordArray   :: Ix ix => ByteArray ix -> ix -> Word
157 indexAddrArray   :: Ix ix => ByteArray ix -> ix -> Addr
158 indexFloatArray  :: Ix ix => ByteArray ix -> ix -> Float
159 indexDoubleArray :: Ix ix => ByteArray ix -> ix -> Double
160
161 {-# SPECIALIZE indexCharArray   :: ByteArray Int -> Int -> Char #-}
162 {-# SPECIALIZE indexIntArray    :: ByteArray Int -> Int -> Int #-}
163 {-# SPECIALIZE indexAddrArray   :: ByteArray Int -> Int -> Addr #-}
164 --NO:{-# SPECIALIZE indexFloatArray  :: ByteArray Int -> Int -> Float #-}
165 {-# SPECIALIZE indexDoubleArray :: ByteArray Int -> Int -> Double #-}
166
167 indexCharArray (ByteArray l u barr#) n
168   = case (index (l,u) n)                of { I# n# ->
169     case indexCharArray# barr# n#       of { r# ->
170     (C# r#)}}
171
172 indexIntArray (ByteArray l u barr#) n
173   = case (index (l,u) n)                of { I# n# ->
174     case indexIntArray# barr# n#        of { r# ->
175     (I# r#)}}
176
177 indexWordArray (ByteArray l u barr#) n
178   = case (index (l,u) n)                of { I# n# ->
179     case indexWordArray# barr# n#       of { r# ->
180     (W# r#)}}
181
182 indexAddrArray (ByteArray l u barr#) n
183   = case (index (l,u) n)                of { I# n# ->
184     case indexAddrArray# barr# n#       of { r# ->
185     (A# r#)}}
186
187 indexFloatArray (ByteArray l u barr#) n
188   = case (index (l,u) n)                of { I# n# ->
189     case indexFloatArray# barr# n#      of { r# ->
190     (F# r#)}}
191
192 indexDoubleArray (ByteArray l u barr#) n
193   = case (index (l,u) n)                of { I# n# ->
194     case indexDoubleArray# barr# n#     of { r# ->
195     (D# r#)}}
196
197 writeCharArray   :: Ix ix => MutableByteArray s ix -> ix -> Char -> ST s () 
198 writeIntArray    :: Ix ix => MutableByteArray s ix -> ix -> Int  -> ST s () 
199 writeWordArray   :: Ix ix => MutableByteArray s ix -> ix -> Word -> ST s () 
200 writeAddrArray   :: Ix ix => MutableByteArray s ix -> ix -> Addr -> ST s () 
201 writeFloatArray  :: Ix ix => MutableByteArray s ix -> ix -> Float -> ST s () 
202 writeDoubleArray :: Ix ix => MutableByteArray s ix -> ix -> Double -> ST s () 
203
204 {-# SPECIALIZE writeCharArray   :: MutableByteArray s Int -> Int -> Char -> ST s () #-}
205 {-# SPECIALIZE writeIntArray    :: MutableByteArray s Int -> Int -> Int  -> ST s () #-}
206 {-# SPECIALIZE writeAddrArray   :: MutableByteArray s Int -> Int -> Addr -> ST s () #-}
207 --NO:{-# SPECIALIZE writeFloatArray  :: MutableByteArray s Int -> Int -> Float -> ST s () #-}
208 {-# SPECIALIZE writeDoubleArray :: MutableByteArray s Int -> Int -> Double -> ST s () #-}
209
210 writeCharArray (MutableByteArray l u barr#) n (C# ele) = ST $ \ s# ->
211     case index (l,u) n                      of { I# n# ->
212     case writeCharArray# barr# n# ele s#    of { s2#   ->
213     (# s2#, () #) }}
214
215 writeIntArray (MutableByteArray l u barr#) n (I# ele) = ST $ \ s# ->
216     case index (l,u) n                      of { I# n# ->
217     case writeIntArray# barr# n# ele s#     of { s2#   ->
218     (# s2#, () #) }}
219
220 writeWordArray (MutableByteArray l u barr#) n (W# ele) = ST $ \ s# ->
221     case index (l,u) n                      of { I# n# ->
222     case writeWordArray# barr# n# ele s#    of { s2#   ->
223     (# s2#, () #) }}
224
225 writeAddrArray (MutableByteArray l u barr#) n (A# ele) = ST $ \ s# ->
226     case index (l,u) n                      of { I# n# ->
227     case writeAddrArray# barr# n# ele s#    of { s2#   ->
228     (# s2#, () #) }}
229
230 writeFloatArray (MutableByteArray l u barr#) n (F# ele) = ST $ \ s# ->
231     case index (l,u) n                      of { I# n# ->
232     case writeFloatArray# barr# n# ele s#   of { s2#   ->
233     (# s2#, () #) }}
234
235 writeDoubleArray (MutableByteArray l u barr#) n (D# ele) = ST $ \ s# ->
236     case index (l,u) n                      of { I# n# ->
237     case writeDoubleArray# barr# n# ele s#  of { s2#   ->
238     (# s2#, () #) }}
239 \end{code}