Skip to content

Commit 13b9bbe

Browse files
authored
Add runtime batch_bool mask overloads for complex batch load/store (#1391)
The scalar batch<T, A> already accepted both batch_bool_constant (compile-time) and batch_bool (runtime) masks for load/store, but batch<std::complex<T>, A> was missing the runtime overloads. This made it impossible to use a dynamic mask with complex batches. Changes: - Declare and define load(value_type const*, batch_bool<T,A>, Mode) and store(value_type*, batch_bool<T,A>, Mode) for batch<std::complex<T>>. - Implement both with a scalar fallback: load zeroes inactive lanes, store only writes active lanes. - Add runtime-mask load/store tests in test_batch_complex.cpp. The existing free-function API in xsimd_api.hpp already documented that batch_bool overloads exist and are slower than batch_bool_constant; no documentation or comment churn in the headers was needed.
1 parent 5491a13 commit 13b9bbe

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

‎include/xsimd/types/xsimd_batch.hpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,8 @@ namespace xsimd
553553
// Compile-time mask overloads
554554
template <class U, bool... Values, class Mode = aligned_mode>
555555
[[nodiscard]] static XSIMD_INLINE batch load(U const* mem, batch_bool_constant<value_type, A, Values...> mask, Mode = {}) noexcept;
556+
template <class Mode = aligned_mode>
557+
[[nodiscard]] static XSIMD_INLINE batch load(value_type const* mem, batch_bool<T, A> mask, Mode = {}) noexcept;
556558
template <class U>
557559
[[nodiscard]] static XSIMD_INLINE batch load(U const* mem, stream_mode) noexcept;
558560
template <class U>
@@ -562,6 +564,8 @@ namespace xsimd
562564
// Compile-time mask overloads
563565
template <class U, bool... Values, class Mode = aligned_mode>
564566
XSIMD_INLINE void store(U* mem, batch_bool_constant<value_type, A, Values...> mask, Mode = {}) const noexcept;
567+
template <class Mode = aligned_mode>
568+
XSIMD_INLINE void store(value_type* mem, batch_bool<T, A> mask, Mode = {}) const noexcept;
565569
template <class U>
566570
XSIMD_INLINE void store(U* mem, stream_mode) const noexcept;
567571

@@ -1506,6 +1510,17 @@ namespace xsimd
15061510
kernel::store_masked<A>(mem, *this, mask, mode, A {});
15071511
}
15081512

1513+
template <class T, class A>
1514+
template <class Mode>
1515+
XSIMD_INLINE void batch<std::complex<T>, A>::store(value_type* mem, batch_bool<T, A> mask, Mode) const noexcept
1516+
{
1517+
alignas(A::alignment()) std::array<value_type, size> buffer;
1518+
store_aligned(buffer.data());
1519+
for (std::size_t i = 0; i < size; ++i)
1520+
if (mask.get(i))
1521+
mem[i] = buffer[i];
1522+
}
1523+
15091524
template <class T, class A>
15101525
XSIMD_INLINE void batch<std::complex<T>, A>::store_aligned(T* real_dst, T* imag_dst) const noexcept
15111526
{
@@ -1544,6 +1559,16 @@ namespace xsimd
15441559
return kernel::load_masked<A>(mem, mask, kernel::convert<value_type> {}, mode, A {});
15451560
}
15461561

1562+
template <class T, class A>
1563+
template <class Mode>
1564+
XSIMD_INLINE batch<std::complex<T>, A> batch<std::complex<T>, A>::load(value_type const* mem, batch_bool<T, A> mask, Mode) noexcept
1565+
{
1566+
alignas(A::alignment()) std::array<value_type, size> buffer {};
1567+
for (std::size_t i = 0; i < size; ++i)
1568+
buffer[i] = mask.get(i) ? mem[i] : value_type(0);
1569+
return load_aligned(buffer.data());
1570+
}
1571+
15471572
template <class T, class A>
15481573
template <class U>
15491574
XSIMD_INLINE batch<std::complex<T>, A> batch<std::complex<T>, A>::load(U const* mem, stream_mode) noexcept

‎test/test_batch_complex.cpp‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,40 @@ struct batch_complex_test
114114
CHECK_EQ(ares_real, areal);
115115
CHECK_EQ(ares_imag, aimag);
116116
}
117+
{
118+
// Runtime-masked load / store
119+
alignas(arch_type::alignment()) array_type amem;
120+
for (size_t i = 0; i < size; ++i)
121+
amem[i] = lhs[i];
122+
123+
uint64_t bits = 0;
124+
for (size_t i = 0; i < size; ++i)
125+
if (i % 2 == 0)
126+
bits |= uint64_t(1) << i;
127+
using mask_type = typename batch_type::batch_bool_type;
128+
auto mask = mask_type::from_mask(bits);
129+
130+
batch_type loaded = batch_type::load(amem.data(), mask, xsimd::aligned_mode());
131+
for (size_t i = 0; i < size; ++i)
132+
{
133+
if (i % 2 == 0)
134+
CHECK_EQ(loaded.get(i), amem[i]);
135+
else
136+
CHECK_EQ(loaded.get(i), value_type(0, 0));
137+
}
138+
139+
batch_type to_store = batch_type::load_unaligned(rhs.data());
140+
alignas(arch_type::alignment()) array_type adest;
141+
std::fill(adest.begin(), adest.end(), value_type(0, 0));
142+
to_store.store(adest.data(), mask, xsimd::aligned_mode());
143+
for (size_t i = 0; i < size; ++i)
144+
{
145+
if (i % 2 == 0)
146+
CHECK_EQ(adest[i], rhs[i]);
147+
else
148+
CHECK_EQ(adest[i], value_type(0, 0));
149+
}
150+
}
117151
}
118152
#ifdef XSIMD_ENABLE_XTL_COMPLEX
119153
void test_load_store_xtl() const

0 commit comments

Comments
 (0)