1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___BIT_ROTATE_H
10 #define _LIBCPP___BIT_ROTATE_H
12 #include <__concepts/arithmetic.h>
14 #include <__type_traits/is_unsigned_integer.h>
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 # pragma GCC system_header
21 _LIBCPP_BEGIN_NAMESPACE_STD
24 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp
__rotr(_Tp __t
, int __cnt
) _NOEXCEPT
{
25 static_assert(__libcpp_is_unsigned_integer
<_Tp
>::value
, "__rotr requires an unsigned integer type");
26 const unsigned int __dig
= numeric_limits
<_Tp
>::digits
;
27 if ((__cnt
% __dig
) == 0)
32 return (__t
<< (__cnt
% __dig
)) | (__t
>> (__dig
- (__cnt
% __dig
))); // rotr with negative __cnt is similar to rotl
35 return (__t
>> (__cnt
% __dig
)) | (__t
<< (__dig
- (__cnt
% __dig
)));
39 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp
__rotl(_Tp __t
, int __cnt
) _NOEXCEPT
{
40 return std::__rotr(__t
, -__cnt
);
43 #if _LIBCPP_STD_VER >= 20
45 template <__libcpp_unsigned_integer _Tp
>
46 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
rotl(_Tp __t
, int __cnt
) noexcept
{
47 return std::__rotl(__t
, __cnt
);
50 template <__libcpp_unsigned_integer _Tp
>
51 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
rotr(_Tp __t
, int __cnt
) noexcept
{
52 return std::__rotr(__t
, __cnt
);
55 #endif // _LIBCPP_STD_VER >= 20
57 _LIBCPP_END_NAMESPACE_STD
59 #endif // _LIBCPP___BIT_ROTATE_H