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