summaryrefslogtreecommitdiffstats
path: root/src/common/alignment.h
diff options
context:
space:
mode:
authorLiam <byteslice@airmail.cc>2023-08-11 03:34:43 +0200
committerLiam <byteslice@airmail.cc>2023-08-15 23:47:25 +0200
commit86f6b6b7b2d930e8203114332b04a5c49a780b06 (patch)
treebf7ff58b0a36051d3c3489a40999d80357c570d0 /src/common/alignment.h
parentMerge pull request #11287 from liamwhite/replaced-bytes (diff)
downloadyuzu-86f6b6b7b2d930e8203114332b04a5c49a780b06.tar
yuzu-86f6b6b7b2d930e8203114332b04a5c49a780b06.tar.gz
yuzu-86f6b6b7b2d930e8203114332b04a5c49a780b06.tar.bz2
yuzu-86f6b6b7b2d930e8203114332b04a5c49a780b06.tar.lz
yuzu-86f6b6b7b2d930e8203114332b04a5c49a780b06.tar.xz
yuzu-86f6b6b7b2d930e8203114332b04a5c49a780b06.tar.zst
yuzu-86f6b6b7b2d930e8203114332b04a5c49a780b06.zip
Diffstat (limited to '')
-rw-r--r--src/common/alignment.h29
1 files changed, 27 insertions, 2 deletions
diff --git a/src/common/alignment.h b/src/common/alignment.h
index fa715d497..0057052af 100644
--- a/src/common/alignment.h
+++ b/src/common/alignment.h
@@ -3,6 +3,7 @@
#pragma once
+#include <bit>
#include <cstddef>
#include <new>
#include <type_traits>
@@ -10,7 +11,7 @@
namespace Common {
template <typename T>
- requires std::is_unsigned_v<T>
+ requires std::is_integral_v<T>
[[nodiscard]] constexpr T AlignUp(T value, size_t size) {
auto mod{static_cast<T>(value % size)};
value -= mod;
@@ -24,7 +25,7 @@ template <typename T>
}
template <typename T>
- requires std::is_unsigned_v<T>
+ requires std::is_integral_v<T>
[[nodiscard]] constexpr T AlignDown(T value, size_t size) {
return static_cast<T>(value - value % size);
}
@@ -55,6 +56,30 @@ template <typename T, typename U>
return (x + (y - 1)) / y;
}
+template <typename T>
+ requires std::is_integral_v<T>
+[[nodiscard]] constexpr T LeastSignificantOneBit(T x) {
+ return x & ~(x - 1);
+}
+
+template <typename T>
+ requires std::is_integral_v<T>
+[[nodiscard]] constexpr T ResetLeastSignificantOneBit(T x) {
+ return x & (x - 1);
+}
+
+template <typename T>
+ requires std::is_integral_v<T>
+[[nodiscard]] constexpr bool IsPowerOfTwo(T x) {
+ return x > 0 && ResetLeastSignificantOneBit(x) == 0;
+}
+
+template <typename T>
+ requires std::is_integral_v<T>
+[[nodiscard]] constexpr T FloorPowerOfTwo(T x) {
+ return T{1} << (sizeof(T) * 8 - std::countl_zero(x) - 1);
+}
+
template <typename T, size_t Align = 16>
class AlignmentAllocator {
public: