Debian Codegraph @

src/python/PyImath/PyImathOperators.h

raw
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5
6 // clang-format off
7
8 #ifndef _PyImathOperators_h_
9 #define _PyImathOperators_h_
10
11 #include "PyImathFixedArray.h"
12 #include "PyImathAutovectorize.h"
13
14 namespace PyImath {
15
16 template <class T1, class T2=T1, class Ret=T1>
17 struct op_add {
18 static inline Ret apply(const T1 &a, const T2 &b) { return a+b; }
19 };
20
21 template <class T1, class T2=T1, class Ret=T1>
22 struct op_sub {
23 static inline Ret apply(const T1 &a, const T2 &b) { return a-b; }
24 };
25
26 template <class T1, class T2=T1, class Ret=T1>
27 struct op_rsub {
28 static inline Ret apply(const T1 &a, const T2 &b) { return b-a; }
29 };
30
31 template <class T1, class T2=T1, class Ret=T1>
32 struct op_mul {
33 static inline Ret apply(const T1 &a, const T2 &b) { return a*b; }
34 };
35
36 template <class T1, class T2=T1, class Ret=T1>
37 struct op_div {
38 static inline Ret apply(const T1 &a, const T2 &b) { return a/b; }
39 };
40
41 template <class T1, class T2=T1, class Ret=T1>
42 struct op_mod {
43 static inline Ret apply(const T1 &a, const T2 &b) { return a%b; }
44 };
45
46 template <class T1, class T2=T1, class Ret=T1>
47 struct op_pow {
48 static inline Ret apply(const T1 &a, const T2 &b) { return std::pow(a,b); }
49 };
50
51 template <class T1, class T2=T1, class Ret=T1>
52 struct op_rpow {
53 static inline Ret apply(const T1 &a, const T2 &b) { return std::pow(b,a); }
54 };
55
56 template <class T1, class Ret=T1>
57 struct op_neg {
58 static inline Ret apply(const T1 &a) { return -a; }
59 };
60
61 template <class T1, class Ret=T1>
62 struct op_abs {
63 static inline Ret apply(const T1 &a) { return std::abs(a); }
64 };
65
66 template <class T1, class Ret=T1>
67 struct op_inverse {
68 static inline Ret apply(const T1 &a) { return ~a; }
69 };
70
71 template <class T1, class T2=T1, class Ret=T1>
72 struct op_lshift {
73 static inline Ret apply(const T1 &a, const T2 &b) { return a << b; }
74 };
75
76 template <class T1, class T2=T1, class Ret=T1>
77 struct op_rshift {
78 static inline Ret apply(const T1 &a, const T2 &b) { return a >> b; }
79 };
80
81 template <class T1, class T2=T1, class Ret=T1>
82 struct op_bitand {
83 static inline Ret apply(const T1 &a, const T2 &b) { return a & b; }
84 };
85
86 template <class T1, class T2=T1, class Ret=T1>
87 struct op_xor {
88 static inline Ret apply(const T1 &a, const T2 &b) { return a ^ b; }
89 };
90
91 template <class T1, class T2=T1, class Ret=T1>
92 struct op_bitor {
93 static inline Ret apply(const T1 &a, const T2 &b) { return a | b; }
94 };
95
96 template <class T1, class T2=T1>
97 struct op_iadd {
98 static inline void apply(T1 &a, const T2 &b) { a += b; }
99 };
100
101 template <class T1, class T2=T1>
102 struct op_isub {
103 static inline void apply(T1 &a, const T2 &b) { a -= b; }
104 };
105
106 template <class T1, class T2=T1>
107 struct op_imul {
108 static inline void apply(T1 &a, const T2 &b) { a *= b; }
109 };
110
111 template <class T1, class T2=T1>
112 struct op_idiv {
113 static inline void apply(T1 &a, const T2 &b) { a /= b; }
114 };
115
116 template <class T1, class T2=T1>
117 struct op_imod {
118 static inline void apply(T1 &a, const T2 &b) { a %= b; }
119 };
120
121 template <class T1, class T2=T1>
122 struct op_ipow {
123 static inline void apply(T1 &a, const T2 &b) { a = std::pow(a,b); }
124 };
125
126 template <class T1, class T2=T1>
127 struct op_ilshift {
128 static inline void apply(T1 &a, const T2 &b) { a <<= b; }
129 };
130
131 template <class T1, class T2=T1>
132 struct op_irshift {
133 static inline void apply(T1 &a, const T2 &b) { a >>= b; }
134 };
135
136 template <class T1, class T2=T1>
137 struct op_ixor {
138 static inline void apply(T1 &a, const T2 &b) { a ^= b; }
139 };
140
141 template <class T1, class T2=T1>
142 struct op_ibitand {
143 static inline void apply(T1 &a, const T2 &b) { a &= b; }
144 };
145
146 template <class T1, class T2=T1>
147 struct op_ibitor {
148 static inline void apply(T1 &a, const T2 &b) { a |= b; }
149 };
150
151 // the logical function return values default to 'int' for use
152 // as mask arrays.
153 template <class T1, class T2=T1, class Ret=int>
154 struct op_lt {
155 static inline Ret apply(const T1 &a, const T2 &b) { return a < b; }
156 };
157
158 template <class T1, class T2=T1, class Ret=int>
159 struct op_gt {
160 static inline Ret apply(const T1 &a, const T2 &b) { return a > b; }
161 };
162
163 template <class T1, class T2=T1, class Ret=int>
164 struct op_le {
165 static inline Ret apply(const T1 &a, const T2 &b) { return a <= b; }
166 };
167
168 template <class T1, class T2=T1, class Ret=int>
169 struct op_ge {
170 static inline Ret apply(const T1 &a, const T2 &b) { return a >= b; }
171 };
172
173 template <class T1, class T2=T1, class Ret=int>
174 struct op_eq {
175 static inline Ret apply(const T1 &a, const T2 &b) { return a == b; }
176 };
177
178 template <class T1, class T2=T1, class Ret=int>
179 struct op_ne {
180 static inline Ret apply(const T1 &a, const T2 &b) { return a != b; }
181 };
182
183 template <class T>
184 static T fa_reduce(const FixedArray<T> &a) {
185 T tmp(T(0)); // should use default construction but V3f doens't initialize
186 size_t len = a.len();
187 for (size_t i=0; i < len; ++i) tmp += a[i];
188 return tmp;
189 }
190
191 template <class T>
192 static T fa_min(const FixedArray<T> &a) {
193 T tmp(T(0));
194 size_t len = a.len();
195 if (len > 0)
196 tmp = a[0];
197 for (size_t i=1; i < len; ++i)
198 if (a[i] < tmp)
199 tmp = a[i];
200 return tmp;
201 }
202
203 template <class T>
204 static T fa_max(const FixedArray<T> &a) {
205 T tmp(T(0));
206 size_t len = a.len();
207 if (len > 0)
208 tmp = a[0];
209 for (size_t i=1; i < len; ++i)
210 if (a[i] > tmp)
211 tmp = a[i];
212 return tmp;
213 }
214
215 template <class T>
216 static void add_arithmetic_math_functions(boost::python::class_<FixedArray<T> > &c) {
217 using boost::mpl::true_;
218 using boost::mpl::false_;
219 generate_member_bindings<op_add<T>, true_ >(c,"__add__", "self+x", boost::python::args("x"));
220 generate_member_bindings<op_add<T>, false_>(c,"__radd__","x+self", boost::python::args("x"));
221 generate_member_bindings<op_sub<T>, true_ >(c,"__sub__", "self-x", boost::python::args("x"));
222 generate_member_bindings<op_rsub<T>,false_>(c,"__rsub__","x-self", boost::python::args("x"));
223 generate_member_bindings<op_mul<T>, true_ >(c,"__mul__", "self*x", boost::python::args("x"));
224 generate_member_bindings<op_mul<T>, false_>(c,"__rmul__","x*self", boost::python::args("x"));
225 generate_member_bindings<op_div<T>, true_ >(c,"__div__", "self/x", boost::python::args("x"));
226 generate_member_bindings<op_div<T>, true_ >(c,"__truediv__", "self/x", boost::python::args("x"));
227 generate_member_bindings<op_neg<T> >(c,"__neg__", "-x");
228 generate_member_bindings<op_iadd<T>,true_ >(c,"__iadd__","self+=x",boost::python::args("x"));
229 generate_member_bindings<op_isub<T>,true_ >(c,"__isub__","self-=x",boost::python::args("x"));
230 generate_member_bindings<op_imul<T>,true_ >(c,"__imul__","self*=x",boost::python::args("x"));
231 generate_member_bindings<op_idiv<T>,true_ >(c,"__idiv__","self/=x",boost::python::args("x"));
232 generate_member_bindings<op_idiv<T>,true_ >(c,"__itruediv__","self/=x",boost::python::args("x"));
233
234 c.def("reduce",&fa_reduce<T>);
235 }
236
237 template <class T>
238 static void add_reduction_functions(boost::python::class_<FixedArray<T> > &c) {
239 c.def("min",&fa_min<T>);
240 c.def("max",&fa_max<T>);
241 }
242
243 template <class T>
244 static void add_pow_math_functions(boost::python::class_<FixedArray<T> > &c) {
245 using boost::mpl::true_;
246 using boost::mpl::false_;
247 generate_member_bindings<op_pow<T>, true_ >(c,"__pow__", "self**x", boost::python::args("x"));
248 generate_member_bindings<op_rpow<T>,false_>(c,"__rpow__","x**self", boost::python::args("x"));
249 generate_member_bindings<op_ipow<T>,true_ >(c,"__ipow__","x**=self",boost::python::args("x"));
250 }
251
252 template <class T>
253 static void add_mod_math_functions(boost::python::class_<FixedArray<T> > &c) {
254 using boost::mpl::true_;
255 generate_member_bindings<op_mod<T>, true_>(c,"__mod__", "self%x", boost::python::args("x"));
256 generate_member_bindings<op_imod<T>,true_>(c,"__imod__","self%=x",boost::python::args("x"));
257 }
258
259 template <class T>
260 static void add_shift_math_functions(boost::python::class_<FixedArray<T> > &c) {
261 using boost::mpl::true_;
262 generate_member_bindings<op_lshift<T>, true_>(c,"__lshift__", "self<<x", boost::python::args("x"));
263 generate_member_bindings<op_ilshift<T>,true_>(c,"__ilshift__","self<<=x",boost::python::args("x"));
264 generate_member_bindings<op_rshift<T>, true_>(c,"__rshift__", "self>>x", boost::python::args("x"));
265 generate_member_bindings<op_irshift<T>,true_>(c,"__irshift__","self>>=x",boost::python::args("x"));
266 }
267
268 template <class T>
269 static void add_bitwise_math_functions(boost::python::class_<FixedArray<T> > &c) {
270 using boost::mpl::true_;
271 generate_member_bindings<op_bitand<T>, true_>(c,"__and__", "self&x", boost::python::args("x"));
272 generate_member_bindings<op_ibitand<T>,true_>(c,"__iand__","self&=x",boost::python::args("x"));
273 generate_member_bindings<op_bitor<T>, true_>(c,"__or__", "self|x", boost::python::args("x"));
274 generate_member_bindings<op_ibitor<T>, true_>(c,"__ior__", "self|=x",boost::python::args("x"));
275 generate_member_bindings<op_xor<T>, true_>(c,"__xor__", "self^x", boost::python::args("x"));
276 generate_member_bindings<op_ixor<T>, true_>(c,"__ixor__","self^=x",boost::python::args("x"));
277 }
278
279 template <class T>
280 static void add_comparison_functions(boost::python::class_<FixedArray<T> > &c) {
281 using boost::mpl::true_;
282 generate_member_bindings<op_eq<T>, true_>(c,"__eq__","self==x",boost::python::args("x"));
283 generate_member_bindings<op_ne<T>, true_>(c,"__ne__","self!=x",boost::python::args("x"));
284 }
285
286 template <class T>
287 static void add_ordered_comparison_functions(boost::python::class_<FixedArray<T> > &c) {
288 using boost::mpl::true_;
289 generate_member_bindings<op_lt<T>, true_>(c,"__lt__","self<x", boost::python::args("x"));
290 generate_member_bindings<op_le<T>, true_>(c,"__le__","self<=x",boost::python::args("x"));
291 generate_member_bindings<op_gt<T>, true_>(c,"__gt__","self>x", boost::python::args("x"));
292 generate_member_bindings<op_ge<T>, true_>(c,"__ge__","self>=x",boost::python::args("x"));
293 }
294
295 template <class S,class T>
296 static void add_explicit_construction_from_type(boost::python::class_<FixedArray<T> > &c) {
297 using namespace boost::python;
298 c.def(init<FixedArray<S> >("copy contents of other array into this one"));
299 }
300
301 }
302
303 #endif // _PyImathOperators_h_