c827c2a7c6c357607deea569b1e7840525c55d03
[packages/dph.git] / dph-common / Data / Array / Parallel / Prelude / Base / Int.hs
1 {-# LANGUAGE PArr #-}
2 module Data.Array.Parallel.Prelude.Base.Int (
3 -- Ord
4 (==), (/=), (<), (<=), (>), (>=), min, max,
5 eqV, neqV, ltV, leV, gtV, geV, minV, maxV,
6
7 minimumP, maximumP, minIndexP, maxIndexP,
8 minimumPA, maximumPA, minIndexPA, maxIndexPA,
9
10 -- Num
11 (+), (-), (*), negate, abs,
12 plusV, minusV, multV, negateV, absV,
13
14 sumP, productP,
15 sumPA, productPA,
16
17 div, divV,
18 mod, modV,
19 sqrt, sqrtV,
20
21 enumFromToPA, enumFromToP,
22 upToP, upToPA
23 ) where
24
25 import Data.Array.Parallel.Prelude.Base.PArr
26
27 import Data.Array.Parallel.Lifted.Combinators
28 import Data.Array.Parallel.Lifted.Instances
29 import Data.Array.Parallel.Lifted.Scalar
30 import Data.Array.Parallel.Lifted.Closure
31 import Data.Array.Parallel.Lifted.PArray
32
33 import Prelude (Int, Bool)
34 import qualified Prelude as P
35 import qualified GHC.PArr
36
37 infixl 7 *
38 infixl 6 +, -
39 infix 4 ==, /=, <, <=, >, >=
40 infixl 7 `div`, `mod`
41
42 eqV, neqV, leV, ltV, geV, gtV :: Int :-> Int :-> Bool
43 {-# INLINE eqV #-}
44 {-# INLINE neqV #-}
45 {-# INLINE leV #-}
46 {-# INLINE ltV #-}
47 {-# INLINE geV #-}
48 {-# INLINE gtV #-}
49 eqV = closure2 dPA_Int (P.==) (scalar_zipWith (P.==))
50 neqV = closure2 dPA_Int (P./=) (scalar_zipWith (P./=))
51 leV = closure2 dPA_Int (P.<=) (scalar_zipWith (P.<=))
52 ltV = closure2 dPA_Int (P.<) (scalar_zipWith (P.<))
53 geV = closure2 dPA_Int (P.>=) (scalar_zipWith (P.>=))
54 gtV = closure2 dPA_Int (P.>) (scalar_zipWith (P.>))
55
56 (==), (/=), (<), (<=), (>), (>=) :: Int -> Int -> Bool
57 (==) = (P.==)
58 (/=) = (P./=)
59 (<=) = (P.<=)
60 (<) = (P.<)
61 (>=) = (P.>=)
62 (>) = (P.>)
63
64 minV, maxV :: Int :-> Int :-> Int
65 {-# INLINE minV #-}
66 {-# INLINE maxV #-}
67 minV = closure2 dPA_Int P.min (scalar_zipWith P.min)
68 maxV = closure2 dPA_Int P.max (scalar_zipWith P.max)
69
70 min, max :: Int -> Int -> Int
71 min = P.min
72 max = P.max
73
74 minimumPA, maximumPA :: PArray Int :-> Int
75 {-# INLINE minimumPA #-}
76 {-# INLINE maximumPA #-}
77 minimumPA = closure1 (scalar_fold1 P.min) (scalar_fold1s P.min)
78 maximumPA = closure1 (scalar_fold1 P.max) (scalar_fold1s P.max)
79
80 minimumP, maximumP :: [:Int:] -> Int
81 minimumP = GHC.PArr.minimumP
82 maximumP = GHC.PArr.maximumP
83
84 minIndexPA :: PArray Int :-> Int
85 {-# INLINE minIndexPA #-}
86 minIndexPA = closure1 (scalar_fold1Index min') (scalar_fold1sIndex min')
87 where
88 min' (i,x) (j,y) | x P.<= y = (i,x)
89 | P.otherwise = (j,y)
90
91 minIndexP :: [:Int:] -> Int
92 {-# NOINLINE minIndexP #-}
93 minIndexP _ = 0
94
95 maxIndexPA :: PArray Int :-> Int
96 {-# INLINE maxIndexPA #-}
97 maxIndexPA = closure1 (scalar_fold1Index max') (scalar_fold1sIndex max')
98 where
99 max' (i,x) (j,y) | x P.>= y = (i,x)
100 | P.otherwise = (j,y)
101
102 maxIndexP :: [:Int:] -> Int
103 {-# NOINLINE maxIndexP #-}
104 maxIndexP _ = 0
105
106
107 plusV, minusV, multV :: Int :-> Int :-> Int
108 {-# INLINE plusV #-}
109 {-# INLINE minusV #-}
110 {-# INLINE multV #-}
111 plusV = closure2 dPA_Int (P.+) (scalar_zipWith (P.+))
112 minusV = closure2 dPA_Int (P.-) (scalar_zipWith (P.-))
113 multV = closure2 dPA_Int (P.*) (scalar_zipWith (P.*))
114
115 (+), (-), (*) :: Int -> Int -> Int
116 (+) = (P.+)
117 (-) = (P.-)
118 (*) = (P.*)
119
120 negateV, absV :: Int :-> Int
121 {-# INLINE negateV #-}
122 {-# INLINE absV #-}
123 negateV = closure1 P.negate (scalar_map P.negate)
124 absV = closure1 P.abs (scalar_map P.abs)
125
126 negate, abs :: Int -> Int
127 negate = P.negate
128 abs = P.abs
129
130 sumPA, productPA :: PArray Int :-> Int
131 {-# INLINE sumPA #-}
132 {-# INLINE productPA #-}
133 sumPA = closure1 (scalar_fold (+) 0) (scalar_folds (+) 0)
134 productPA = closure1 (scalar_fold (*) 1) (scalar_folds (*) 1)
135
136 sumP, productP :: [:Int:] -> Int
137 sumP = GHC.PArr.sumP
138 productP = GHC.PArr.productP
139
140
141 divV, modV :: Int :-> Int :-> Int
142 {-# INLINE divV #-}
143 {-# INLINE modV #-}
144 divV = closure2 dPA_Int P.div (scalar_zipWith P.div)
145 modV = closure2 dPA_Int P.mod (scalar_zipWith P.mod)
146
147 div, mod :: Int -> Int -> Int
148 div = P.div
149 mod = P.mod
150
151 sqrt :: Int -> Int
152 sqrt n = P.floor (P.sqrt (P.fromIntegral n))
153
154 sqrtV :: Int :-> Int
155 {-# INLINE sqrtV #-}
156 sqrtV = closure1 (sqrt) (scalar_map sqrt)
157
158 enumFromToPA :: Int :-> Int :-> PArray Int
159 {-# INLINE enumFromToPA #-}
160 enumFromToPA = enumFromToPA_Int
161
162 enumFromToP :: Int -> Int -> [:Int:]
163 {-# NOINLINE enumFromToP #-}
164 enumFromToP n m = [:n..m:]
165
166 upToPA :: Int :-> PArray Int
167 {-# INLINE upToPA #-}
168 upToPA = closure1 upToPA_Int (\_ -> P.error "Int.upToPA lifted")
169
170 upToP :: Int -> [:Int:]
171 {-# NOINLINE upToP #-}
172 upToP n = enumFromToP 0 n
173