1bdd1243dSDimitry Andric //===----------------------------------------------------------------------===// 2bdd1243dSDimitry Andric // 3bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6bdd1243dSDimitry Andric // 7bdd1243dSDimitry Andric //===----------------------------------------------------------------------===// 8bdd1243dSDimitry Andric 9bdd1243dSDimitry Andric #ifndef _LIBCPP___BIT_ROTATE_H 10bdd1243dSDimitry Andric #define _LIBCPP___BIT_ROTATE_H 11bdd1243dSDimitry Andric 12bdd1243dSDimitry Andric #include <__concepts/arithmetic.h> 13bdd1243dSDimitry Andric #include <__config> 14bdd1243dSDimitry Andric #include <__type_traits/is_unsigned_integer.h> 15bdd1243dSDimitry Andric #include <limits> 16bdd1243dSDimitry Andric 17bdd1243dSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 18bdd1243dSDimitry Andric # pragma GCC system_header 19bdd1243dSDimitry Andric #endif 20bdd1243dSDimitry Andric 21bdd1243dSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 22bdd1243dSDimitry Andric 23*62987288SDimitry Andric // Writing two full functions for rotl and rotr makes it easier for the compiler 24*62987288SDimitry Andric // to optimize the code. On x86 this function becomes the ROL instruction and 25*62987288SDimitry Andric // the rotr function becomes the ROR instruction. 26bdd1243dSDimitry Andric template <class _Tp> 27*62987288SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __rotl(_Tp __x, int __s) _NOEXCEPT { 28*62987288SDimitry Andric static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotl requires an unsigned integer type"); 29*62987288SDimitry Andric const int __N = numeric_limits<_Tp>::digits; 30*62987288SDimitry Andric int __r = __s % __N; 31*62987288SDimitry Andric 32*62987288SDimitry Andric if (__r == 0) 33*62987288SDimitry Andric return __x; 34*62987288SDimitry Andric 35*62987288SDimitry Andric if (__r > 0) 36*62987288SDimitry Andric return (__x << __r) | (__x >> (__N - __r)); 37*62987288SDimitry Andric 38*62987288SDimitry Andric return (__x >> -__r) | (__x << (__N + __r)); 39*62987288SDimitry Andric } 40*62987288SDimitry Andric 41*62987288SDimitry Andric template <class _Tp> 42*62987288SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __rotr(_Tp __x, int __s) _NOEXCEPT { 43bdd1243dSDimitry Andric static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type"); 44*62987288SDimitry Andric const int __N = numeric_limits<_Tp>::digits; 45*62987288SDimitry Andric int __r = __s % __N; 465f757f3fSDimitry Andric 47*62987288SDimitry Andric if (__r == 0) 48*62987288SDimitry Andric return __x; 495f757f3fSDimitry Andric 50*62987288SDimitry Andric if (__r > 0) 51*62987288SDimitry Andric return (__x >> __r) | (__x << (__N - __r)); 52bdd1243dSDimitry Andric 53*62987288SDimitry Andric return (__x << -__r) | (__x >> (__N + __r)); 545f757f3fSDimitry Andric } 555f757f3fSDimitry Andric 56bdd1243dSDimitry Andric #if _LIBCPP_STD_VER >= 20 57bdd1243dSDimitry Andric 58bdd1243dSDimitry Andric template <__libcpp_unsigned_integer _Tp> 595f757f3fSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp rotl(_Tp __t, int __cnt) noexcept { 605f757f3fSDimitry Andric return std::__rotl(__t, __cnt); 61bdd1243dSDimitry Andric } 62bdd1243dSDimitry Andric 63bdd1243dSDimitry Andric template <__libcpp_unsigned_integer _Tp> 645f757f3fSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp rotr(_Tp __t, int __cnt) noexcept { 65bdd1243dSDimitry Andric return std::__rotr(__t, __cnt); 66bdd1243dSDimitry Andric } 67bdd1243dSDimitry Andric 68bdd1243dSDimitry Andric #endif // _LIBCPP_STD_VER >= 20 69bdd1243dSDimitry Andric 70bdd1243dSDimitry Andric _LIBCPP_END_NAMESPACE_STD 71bdd1243dSDimitry Andric 72bdd1243dSDimitry Andric #endif // _LIBCPP___BIT_ROTATE_H 73