0 | module Data.NumIdr.Array.Rep
19 | data Order : Type where
32 | COrder == COrder = True
33 | FOrder == FOrder = True
34 | COrder == FOrder = False
35 | FOrder == COrder = False
40 | calcStrides : Order -> Vect rk Nat -> Vect rk Nat
41 | calcStrides _ [] = []
42 | calcStrides COrder v@(_::_) = scanr (*) 1 $
tail v
43 | calcStrides FOrder v@(_::_) = scanl (*) 1 $
init v
56 | data Rep : Type where
64 | Bytes : (o : Order) -> Rep
71 | Boxed : (o : Order) -> Rep
109 | Bytes o == Bytes o' = o == o'
110 | Boxed o == Boxed o' = o == o'
111 | Linked == Linked = True
112 | Delayed == Delayed = True
118 | data LinearRep : Rep -> Type where
119 | BytesIsL : LinearRep (Bytes o)
120 | BoxedIsL : LinearRep (Boxed o)
124 | forceRepNC : Rep -> Rep
125 | forceRepNC (Bytes o) = Boxed o
129 | mergeRep : Rep -> Rep -> Rep
130 | mergeRep r r' = if r == Delayed || r' == Delayed then Delayed else r
133 | mergeRepNC : Rep -> Rep -> Rep
135 | if r == Delayed || r' == Delayed then Delayed
137 | Bytes _ => case r' of
143 | data NoConstraintRep : Rep -> Type where
144 | BoxedNC : NoConstraintRep (Boxed o)
145 | LinkedNC : NoConstraintRep Linked
146 | DelayedNC : NoConstraintRep Delayed
149 | mergeRepNCCorrect : (r, r' : Rep) -> NoConstraintRep (mergeRepNC r r')
150 | mergeRepNCCorrect Delayed _ = DelayedNC
151 | mergeRepNCCorrect (Bytes y) (Bytes x) = BoxedNC
152 | mergeRepNCCorrect (Boxed y) (Bytes x) = BoxedNC
153 | mergeRepNCCorrect Linked (Bytes x) = LinkedNC
154 | mergeRepNCCorrect (Bytes y) (Boxed x) = BoxedNC
155 | mergeRepNCCorrect (Boxed y) (Boxed x) = BoxedNC
156 | mergeRepNCCorrect Linked (Boxed x) = LinkedNC
157 | mergeRepNCCorrect (Bytes x) Linked = LinkedNC
158 | mergeRepNCCorrect (Boxed x) Linked = BoxedNC
159 | mergeRepNCCorrect Linked Linked = LinkedNC
160 | mergeRepNCCorrect (Bytes x) Delayed = DelayedNC
161 | mergeRepNCCorrect (Boxed x) Delayed = DelayedNC
162 | mergeRepNCCorrect Linked Delayed = DelayedNC
165 | forceRepNCCorrect : (r : Rep) -> NoConstraintRep (forceRepNC r)
166 | forceRepNCCorrect (Bytes x) = BoxedNC
167 | forceRepNCCorrect (Boxed x) = BoxedNC
168 | forceRepNCCorrect Linked = LinkedNC
169 | forceRepNCCorrect Delayed = DelayedNC
182 | interface ByteRep a where
187 | toBytes : a -> Vect bytes Bits8
190 | fromBytes : Vect bytes Bits8 -> a
193 | ByteRep Bits8 where
200 | ByteRep Bits16 where
203 | toBytes x = [cast (x `shiftR` 8), cast x]
204 | fromBytes [b1,b2] = cast b1 `shiftL` 8 .|. cast b2
207 | ByteRep Bits32 where
210 | toBytes x = [cast (x `shiftR` 24),
211 | cast (x `shiftR` 16),
212 | cast (x `shiftR` 8),
214 | fromBytes [b1,b2,b3,b4] =
215 | cast b1 `shiftL` 24 .|.
216 | cast b2 `shiftL` 16 .|.
217 | cast b3 `shiftL` 8 .|.
221 | ByteRep Bits64 where
224 | toBytes x = [cast (x `shiftR` 56),
225 | cast (x `shiftR` 48),
226 | cast (x `shiftR` 40),
227 | cast (x `shiftR` 32),
228 | cast (x `shiftR` 24),
229 | cast (x `shiftR` 16),
230 | cast (x `shiftR` 8),
232 | fromBytes [b1,b2,b3,b4,b5,b6,b7,b8] =
233 | cast b1 `shiftL` 56 .|.
234 | cast b2 `shiftL` 48 .|.
235 | cast b3 `shiftL` 40 .|.
236 | cast b4 `shiftL` 32 .|.
237 | cast b5 `shiftL` 24 .|.
238 | cast b6 `shiftL` 16 .|.
239 | cast b7 `shiftL` 8 .|.
246 | toBytes x = [cast (x `shiftR` 56),
247 | cast (x `shiftR` 48),
248 | cast (x `shiftR` 40),
249 | cast (x `shiftR` 32),
250 | cast (x `shiftR` 24),
251 | cast (x `shiftR` 16),
252 | cast (x `shiftR` 8),
254 | fromBytes [b1,b2,b3,b4,b5,b6,b7,b8] =
255 | cast b1 `shiftL` 56 .|.
256 | cast b2 `shiftL` 48 .|.
257 | cast b3 `shiftL` 40 .|.
258 | cast b4 `shiftL` 32 .|.
259 | cast b5 `shiftL` 24 .|.
260 | cast b6 `shiftL` 16 .|.
261 | cast b7 `shiftL` 8 .|.
268 | toBytes x = [cast x]
269 | fromBytes [x] = cast x
272 | ByteRep Int16 where
275 | toBytes x = [cast (x `shiftR` 8), cast x]
276 | fromBytes [b1,b2] = cast b1 `shiftL` 8 .|. cast b2
279 | ByteRep Int32 where
282 | toBytes x = [cast (x `shiftR` 24),
283 | cast (x `shiftR` 16),
284 | cast (x `shiftR` 8),
286 | fromBytes [b1,b2,b3,b4] =
287 | cast b1 `shiftL` 24 .|.
288 | cast b2 `shiftL` 16 .|.
289 | cast b3 `shiftL` 8 .|.
293 | ByteRep Int64 where
296 | toBytes x = [cast (x `shiftR` 56),
297 | cast (x `shiftR` 48),
298 | cast (x `shiftR` 40),
299 | cast (x `shiftR` 32),
300 | cast (x `shiftR` 24),
301 | cast (x `shiftR` 16),
302 | cast (x `shiftR` 8),
304 | fromBytes [b1,b2,b3,b4,b5,b6,b7,b8] =
305 | cast b1 `shiftL` 56 .|.
306 | cast b2 `shiftL` 48 .|.
307 | cast b3 `shiftL` 40 .|.
308 | cast b4 `shiftL` 32 .|.
309 | cast b5 `shiftL` 24 .|.
310 | cast b6 `shiftL` 16 .|.
311 | cast b7 `shiftL` 8 .|.
318 | toBytes b = [if b then 1 else 0]
319 | fromBytes [x] = x /= 0
322 | ByteRep a => ByteRep b => ByteRep (a, b) where
323 | bytes = bytes {a} + bytes {a=b}
325 | toBytes (x,y) = toBytes x ++ toBytes y
326 | fromBytes = bimap fromBytes fromBytes . splitAt _
329 | {n : _} -> ByteRep a => ByteRep (Vect n a) where
330 | bytes = n * bytes {a}
332 | toBytes xs = concat $
map toBytes xs
333 | fromBytes {n = 0} bs = []
334 | fromBytes {n = S n} bs =
335 | let (bs1, bs2) = splitAt _ bs
336 | in fromBytes bs1 :: fromBytes bs2
345 | RepConstraint : Rep -> Type -> Type
346 | RepConstraint (Bytes _) a = ByteRep a
347 | RepConstraint (Boxed _) a = ()
348 | RepConstraint Linked a = ()
349 | RepConstraint Delayed a = ()