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 T Log2Floor(const T value) {
|
2026-09-27 15:50:47 +00:00
|
|
|
return std::bit_width(value) - 1;
|
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) {
|
2026-09-27 15:50:47 +00:00
|
|
|
return std::bit_width(value - 1);
|
2021-11-20 14:46:19 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-23 10:08:32 -08:00
|
|
|
template <size_t bit_index, typename T>
|
2026-09-27 15:50:47 +00:00
|
|
|
requires (std::is_integral_v<T> && bit_index < BitSize<T>())
|
2022-02-23 10:08:32 -08:00
|
|
|
[[nodiscard]] constexpr bool Bit(const T value) {
|
2026-09-27 15:50:47 +00:00
|
|
|
return (T(value >> bit_index) & T(1)) == T(1);
|
2022-02-23 10:08:32 -08:00
|
|
|
}
|
|
|
|
|
|
2018-12-19 15:25:12 -05:00
|
|
|
} // namespace Common
|