1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-31 18:55:23 +01:00
Files
OpenRCT2/src/openrct2/core/Numerics.hpp
2020-07-21 15:04:34 +02:00

72 lines
2.3 KiB
C++

/*****************************************************************************
* Copyright (c) 2014-2020 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#pragma once
#include <cstddef>
#include <cstdint>
#include <limits>
#include <type_traits>
namespace Numerics
{
/**
* Bitwise left rotate
* @tparam _UIntType unsigned integral type
* @param x value
* @param shift positions to shift
* @return rotated value
*/
template<typename _UIntType> static constexpr _UIntType rol(_UIntType x, size_t shift)
{
static_assert(std::is_unsigned<_UIntType>::value, "result_type must be an unsigned integral type");
using limits = typename std::numeric_limits<_UIntType>;
return ((static_cast<_UIntType>(x) << shift) | (static_cast<_UIntType>(x) >> (limits::digits - shift)));
}
/**
* Bitwise left rotate of lowest 4 bits
* @param x unsigned 8-bit integer value
* @param shift positions to shift
* @return rotated value
*/
[[maybe_unused]] static constexpr uint8_t rol4(uint8_t x, size_t shift)
{
x &= 0x0F;
return (x << shift | x >> (4 - shift)) & 0x0F;
}
/**
* Bitwise right rotate
* @tparam _UIntType unsigned integral type
* @param x value
* @param shift positions to shift
* @return rotated value
*/
template<typename _UIntType> static constexpr _UIntType ror(_UIntType x, size_t shift)
{
static_assert(std::is_unsigned<_UIntType>::value, "result_type must be an unsigned integral type");
using limits = std::numeric_limits<_UIntType>;
return ((static_cast<_UIntType>(x) >> shift) | (static_cast<_UIntType>(x) << (limits::digits - shift)));
}
/**
* Bitwise right rotate of lowest 4 bits
* @param x unsigned 8-bit integer value
* @param shift positions to shift
* @return rotated value
*/
[[maybe_unused]] static constexpr uint8_t ror4(uint8_t x, size_t shift)
{
x &= 0x0F;
return (x >> shift | x << (4 - shift)) & 0x0F;
}
} // namespace Numerics