Skip to content

<bit>: Implement rotl() and rotr() with intrinsics #2367

Description

STL/stl/inc/bit

Lines 92 to 118 in 3c2fd04

template <class _Ty, enable_if_t<_Is_standard_unsigned_integer<_Ty>, int> = 0>
_NODISCARD constexpr _Ty rotl(const _Ty _Val, const int _Rotation) noexcept {
constexpr auto _Digits = numeric_limits<_Ty>::digits;
const auto _Remainder = _Rotation % _Digits;
if (_Remainder > 0) {
return static_cast<_Ty>(
static_cast<_Ty>(_Val << _Remainder) | static_cast<_Ty>(_Val >> (_Digits - _Remainder)));
} else if (_Remainder == 0) {
return _Val;
} else { // _Remainder < 0
return _STD rotr(_Val, -_Remainder);
}
}
template <class _Ty, enable_if_t<_Is_standard_unsigned_integer<_Ty>, int> _Enabled>
_NODISCARD constexpr _Ty rotr(const _Ty _Val, const int _Rotation) noexcept {
constexpr auto _Digits = numeric_limits<_Ty>::digits;
const auto _Remainder = _Rotation % _Digits;
if (_Remainder > 0) {
return static_cast<_Ty>(
static_cast<_Ty>(_Val >> _Remainder) | static_cast<_Ty>(_Val << (_Digits - _Remainder)));
} else if (_Remainder == 0) {
return _Val;
} else { // _Remainder < 0
return _STD rotl(_Val, -_Remainder);
}
}

intrin.h declares:

__MACHINE(unsigned char __cdecl _rotl8(unsigned char _Value, unsigned char _Shift))
__MACHINE(unsigned short __cdecl _rotl16(unsigned short _Value, unsigned char _Shift))
__MACHINE(unsigned int __cdecl _rotl(_In_ unsigned int _Value, _In_ int _Shift))
__MACHINE(unsigned __int64 __cdecl _rotl64(_In_ unsigned __int64 _Value, _In_ int _Shift))

__MACHINE(unsigned char __cdecl _rotr8(unsigned char _Value, unsigned char _Shift))
__MACHINE(unsigned short __cdecl _rotr16(unsigned short _Value, unsigned char _Shift))
__MACHINE(unsigned int __cdecl _rotr(_In_ unsigned int _Value, _In_ int _Shift))
__MACHINE(unsigned __int64 __cdecl _rotr64(_In_ unsigned __int64 _Value, _In_ int _Shift))
  • We'll need to move these to intrin0.h (physically, intrin0.inl.h) if we want throughput.
  • We'll need to make sure this works for Clang.
  • <bit> is C++20, so we don't need to worry about /clr:pure (for sure), CUDA (for sure), or the Intel C++ Compiler (I think).

Originally reported as DevCom-1590917 and VSO-1440998 / AB#1440998 .

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixedSomething works now, yay!performanceMust go faster

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions