Improve generic version of complex masked store - #1399
Open
serge-sans-paille wants to merge 1 commit into
Open
Conversation
Contributor
Author
|
@DiamonDinoia this is the generic implementation I mentioned in #1391 |
serge-sans-paille
force-pushed
the
bug/1391x
branch
18 times, most recently
from
August 6, 2026 16:24
bc5c258 to
fb39929
Compare
Contributor
Author
|
@DiamonDinoia : (edit: not) ready for review! |
serge-sans-paille
force-pushed
the
bug/1391x
branch
3 times, most recently
from
August 7, 2026 07:28
d862c74 to
c17f5af
Compare
Contributor
|
I did not implement it like this in one go because it is not that trivial as you can see :) |
serge-sans-paille
force-pushed
the
bug/1391x
branch
5 times, most recently
from
August 14, 2026 17:46
71a0eec to
8f5123e
Compare
serge-sans-paille
force-pushed
the
bug/1391x
branch
9 times, most recently
from
August 18, 2026 06:57
3da3b78 to
3af0f7d
Compare
Contributor
Author
|
@DiamonDinoia should be good for review now |
Use the usual kernel mechanism which allows for specialization. Implement specialization for avx and avx512. Follow-up to #1391
serge-sans-paille
force-pushed
the
bug/1391x
branch
from
August 18, 2026 14:13
3af0f7d to
f9732b7
Compare
DiamonDinoia
requested changes
Aug 20, 2026
DiamonDinoia
left a comment
Contributor
There was a problem hiding this comment.
These changes have better codegen on my machine for the generic and sse case.
--- a/include/xsimd/arch/common/xsimd_common_memory.hpp
+++ b/include/xsimd/arch/common/xsimd_common_memory.hpp
@@ -457,20 +457,10 @@ namespace xsimd
// Scalar fallback: only active lanes are touched. Arches with
// hardware predicated loads should override this.
constexpr std::size_t size = batch<T, A>::size;
- alignas(A::alignment()) std::array<T, size> buffer_real;
- alignas(A::alignment()) std::array<T, size> buffer_imag;
+ alignas(A::alignment()) std::array<std::complex<T>, size> buffer;
for (std::size_t i = 0; i < size; ++i)
- if (mask.get(i))
- {
- buffer_real[i] = mem[i].real();
- buffer_imag[i] = mem[i].imag();
- }
- else
- {
- buffer_real[i] = T(0);
- buffer_imag[i] = T(0);
- }
- return batch<std::complex<T>, A>::load_aligned(buffer_real.data(), buffer_imag.data());
+ buffer[i] = mask.get(i) ? mem[i] : std::complex<T>(0);
+ return batch<std::complex<T>, A>::load_aligned(buffer.data());
}
template <class A, class T_in, class T_out, bool... Values, class alignment>
@@ -829,6 +819,40 @@ namespace xsimd
{
static_assert(std::is_same_v<T, void>, "complex_low not implemented for the required architecture");
}
+
+ // Split a complex mask into the two real masks covering the first and
+ // the second half of memory, by duplicating each lane. A bitmask arch
+ // spreads the lane bits instead;
+ template <class A, class T>
+ XSIMD_INLINE std::array<batch_bool<T, A>, 2> zip_complex_mask_vector(batch_bool<T, A> mask) noexcept
+ {
+ batch<T, A> nmask(mask.to_native());
+ return { batch_bool<T, A>(zip_lo(nmask, nmask).to_native()),
+ batch_bool<T, A>(zip_hi(nmask, nmask).to_native()) };
+ }
+
+ // A complex batch occupies two real registers, so one masked complex
+ // access is two masked real accesses over the two halves of memory.
+ template <class A, class T, class Mode>
+ XSIMD_INLINE batch<std::complex<T>, A>
+ load_complex_masked_halves(std::complex<T> const* mem, std::array<batch_bool<T, A>, 2> masks, Mode mode) noexcept
+ {
+ batch<T, A> res_lo = batch<T, A>::load(reinterpret_cast<T const*>(mem), masks[0], mode);
+ batch<T, A> res_hi = batch<T, A>::load(reinterpret_cast<T const*>(mem) + batch<T, A>::size, masks[1], mode);
+ return load_complex(res_lo, res_hi, A {});
+ }
+
+ template <class A, class T, class Mode>
+ XSIMD_INLINE void
+ store_complex_masked_halves(std::complex<T>* mem, batch<std::complex<T>, A> const& src, std::array<batch_bool<T, A>, 2> masks, Mode mode) noexcept
+ {
+ batch<T, A> src_lo = complex_low(src, A {});
+ batch<T, A> src_hi = complex_high(src, A {});
+ // Qualified: kernel::detail has its own store_masked overloads, which
+ // would hide the arch dispatchers in kernel.| namespace detail | ||
| { | ||
| template <class A, class T> | ||
| std::array<batch_bool<T, A>, 2> zip_complex_mask(batch_bool<T, A> mask) |
Contributor
There was a problem hiding this comment.
this needs XSIMD_INLINE sometimes does not get inlined
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Use the usual kernel mechanism which allows for specialization. Leverage existing masked store mechanism instead of implementing a new one.
Follow-up to #1391