Adapted to simplified VECTORISE pragmas
[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)
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 import Prelude (Int)
32
33 infixl 7 *
34 infixl 6 +, -
35 infix 4 ==, /=, <, <=, >, >=
36 infixl 7 `div`, `mod`
37
38 -- Ord ------------------------------------------------------------------------
39 (==), (/=), (<), (<=), (>), (>=) :: Int -> Int -> Bool
40 (==) = (P.==)
41 (/=) = (P./=)
42 (<=) = (P.<=)
43 (<) = (P.<)
44 (>=) = (P.>=)
45 (>) = (P.>)
46
47
48 -- min/max ----------------------------
49 min, max :: Int -> Int -> Int
50 min = P.min
51 max = P.max
52
53
54 -- minimum/maximum --------------------
55 minimumP, maximumP :: PArr Int -> Int
56
57 minimumP arr = headPArr arr
58 {-# NOINLINE minimumP #-}
59 {-# VECTORISE minimumP = minimumPP #-}
60
61 maximumP arr = headPArr arr
62 {-# NOINLINE maximumP #-}
63 {-# VECTORISE maximumP = maximumPP #-}
64
65 minimumPP, maximumPP :: PArray Int :-> Int
66 minimumPP = L.closure1' (SC.fold1 P.min) (SC.fold1s P.min)
67 {-# INLINE minimumPP #-}
68 {-# NOVECTORISE minimumPP #-}
69
70 maximumPP = L.closure1' (SC.fold1 P.max) (SC.fold1s P.max)
71 {-# INLINE maximumPP #-}
72 {-# NOVECTORISE maximumPP #-}
73
74
75 -- minIndex/maxIndex ------------------
76 minIndexP :: PArr Int -> Int
77 minIndexP !_ = 0
78 {-# NOINLINE minIndexP #-}
79 {-# VECTORISE minIndexP = minIndexPP #-}
80
81 minIndexPP :: PArray Int :-> Int
82 minIndexPP = L.closure1' (SC.fold1Index min') (SC.fold1sIndex min')
83 {-# INLINE minIndexPP #-}
84 {-# NOVECTORISE minIndexPP #-}
85
86 min' :: P.Ord b => (a, b) -> (a, b) -> (a, b)
87 min' (i,x) (j,y) | x P.<= y = (i,x)
88 | P.otherwise = (j,y)
89 {-# NOVECTORISE min' #-}
90
91
92 maxIndexP :: PArr Int -> Int
93 maxIndexP _ = 0
94 {-# NOINLINE maxIndexP #-}
95 {-# VECTORISE maxIndexP = maxIndexPP #-}
96
97 maxIndexPP :: PArray Int :-> Int
98 maxIndexPP = L.closure1' (SC.fold1Index max') (SC.fold1sIndex max')
99 {-# INLINE maxIndexPP #-}
100 {-# NOVECTORISE maxIndexPP #-}
101
102 max' :: P.Ord b => (a, b) -> (a, b) -> (a, b)
103 max' (i,x) (j,y) | x P.>= y = (i,x)
104 | P.otherwise = (j,y)
105 {-# NOVECTORISE max' #-}
106
107
108 -- Num ------------------------------------------------------------------------
109 (+), (-), (*) :: Int -> Int -> Int
110 (+) = (P.+)
111 (-) = (P.-)
112 (*) = (P.*)
113
114
115 -- negate/abs -------------------------
116 negate, abs :: Int -> Int
117 negate = P.negate
118 abs = P.abs
119
120 -- sum/product ------------------------
121 sumP, productP :: PArr Int -> Int
122
123 sumP arr = headPArr arr
124 {-# NOINLINE sumP #-}
125 {-# VECTORISE sumP = sumPP #-}
126
127 productP arr = headPArr arr
128 {-# NOINLINE productP #-}
129 {-# VECTORISE productP = productPP #-}
130
131 sumPP, productPP :: PArray Int :-> Int
132 sumPP = L.closure1' (SC.fold (+) 0) (SC.folds (+) 0)
133 {-# INLINE sumPP #-}
134 {-# NOVECTORISE sumPP #-}
135
136 productPP = L.closure1' (SC.fold (*) 1) (SC.folds (*) 1)
137 {-# INLINE productPP #-}
138 {-# NOVECTORISE productPP #-}
139
140
141 -- Integral -------------------------------------------------------------------
142 div, mod :: Int -> Int -> Int
143 div = P.div
144 mod = P.mod
145
146 sqrt :: Int -> Int
147 sqrt n = P.floor (P.sqrt (P.fromIntegral n) :: P.Double)
148
149 -- Enum -----------------------------------------------------------------------
150 enumFromToP :: Int -> Int -> PArr Int
151 enumFromToP !_ !_ = emptyPArr
152 {-# NOINLINE enumFromToP #-}
153 {-# VECTORISE enumFromToP = enumFromToPP #-}
154
155 enumFromToPP :: Int :-> Int :-> PArray Int
156 enumFromToPP = L.closure2' SC.enumFromTo SC.enumFromTol
157 {-# INLINE enumFromToPP #-}
158 {-# NOVECTORISE enumFromToPP #-}