Skip to content
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

Implement "arithmetic" lane wise shift right. #90

Open
thecppzoo opened this issue Jun 3, 2024 · 0 comments
Open

Implement "arithmetic" lane wise shift right. #90

thecppzoo opened this issue Jun 3, 2024 · 0 comments
Assignees

Comments

@thecppzoo
Copy link
Owner

Currently we support the shifting of lanes both left and right, with the "logical" semantics.
For example, shiftRightLanes:

zoo/inc/zoo/swar/SWAR.h

Lines 144 to 171 in 4a13f5c

constexpr SWAR shiftLanesLeft(int laneCount) const noexcept {
return SWAR(value() << (NBits * laneCount));
}
constexpr SWAR shiftLanesRight(int laneCount) const noexcept {
return SWAR(value() >> (NBits * laneCount));
}
/// \brief as the name suggests
/// \param protectiveMask should clear the bits that would cross the lane.
/// The bits that will be cleared are directly related to the count of
/// shifts, it is natural to maintain the protective mask by the caller,
/// otherwise, the mask would have to be computed in all invocations.
/// We are not sure the optimizer would maintain this mask somewhere, if it
/// were to recalculate it, it would be disastrous for performance
/// \note the \c static_cast are necessary because of narrowing conversions
#define SHIFT_INTRALANE_OP_X_LIST X(Left, <<) X(Right, >>)
#define X(name, op) \
constexpr SWAR \
shiftIntraLane##name(int bitCount, SWAR protectiveMask) const noexcept { \
T shiftC = static_cast<T>(bitCount); \
auto V = (*this & protectiveMask).value(); \
auto rv = static_cast<T>(V op shiftC); \
return SWAR{rv}; \
}
SHIFT_INTRALANE_OP_X_LIST
#undef X
#undef SHIFT_INTRALANE_OP_X_LIST

There are many arithmetic operations that would like to have "arithmetic" shift right, shift right can be used as cheap division by two, then there are very clear use cases that we can anticipate enough for implementing this operation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants