Files
eden/src/common/bit_util.h
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

44 lines
1.1 KiB
C++
Raw Normal View History

// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
2021-01-21 15:30:28 -08:00
#include <bit>
#include <climits>
#include <cstddef>
#include <type_traits>
#include "common/common_types.h"
namespace Common {
/// Gets the size of a specified type T in bits.
template <typename T>
requires std::is_integral_v<T>
[[nodiscard]] constexpr std::size_t BitSize() {
return std::size_t(sizeof(T) * CHAR_BIT);
}
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;
}
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);
}
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
}
} // namespace Common