2026-07-09 03:47:04 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
|
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
2022-04-23 04:59:50 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-12-19 15:25:12 -05:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2021-01-21 15:30:28 -08:00
|
|
|
#include <bit>
|
2018-12-19 15:25:12 -05:00
|
|
|
#include <climits>
|
|
|
|
|
#include <cstddef>
|
2021-11-20 14:46:19 +01:00
|
|
|
#include <type_traits>
|
2018-12-19 15:25:12 -05:00
|
|
|
|
|
|
|
|
#include "common/common_types.h"
|
|
|
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
|
|
|
|
|
/// Gets the size of a specified type T in bits.
|
|
|
|
|
template <typename T>
|
2026-07-09 03:47:04 +02:00
|
|
|
requires std::is_integral_v<T>
|
2020-08-14 09:38:45 -04:00
|
|
|
[[nodiscard]] constexpr std::size_t BitSize() {
|
2026-07-09 03:47:04 +02:00
|
|
|
return std::size_t(sizeof(T) * CHAR_BIT);
|
2019-05-10 22:12:35 -04:00
|
|
|
}
|
|
|
|
|
|
2026-07-09 03:47:04 +02:00
|
|
|
template<typename T>
|
|
|
|
|
requires std::is_integral_v<T>
|
|
|
|
|
[[nodiscard]] constexpr u32 MostSignificantBit(const T value) {
|
|
|
|
|
return u32(sizeof(T) * CHAR_BIT - 1 - std::countl_zero(value));
|
2019-05-10 22:12:35 -04:00
|
|
|
}
|
|
|
|
|
|
2026-07-09 03:47:04 +02:00
|
|
|
template<typename T>
|
|
|
|
|
requires std::is_integral_v<T>
|
|
|
|
|
[[nodiscard]] constexpr T Log2Floor(const T value) {
|
|
|
|
|
return T(MostSignificantBit<T>(value));
|
2019-05-10 22:12:35 -04:00
|
|
|
}
|
|
|
|
|
|
2026-07-09 03:47:04 +02:00
|
|
|
template<typename T>
|
|
|
|
|
requires std::is_integral_v<T>
|
|
|
|
|
[[nodiscard]] constexpr T Log2Ceil(const T value) {
|
|
|
|
|
const T log2_f = Log2Floor<T>(value);
|
|
|
|
|
return T(log2_f + T((value ^ (T(1ULL) << log2_f)) != T(0ULL)));
|
2022-01-10 19:44:19 -05:00
|
|
|
}
|
|
|
|
|
|
2021-11-20 14:46:19 +01:00
|
|
|
template <typename T>
|
2023-01-29 13:54:13 -07:00
|
|
|
requires std::is_integral_v<T>
|
2021-11-20 14:46:19 +01:00
|
|
|
[[nodiscard]] T NextPow2(T value) {
|
2026-07-09 03:47:04 +02:00
|
|
|
return T(1ULL << (sizeof(T) * CHAR_BIT - std::countl_zero(value - 1U)));
|
2021-11-20 14:46:19 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-23 10:08:32 -08:00
|
|
|
template <size_t bit_index, typename T>
|
2023-01-29 13:54:13 -07:00
|
|
|
requires std::is_integral_v<T>
|
2022-02-23 10:08:32 -08:00
|
|
|
[[nodiscard]] constexpr bool Bit(const T value) {
|
|
|
|
|
static_assert(bit_index < BitSize<T>(), "bit_index must be smaller than size of T");
|
|
|
|
|
return ((value >> bit_index) & T(1)) == T(1);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-19 15:25:12 -05:00
|
|
|
} // namespace Common
|