Replace BitUtils with C++20: RotateLeft/RotateRight

Now that we've flipped the C++20 switch, let's start making use of
the nice new <bit> header.

I'm planning on handling this move away from BitUtils.h incrementally
in a series of PRs. There may be a few functions remaining in
BitUtils.h by the end that C++20 doesn't have any equivalents for.
This commit is contained in:
JosJuice
2022-08-05 15:39:00 +02:00
parent 48ce5318e1
commit 454537d53e
11 changed files with 63 additions and 141 deletions

View File

@ -3,6 +3,7 @@
#pragma once
#include <bit>
#include <cstring>
#include <functional>
#include <optional>
@ -558,15 +559,15 @@ struct LogicalImm
// pick the next sequence of ones. This ensures we get a complete element
// that has not been cut-in-half due to rotation across the word boundary.
const size_t rotation = Common::CountTrailingZeros(value & (value + 1));
const u64 normalized = Common::RotateRight(value, rotation);
const int rotation = Common::CountTrailingZeros(value & (value + 1));
const u64 normalized = std::rotr(value, rotation);
const size_t element_size = Common::CountTrailingZeros(normalized & (normalized + 1));
const size_t ones = Common::CountTrailingZeros(~normalized);
const int element_size = Common::CountTrailingZeros(normalized & (normalized + 1));
const int ones = Common::CountTrailingZeros(~normalized);
// Check the value is repeating; also ensures element size is a power of two.
if (Common::RotateRight(value, element_size) != value)
if (std::rotr(value, element_size) != value)
{
valid = false;
return;

View File

@ -107,50 +107,6 @@ constexpr Result ExtractBits(const T src) noexcept
return ExtractBits<T, Result>(src, begin, end);
}
///
/// Rotates a value left (ROL).
///
/// @param value The value to rotate.
/// @param amount The number of bits to rotate the value.
/// @tparam T An unsigned type.
///
/// @return The rotated value.
///
template <typename T>
constexpr T RotateLeft(const T value, size_t amount) noexcept
{
static_assert(std::is_unsigned<T>(), "Can only rotate unsigned types left.");
amount %= BitSize<T>();
if (amount == 0)
return value;
return static_cast<T>((value << amount) | (value >> (BitSize<T>() - amount)));
}
///
/// Rotates a value right (ROR).
///
/// @param value The value to rotate.
/// @param amount The number of bits to rotate the value.
/// @tparam T An unsigned type.
///
/// @return The rotated value.
///
template <typename T>
constexpr T RotateRight(const T value, size_t amount) noexcept
{
static_assert(std::is_unsigned<T>(), "Can only rotate unsigned types right.");
amount %= BitSize<T>();
if (amount == 0)
return value;
return static_cast<T>((value >> amount) | (value << (BitSize<T>() - amount)));
}
///
/// Verifies whether the supplied value is a valid bit mask of the form 0b00...0011...11.
/// Both edge cases of all zeros and all ones are considered valid masks, too.

View File

@ -2,12 +2,12 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include <array>
#include <bit>
#include <memory>
#include <mbedtls/aes.h>
#include "Common/Assert.h"
#include "Common/BitUtils.h"
#include "Common/CPUDetect.h"
#include "Common/Crypto/AES.h"
@ -294,7 +294,7 @@ public:
{
const uint8x16_t enc = vaeseq_u8(vreinterpretq_u8_u32(vmovq_n_u32(rk[i + 3])), vmovq_n_u8(0));
const u32 temp = vgetq_lane_u32(vreinterpretq_u32_u8(enc), 0);
rk[i + 4] = rk[i + 0] ^ Common::RotateRight(temp, 8) ^ rcon[i / Nk];
rk[i + 4] = rk[i + 0] ^ std::rotr(temp, 8) ^ rcon[i / Nk];
rk[i + 5] = rk[i + 4] ^ rk[i + 1];
rk[i + 6] = rk[i + 5] ^ rk[i + 2];
rk[i + 7] = rk[i + 6] ^ rk[i + 3];

View File

@ -4,7 +4,9 @@
#include "Common/Hash.h"
#include <algorithm>
#include <bit>
#include <cstring>
#include <zlib.h>
#include "Common/BitUtils.h"
@ -60,15 +62,15 @@ static u64 getblock(const u64* p, int i)
static void bmix64(u64& h1, u64& h2, u64& k1, u64& k2, u64& c1, u64& c2)
{
k1 *= c1;
k1 = Common::RotateLeft(k1, 23);
k1 = std::rotl(k1, 23);
k1 *= c2;
h1 ^= k1;
h1 += h2;
h2 = Common::RotateLeft(h2, 41);
h2 = std::rotl(h2, 41);
k2 *= c2;
k2 = Common::RotateLeft(k2, 23);
k2 = std::rotl(k2, 23);
k2 *= c1;
h2 ^= k2;
h2 += h1;