summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/CMakeLists.txt2
-rw-r--r--src/common/alignment.h6
-rw-r--r--src/common/binary_find.h21
-rw-r--r--src/common/bit_util.h44
-rw-r--r--src/common/common_funcs.h1
5 files changed, 74 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 198b3fe07..2554add28 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -44,6 +44,7 @@ add_custom_command(OUTPUT scm_rev.cpp
44 "${VIDEO_CORE}/shader/decode/half_set.cpp" 44 "${VIDEO_CORE}/shader/decode/half_set.cpp"
45 "${VIDEO_CORE}/shader/decode/half_set_predicate.cpp" 45 "${VIDEO_CORE}/shader/decode/half_set_predicate.cpp"
46 "${VIDEO_CORE}/shader/decode/hfma2.cpp" 46 "${VIDEO_CORE}/shader/decode/hfma2.cpp"
47 "${VIDEO_CORE}/shader/decode/image.cpp"
47 "${VIDEO_CORE}/shader/decode/integer_set.cpp" 48 "${VIDEO_CORE}/shader/decode/integer_set.cpp"
48 "${VIDEO_CORE}/shader/decode/integer_set_predicate.cpp" 49 "${VIDEO_CORE}/shader/decode/integer_set_predicate.cpp"
49 "${VIDEO_CORE}/shader/decode/memory.cpp" 50 "${VIDEO_CORE}/shader/decode/memory.cpp"
@@ -74,6 +75,7 @@ add_library(common STATIC
74 assert.h 75 assert.h
75 detached_tasks.cpp 76 detached_tasks.cpp
76 detached_tasks.h 77 detached_tasks.h
78 binary_find.h
77 bit_field.h 79 bit_field.h
78 bit_util.h 80 bit_util.h
79 cityhash.cpp 81 cityhash.cpp
diff --git a/src/common/alignment.h b/src/common/alignment.h
index d94a2291f..617b14d9b 100644
--- a/src/common/alignment.h
+++ b/src/common/alignment.h
@@ -20,6 +20,12 @@ constexpr T AlignDown(T value, std::size_t size) {
20} 20}
21 21
22template <typename T> 22template <typename T>
23constexpr T AlignBits(T value, std::size_t align) {
24 static_assert(std::is_unsigned_v<T>, "T must be an unsigned value.");
25 return static_cast<T>((value + ((1ULL << align) - 1)) >> align << align);
26}
27
28template <typename T>
23constexpr bool Is4KBAligned(T value) { 29constexpr bool Is4KBAligned(T value) {
24 static_assert(std::is_unsigned_v<T>, "T must be an unsigned value."); 30 static_assert(std::is_unsigned_v<T>, "T must be an unsigned value.");
25 return (value & 0xFFF) == 0; 31 return (value & 0xFFF) == 0;
diff --git a/src/common/binary_find.h b/src/common/binary_find.h
new file mode 100644
index 000000000..5cc523bf9
--- /dev/null
+++ b/src/common/binary_find.h
@@ -0,0 +1,21 @@
1// Copyright 2019 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <algorithm>
8
9namespace Common {
10
11template <class ForwardIt, class T, class Compare = std::less<>>
12ForwardIt BinaryFind(ForwardIt first, ForwardIt last, const T& value, Compare comp = {}) {
13 // Note: BOTH type T and the type after ForwardIt is dereferenced
14 // must be implicitly convertible to BOTH Type1 and Type2, used in Compare.
15 // This is stricter than lower_bound requirement (see above)
16
17 first = std::lower_bound(first, last, value, comp);
18 return first != last && !comp(value, *first) ? first : last;
19}
20
21} // namespace Common
diff --git a/src/common/bit_util.h b/src/common/bit_util.h
index d032df413..6f7d5a947 100644
--- a/src/common/bit_util.h
+++ b/src/common/bit_util.h
@@ -97,4 +97,48 @@ inline u32 CountTrailingZeroes64(u64 value) {
97} 97}
98#endif 98#endif
99 99
100#ifdef _MSC_VER
101
102inline u32 MostSignificantBit32(const u32 value) {
103 unsigned long result;
104 _BitScanReverse(&result, value);
105 return static_cast<u32>(result);
106}
107
108inline u32 MostSignificantBit64(const u64 value) {
109 unsigned long result;
110 _BitScanReverse64(&result, value);
111 return static_cast<u32>(result);
112}
113
114#else
115
116inline u32 MostSignificantBit32(const u32 value) {
117 return 31U - static_cast<u32>(__builtin_clz(value));
118}
119
120inline u32 MostSignificantBit64(const u64 value) {
121 return 63U - static_cast<u32>(__builtin_clzll(value));
122}
123
124#endif
125
126inline u32 Log2Floor32(const u32 value) {
127 return MostSignificantBit32(value);
128}
129
130inline u32 Log2Ceil32(const u32 value) {
131 const u32 log2_f = Log2Floor32(value);
132 return log2_f + ((value ^ (1U << log2_f)) != 0U);
133}
134
135inline u32 Log2Floor64(const u64 value) {
136 return MostSignificantBit64(value);
137}
138
139inline u32 Log2Ceil64(const u64 value) {
140 const u64 log2_f = static_cast<u64>(Log2Floor64(value));
141 return static_cast<u32>(log2_f + ((value ^ (1ULL << log2_f)) != 0ULL));
142}
143
100} // namespace Common 144} // namespace Common
diff --git a/src/common/common_funcs.h b/src/common/common_funcs.h
index 8b0d34da6..04ecac959 100644
--- a/src/common/common_funcs.h
+++ b/src/common/common_funcs.h
@@ -4,6 +4,7 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <algorithm>
7#include <string> 8#include <string>
8 9
9#if !defined(ARCHITECTURE_x86_64) 10#if !defined(ARCHITECTURE_x86_64)