Path: blob/main/contrib/llvm-project/libcxx/include/__bit/rotate.h
35260 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___BIT_ROTATE_H9#define _LIBCPP___BIT_ROTATE_H1011#include <__concepts/arithmetic.h>12#include <__config>13#include <__type_traits/is_unsigned_integer.h>14#include <limits>1516#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)17# pragma GCC system_header18#endif1920_LIBCPP_BEGIN_NAMESPACE_STD2122// Writing two full functions for rotl and rotr makes it easier for the compiler23// to optimize the code. On x86 this function becomes the ROL instruction and24// the rotr function becomes the ROR instruction.25template <class _Tp>26_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __rotl(_Tp __x, int __s) _NOEXCEPT {27static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotl requires an unsigned integer type");28const int __N = numeric_limits<_Tp>::digits;29int __r = __s % __N;3031if (__r == 0)32return __x;3334if (__r > 0)35return (__x << __r) | (__x >> (__N - __r));3637return (__x >> -__r) | (__x << (__N + __r));38}3940template <class _Tp>41_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __rotr(_Tp __x, int __s) _NOEXCEPT {42static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type");43const int __N = numeric_limits<_Tp>::digits;44int __r = __s % __N;4546if (__r == 0)47return __x;4849if (__r > 0)50return (__x >> __r) | (__x << (__N - __r));5152return (__x << -__r) | (__x >> (__N + __r));53}5455#if _LIBCPP_STD_VER >= 205657template <__libcpp_unsigned_integer _Tp>58[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp rotl(_Tp __t, int __cnt) noexcept {59return std::__rotl(__t, __cnt);60}6162template <__libcpp_unsigned_integer _Tp>63[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp rotr(_Tp __t, int __cnt) noexcept {64return std::__rotr(__t, __cnt);65}6667#endif // _LIBCPP_STD_VER >= 206869_LIBCPP_END_NAMESPACE_STD7071#endif // _LIBCPP___BIT_ROTATE_H727374