-
Notifications
You must be signed in to change notification settings - Fork 296
Add shift by multiple constant #1220
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
AntoinePrv
wants to merge
7
commits into
xtensor-stack:master
Choose a base branch
from
AntoinePrv:shift-var
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.
+249
−10
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b135fbb
Fix warning
AntoinePrv 975c642
Fix apple detection
AntoinePrv 8baeb4c
Add bitwise-shift batch constant api
AntoinePrv 8f93dc6
Add x86 optimizations
AntoinePrv 6da647a
Fix merge
AntoinePrv 0aea244
Add single shift optimization
AntoinePrv ceb6683
Strenghen tests
AntoinePrv 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /*************************************************************************** | ||
| * Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and * | ||
| * Martin Renou * | ||
| * Copyright (c) QuantStack * | ||
| * Copyright (c) Serge Guelton * | ||
| * Copyright (c) Marco Barbone * | ||
| * * | ||
| * Distributed under the terms of the BSD 3-Clause License. * | ||
| * * | ||
| * The full license is in the file LICENSE, distributed with this software. * | ||
| ****************************************************************************/ | ||
|
|
||
| #ifndef XSIMD_UTILS_SHIFTS_HPP | ||
| #define XSIMD_UTILS_SHIFTS_HPP | ||
|
|
||
| #include "../../config/xsimd_inline.hpp" | ||
| #include "../../types/xsimd_batch.hpp" | ||
| #include "../../types/xsimd_batch_constant.hpp" | ||
|
|
||
| namespace xsimd | ||
| { | ||
| namespace kernel | ||
| { | ||
| namespace utils | ||
| { | ||
| template <typename I, I offset, I length, I... Vs> | ||
| struct select_stride | ||
| { | ||
| static constexpr I values_array[] = { Vs... }; | ||
|
|
||
| template <typename K> | ||
| static constexpr K get(K i, K) | ||
| { | ||
| return static_cast<K>(values_array[length * i + offset]); | ||
| } | ||
| }; | ||
|
|
||
| template <typename I> | ||
| constexpr I lsb_mask(I bit_index) | ||
| { | ||
| if (bit_index == 8 * sizeof(I)) | ||
| { | ||
| return ~I { 0 }; | ||
| } | ||
| return static_cast<I>((I { 1 } << bit_index) - I { 1 }); | ||
| } | ||
|
|
||
| template <class T, class A, T... Vs> | ||
| constexpr bool all_equals(batch_constant<T, A, Vs...> c) | ||
| { | ||
| static_assert(sizeof...(Vs) > 0, "There must be at least one value"); | ||
|
|
||
| bool out = true; | ||
| for (std::size_t k = 0; k < sizeof...(Vs); ++k) | ||
| { | ||
| out &= c.get(k) == c.get(0); | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| template <class T, class T2, class A, T... Vs> | ||
| XSIMD_INLINE batch<T, A> bitwise_lshift_as_twice_larger( | ||
| batch<T, A> const& self, batch_constant<T, A, Vs...>) noexcept | ||
| { | ||
| static_assert(sizeof(T2) == 2 * sizeof(T), "One size must be twice the other"); | ||
|
|
||
| const auto self2 = bitwise_cast<T2>(self); | ||
|
|
||
| // Lower byte: shift as twice the size and mask bits flowing to higher byte. | ||
| constexpr auto shifts_lo = make_batch_constant<T2, select_stride<T, 0, 2, Vs...>, A>(); | ||
| constexpr auto mask_lo = lsb_mask<T2>(8 * sizeof(T)); | ||
| const auto shifted_lo = bitwise_lshift(self2, shifts_lo); | ||
| constexpr auto batch_mask_lo = make_batch_constant<T2, mask_lo, A>(); | ||
| const auto masked_lo = bitwise_and(shifted_lo, batch_mask_lo.as_batch()); | ||
|
|
||
| // Higher byte: mask bits that would flow from lower byte and shift as twice the size. | ||
| constexpr auto shifts_hi = make_batch_constant<T2, select_stride<T, 1, 2, Vs...>, A>(); | ||
| constexpr auto mask_hi = mask_lo << (8 * sizeof(T)); | ||
| constexpr auto batch_mask_hi = make_batch_constant<T2, mask_hi, A>(); | ||
| const auto masked_hi = bitwise_and(self2, batch_mask_hi.as_batch()); | ||
| const auto shifted_hi = bitwise_lshift(masked_hi, shifts_hi); | ||
|
|
||
| return bitwise_cast<T>(bitwise_or(masked_lo, shifted_hi)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #endif |
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 |
|---|---|---|
|
|
@@ -353,6 +353,41 @@ namespace xsimd | |
| return kernel::bitwise_cast<A>(x, batch<T_out, A> {}, A {}); | ||
| } | ||
|
|
||
| namespace detail | ||
| { | ||
| // Detection for kernel overloads accepting ``batch_constant`` in ``bitwise_lshift`` | ||
| // directly (or in a parent register function). | ||
| // The ``batch_constant`` overload is a rare but useful optimization. | ||
| // Running the detection here is less error prone than to add a fallback to all | ||
| // architectures. | ||
|
|
||
| template <class A, class B, class C, class = void> | ||
| struct has_bitwise_lshift_batch_const : std::false_type | ||
| { | ||
| }; | ||
|
|
||
| template <class A, class B, class C> | ||
| struct has_bitwise_lshift_batch_const<A, B, C, | ||
| void_t<decltype(kernel::bitwise_lshift<A>(std::declval<B>(), std::declval<C>(), A {}))>> | ||
| : std::true_type | ||
| { | ||
| }; | ||
|
Comment on lines
+356
to
+374
Contributor
Author
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. We run the detection of an optimization in the |
||
|
|
||
| template <class A, class T, T... Values> | ||
| XSIMD_INLINE batch<T, A> bitwise_lshift_batch_const(batch<T, A> const& x, batch_constant<T, A, Values...> shift, std::true_type) noexcept | ||
| { | ||
| // Optimized ``batch_constant`` implementation | ||
| return kernel::bitwise_lshift<A>(x, shift, A {}); | ||
| } | ||
|
|
||
| template <class A, class T, T... Values> | ||
| XSIMD_INLINE batch<T, A> bitwise_lshift_batch_const(batch<T, A> const& x, batch_constant<T, A, Values...> shift, std::false_type) noexcept | ||
| { | ||
| // Fallback to regular run-time implementation | ||
| return kernel::bitwise_lshift<A>(x, shift.as_batch(), A {}); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @ingroup batch_bitwise | ||
| * | ||
|
|
@@ -367,17 +402,24 @@ namespace xsimd | |
| detail::static_check_supported_config<T, A>(); | ||
| return kernel::bitwise_lshift<A>(x, shift, A {}); | ||
| } | ||
| template <size_t shift, class T, class A> | ||
| XSIMD_INLINE batch<T, A> bitwise_lshift(batch<T, A> const& x) noexcept | ||
| { | ||
| detail::static_check_supported_config<T, A>(); | ||
| return kernel::bitwise_lshift<shift, A>(x, A {}); | ||
| } | ||
| template <class T, class A> | ||
| XSIMD_INLINE batch<T, A> bitwise_lshift(batch<T, A> const& x, batch<T, A> const& shift) noexcept | ||
| { | ||
| detail::static_check_supported_config<T, A>(); | ||
| return kernel::bitwise_lshift<A>(x, shift, A {}); | ||
| } | ||
| template <size_t shift, class T, class A> | ||
| XSIMD_INLINE batch<T, A> bitwise_lshift(batch<T, A> const& x) noexcept | ||
| template <class T, class A, T... Values> | ||
| XSIMD_INLINE batch<T, A> bitwise_lshift(batch<T, A> const& x, batch_constant<T, A, Values...> shift) noexcept | ||
| { | ||
| detail::static_check_supported_config<T, A>(); | ||
| return kernel::bitwise_lshift<shift, A>(x, A {}); | ||
| using has_batch_const_impl = detail::has_bitwise_lshift_batch_const<A, decltype(x), decltype(shift)>; | ||
| return detail::bitwise_lshift_batch_const<A>(x, shift, has_batch_const_impl {}); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
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
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.
I named this
utilascommonseems to be more used for implementing thecommonarchitecture.