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