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