Bolt  1.3
C++ template library with support for OpenCL
functional.h
Go to the documentation of this file.
1 /***************************************************************************
2 * © 2012,2014 Advanced Micro Devices, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 
16 ***************************************************************************/
17 
23 #pragma once
24 #if !defined( BOLT_AMP_FUNCTIONAL_H )
25 #define BOLT_AMP_FUNCTIONAL_H
26 
27 #include "bolt/amp/bolt.h"
28 
29 
30 namespace bolt {
31 namespace amp {
32  template<typename Argument1,
33  typename Result>
35  : public std::unary_function<Argument1, Result>
36  {
37  };
38 
39  template<typename Argument1,
40  typename Argument2,
41  typename Result>
43  : public std::binary_function<Argument1, Argument2, Result>
44  {
45  };
46 
47 /******************************************************************************
48  * Unary Operators
49  *****************************************************************************/
50  template <typename T>
51  struct square : public unary_function<T,T>
52  {
53  T operator() (const T& x) const restrict(cpu,amp) {
54  return x * x;
55  }
56  };
57 
58  template< typename T >
59  struct cube : public unary_function<T,T>
60  {
61  T operator() (const T& x) const restrict(cpu,amp) { return x * x * x; }
62  };
63 
64 
65  template<typename T>
66  struct negate : public unary_function<T,T>
67  {
68  T operator()(const T &__x) const restrict(cpu,amp) {return -__x;}
69  };
70 
71 /******************************************************************************
72  * Binary Operators
73  *****************************************************************************/
74 
75  template<typename T>
76  struct plus : public binary_function<T,T,T>
77  {
78  T operator()(const T &lhs, const T &rhs) const restrict(cpu,amp) {return lhs + rhs;}
79  };
80 
81  template<typename T>
82  struct minus : public binary_function<T,T,T>
83  {
84  T operator()(const T &lhs, const T &rhs) const restrict(cpu,amp) {return lhs - rhs;}
85  };
86 
87  template<typename T>
88  struct multiplies : public binary_function<T,T,T>
89  {
90  T operator()(const T &lhs, const T &rhs) const restrict(cpu,amp) {return lhs * rhs;}
91  };
92 
93  template<typename T>
94  struct divides : public binary_function<T,T,T>
95  {
96  T operator()(const T &lhs, const T &rhs) const restrict(cpu,amp) {return lhs / rhs;}
97  };
98 
99 
100  template<typename T>
101  struct maximum : public binary_function<T,T,T>
102  {
103  T operator()(const T &lhs, const T &rhs) const restrict(cpu,amp) {return rhs > lhs ? rhs:lhs;}
104  };
105 
106  template<typename T>
107  struct minimum : public binary_function<T,T,T>
108  {
109  T operator()(const T &lhs, const T &rhs) const restrict(cpu,amp) {return rhs < lhs ? rhs:lhs;}
110  };
111 
112  template<typename T>
113  struct modulus : public binary_function<T,T,T>
114  {
115  T operator()(const T &lhs, const T &rhs) const restrict(cpu,amp) {return lhs % rhs;}
116  };
117 
118  template<typename T>
119  struct bit_and : public binary_function<T,T,T>
120  {
121  T operator()(const T &lhs, const T &rhs) const restrict(cpu,amp) {return lhs & rhs;}
122  };
123 
124  template<typename T>
125  struct bit_or : public binary_function<T,T,T>
126  {
127  T operator()(const T &lhs, const T &rhs) const restrict(cpu,amp) {return lhs | rhs;}
128  };
129 
130  template<typename T>
131  struct bit_xor : public binary_function<T,T,T>
132  {
133  T operator()(const T &lhs, const T &rhs) const restrict(cpu,amp) {return lhs ^ rhs;}
134  };
135 
136 
137  /******************************************************************************
138  * Unary Predicates
139  *****************************************************************************/
140 
141  template<typename T>
142  struct logical_not : public unary_function<T,T>
143  {
144  bool operator()(const T &x) const restrict(cpu,amp) {return !x;}
145  };
146 
147 
148  /******************************************************************************
149  * Binary Predicates
150  *****************************************************************************/
151 
152  template<typename T>
153  struct equal_to : public binary_function<T,T,T>
154  {
155  bool operator()(const T &lhs, const T &rhs) const restrict(cpu,amp) {return lhs == rhs;}
156  };
157 
158  template<typename T>
159  struct not_equal_to : public binary_function<T,T,T>
160  {
161  bool operator()(const T &lhs, const T &rhs) const restrict(cpu,amp) {return lhs != rhs;}
162  };
163 
164  template<typename T>
165  struct greater : public binary_function<T,T,T>
166  {
167  bool operator()(const T &lhs, const T &rhs) const restrict(cpu,amp) {return lhs > rhs;}
168  };
169 
170  template<typename T>
171  struct identity : public unary_function<T,T>
172  {
173  T operator()(const T &x) const restrict(cpu,amp) {return x;}
174  };
175 
176  template<typename T>
177  struct less : public binary_function<T,T,T>
178  {
179  bool operator()(const T &lhs, const T &rhs) const restrict(cpu,amp) {return lhs < rhs;}
180  };
181 
182  template<typename T>
183  struct greater_equal : public binary_function<T,T,T>
184  {
185  bool operator()(const T &lhs, const T &rhs) const restrict(cpu,amp) {return lhs >= rhs;}
186  };
187 
188  template<typename T>
189  struct less_equal : public binary_function<T,T,T>
190  {
191  bool operator()(const T &lhs, const T &rhs) const restrict(cpu,amp) {return lhs <= rhs;}
192  };
193 
194  template<typename T>
195  struct logical_and : public binary_function<T,T,T>
196  {
197  bool operator()(const T &lhs, const T &rhs) const restrict(cpu,amp) {return lhs && rhs;}
198  };
199 
200  template<typename T>
201  struct logical_or : public binary_function<T,T,T>
202  {
203  bool operator()(const T &lhs, const T &rhs) const restrict(cpu,amp) {return lhs || rhs;}
204  };
205 
206 
207  template <typename T>
209  {
210  equal_to_value(const T &targetValue) : _targetValue(targetValue)
211  { };
212  equal_to_value(){}
213  bool operator() (const T &x) const restrict(amp,cpu)
214  {
215  T temp= _targetValue;
216  return x == temp;
217  };
218  private:
219  T _targetValue;
220  };
221 
222  template<typename predicate, typename T>
223  struct not_pred
224  {
225  not_pred(const predicate &targetPred) : _targetPred(targetPred)
226  { };
227  not_pred(){}
228  bool operator()(const T &x) const restrict(cpu,amp)
229  {
230  return (_targetPred(x)?false:true);
231  }
232  private:
233  predicate _targetPred;
234  };
235 
236 };
237 };
238 
239 #endif