259bec07ba395ff5e0755df5f0a9d78d59ba5dff
[packages/dph.git] / dph-lifted-vseg / Data / Array / Parallel / Prelude / Int.hs
1 {-# OPTIONS_GHC -fvectorise #-}
2
3 module Data.Array.Parallel.Prelude.Int
4 ( Int
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 -- * Enum
20 , enumFromToP)
21 where
22 -- Primitives needed by the vectoriser.
23 import Data.Array.Parallel.Prim ()
24 import Data.Array.Parallel.Prelude.Base (Bool, Int, Eq, Ord, Num)
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
32
33 {-# VECTORISE SCALAR instance Eq Int #-}
34 {-# VECTORISE SCALAR instance Ord Int #-}
35 {-# VECTORISE SCALAR instance Num Int #-}
36
37
38 infixl 7 *
39 infixl 6 +, -
40 infix 4 ==, /=, <, <=, >, >=
41 infixl 7 `div`, `mod`
42
43 -- Ord ------------------------------------------------------------------------
44 (==), (/=), (<), (<=), (>), (>=) :: Int -> Int -> Bool
45
46 (==) = (P.==)
47 {-# VECTORISE SCALAR (==) #-}
48
49 (/=) = (P./=)
50 {-# VECTORISE SCALAR (/=) #-}
51
52 (<=) = (P.<=)
53 {-# VECTORISE SCALAR (<=) #-}
54
55 (<) = (P.<)
56 {-# VECTORISE SCALAR (<) #-}
57
58 (>=) = (P.>=)
59 {-# VECTORISE SCALAR (>=) #-}
60
61 (>) = (P.>)
62 {-# VECTORISE SCALAR (>) #-}
63
64
65 -- min/max ----------------------------
66 min, max :: Int -> Int -> Int
67
68 min = P.min
69 {-# VECTORISE SCALAR min #-}
70
71 max = P.max
72 {-# VECTORISE SCALAR max #-}
73
74
75 -- minimum/maximum --------------------
76 minimumP, maximumP :: PArr Int -> Int
77
78 minimumP arr = headPArr arr
79 {-# NOINLINE minimumP #-}
80 {-# VECTORISE minimumP = minimumPP #-}
81
82 maximumP arr = headPArr arr
83 {-# NOINLINE maximumP #-}
84 {-# VECTORISE maximumP = maximumPP #-}
85
86 minimumPP, maximumPP :: PArray Int :-> Int
87 minimumPP = L.closure1' (SC.fold1 P.min) (SC.fold1s P.min)
88 {-# INLINE minimumPP #-}
89 {-# NOVECTORISE minimumPP #-}
90
91 maximumPP = L.closure1' (SC.fold1 P.max) (SC.fold1s P.max)
92 {-# INLINE maximumPP #-}
93 {-# NOVECTORISE maximumPP #-}
94
95
96 -- minIndex/maxIndex ------------------
97 minIndexP :: PArr Int -> Int
98 minIndexP !_ = 0
99 {-# NOINLINE minIndexP #-}
100 {-# VECTORISE minIndexP = minIndexPP #-}
101
102 minIndexPP :: PArray Int :-> Int
103 minIndexPP = L.closure1' (SC.fold1Index min') (SC.fold1sIndex min')
104 {-# INLINE minIndexPP #-}
105 {-# NOVECTORISE minIndexPP #-}
106
107 min' :: P.Ord b => (a, b) -> (a, b) -> (a, b)
108 min' (i,x) (j,y) | x P.<= y = (i,x)
109 | P.otherwise = (j,y)
110 {-# NOVECTORISE min' #-}
111
112
113 maxIndexP :: PArr Int -> Int
114 maxIndexP _ = 0
115 {-# NOINLINE maxIndexP #-}
116 {-# VECTORISE maxIndexP = maxIndexPP #-}
117
118 maxIndexPP :: PArray Int :-> Int
119 maxIndexPP = L.closure1' (SC.fold1Index max') (SC.fold1sIndex max')
120 {-# INLINE maxIndexPP #-}
121 {-# NOVECTORISE maxIndexPP #-}
122
123 max' :: P.Ord b => (a, b) -> (a, b) -> (a, b)
124 max' (i,x) (j,y) | x P.>= y = (i,x)
125 | P.otherwise = (j,y)
126 {-# NOVECTORISE max' #-}
127
128
129 -- Num ------------------------------------------------------------------------
130 (+), (-), (*) :: Int -> Int -> Int
131
132 (+) = (P.+)
133 {-# VECTORISE SCALAR (+) #-}
134
135 (-) = (P.-)
136 {-# VECTORISE SCALAR (-) #-}
137
138 (*) = (P.*)
139 {-# VECTORISE SCALAR (*) #-}
140
141
142 -- negate/abs -------------------------
143 negate, abs :: Int -> Int
144
145 negate = P.negate
146 {-# VECTORISE SCALAR negate #-}
147
148 abs = P.abs
149 {-# VECTORISE SCALAR abs #-}
150
151
152 -- sum/product ------------------------
153 sumP, productP :: PArr Int -> Int
154
155 sumP arr = headPArr arr
156 {-# NOINLINE sumP #-}
157 {-# VECTORISE sumP = sumPP #-}
158
159 productP arr = headPArr arr
160 {-# NOINLINE productP #-}
161 {-# VECTORISE productP = productPP #-}
162
163 sumPP, productPP :: PArray Int :-> Int
164 sumPP = L.closure1' (SC.fold (+) 0) (SC.folds (+) 0)
165 {-# INLINE sumPP #-}
166 {-# NOVECTORISE sumPP #-}
167
168 productPP = L.closure1' (SC.fold (*) 1) (SC.folds (*) 1)
169 {-# INLINE productPP #-}
170 {-# NOVECTORISE productPP #-}
171
172
173 -- Integral -------------------------------------------------------------------
174 div, mod :: Int -> Int -> Int
175
176 div = P.div
177 {-# VECTORISE SCALAR div #-}
178
179 mod = P.mod
180 {-# VECTORISE SCALAR mod #-}
181
182
183 sqrt :: Int -> Int
184 sqrt n = P.floor (P.sqrt (P.fromIntegral n) :: P.Double)
185 {-# VECTORISE SCALAR sqrt #-}
186
187
188 -- Enum -----------------------------------------------------------------------
189 enumFromToP :: Int -> Int -> PArr Int
190 enumFromToP !_ !_ = emptyPArr
191 {-# NOINLINE enumFromToP #-}
192 {-# VECTORISE enumFromToP = enumFromToPP #-}
193
194 enumFromToPP :: Int :-> Int :-> PArray Int
195 enumFromToPP = L.closure2' SC.enumFromTo SC.enumFromTol
196 {-# INLINE enumFromToPP #-}
197 {-# NOVECTORISE enumFromToPP #-}