Revert rename of dph-common for now
[packages/dph.git] / dph-common / Data / Array / Parallel / Prelude / Int.hs
1 {-# LANGUAGE ParallelArrays #-}
2 {-# OPTIONS_GHC -fvectorise #-}
3 -- NB: Cannot use any parallel array syntax except the type constructor
4
5 module Data.Array.Parallel.Prelude.Int (
6 Int,
7
8 -- Ord
9 (==), (/=), (<), (<=), (>), (>=), min, max,
10
11 minimumP, maximumP, minIndexP, maxIndexP,
12
13 -- Num
14 (+), (-), (*), negate, abs,
15
16 sumP, productP,
17
18 -- Integral
19 div, mod, sqrt,
20
21 -- Enum
22 enumFromToP
23 ) where
24
25 import Data.Array.Parallel.VectDepend () -- see Note [Vectoriser dependencies] in the same module
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 import Data.Array.Parallel.Prelude.Bool
32
33 import Prelude (Int)
34 import qualified Prelude as P
35
36 infixl 7 *
37 infixl 6 +, -
38 infix 4 ==, /=, <, <=, >, >=
39 infixl 7 `div`, `mod`
40
41 {-# VECTORISE SCALAR type Int #-}
42
43 (==), (/=), (<), (<=), (>), (>=) :: Int -> Int -> Bool
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 (>) = (P.>)
55 {-# VECTORISE SCALAR (>) #-}
56
57 min, max :: Int -> Int -> Int
58 min = P.min
59 {-# VECTORISE SCALAR min #-}
60 max = P.max
61 {-# VECTORISE SCALAR max #-}
62
63 minimumP, maximumP :: [:Int:] -> Int
64 {-# NOINLINE minimumP #-}
65 minimumP a = a `indexPArr` 0
66 {-# VECTORISE minimumP = minimumP_v #-}
67 {-# NOINLINE maximumP #-}
68 maximumP a = a `indexPArr` 0
69 {-# VECTORISE maximumP = maximumP_v #-}
70
71 minimumP_v, maximumP_v:: PArray Int :-> Int
72 {-# INLINE minimumP_v #-}
73 minimumP_v = closure1 (scalar_fold1 P.min) (scalar_fold1s P.min)
74 {-# NOVECTORISE minimumP_v #-}
75 {-# INLINE maximumP_v #-}
76 maximumP_v = closure1 (scalar_fold1 P.max) (scalar_fold1s P.max)
77 {-# NOVECTORISE maximumP_v #-}
78
79 minIndexP :: [:Int:] -> Int
80 {-# NOINLINE minIndexP #-}
81 minIndexP _ = 0 -- FIXME: add proper implementation
82 {-# VECTORISE minIndexP = minIndexPA #-}
83
84 minIndexPA :: PArray Int :-> Int
85 {-# INLINE minIndexPA #-}
86 minIndexPA = closure1 (scalar_fold1Index min') (scalar_fold1sIndex min')
87 {-# NOVECTORISE minIndexPA #-}
88
89 min' (i,x) (j,y) | x P.<= y = (i,x)
90 | P.otherwise = (j,y)
91 {-# NOVECTORISE min' #-}
92
93 maxIndexP :: [:Int:] -> Int
94 {-# NOINLINE maxIndexP #-}
95 maxIndexP _ = 0 -- FIXME: add proper implementation
96 {-# VECTORISE maxIndexP = maxIndexPA #-}
97
98 maxIndexPA :: PArray Int :-> Int
99 {-# INLINE maxIndexPA #-}
100 maxIndexPA = closure1 (scalar_fold1Index max') (scalar_fold1sIndex max')
101 {-# NOVECTORISE maxIndexPA #-}
102
103 max' (i,x) (j,y) | x P.>= y = (i,x)
104 | P.otherwise = (j,y)
105 {-# NOVECTORISE max' #-}
106
107 (+), (-), (*) :: Int -> Int -> Int
108 (+) = (P.+)
109 {-# VECTORISE SCALAR (+) #-}
110 (-) = (P.-)
111 {-# VECTORISE SCALAR (-) #-}
112 (*) = (P.*)
113 {-# VECTORISE SCALAR (*) #-}
114
115 negate, abs :: Int -> Int
116 negate = P.negate
117 {-# VECTORISE SCALAR negate #-}
118 abs = P.abs
119 {-# VECTORISE SCALAR abs #-}
120
121 sumP, productP :: [:Int:] -> Int
122 {-# NOINLINE sumP #-}
123 sumP a = a `indexPArr` 0
124 {-# VECTORISE sumP = sumP_v #-}
125 {-# NOINLINE productP #-}
126 productP a = a `indexPArr` 0
127 {-# VECTORISE productP = productP_v #-}
128
129 sumP_v, productP_v:: PArray Int :-> Int
130 {-# INLINE sumP_v #-}
131 sumP_v = closure1 (scalar_fold (+) 0) (scalar_folds (+) 0)
132 {-# NOVECTORISE sumP_v #-}
133 {-# INLINE productP_v #-}
134 productP_v = closure1 (scalar_fold (*) 1) (scalar_folds (*) 1)
135 {-# NOVECTORISE productP_v #-}
136
137 div, mod :: Int -> Int -> Int
138 div = P.div
139 {-# VECTORISE SCALAR div #-}
140 mod = P.mod
141 {-# VECTORISE SCALAR mod #-}
142
143 sqrt :: Int -> Int
144 sqrt n = P.floor (P.sqrt (P.fromIntegral n) :: P.Double)
145 {-# VECTORISE SCALAR sqrt #-}
146
147 enumFromToP :: Int -> Int -> [:Int:]
148 {-# NOINLINE enumFromToP #-}
149 enumFromToP x y = singletonPArr (x + y)
150 {-# VECTORISE enumFromToP = enumFromToPA_Int #-}