Skip to content

Commit d3732a7

Browse files
committed
Avoid implicit conversions for bitwise operators
1 parent d632ee7 commit d3732a7

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

include/xtensor/xoperation.hpp

+43-4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,45 @@ namespace xt
8181
} \
8282
}
8383

84+
// This functor avoids implicit conversions of small integral types to 'int'
85+
// by returning the same type instead of 'auto'.
86+
#define UNARY_BITWISE_OPERATOR_FUNCTOR(NAME, OP) \
87+
struct NAME \
88+
{ \
89+
template <class A1> \
90+
constexpr std::decay_t<A1> operator()(const A1& arg) const \
91+
{ \
92+
return OP arg; \
93+
} \
94+
template <class B> \
95+
constexpr auto simd_apply(const B& arg) const \
96+
{ \
97+
return OP arg; \
98+
} \
99+
}
100+
101+
// This functor avoids implicit conversions of small integral types to 'int'
102+
// by returning the largest type instead of 'auto'.
103+
#define BINARY_BITWISE_OPERATOR_FUNCTOR(NAME, OP) \
104+
struct NAME \
105+
{ \
106+
template <class T1, class T2> \
107+
constexpr \
108+
std::conditional_t<(sizeof(std::decay_t<T1>) > sizeof(std::decay_t<T2>)), \
109+
std::decay_t<T1>, std::decay_t<T2>> \
110+
operator()(T1&& arg1, T2&& arg2) const \
111+
{ \
112+
using xt::detail::operator OP; \
113+
return (std::forward<T1>(arg1) OP std::forward<T2>(arg2)); \
114+
} \
115+
template <class B> \
116+
constexpr auto simd_apply(const B& arg1, const B& arg2) const \
117+
{ \
118+
return (arg1 OP arg2); \
119+
} \
120+
}
121+
122+
84123
namespace detail
85124
{
86125
DEFINE_COMPLEX_OVERLOAD(+);
@@ -112,10 +151,10 @@ namespace xt
112151
BINARY_OPERATOR_FUNCTOR(logical_or, ||);
113152
BINARY_OPERATOR_FUNCTOR(logical_and, &&);
114153
UNARY_OPERATOR_FUNCTOR(logical_not, !);
115-
BINARY_OPERATOR_FUNCTOR(bitwise_or, |);
116-
BINARY_OPERATOR_FUNCTOR(bitwise_and, &);
117-
BINARY_OPERATOR_FUNCTOR(bitwise_xor, ^);
118-
UNARY_OPERATOR_FUNCTOR(bitwise_not, ~);
154+
BINARY_BITWISE_OPERATOR_FUNCTOR(bitwise_or, |);
155+
BINARY_BITWISE_OPERATOR_FUNCTOR(bitwise_and, &);
156+
BINARY_BITWISE_OPERATOR_FUNCTOR(bitwise_xor, ^);
157+
UNARY_BITWISE_OPERATOR_FUNCTOR(bitwise_not, ~);
119158
BINARY_OPERATOR_FUNCTOR(left_shift, <<);
120159
BINARY_OPERATOR_FUNCTOR(right_shift, >>);
121160
BINARY_OPERATOR_FUNCTOR(less, <);

0 commit comments

Comments
 (0)