Skip to content

Commit 9849b8b

Browse files
committed
Add batch_constant comparison operators
1 parent 7f3e01c commit 9849b8b

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

include/xsimd/types/xsimd_batch.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ namespace xsimd
296296
static constexpr std::size_t size = sizeof(types::simd_register<T, A>) / sizeof(T); ///< Number of scalar elements in this batch.
297297

298298
using value_type = bool; ///< Type of the scalar elements within this batch.
299+
using operand_type = T;
299300
using arch_type = A; ///< SIMD Architecture abstracted by this batch.
300301
using register_type = typename base_type::register_type; ///< SIMD register type abstracted by this batch.
301302
using batch_type = batch<T, A>; ///< Associated batch type this batch represents logical operations for.

include/xsimd/types/xsimd_batch_constant.hpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace xsimd
3131
using batch_type = batch_bool<T, A>;
3232
static constexpr std::size_t size = sizeof...(Values);
3333
using value_type = bool;
34+
using operand_type = T;
3435
static_assert(sizeof...(Values) == batch_type::size, "consistent batch size");
3536

3637
public:
@@ -227,6 +228,63 @@ namespace xsimd
227228

228229
#undef MAKE_BINARY_OP
229230

231+
struct boolean_eq
232+
{
233+
constexpr bool operator()(T x, T y) const { return x == y; }
234+
};
235+
struct boolean_ne
236+
{
237+
constexpr bool operator()(T x, T y) const { return x != y; }
238+
};
239+
struct boolean_gt
240+
{
241+
constexpr bool operator()(T x, T y) const { return x > y; }
242+
};
243+
struct boolean_ge
244+
{
245+
constexpr bool operator()(T x, T y) const { return x >= y; }
246+
};
247+
struct boolean_lt
248+
{
249+
constexpr bool operator()(T x, T y) const { return x < y; }
250+
};
251+
struct boolean_le
252+
{
253+
constexpr bool operator()(T x, T y) const { return x <= y; }
254+
};
255+
256+
template <class F, class SelfPack, class OtherPack, size_t... Indices>
257+
static constexpr batch_bool_constant<T, A, F()(std::tuple_element<Indices, SelfPack>::type::value, std::tuple_element<Indices, OtherPack>::type::value)...>
258+
apply_bool(detail::index_sequence<Indices...>)
259+
{
260+
return {};
261+
}
262+
263+
template <class F, T... OtherValues>
264+
static constexpr auto apply_bool(batch_constant<T, A, Values...>, batch_constant<T, A, OtherValues...>)
265+
-> decltype(apply_bool<F, std::tuple<std::integral_constant<T, Values>...>, std::tuple<std::integral_constant<T, OtherValues>...>>(detail::make_index_sequence<sizeof...(Values)>()))
266+
{
267+
static_assert(sizeof...(Values) == sizeof...(OtherValues), "compatible constant batches");
268+
return {};
269+
}
270+
271+
#define MAKE_BINARY_BOOL_OP(OP, NAME) \
272+
template <T... OtherValues> \
273+
constexpr auto operator OP(batch_constant<T, A, OtherValues...> other) const \
274+
-> decltype(apply_bool<NAME>(*this, other)) \
275+
{ \
276+
return {}; \
277+
}
278+
279+
MAKE_BINARY_BOOL_OP(==, boolean_eq)
280+
MAKE_BINARY_BOOL_OP(!=, boolean_ne)
281+
MAKE_BINARY_BOOL_OP(<, boolean_lt)
282+
MAKE_BINARY_BOOL_OP(<=, boolean_le)
283+
MAKE_BINARY_BOOL_OP(>, boolean_gt)
284+
MAKE_BINARY_BOOL_OP(>=, boolean_ge)
285+
286+
#undef MAKE_BINARY_BOOL_OP
287+
230288
constexpr batch_constant<T, A, (T)-Values...> operator-() const
231289
{
232290
return {};

test/test_batch_constant.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,30 @@ struct constant_batch_test
146146
constexpr auto n12_usub = -n12;
147147
constexpr auto n12_usub_ = xsimd::make_batch_constant<value_type, constant<(value_type)-12>, arch_type>();
148148
static_assert(std::is_same<decltype(n12_usub), decltype(n12_usub_)>::value, "-n12 == n12_usub");
149+
150+
// comparison operators
151+
using true_batch_type = decltype(xsimd::make_batch_bool_constant<value_type, true, arch_type>());
152+
using false_batch_type = decltype(xsimd::make_batch_bool_constant<value_type, false, arch_type>());
153+
154+
static_assert(std::is_same<typename decltype(n12 == n12)::operand_type, typename decltype(n12)::value_type>::value, "same type");
155+
156+
static_assert(std::is_same<decltype(n12 == n12), true_batch_type>::value, "n12 == n12");
157+
static_assert(std::is_same<decltype(n12 == n3), false_batch_type>::value, "n12 == n3");
158+
159+
static_assert(std::is_same<decltype(n12 != n12), false_batch_type>::value, "n12 != n12");
160+
static_assert(std::is_same<decltype(n12 != n3), true_batch_type>::value, "n12 != n3");
161+
162+
static_assert(std::is_same<decltype(n12 < n12), false_batch_type>::value, "n12 < n12");
163+
static_assert(std::is_same<decltype(n12 < n3), false_batch_type>::value, "n12 < n3");
164+
165+
static_assert(std::is_same<decltype(n12 > n12), false_batch_type>::value, "n12 > n12");
166+
static_assert(std::is_same<decltype(n12 > n3), true_batch_type>::value, "n12 > n3");
167+
168+
static_assert(std::is_same<decltype(n12 <= n12), true_batch_type>::value, "n12 <= n12");
169+
static_assert(std::is_same<decltype(n12 <= n3), false_batch_type>::value, "n12 <= n3");
170+
171+
static_assert(std::is_same<decltype(n12 >= n12), true_batch_type>::value, "n12 >= n12");
172+
static_assert(std::is_same<decltype(n12 >= n3), true_batch_type>::value, "n12 >= n3");
149173
}
150174
};
151175

0 commit comments

Comments
 (0)