// // SPDX-License-Identifier: BSD-3-Clause // Copyright Contributors to the OpenEXR Project. // // clang-format off #ifndef _PyImathOperators_h_ #define _PyImathOperators_h_ #include "PyImathFixedArray.h" #include "PyImathAutovectorize.h" namespace PyImath { template struct op_add { static inline Ret apply(const T1 &a, const T2 &b) { return a+b; } }; template struct op_sub { static inline Ret apply(const T1 &a, const T2 &b) { return a-b; } }; template struct op_rsub { static inline Ret apply(const T1 &a, const T2 &b) { return b-a; } }; template struct op_mul { static inline Ret apply(const T1 &a, const T2 &b) { return a*b; } }; template struct op_div { static inline Ret apply(const T1 &a, const T2 &b) { return a/b; } }; template struct op_mod { static inline Ret apply(const T1 &a, const T2 &b) { return a%b; } }; template struct op_pow { static inline Ret apply(const T1 &a, const T2 &b) { return std::pow(a,b); } }; template struct op_rpow { static inline Ret apply(const T1 &a, const T2 &b) { return std::pow(b,a); } }; template struct op_neg { static inline Ret apply(const T1 &a) { return -a; } }; template struct op_abs { static inline Ret apply(const T1 &a) { return std::abs(a); } }; template struct op_inverse { static inline Ret apply(const T1 &a) { return ~a; } }; template struct op_lshift { static inline Ret apply(const T1 &a, const T2 &b) { return a << b; } }; template struct op_rshift { static inline Ret apply(const T1 &a, const T2 &b) { return a >> b; } }; template struct op_bitand { static inline Ret apply(const T1 &a, const T2 &b) { return a & b; } }; template struct op_xor { static inline Ret apply(const T1 &a, const T2 &b) { return a ^ b; } }; template struct op_bitor { static inline Ret apply(const T1 &a, const T2 &b) { return a | b; } }; template struct op_iadd { static inline void apply(T1 &a, const T2 &b) { a += b; } }; template struct op_isub { static inline void apply(T1 &a, const T2 &b) { a -= b; } }; template struct op_imul { static inline void apply(T1 &a, const T2 &b) { a *= b; } }; template struct op_idiv { static inline void apply(T1 &a, const T2 &b) { a /= b; } }; template struct op_imod { static inline void apply(T1 &a, const T2 &b) { a %= b; } }; template struct op_ipow { static inline void apply(T1 &a, const T2 &b) { a = std::pow(a,b); } }; template struct op_ilshift { static inline void apply(T1 &a, const T2 &b) { a <<= b; } }; template struct op_irshift { static inline void apply(T1 &a, const T2 &b) { a >>= b; } }; template struct op_ixor { static inline void apply(T1 &a, const T2 &b) { a ^= b; } }; template struct op_ibitand { static inline void apply(T1 &a, const T2 &b) { a &= b; } }; template struct op_ibitor { static inline void apply(T1 &a, const T2 &b) { a |= b; } }; // the logical function return values default to 'int' for use // as mask arrays. template struct op_lt { static inline Ret apply(const T1 &a, const T2 &b) { return a < b; } }; template struct op_gt { static inline Ret apply(const T1 &a, const T2 &b) { return a > b; } }; template struct op_le { static inline Ret apply(const T1 &a, const T2 &b) { return a <= b; } }; template struct op_ge { static inline Ret apply(const T1 &a, const T2 &b) { return a >= b; } }; template struct op_eq { static inline Ret apply(const T1 &a, const T2 &b) { return a == b; } }; template struct op_ne { static inline Ret apply(const T1 &a, const T2 &b) { return a != b; } }; template static T fa_reduce(const FixedArray &a) { T tmp(T(0)); // should use default construction but V3f doens't initialize size_t len = a.len(); for (size_t i=0; i < len; ++i) tmp += a[i]; return tmp; } template static T fa_min(const FixedArray &a) { T tmp(T(0)); size_t len = a.len(); if (len > 0) tmp = a[0]; for (size_t i=1; i < len; ++i) if (a[i] < tmp) tmp = a[i]; return tmp; } template static T fa_max(const FixedArray &a) { T tmp(T(0)); size_t len = a.len(); if (len > 0) tmp = a[0]; for (size_t i=1; i < len; ++i) if (a[i] > tmp) tmp = a[i]; return tmp; } template static void add_arithmetic_math_functions(boost::python::class_ > &c) { using boost::mpl::true_; using boost::mpl::false_; generate_member_bindings, true_ >(c,"__add__", "self+x", boost::python::args("x")); generate_member_bindings, false_>(c,"__radd__","x+self", boost::python::args("x")); generate_member_bindings, true_ >(c,"__sub__", "self-x", boost::python::args("x")); generate_member_bindings,false_>(c,"__rsub__","x-self", boost::python::args("x")); generate_member_bindings, true_ >(c,"__mul__", "self*x", boost::python::args("x")); generate_member_bindings, false_>(c,"__rmul__","x*self", boost::python::args("x")); generate_member_bindings, true_ >(c,"__div__", "self/x", boost::python::args("x")); generate_member_bindings, true_ >(c,"__truediv__", "self/x", boost::python::args("x")); generate_member_bindings >(c,"__neg__", "-x"); generate_member_bindings,true_ >(c,"__iadd__","self+=x",boost::python::args("x")); generate_member_bindings,true_ >(c,"__isub__","self-=x",boost::python::args("x")); generate_member_bindings,true_ >(c,"__imul__","self*=x",boost::python::args("x")); generate_member_bindings,true_ >(c,"__idiv__","self/=x",boost::python::args("x")); generate_member_bindings,true_ >(c,"__itruediv__","self/=x",boost::python::args("x")); c.def("reduce",&fa_reduce); } template static void add_reduction_functions(boost::python::class_ > &c) { c.def("min",&fa_min); c.def("max",&fa_max); } template static void add_pow_math_functions(boost::python::class_ > &c) { using boost::mpl::true_; using boost::mpl::false_; generate_member_bindings, true_ >(c,"__pow__", "self**x", boost::python::args("x")); generate_member_bindings,false_>(c,"__rpow__","x**self", boost::python::args("x")); generate_member_bindings,true_ >(c,"__ipow__","x**=self",boost::python::args("x")); } template static void add_mod_math_functions(boost::python::class_ > &c) { using boost::mpl::true_; generate_member_bindings, true_>(c,"__mod__", "self%x", boost::python::args("x")); generate_member_bindings,true_>(c,"__imod__","self%=x",boost::python::args("x")); } template static void add_shift_math_functions(boost::python::class_ > &c) { using boost::mpl::true_; generate_member_bindings, true_>(c,"__lshift__", "self<,true_>(c,"__ilshift__","self<<=x",boost::python::args("x")); generate_member_bindings, true_>(c,"__rshift__", "self>>x", boost::python::args("x")); generate_member_bindings,true_>(c,"__irshift__","self>>=x",boost::python::args("x")); } template static void add_bitwise_math_functions(boost::python::class_ > &c) { using boost::mpl::true_; generate_member_bindings, true_>(c,"__and__", "self&x", boost::python::args("x")); generate_member_bindings,true_>(c,"__iand__","self&=x",boost::python::args("x")); generate_member_bindings, true_>(c,"__or__", "self|x", boost::python::args("x")); generate_member_bindings, true_>(c,"__ior__", "self|=x",boost::python::args("x")); generate_member_bindings, true_>(c,"__xor__", "self^x", boost::python::args("x")); generate_member_bindings, true_>(c,"__ixor__","self^=x",boost::python::args("x")); } template static void add_comparison_functions(boost::python::class_ > &c) { using boost::mpl::true_; generate_member_bindings, true_>(c,"__eq__","self==x",boost::python::args("x")); generate_member_bindings, true_>(c,"__ne__","self!=x",boost::python::args("x")); } template static void add_ordered_comparison_functions(boost::python::class_ > &c) { using boost::mpl::true_; generate_member_bindings, true_>(c,"__lt__","self, true_>(c,"__le__","self<=x",boost::python::args("x")); generate_member_bindings, true_>(c,"__gt__","self>x", boost::python::args("x")); generate_member_bindings, true_>(c,"__ge__","self>=x",boost::python::args("x")); } template static void add_explicit_construction_from_type(boost::python::class_ > &c) { using namespace boost::python; c.def(init >("copy contents of other array into this one")); } } #endif // _PyImathOperators_h_