Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:13

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___BIT_ROTATE_H
0010 #define _LIBCPP___BIT_ROTATE_H
0011 
0012 #include <__concepts/arithmetic.h>
0013 #include <__config>
0014 #include <__type_traits/is_unsigned_integer.h>
0015 #include <limits>
0016 
0017 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0018 #  pragma GCC system_header
0019 #endif
0020 
0021 _LIBCPP_BEGIN_NAMESPACE_STD
0022 
0023 // Writing two full functions for rotl and rotr makes it easier for the compiler
0024 // to optimize the code. On x86 this function becomes the ROL instruction and
0025 // the rotr function becomes the ROR instruction.
0026 template <class _Tp>
0027 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __rotl(_Tp __x, int __s) _NOEXCEPT {
0028   static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotl requires an unsigned integer type");
0029   const int __n = numeric_limits<_Tp>::digits;
0030   int __r       = __s % __n;
0031 
0032   if (__r == 0)
0033     return __x;
0034 
0035   if (__r > 0)
0036     return (__x << __r) | (__x >> (__n - __r));
0037 
0038   return (__x >> -__r) | (__x << (__n + __r));
0039 }
0040 
0041 template <class _Tp>
0042 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __rotr(_Tp __x, int __s) _NOEXCEPT {
0043   static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type");
0044   const int __n = numeric_limits<_Tp>::digits;
0045   int __r       = __s % __n;
0046 
0047   if (__r == 0)
0048     return __x;
0049 
0050   if (__r > 0)
0051     return (__x >> __r) | (__x << (__n - __r));
0052 
0053   return (__x << -__r) | (__x >> (__n + __r));
0054 }
0055 
0056 #if _LIBCPP_STD_VER >= 20
0057 
0058 template <__libcpp_unsigned_integer _Tp>
0059 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp rotl(_Tp __t, int __cnt) noexcept {
0060   return std::__rotl(__t, __cnt);
0061 }
0062 
0063 template <__libcpp_unsigned_integer _Tp>
0064 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp rotr(_Tp __t, int __cnt) noexcept {
0065   return std::__rotr(__t, __cnt);
0066 }
0067 
0068 #endif // _LIBCPP_STD_VER >= 20
0069 
0070 _LIBCPP_END_NAMESPACE_STD
0071 
0072 #endif // _LIBCPP___BIT_ROTATE_H