1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___BIT_ROTATE_H
10 #define _LIBCPP___BIT_ROTATE_H
11 
12 #include <__concepts/arithmetic.h>
13 #include <__config>
14 #include <__type_traits/is_unsigned_integer.h>
15 #include <limits>
16 
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 #  pragma GCC system_header
19 #endif
20 
21 _LIBCPP_BEGIN_NAMESPACE_STD
22 
23 template<class _Tp>
24 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
25 _Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
26 {
27     static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type");
28     const unsigned int __dig = numeric_limits<_Tp>::digits;
29     if ((__cnt % __dig) == 0)
30         return __t;
31     return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig)));
32 }
33 
34 #if _LIBCPP_STD_VER >= 20
35 
36 template <__libcpp_unsigned_integer _Tp>
37 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp rotl(_Tp __t, unsigned int __cnt) noexcept {
38   const unsigned int __dig = numeric_limits<_Tp>::digits;
39   if ((__cnt % __dig) == 0)
40     return __t;
41   return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig)));
42 }
43 
44 template <__libcpp_unsigned_integer _Tp>
45 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp rotr(_Tp __t, unsigned int __cnt) noexcept {
46   return std::__rotr(__t, __cnt);
47 }
48 
49 #endif // _LIBCPP_STD_VER >= 20
50 
51 _LIBCPP_END_NAMESPACE_STD
52 
53 #endif // _LIBCPP___BIT_ROTATE_H
54