-
Notifications
You must be signed in to change notification settings - Fork 302
feat: add runtime batch_bool mask overloads for load_masked/store_masked #1332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DiamonDinoia
wants to merge
3
commits into
xtensor-stack:master
Choose a base branch
from
DiamonDinoia:feat/dynamic-masks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,18 +117,38 @@ namespace xsimd | |
| } | ||
| } | ||
|
|
||
| // load_masked | ||
| // AVX2 low-level helpers (operate on raw SIMD registers) | ||
| // load_masked / store_masked: AVX2 has _mm256_maskload/maskstore_epi{32,64}; | ||
| // 8/16-bit integers fall back to the common scalar path. | ||
| namespace detail | ||
| { | ||
| XSIMD_INLINE __m256i maskload(const int32_t* mem, __m256i mask) noexcept | ||
| template <class T> | ||
| XSIMD_INLINE __m256i maskload(T const* mem, __m256i mask) noexcept | ||
| { | ||
| return _mm256_maskload_epi32(mem, mask); | ||
| XSIMD_IF_CONSTEXPR(sizeof(T) == 4) | ||
| { | ||
| static_assert(sizeof(int) == 4, "_mm256_maskload_epi32 requires a 4-byte int"); | ||
| return _mm256_maskload_epi32(reinterpret_cast<int const*>(mem), mask); | ||
| } | ||
| else | ||
| { | ||
| static_assert(sizeof(long long) == 8, "_mm256_maskload_epi64 requires an 8-byte long long"); | ||
| return _mm256_maskload_epi64(reinterpret_cast<long long const*>(mem), mask); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here for |
||
| } | ||
| } | ||
|
|
||
| XSIMD_INLINE __m256i maskload(const long long* mem, __m256i mask) noexcept | ||
| template <class T> | ||
| XSIMD_INLINE void maskstore(T* mem, __m256i mask, __m256i src) noexcept | ||
| { | ||
| return _mm256_maskload_epi64(reinterpret_cast<long long const*>(mem), mask); | ||
| XSIMD_IF_CONSTEXPR(sizeof(T) == 4) | ||
| { | ||
| static_assert(sizeof(int) == 4, "_mm256_maskstore_epi32 requires a 4-byte int"); | ||
| _mm256_maskstore_epi32(reinterpret_cast<int*>(mem), mask, src); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and there |
||
| } | ||
| else | ||
| { | ||
| static_assert(sizeof(long long) == 8, "_mm256_maskstore_epi64 requires an 8-byte long long"); | ||
| _mm256_maskstore_epi64(reinterpret_cast<long long*>(mem), mask, src); | ||
| } | ||
| } | ||
|
|
||
| XSIMD_INLINE __m256i zero_extend(__m128i hi) noexcept | ||
|
|
@@ -137,61 +157,22 @@ namespace xsimd | |
| } | ||
| } | ||
|
|
||
| // single templated implementation for integer masked loads (32/64-bit) | ||
| template <class A, class T, bool... Values, class Mode> | ||
| XSIMD_INLINE std::enable_if_t<std::is_integral<T>::value && (sizeof(T) >= 4), batch<T, A>> | ||
| XSIMD_INLINE std::enable_if_t<std::is_integral<T>::value && (sizeof(T) == 4 || sizeof(T) == 8), batch<T, A>> | ||
| load_masked(T const* mem, batch_bool_constant<T, A, Values...> mask, convert<T>, Mode, requires_arch<avx2>) noexcept | ||
| { | ||
| static_assert(sizeof(T) == 4 || sizeof(T) == 8, "load_masked supports only 32/64-bit integers on AVX2"); | ||
| using int_t = std::conditional_t<sizeof(T) == 4, int32_t, long long>; | ||
| // Use the raw register-level maskload helpers for the remaining cases. | ||
| return detail::maskload(reinterpret_cast<const int_t*>(mem), mask.as_batch()); | ||
| return detail::maskload(mem, mask.as_batch()); | ||
| } | ||
|
|
||
| template <class A, bool... Values, class Mode> | ||
| XSIMD_INLINE batch<int32_t, A> load_masked(int32_t const* mem, batch_bool_constant<int32_t, A, Values...> mask, convert<int32_t>, Mode, requires_arch<avx2>) noexcept | ||
| template <class A, class T, class Mode> | ||
| XSIMD_INLINE std::enable_if_t<std::is_integral<T>::value && (sizeof(T) == 4 || sizeof(T) == 8), batch<T, A>> | ||
| load_masked(T const* mem, batch_bool<T, A> mask, convert<T>, Mode, requires_arch<avx2>) noexcept | ||
| { | ||
| return load_masked<A, int32_t>(mem, mask, convert<int32_t> {}, Mode {}, avx2 {}); | ||
| } | ||
|
|
||
| template <class A, bool... Values, class Mode> | ||
| XSIMD_INLINE batch<uint32_t, A> load_masked(uint32_t const* mem, batch_bool_constant<uint32_t, A, Values...>, convert<uint32_t>, Mode, requires_arch<avx2>) noexcept | ||
| { | ||
| const auto r = load_masked<A, int32_t>(reinterpret_cast<int32_t const*>(mem), batch_bool_constant<int32_t, A, Values...> {}, convert<int32_t> {}, Mode {}, avx2 {}); | ||
| return bitwise_cast<uint32_t>(r); | ||
| } | ||
|
|
||
| template <class A, bool... Values, class Mode> | ||
| XSIMD_INLINE batch<int64_t, A> load_masked(int64_t const* mem, batch_bool_constant<int64_t, A, Values...> mask, convert<int64_t>, Mode, requires_arch<avx2>) noexcept | ||
| { | ||
| return load_masked<A, int64_t>(mem, mask, convert<int64_t> {}, Mode {}, avx2 {}); | ||
| } | ||
|
|
||
| template <class A, bool... Values, class Mode> | ||
| XSIMD_INLINE batch<uint64_t, A> load_masked(uint64_t const* mem, batch_bool_constant<uint64_t, A, Values...>, convert<uint64_t>, Mode, requires_arch<avx2>) noexcept | ||
| { | ||
| const auto r = load_masked<A, int64_t>(reinterpret_cast<int64_t const*>(mem), batch_bool_constant<int64_t, A, Values...> {}, convert<int64_t> {}, Mode {}, avx2 {}); | ||
| return bitwise_cast<uint64_t>(r); | ||
| } | ||
|
|
||
| // store_masked | ||
| namespace detail | ||
| { | ||
| template <class T, class A> | ||
| XSIMD_INLINE void maskstore(int32_t* mem, __m256i mask, __m256i src) noexcept | ||
| { | ||
| _mm256_maskstore_epi32(reinterpret_cast<int*>(mem), mask, src); | ||
| } | ||
|
|
||
| template <class T, class A> | ||
| XSIMD_INLINE void maskstore(int64_t* mem, __m256i mask, __m256i src) noexcept | ||
| { | ||
| _mm256_maskstore_epi64(reinterpret_cast<long long*>(mem), mask, src); | ||
| } | ||
| return detail::maskload(mem, __m256i(mask)); | ||
| } | ||
|
|
||
| template <class A, class T, bool... Values, class Mode, | ||
| typename = std::enable_if_t<std::is_integral<T>::value && (sizeof(T) >= 4)>> | ||
| typename = std::enable_if_t<std::is_integral<T>::value && (sizeof(T) == 4 || sizeof(T) == 8)>> | ||
| XSIMD_INLINE void store_masked(T* mem, batch<T, A> const& src, batch_bool_constant<T, A, Values...> mask, Mode, requires_arch<avx2>) noexcept | ||
| { | ||
| constexpr size_t lanes_per_half = batch<T, A>::size / 2; | ||
|
|
@@ -214,22 +195,15 @@ namespace xsimd | |
| } | ||
| else | ||
| { | ||
| detail::maskstore<T, A>(mem, mask.as_batch(), src); | ||
| detail::maskstore(mem, mask.as_batch(), src); | ||
| } | ||
| } | ||
|
|
||
| template <class A, bool... Values, class Mode> | ||
| XSIMD_INLINE void store_masked(uint32_t* mem, batch<uint32_t, A> const& src, batch_bool_constant<uint32_t, A, Values...>, Mode, requires_arch<avx2>) noexcept | ||
| { | ||
| const auto s32 = bitwise_cast<int32_t>(src); | ||
| store_masked<A>(reinterpret_cast<int32_t*>(mem), s32, batch_bool_constant<int32_t, A, Values...> {}, Mode {}, avx2 {}); | ||
| } | ||
|
|
||
| template <class A, bool... Values, class Mode> | ||
| XSIMD_INLINE void store_masked(uint64_t* mem, batch<uint64_t, A> const& src, batch_bool_constant<uint64_t, A, Values...>, Mode, requires_arch<avx2>) noexcept | ||
| template <class A, class T, class Mode> | ||
| XSIMD_INLINE std::enable_if_t<std::is_integral<T>::value && (sizeof(T) == 4 || sizeof(T) == 8), void> | ||
| store_masked(T* mem, batch<T, A> const& src, batch_bool<T, A> mask, Mode, requires_arch<avx2>) noexcept | ||
| { | ||
| const auto s64 = bitwise_cast<int64_t>(src); | ||
| store_masked<A>(reinterpret_cast<int64_t*>(mem), s64, batch_bool_constant<int64_t, A, Values...> {}, Mode {}, avx2 {}); | ||
| detail::maskstore(mem, __m256i(mask), __m256i(src)); | ||
| } | ||
|
|
||
| // load_stream | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you
static_assert(sizeof(int) == 4)here? It's likely that because the condition is not always constexpr (until we reach C++17) you might need to just assert.