Adapted to simplified VECTORISE pragmas
[packages/dph.git] / dph-lifted-vseg / 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 , maximumP, minimumP
9 , maxIndexP, minIndexP
10
11 -- * Num
12 , (+), (-), (*)
13 , negate, abs
14 , sumP, productP
15
16 -- * Integral
17 , div, mod, sqrt
18
19 -- * Conversion
20 , fromInt
21 , toInt)
22 where
23 import Data.Array.Parallel.Prim ()
24 import Data.Array.Parallel.Prelude.Base (Bool)
25 import Data.Array.Parallel.PArr
26 import Data.Array.Parallel.PArray
27 import Data.Array.Parallel.Lifted ((:->)(..))
28 import qualified Data.Array.Parallel.Lifted as L
29 import qualified Data.Array.Parallel.PArray.Scalar as SC
30 import qualified Prelude as P
31 import Prelude (Int)
32
33 import Data.Word (Word8)
34
35 infixl 7 *
36 infixl 6 +, -
37 infix 4 ==, /=, <, <=, >, >=
38 infixl 7 `div`, `mod`
39
40 -- Ord ------------------------------------------------------------------------
41 (==), (/=), (<), (<=), (>), (>=) :: Word8 -> Word8 -> Bool
42 (==) = (P.==)
43 (/=) = (P./=)
44 (<=) = (P.<=)
45 (<) = (P.<)
46 (>=) = (P.>=)
47 (>) = (P.>)
48
49 -- min/max ----------------------------
50 min, max :: Word8 -> Word8 -> Word8
51 min = P.min
52 max = P.max
53
54 -- minimum/maximum --------------------
55 minimumP, maximumP :: PArr Word8 -> Word8
56
57 minimumP arr = headPArr arr
58 {-# NOINLINE minimumP #-}
59 {-# VECTORISE minimumP = minimumPP #-}
60
61 maximumP arr = headPArr arr
62 {-# NOINLINE maximumP #-}
63 {-# VECTORISE maximumP = maximumPP #-}
64
65 minimumPP, maximumPP :: PArray Word8 :-> Word8
66 minimumPP = L.closure1' (SC.fold1 P.min) (SC.fold1s P.min)
67 {-# INLINE minimumPP #-}
68 {-# NOVECTORISE minimumPP #-}
69
70 maximumPP = L.closure1' (SC.fold1 P.max) (SC.fold1s P.max)
71 {-# INLINE maximumPP #-}
72 {-# NOVECTORISE maximumPP #-}
73
74
75 -- minIndex/maxIndex ------------------
76 minIndexP :: PArr Word8 -> Int
77 minIndexP !_ = 0
78 {-# NOINLINE minIndexP #-}
79 {-# VECTORISE minIndexP = minIndexPP #-}
80
81 minIndexPP :: PArray Word8 :-> Int
82 minIndexPP = L.closure1' (SC.fold1Index min') (SC.fold1sIndex min')
83 {-# INLINE minIndexPP #-}
84 {-# NOVECTORISE minIndexPP #-}
85
86 min' :: P.Ord b => (a, b) -> (a, b) -> (a, b)
87 min' (i,x) (j,y) | x P.<= y = (i,x)
88 | P.otherwise = (j,y)
89 {-# NOVECTORISE min' #-}
90
91
92 maxIndexP :: PArr Word8 -> Int
93 maxIndexP _ = 0
94 {-# NOINLINE maxIndexP #-}
95 {-# VECTORISE maxIndexP = maxIndexPP #-}
96
97 maxIndexPP :: PArray Word8 :-> Int
98 maxIndexPP = L.closure1' (SC.fold1Index max') (SC.fold1sIndex max')
99 {-# INLINE maxIndexPP #-}
100 {-# NOVECTORISE maxIndexPP #-}
101
102 max' :: P.Ord b => (a, b) -> (a, b) -> (a, b)
103 max' (i,x) (j,y) | x P.>= y = (i,x)
104 | P.otherwise = (j,y)
105 {-# NOVECTORISE max' #-}
106
107
108 -- Num ------------------------------------------------------------------------
109 (+), (-), (*) :: Word8 -> Word8 -> Word8
110 (+) = (P.+)
111 (-) = (P.-)
112 (*) = (P.*)
113
114 -- negate/abs -------------------------
115 negate, abs :: Word8 -> Word8
116 negate = P.negate
117 abs = P.abs
118
119 -- sum/product ------------------------
120 sumP, productP :: PArr Word8 -> Word8
121
122 sumP arr = headPArr arr
123 {-# NOINLINE sumP #-}
124 {-# VECTORISE sumP = sumPP #-}
125
126 productP arr = headPArr arr
127 {-# NOINLINE productP #-}
128 {-# VECTORISE productP = productPP #-}
129
130 sumPP, productPP :: PArray Word8 :-> Word8
131 sumPP = L.closure1' (SC.fold (+) 0) (SC.folds (+) 0)
132 {-# INLINE sumPP #-}
133 {-# NOVECTORISE sumPP #-}
134
135 productPP = L.closure1' (SC.fold (*) 1) (SC.folds (*) 1)
136 {-# INLINE productPP #-}
137 {-# NOVECTORISE productPP #-}
138
139
140 -- Integral -------------------------------------------------------------------
141 div, mod :: Word8 -> Word8 -> Word8
142 div = P.div
143 mod = P.mod
144
145 sqrt :: Word8 -> Word8
146 sqrt n = P.floor (P.sqrt (P.fromIntegral n) :: P.Double)
147
148 -- Conversion -----------------------------------------------------------------
149 toInt :: Word8 -> Int
150 toInt = P.fromIntegral
151
152 fromInt :: Int -> Word8
153 fromInt = P.fromIntegral