Revert rename of dph-common for now
[packages/dph.git] / dph-common / Data / Array / Parallel / Prelude / Word8.hs
1 {-# LANGUAGE ParallelArrays #-}
2 {-# OPTIONS_GHC -fvectorise #-}
3 -- NB: Cannot use any parallel array syntax except the type constructor
4
5 module Data.Array.Parallel.Prelude.Word8 (
6 Word8,
7
8 -- Ord
9 (==), (/=), (<), (<=), (>), (>=), min, max,
10
11 minimumP, maximumP, minIndexP, maxIndexP,
12
13 -- Num
14 (+), (-), (*), negate, abs,
15
16 sumP, productP,
17
18 -- Integral
19 div, mod, sqrt,
20
21 toInt, fromInt
22 ) where
23
24 import Data.Array.Parallel.VectDepend () -- see Note [Vectoriser dependencies] in the same module
25
26 import Data.Array.Parallel.PArr
27 import Data.Array.Parallel.Prelude.Int (Int) -- get the vectorised version
28 import Data.Array.Parallel.Lifted.Scalar
29 import Data.Array.Parallel.Lifted.Closure
30 import Data.Array.Parallel.Prelude.Bool
31
32 import Data.Word (Word8)
33 import qualified Prelude as P
34
35 infixl 7 *
36 infixl 6 +, -
37 infix 4 ==, /=, <, <=, >, >=
38 infixl 7 `div`, `mod`
39
40 {-# VECTORISE SCALAR type Word8 #-}
41
42 (==), (/=), (<), (<=), (>), (>=) :: Word8 -> Word8 -> Bool
43 (==) = (P.==)
44 {-# VECTORISE SCALAR (==) #-}
45 (/=) = (P./=)
46 {-# VECTORISE SCALAR (/=) #-}
47 (<=) = (P.<=)
48 {-# VECTORISE SCALAR (<=) #-}
49 (<) = (P.<)
50 {-# VECTORISE SCALAR (<) #-}
51 (>=) = (P.>=)
52 {-# VECTORISE SCALAR (>=) #-}
53 (>) = (P.>)
54 {-# VECTORISE SCALAR (>) #-}
55
56 min, max :: Word8 -> Word8 -> Word8
57 min = P.min
58 {-# VECTORISE SCALAR min #-}
59 max = P.max
60 {-# VECTORISE SCALAR max #-}
61
62 minimumP, maximumP :: [:Word8:] -> Word8
63 {-# NOINLINE minimumP #-}
64 minimumP a = a `indexPArr` 0
65 {-# VECTORISE minimumP = minimumP_v #-}
66 {-# NOINLINE maximumP #-}
67 maximumP a = a `indexPArr` 0
68 {-# VECTORISE maximumP = maximumP_v #-}
69
70 minimumP_v, maximumP_v:: PArray Word8 :-> Word8
71 {-# INLINE minimumP_v #-}
72 minimumP_v = closure1 (scalar_fold1 P.min) (scalar_fold1s P.min)
73 {-# NOVECTORISE minimumP_v #-}
74 {-# INLINE maximumP_v #-}
75 maximumP_v = closure1 (scalar_fold1 P.max) (scalar_fold1s P.max)
76 {-# NOVECTORISE maximumP_v #-}
77
78 minIndexP :: [:Word8:] -> Int
79 {-# NOINLINE minIndexP #-}
80 minIndexP _ = 0 -- FIXME: add proper implementation
81 {-# VECTORISE minIndexP = minIndexPA #-}
82
83 minIndexPA :: PArray Word8 :-> Int
84 {-# INLINE minIndexPA #-}
85 minIndexPA = closure1 (scalar_fold1Index min') (scalar_fold1sIndex min')
86 {-# NOVECTORISE minIndexPA #-}
87
88 min' (i,x) (j,y) | x P.<= y = (i,x)
89 | P.otherwise = (j,y)
90 {-# NOVECTORISE min' #-}
91
92 maxIndexP :: [:Word8:] -> Int
93 {-# NOINLINE maxIndexP #-}
94 maxIndexP _ = 0 -- FIXME: add proper implementation
95 {-# VECTORISE maxIndexP = maxIndexPA #-}
96
97 maxIndexPA :: PArray Word8 :-> Int
98 {-# INLINE maxIndexPA #-}
99 maxIndexPA = closure1 (scalar_fold1Index max') (scalar_fold1sIndex max')
100 {-# NOVECTORISE maxIndexPA #-}
101
102 max' (i,x) (j,y) | x P.>= y = (i,x)
103 | P.otherwise = (j,y)
104 {-# NOVECTORISE max' #-}
105
106 (+), (-), (*) :: Word8 -> Word8 -> Word8
107 (+) = (P.+)
108 {-# VECTORISE SCALAR (+) #-}
109 (-) = (P.-)
110 {-# VECTORISE SCALAR (-) #-}
111 (*) = (P.*)
112 {-# VECTORISE SCALAR (*) #-}
113
114 negate, abs :: Word8 -> Word8
115 negate = P.negate
116 {-# VECTORISE SCALAR negate #-}
117 abs = P.abs
118 {-# VECTORISE SCALAR abs #-}
119
120 sumP, productP :: [:Word8:] -> Word8
121 {-# NOINLINE sumP #-}
122 sumP a = a `indexPArr` 0
123 {-# VECTORISE sumP = sumP_v #-}
124 {-# NOINLINE productP #-}
125 productP a = a `indexPArr` 0
126 {-# VECTORISE productP = productP_v #-}
127
128 sumP_v, productP_v:: PArray Word8 :-> Word8
129 {-# INLINE sumP_v #-}
130 sumP_v = closure1 (scalar_fold (+) 0) (scalar_folds (+) 0)
131 {-# NOVECTORISE sumP_v #-}
132 {-# INLINE productP_v #-}
133 productP_v = closure1 (scalar_fold (*) 1) (scalar_folds (*) 1)
134 {-# NOVECTORISE productP_v #-}
135
136 div, mod :: Word8 -> Word8 -> Word8
137 div = P.div
138 {-# VECTORISE SCALAR div #-}
139 mod = P.mod
140 {-# VECTORISE SCALAR mod #-}
141
142 sqrt :: Word8 -> Word8
143 sqrt n = P.floor (P.sqrt (P.fromIntegral n) :: P.Double)
144 {-# VECTORISE SCALAR sqrt #-}
145
146 toInt :: Word8 -> Int
147 toInt = P.fromIntegral
148 {-# VECTORISE SCALAR toInt #-}
149
150 fromInt :: Int -> Word8
151 fromInt = P.fromIntegral
152 {-# VECTORISE SCALAR fromInt #-}