Path: blob/main/contrib/llvm-project/libcxx/include/__cxx03/__bit/rotate.h
213799 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___CXX03___BIT_ROTATE_H9#define _LIBCPP___CXX03___BIT_ROTATE_H1011#include <__cxx03/__config>12#include <__cxx03/__type_traits/is_unsigned_integer.h>13#include <__cxx03/limits>1415#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)16# pragma GCC system_header17#endif1819_LIBCPP_BEGIN_NAMESPACE_STD2021// Writing two full functions for rotl and rotr makes it easier for the compiler22// to optimize the code. On x86 this function becomes the ROL instruction and23// the rotr function becomes the ROR instruction.24template <class _Tp>25_LIBCPP_HIDE_FROM_ABI _Tp __rotl(_Tp __x, int __s) _NOEXCEPT {26static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotl requires an unsigned integer type");27const int __N = numeric_limits<_Tp>::digits;28int __r = __s % __N;2930if (__r == 0)31return __x;3233if (__r > 0)34return (__x << __r) | (__x >> (__N - __r));3536return (__x >> -__r) | (__x << (__N + __r));37}3839template <class _Tp>40_LIBCPP_HIDE_FROM_ABI _Tp __rotr(_Tp __x, int __s) _NOEXCEPT {41static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type");42const int __N = numeric_limits<_Tp>::digits;43int __r = __s % __N;4445if (__r == 0)46return __x;4748if (__r > 0)49return (__x >> __r) | (__x << (__N - __r));5051return (__x << -__r) | (__x >> (__N + __r));52}5354_LIBCPP_END_NAMESPACE_STD5556#endif // _LIBCPP___CXX03___BIT_ROTATE_H575859