summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/alignment.h4
-rw-r--r--src/common/bit_set.h2
-rw-r--r--src/common/file_util.h22
-rw-r--r--src/common/hash.h4
-rw-r--r--src/common/vector_math.h362
-rw-r--r--src/common/x64/xbyak_util.h2
6 files changed, 201 insertions, 195 deletions
diff --git a/src/common/alignment.h b/src/common/alignment.h
index b77da4a92..b9dd38746 100644
--- a/src/common/alignment.h
+++ b/src/common/alignment.h
@@ -9,13 +9,13 @@ namespace Common {
9 9
10template <typename T> 10template <typename T>
11constexpr T AlignUp(T value, size_t size) { 11constexpr T AlignUp(T value, size_t size) {
12 static_assert(std::is_unsigned<T>::value, "T must be an unsigned value."); 12 static_assert(std::is_unsigned_v<T>, "T must be an unsigned value.");
13 return static_cast<T>(value + (size - value % size) % size); 13 return static_cast<T>(value + (size - value % size) % size);
14} 14}
15 15
16template <typename T> 16template <typename T>
17constexpr T AlignDown(T value, size_t size) { 17constexpr T AlignDown(T value, size_t size) {
18 static_assert(std::is_unsigned<T>::value, "T must be an unsigned value."); 18 static_assert(std::is_unsigned_v<T>, "T must be an unsigned value.");
19 return static_cast<T>(value - value % size); 19 return static_cast<T>(value - value % size);
20} 20}
21 21
diff --git a/src/common/bit_set.h b/src/common/bit_set.h
index 84e3cbe58..5a197d8c1 100644
--- a/src/common/bit_set.h
+++ b/src/common/bit_set.h
@@ -96,7 +96,7 @@ static inline int LeastSignificantSetBit(u64 val) {
96 96
97template <typename IntTy> 97template <typename IntTy>
98class BitSet { 98class BitSet {
99 static_assert(!std::is_signed<IntTy>::value, "BitSet should not be used with signed types"); 99 static_assert(!std::is_signed_v<IntTy>, "BitSet should not be used with signed types");
100 100
101public: 101public:
102 // A reference to a particular bit, returned from operator[]. 102 // A reference to a particular bit, returned from operator[].
diff --git a/src/common/file_util.h b/src/common/file_util.h
index 28697d527..d0987fb57 100644
--- a/src/common/file_util.h
+++ b/src/common/file_util.h
@@ -8,6 +8,7 @@
8#include <cstdio> 8#include <cstdio>
9#include <fstream> 9#include <fstream>
10#include <functional> 10#include <functional>
11#include <limits>
11#include <string> 12#include <string>
12#include <string_view> 13#include <string_view>
13#include <type_traits> 14#include <type_traits>
@@ -207,39 +208,42 @@ public:
207 208
208 template <typename T> 209 template <typename T>
209 size_t ReadArray(T* data, size_t length) const { 210 size_t ReadArray(T* data, size_t length) const {
210 static_assert(std::is_trivially_copyable<T>(), 211 static_assert(std::is_trivially_copyable_v<T>,
211 "Given array does not consist of trivially copyable objects"); 212 "Given array does not consist of trivially copyable objects");
212 213
213 if (!IsOpen()) 214 if (!IsOpen()) {
214 return -1; 215 return std::numeric_limits<size_t>::max();
216 }
215 217
216 return std::fread(data, sizeof(T), length, m_file); 218 return std::fread(data, sizeof(T), length, m_file);
217 } 219 }
218 220
219 template <typename T> 221 template <typename T>
220 size_t WriteArray(const T* data, size_t length) { 222 size_t WriteArray(const T* data, size_t length) {
221 static_assert(std::is_trivially_copyable<T>(), 223 static_assert(std::is_trivially_copyable_v<T>,
222 "Given array does not consist of trivially copyable objects"); 224 "Given array does not consist of trivially copyable objects");
223 if (!IsOpen()) 225 if (!IsOpen()) {
224 return -1; 226 return std::numeric_limits<size_t>::max();
227 }
228
225 return std::fwrite(data, sizeof(T), length, m_file); 229 return std::fwrite(data, sizeof(T), length, m_file);
226 } 230 }
227 231
228 template <typename T> 232 template <typename T>
229 size_t ReadBytes(T* data, size_t length) const { 233 size_t ReadBytes(T* data, size_t length) const {
230 static_assert(std::is_trivially_copyable<T>(), "T must be trivially copyable"); 234 static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable");
231 return ReadArray(reinterpret_cast<char*>(data), length); 235 return ReadArray(reinterpret_cast<char*>(data), length);
232 } 236 }
233 237
234 template <typename T> 238 template <typename T>
235 size_t WriteBytes(const T* data, size_t length) { 239 size_t WriteBytes(const T* data, size_t length) {
236 static_assert(std::is_trivially_copyable<T>(), "T must be trivially copyable"); 240 static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable");
237 return WriteArray(reinterpret_cast<const char*>(data), length); 241 return WriteArray(reinterpret_cast<const char*>(data), length);
238 } 242 }
239 243
240 template <typename T> 244 template <typename T>
241 size_t WriteObject(const T& object) { 245 size_t WriteObject(const T& object) {
242 static_assert(!std::is_pointer<T>::value, "Given object is a pointer"); 246 static_assert(!std::is_pointer_v<T>, "WriteObject arguments must not be a pointer");
243 return WriteArray(&object, 1); 247 return WriteArray(&object, 1);
244 } 248 }
245 249
diff --git a/src/common/hash.h b/src/common/hash.h
index 73c326980..2c761e545 100644
--- a/src/common/hash.h
+++ b/src/common/hash.h
@@ -28,7 +28,7 @@ static inline u64 ComputeHash64(const void* data, size_t len) {
28 */ 28 */
29template <typename T> 29template <typename T>
30static inline u64 ComputeStructHash64(const T& data) { 30static inline u64 ComputeStructHash64(const T& data) {
31 static_assert(std::is_trivially_copyable<T>(), 31 static_assert(std::is_trivially_copyable_v<T>,
32 "Type passed to ComputeStructHash64 must be trivially copyable"); 32 "Type passed to ComputeStructHash64 must be trivially copyable");
33 return ComputeHash64(&data, sizeof(data)); 33 return ComputeHash64(&data, sizeof(data));
34} 34}
@@ -38,7 +38,7 @@ template <typename T>
38struct HashableStruct { 38struct HashableStruct {
39 // In addition to being trivially copyable, T must also have a trivial default constructor, 39 // In addition to being trivially copyable, T must also have a trivial default constructor,
40 // because any member initialization would be overridden by memset 40 // because any member initialization would be overridden by memset
41 static_assert(std::is_trivial<T>(), "Type passed to HashableStruct must be trivial"); 41 static_assert(std::is_trivial_v<T>, "Type passed to HashableStruct must be trivial");
42 /* 42 /*
43 * We use a union because "implicitly-defined copy/move constructor for a union X copies the 43 * We use a union because "implicitly-defined copy/move constructor for a union X copies the
44 * object representation of X." and "implicitly-defined copy assignment operator for a union X 44 * object representation of X." and "implicitly-defined copy assignment operator for a union X
diff --git a/src/common/vector_math.h b/src/common/vector_math.h
index cca43bd4c..5c94fcda3 100644
--- a/src/common/vector_math.h
+++ b/src/common/vector_math.h
@@ -43,139 +43,135 @@ template <typename T>
43class Vec4; 43class Vec4;
44 44
45template <typename T> 45template <typename T>
46static inline Vec2<T> MakeVec(const T& x, const T& y);
47template <typename T>
48static inline Vec3<T> MakeVec(const T& x, const T& y, const T& z);
49template <typename T>
50static inline Vec4<T> MakeVec(const T& x, const T& y, const T& z, const T& w);
51
52template <typename T>
53class Vec2 { 46class Vec2 {
54public: 47public:
55 T x{}; 48 T x{};
56 T y{}; 49 T y{};
57 50
58 Vec2() = default; 51 constexpr Vec2() = default;
59 Vec2(const T& _x, const T& _y) : x(_x), y(_y) {} 52 constexpr Vec2(const T& x_, const T& y_) : x(x_), y(y_) {}
60 53
61 template <typename T2> 54 template <typename T2>
62 Vec2<T2> Cast() const { 55 constexpr Vec2<T2> Cast() const {
63 return Vec2<T2>((T2)x, (T2)y); 56 return Vec2<T2>(static_cast<T2>(x), static_cast<T2>(y));
64 } 57 }
65 58
66 static Vec2 AssignToAll(const T& f) { 59 static constexpr Vec2 AssignToAll(const T& f) {
67 return Vec2<T>(f, f); 60 return Vec2{f, f};
68 } 61 }
69 62
70 Vec2<decltype(T{} + T{})> operator+(const Vec2& other) const { 63 constexpr Vec2<decltype(T{} + T{})> operator+(const Vec2& other) const {
71 return MakeVec(x + other.x, y + other.y); 64 return {x + other.x, y + other.y};
72 } 65 }
73 void operator+=(const Vec2& other) { 66 constexpr Vec2& operator+=(const Vec2& other) {
74 x += other.x; 67 x += other.x;
75 y += other.y; 68 y += other.y;
69 return *this;
76 } 70 }
77 Vec2<decltype(T{} - T{})> operator-(const Vec2& other) const { 71 constexpr Vec2<decltype(T{} - T{})> operator-(const Vec2& other) const {
78 return MakeVec(x - other.x, y - other.y); 72 return {x - other.x, y - other.y};
79 } 73 }
80 void operator-=(const Vec2& other) { 74 constexpr Vec2& operator-=(const Vec2& other) {
81 x -= other.x; 75 x -= other.x;
82 y -= other.y; 76 y -= other.y;
77 return *this;
83 } 78 }
84 79
85 template <typename U = T> 80 template <typename U = T>
86 Vec2<std::enable_if_t<std::is_signed<U>::value, U>> operator-() const { 81 constexpr Vec2<std::enable_if_t<std::is_signed<U>::value, U>> operator-() const {
87 return MakeVec(-x, -y); 82 return {-x, -y};
88 } 83 }
89 Vec2<decltype(T{} * T{})> operator*(const Vec2& other) const { 84 constexpr Vec2<decltype(T{} * T{})> operator*(const Vec2& other) const {
90 return MakeVec(x * other.x, y * other.y); 85 return {x * other.x, y * other.y};
91 } 86 }
87
92 template <typename V> 88 template <typename V>
93 Vec2<decltype(T{} * V{})> operator*(const V& f) const { 89 constexpr Vec2<decltype(T{} * V{})> operator*(const V& f) const {
94 return MakeVec(x * f, y * f); 90 return {x * f, y * f};
95 } 91 }
92
96 template <typename V> 93 template <typename V>
97 void operator*=(const V& f) { 94 constexpr Vec2& operator*=(const V& f) {
98 *this = *this * f; 95 *this = *this * f;
96 return *this;
99 } 97 }
98
100 template <typename V> 99 template <typename V>
101 Vec2<decltype(T{} / V{})> operator/(const V& f) const { 100 constexpr Vec2<decltype(T{} / V{})> operator/(const V& f) const {
102 return MakeVec(x / f, y / f); 101 return {x / f, y / f};
103 } 102 }
103
104 template <typename V> 104 template <typename V>
105 void operator/=(const V& f) { 105 constexpr Vec2& operator/=(const V& f) {
106 *this = *this / f; 106 *this = *this / f;
107 return *this;
107 } 108 }
108 109
109 T Length2() const { 110 constexpr T Length2() const {
110 return x * x + y * y; 111 return x * x + y * y;
111 } 112 }
112 113
113 // Only implemented for T=float 114 // Only implemented for T=float
114 float Length() const; 115 float Length() const;
115 void SetLength(const float l);
116 Vec2 WithLength(const float l) const;
117 float Distance2To(Vec2& other);
118 Vec2 Normalized() const;
119 float Normalize(); // returns the previous length, which is often useful 116 float Normalize(); // returns the previous length, which is often useful
120 117
121 T& operator[](int i) // allow vector[1] = 3 (vector.y=3) 118 constexpr T& operator[](std::size_t i) {
122 {
123 return *((&x) + i); 119 return *((&x) + i);
124 } 120 }
125 T operator[](const int i) const { 121 constexpr const T& operator[](std::size_t i) const {
126 return *((&x) + i); 122 return *((&x) + i);
127 } 123 }
128 124
129 void SetZero() { 125 constexpr void SetZero() {
130 x = 0; 126 x = 0;
131 y = 0; 127 y = 0;
132 } 128 }
133 129
134 // Common aliases: UV (texel coordinates), ST (texture coordinates) 130 // Common aliases: UV (texel coordinates), ST (texture coordinates)
135 T& u() { 131 constexpr T& u() {
136 return x; 132 return x;
137 } 133 }
138 T& v() { 134 constexpr T& v() {
139 return y; 135 return y;
140 } 136 }
141 T& s() { 137 constexpr T& s() {
142 return x; 138 return x;
143 } 139 }
144 T& t() { 140 constexpr T& t() {
145 return y; 141 return y;
146 } 142 }
147 143
148 const T& u() const { 144 constexpr const T& u() const {
149 return x; 145 return x;
150 } 146 }
151 const T& v() const { 147 constexpr const T& v() const {
152 return y; 148 return y;
153 } 149 }
154 const T& s() const { 150 constexpr const T& s() const {
155 return x; 151 return x;
156 } 152 }
157 const T& t() const { 153 constexpr const T& t() const {
158 return y; 154 return y;
159 } 155 }
160 156
161 // swizzlers - create a subvector of specific components 157 // swizzlers - create a subvector of specific components
162 const Vec2 yx() const { 158 constexpr Vec2 yx() const {
163 return Vec2(y, x); 159 return Vec2(y, x);
164 } 160 }
165 const Vec2 vu() const { 161 constexpr Vec2 vu() const {
166 return Vec2(y, x); 162 return Vec2(y, x);
167 } 163 }
168 const Vec2 ts() const { 164 constexpr Vec2 ts() const {
169 return Vec2(y, x); 165 return Vec2(y, x);
170 } 166 }
171}; 167};
172 168
173template <typename T, typename V> 169template <typename T, typename V>
174Vec2<T> operator*(const V& f, const Vec2<T>& vec) { 170constexpr Vec2<T> operator*(const V& f, const Vec2<T>& vec) {
175 return Vec2<T>(f * vec.x, f * vec.y); 171 return Vec2<T>(f * vec.x, f * vec.y);
176} 172}
177 173
178typedef Vec2<float> Vec2f; 174using Vec2f = Vec2<float>;
179 175
180template <> 176template <>
181inline float Vec2<float>::Length() const { 177inline float Vec2<float>::Length() const {
@@ -196,147 +192,151 @@ public:
196 T y{}; 192 T y{};
197 T z{}; 193 T z{};
198 194
199 Vec3() = default; 195 constexpr Vec3() = default;
200 Vec3(const T& _x, const T& _y, const T& _z) : x(_x), y(_y), z(_z) {} 196 constexpr Vec3(const T& x_, const T& y_, const T& z_) : x(x_), y(y_), z(z_) {}
201 197
202 template <typename T2> 198 template <typename T2>
203 Vec3<T2> Cast() const { 199 constexpr Vec3<T2> Cast() const {
204 return MakeVec<T2>((T2)x, (T2)y, (T2)z); 200 return Vec3<T2>(static_cast<T2>(x), static_cast<T2>(y), static_cast<T2>(z));
205 } 201 }
206 202
207 // Only implemented for T=int and T=float 203 static constexpr Vec3 AssignToAll(const T& f) {
208 static Vec3 FromRGB(unsigned int rgb); 204 return Vec3(f, f, f);
209 unsigned int ToRGB() const; // alpha bits set to zero
210
211 static Vec3 AssignToAll(const T& f) {
212 return MakeVec(f, f, f);
213 } 205 }
214 206
215 Vec3<decltype(T{} + T{})> operator+(const Vec3& other) const { 207 constexpr Vec3<decltype(T{} + T{})> operator+(const Vec3& other) const {
216 return MakeVec(x + other.x, y + other.y, z + other.z); 208 return {x + other.x, y + other.y, z + other.z};
217 } 209 }
218 void operator+=(const Vec3& other) { 210
211 constexpr Vec3& operator+=(const Vec3& other) {
219 x += other.x; 212 x += other.x;
220 y += other.y; 213 y += other.y;
221 z += other.z; 214 z += other.z;
215 return *this;
222 } 216 }
223 Vec3<decltype(T{} - T{})> operator-(const Vec3& other) const { 217
224 return MakeVec(x - other.x, y - other.y, z - other.z); 218 constexpr Vec3<decltype(T{} - T{})> operator-(const Vec3& other) const {
219 return {x - other.x, y - other.y, z - other.z};
225 } 220 }
226 void operator-=(const Vec3& other) { 221
222 constexpr Vec3& operator-=(const Vec3& other) {
227 x -= other.x; 223 x -= other.x;
228 y -= other.y; 224 y -= other.y;
229 z -= other.z; 225 z -= other.z;
226 return *this;
230 } 227 }
231 228
232 template <typename U = T> 229 template <typename U = T>
233 Vec3<std::enable_if_t<std::is_signed<U>::value, U>> operator-() const { 230 constexpr Vec3<std::enable_if_t<std::is_signed<U>::value, U>> operator-() const {
234 return MakeVec(-x, -y, -z); 231 return {-x, -y, -z};
235 } 232 }
236 Vec3<decltype(T{} * T{})> operator*(const Vec3& other) const { 233
237 return MakeVec(x * other.x, y * other.y, z * other.z); 234 constexpr Vec3<decltype(T{} * T{})> operator*(const Vec3& other) const {
235 return {x * other.x, y * other.y, z * other.z};
238 } 236 }
237
239 template <typename V> 238 template <typename V>
240 Vec3<decltype(T{} * V{})> operator*(const V& f) const { 239 constexpr Vec3<decltype(T{} * V{})> operator*(const V& f) const {
241 return MakeVec(x * f, y * f, z * f); 240 return {x * f, y * f, z * f};
242 } 241 }
242
243 template <typename V> 243 template <typename V>
244 void operator*=(const V& f) { 244 constexpr Vec3& operator*=(const V& f) {
245 *this = *this * f; 245 *this = *this * f;
246 return *this;
246 } 247 }
247 template <typename V> 248 template <typename V>
248 Vec3<decltype(T{} / V{})> operator/(const V& f) const { 249 constexpr Vec3<decltype(T{} / V{})> operator/(const V& f) const {
249 return MakeVec(x / f, y / f, z / f); 250 return {x / f, y / f, z / f};
250 } 251 }
252
251 template <typename V> 253 template <typename V>
252 void operator/=(const V& f) { 254 constexpr Vec3& operator/=(const V& f) {
253 *this = *this / f; 255 *this = *this / f;
256 return *this;
254 } 257 }
255 258
256 T Length2() const { 259 constexpr T Length2() const {
257 return x * x + y * y + z * z; 260 return x * x + y * y + z * z;
258 } 261 }
259 262
260 // Only implemented for T=float 263 // Only implemented for T=float
261 float Length() const; 264 float Length() const;
262 void SetLength(const float l);
263 Vec3 WithLength(const float l) const;
264 float Distance2To(Vec3& other);
265 Vec3 Normalized() const; 265 Vec3 Normalized() const;
266 float Normalize(); // returns the previous length, which is often useful 266 float Normalize(); // returns the previous length, which is often useful
267 267
268 T& operator[](int i) // allow vector[2] = 3 (vector.z=3) 268 constexpr T& operator[](std::size_t i) {
269 {
270 return *((&x) + i); 269 return *((&x) + i);
271 } 270 }
272 T operator[](const int i) const { 271
272 constexpr const T& operator[](std::size_t i) const {
273 return *((&x) + i); 273 return *((&x) + i);
274 } 274 }
275 275
276 void SetZero() { 276 constexpr void SetZero() {
277 x = 0; 277 x = 0;
278 y = 0; 278 y = 0;
279 z = 0; 279 z = 0;
280 } 280 }
281 281
282 // Common aliases: UVW (texel coordinates), RGB (colors), STQ (texture coordinates) 282 // Common aliases: UVW (texel coordinates), RGB (colors), STQ (texture coordinates)
283 T& u() { 283 constexpr T& u() {
284 return x; 284 return x;
285 } 285 }
286 T& v() { 286 constexpr T& v() {
287 return y; 287 return y;
288 } 288 }
289 T& w() { 289 constexpr T& w() {
290 return z; 290 return z;
291 } 291 }
292 292
293 T& r() { 293 constexpr T& r() {
294 return x; 294 return x;
295 } 295 }
296 T& g() { 296 constexpr T& g() {
297 return y; 297 return y;
298 } 298 }
299 T& b() { 299 constexpr T& b() {
300 return z; 300 return z;
301 } 301 }
302 302
303 T& s() { 303 constexpr T& s() {
304 return x; 304 return x;
305 } 305 }
306 T& t() { 306 constexpr T& t() {
307 return y; 307 return y;
308 } 308 }
309 T& q() { 309 constexpr T& q() {
310 return z; 310 return z;
311 } 311 }
312 312
313 const T& u() const { 313 constexpr const T& u() const {
314 return x; 314 return x;
315 } 315 }
316 const T& v() const { 316 constexpr const T& v() const {
317 return y; 317 return y;
318 } 318 }
319 const T& w() const { 319 constexpr const T& w() const {
320 return z; 320 return z;
321 } 321 }
322 322
323 const T& r() const { 323 constexpr const T& r() const {
324 return x; 324 return x;
325 } 325 }
326 const T& g() const { 326 constexpr const T& g() const {
327 return y; 327 return y;
328 } 328 }
329 const T& b() const { 329 constexpr const T& b() const {
330 return z; 330 return z;
331 } 331 }
332 332
333 const T& s() const { 333 constexpr const T& s() const {
334 return x; 334 return x;
335 } 335 }
336 const T& t() const { 336 constexpr const T& t() const {
337 return y; 337 return y;
338 } 338 }
339 const T& q() const { 339 constexpr const T& q() const {
340 return z; 340 return z;
341 } 341 }
342 342
@@ -345,7 +345,7 @@ public:
345// _DEFINE_SWIZZLER2 defines a single such function, DEFINE_SWIZZLER2 defines all of them for all 345// _DEFINE_SWIZZLER2 defines a single such function, DEFINE_SWIZZLER2 defines all of them for all
346// component names (x<->r) and permutations (xy<->yx) 346// component names (x<->r) and permutations (xy<->yx)
347#define _DEFINE_SWIZZLER2(a, b, name) \ 347#define _DEFINE_SWIZZLER2(a, b, name) \
348 const Vec2<T> name() const { \ 348 constexpr Vec2<T> name() const { \
349 return Vec2<T>(a, b); \ 349 return Vec2<T>(a, b); \
350 } 350 }
351#define DEFINE_SWIZZLER2(a, b, a2, b2, a3, b3, a4, b4) \ 351#define DEFINE_SWIZZLER2(a, b, a2, b2, a3, b3, a4, b4) \
@@ -366,7 +366,7 @@ public:
366}; 366};
367 367
368template <typename T, typename V> 368template <typename T, typename V>
369Vec3<T> operator*(const V& f, const Vec3<T>& vec) { 369constexpr Vec3<T> operator*(const V& f, const Vec3<T>& vec) {
370 return Vec3<T>(f * vec.x, f * vec.y, f * vec.z); 370 return Vec3<T>(f * vec.x, f * vec.y, f * vec.z);
371} 371}
372 372
@@ -387,7 +387,7 @@ inline float Vec3<float>::Normalize() {
387 return length; 387 return length;
388} 388}
389 389
390typedef Vec3<float> Vec3f; 390using Vec3f = Vec3<float>;
391 391
392template <typename T> 392template <typename T>
393class Vec4 { 393class Vec4 {
@@ -397,86 +397,88 @@ public:
397 T z{}; 397 T z{};
398 T w{}; 398 T w{};
399 399
400 Vec4() = default; 400 constexpr Vec4() = default;
401 Vec4(const T& _x, const T& _y, const T& _z, const T& _w) : x(_x), y(_y), z(_z), w(_w) {} 401 constexpr Vec4(const T& x_, const T& y_, const T& z_, const T& w_)
402 : x(x_), y(y_), z(z_), w(w_) {}
402 403
403 template <typename T2> 404 template <typename T2>
404 Vec4<T2> Cast() const { 405 constexpr Vec4<T2> Cast() const {
405 return Vec4<T2>((T2)x, (T2)y, (T2)z, (T2)w); 406 return Vec4<T2>(static_cast<T2>(x), static_cast<T2>(y), static_cast<T2>(z),
407 static_cast<T2>(w));
406 } 408 }
407 409
408 // Only implemented for T=int and T=float 410 static constexpr Vec4 AssignToAll(const T& f) {
409 static Vec4 FromRGBA(unsigned int rgba); 411 return Vec4(f, f, f, f);
410 unsigned int ToRGBA() const;
411
412 static Vec4 AssignToAll(const T& f) {
413 return Vec4<T>(f, f, f, f);
414 } 412 }
415 413
416 Vec4<decltype(T{} + T{})> operator+(const Vec4& other) const { 414 constexpr Vec4<decltype(T{} + T{})> operator+(const Vec4& other) const {
417 return MakeVec(x + other.x, y + other.y, z + other.z, w + other.w); 415 return {x + other.x, y + other.y, z + other.z, w + other.w};
418 } 416 }
419 void operator+=(const Vec4& other) { 417
418 constexpr Vec4& operator+=(const Vec4& other) {
420 x += other.x; 419 x += other.x;
421 y += other.y; 420 y += other.y;
422 z += other.z; 421 z += other.z;
423 w += other.w; 422 w += other.w;
423 return *this;
424 } 424 }
425 Vec4<decltype(T{} - T{})> operator-(const Vec4& other) const { 425
426 return MakeVec(x - other.x, y - other.y, z - other.z, w - other.w); 426 constexpr Vec4<decltype(T{} - T{})> operator-(const Vec4& other) const {
427 return {x - other.x, y - other.y, z - other.z, w - other.w};
427 } 428 }
428 void operator-=(const Vec4& other) { 429
430 constexpr Vec4& operator-=(const Vec4& other) {
429 x -= other.x; 431 x -= other.x;
430 y -= other.y; 432 y -= other.y;
431 z -= other.z; 433 z -= other.z;
432 w -= other.w; 434 w -= other.w;
435 return *this;
433 } 436 }
434 437
435 template <typename U = T> 438 template <typename U = T>
436 Vec4<std::enable_if_t<std::is_signed<U>::value, U>> operator-() const { 439 constexpr Vec4<std::enable_if_t<std::is_signed<U>::value, U>> operator-() const {
437 return MakeVec(-x, -y, -z, -w); 440 return {-x, -y, -z, -w};
438 } 441 }
439 Vec4<decltype(T{} * T{})> operator*(const Vec4& other) const { 442
440 return MakeVec(x * other.x, y * other.y, z * other.z, w * other.w); 443 constexpr Vec4<decltype(T{} * T{})> operator*(const Vec4& other) const {
444 return {x * other.x, y * other.y, z * other.z, w * other.w};
441 } 445 }
446
442 template <typename V> 447 template <typename V>
443 Vec4<decltype(T{} * V{})> operator*(const V& f) const { 448 constexpr Vec4<decltype(T{} * V{})> operator*(const V& f) const {
444 return MakeVec(x * f, y * f, z * f, w * f); 449 return {x * f, y * f, z * f, w * f};
445 } 450 }
451
446 template <typename V> 452 template <typename V>
447 void operator*=(const V& f) { 453 constexpr Vec4& operator*=(const V& f) {
448 *this = *this * f; 454 *this = *this * f;
455 return *this;
449 } 456 }
457
450 template <typename V> 458 template <typename V>
451 Vec4<decltype(T{} / V{})> operator/(const V& f) const { 459 constexpr Vec4<decltype(T{} / V{})> operator/(const V& f) const {
452 return MakeVec(x / f, y / f, z / f, w / f); 460 return {x / f, y / f, z / f, w / f};
453 } 461 }
462
454 template <typename V> 463 template <typename V>
455 void operator/=(const V& f) { 464 constexpr Vec4& operator/=(const V& f) {
456 *this = *this / f; 465 *this = *this / f;
466 return *this;
457 } 467 }
458 468
459 T Length2() const { 469 constexpr T Length2() const {
460 return x * x + y * y + z * z + w * w; 470 return x * x + y * y + z * z + w * w;
461 } 471 }
462 472
463 // Only implemented for T=float 473 constexpr T& operator[](std::size_t i) {
464 float Length() const;
465 void SetLength(const float l);
466 Vec4 WithLength(const float l) const;
467 float Distance2To(Vec4& other);
468 Vec4 Normalized() const;
469 float Normalize(); // returns the previous length, which is often useful
470
471 T& operator[](int i) // allow vector[2] = 3 (vector.z=3)
472 {
473 return *((&x) + i); 474 return *((&x) + i);
474 } 475 }
475 T operator[](const int i) const { 476
477 constexpr const T& operator[](std::size_t i) const {
476 return *((&x) + i); 478 return *((&x) + i);
477 } 479 }
478 480
479 void SetZero() { 481 constexpr void SetZero() {
480 x = 0; 482 x = 0;
481 y = 0; 483 y = 0;
482 z = 0; 484 z = 0;
@@ -484,29 +486,29 @@ public:
484 } 486 }
485 487
486 // Common alias: RGBA (colors) 488 // Common alias: RGBA (colors)
487 T& r() { 489 constexpr T& r() {
488 return x; 490 return x;
489 } 491 }
490 T& g() { 492 constexpr T& g() {
491 return y; 493 return y;
492 } 494 }
493 T& b() { 495 constexpr T& b() {
494 return z; 496 return z;
495 } 497 }
496 T& a() { 498 constexpr T& a() {
497 return w; 499 return w;
498 } 500 }
499 501
500 const T& r() const { 502 constexpr const T& r() const {
501 return x; 503 return x;
502 } 504 }
503 const T& g() const { 505 constexpr const T& g() const {
504 return y; 506 return y;
505 } 507 }
506 const T& b() const { 508 constexpr const T& b() const {
507 return z; 509 return z;
508 } 510 }
509 const T& a() const { 511 constexpr const T& a() const {
510 return w; 512 return w;
511 } 513 }
512 514
@@ -518,7 +520,7 @@ public:
518// DEFINE_SWIZZLER2_COMP2 defines two component functions for all component names (x<->r) and 520// DEFINE_SWIZZLER2_COMP2 defines two component functions for all component names (x<->r) and
519// permutations (xy<->yx) 521// permutations (xy<->yx)
520#define _DEFINE_SWIZZLER2(a, b, name) \ 522#define _DEFINE_SWIZZLER2(a, b, name) \
521 const Vec2<T> name() const { \ 523 constexpr Vec2<T> name() const { \
522 return Vec2<T>(a, b); \ 524 return Vec2<T>(a, b); \
523 } 525 }
524#define DEFINE_SWIZZLER2_COMP1(a, a2) \ 526#define DEFINE_SWIZZLER2_COMP1(a, a2) \
@@ -545,7 +547,7 @@ public:
545#undef _DEFINE_SWIZZLER2 547#undef _DEFINE_SWIZZLER2
546 548
547#define _DEFINE_SWIZZLER3(a, b, c, name) \ 549#define _DEFINE_SWIZZLER3(a, b, c, name) \
548 const Vec3<T> name() const { \ 550 constexpr Vec3<T> name() const { \
549 return Vec3<T>(a, b, c); \ 551 return Vec3<T>(a, b, c); \
550 } 552 }
551#define DEFINE_SWIZZLER3_COMP1(a, a2) \ 553#define DEFINE_SWIZZLER3_COMP1(a, a2) \
@@ -579,51 +581,51 @@ public:
579}; 581};
580 582
581template <typename T, typename V> 583template <typename T, typename V>
582Vec4<decltype(V{} * T{})> operator*(const V& f, const Vec4<T>& vec) { 584constexpr Vec4<decltype(V{} * T{})> operator*(const V& f, const Vec4<T>& vec) {
583 return MakeVec(f * vec.x, f * vec.y, f * vec.z, f * vec.w); 585 return {f * vec.x, f * vec.y, f * vec.z, f * vec.w};
584} 586}
585 587
586typedef Vec4<float> Vec4f; 588using Vec4f = Vec4<float>;
587 589
588template <typename T> 590template <typename T>
589static inline decltype(T{} * T{} + T{} * T{}) Dot(const Vec2<T>& a, const Vec2<T>& b) { 591constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec2<T>& a, const Vec2<T>& b) {
590 return a.x * b.x + a.y * b.y; 592 return a.x * b.x + a.y * b.y;
591} 593}
592 594
593template <typename T> 595template <typename T>
594static inline decltype(T{} * T{} + T{} * T{}) Dot(const Vec3<T>& a, const Vec3<T>& b) { 596constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec3<T>& a, const Vec3<T>& b) {
595 return a.x * b.x + a.y * b.y + a.z * b.z; 597 return a.x * b.x + a.y * b.y + a.z * b.z;
596} 598}
597 599
598template <typename T> 600template <typename T>
599static inline decltype(T{} * T{} + T{} * T{}) Dot(const Vec4<T>& a, const Vec4<T>& b) { 601constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec4<T>& a, const Vec4<T>& b) {
600 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; 602 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
601} 603}
602 604
603template <typename T> 605template <typename T>
604static inline Vec3<decltype(T{} * T{} - T{} * T{})> Cross(const Vec3<T>& a, const Vec3<T>& b) { 606constexpr Vec3<decltype(T{} * T{} - T{} * T{})> Cross(const Vec3<T>& a, const Vec3<T>& b) {
605 return MakeVec(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); 607 return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x};
606} 608}
607 609
608// linear interpolation via float: 0.0=begin, 1.0=end 610// linear interpolation via float: 0.0=begin, 1.0=end
609template <typename X> 611template <typename X>
610static inline decltype(X{} * float{} + X{} * float{}) Lerp(const X& begin, const X& end, 612constexpr decltype(X{} * float{} + X{} * float{}) Lerp(const X& begin, const X& end,
611 const float t) { 613 const float t) {
612 return begin * (1.f - t) + end * t; 614 return begin * (1.f - t) + end * t;
613} 615}
614 616
615// linear interpolation via int: 0=begin, base=end 617// linear interpolation via int: 0=begin, base=end
616template <typename X, int base> 618template <typename X, int base>
617static inline decltype((X{} * int{} + X{} * int{}) / base) LerpInt(const X& begin, const X& end, 619constexpr decltype((X{} * int{} + X{} * int{}) / base) LerpInt(const X& begin, const X& end,
618 const int t) { 620 const int t) {
619 return (begin * (base - t) + end * t) / base; 621 return (begin * (base - t) + end * t) / base;
620} 622}
621 623
622// bilinear interpolation. s is for interpolating x00-x01 and x10-x11, and t is for the second 624// bilinear interpolation. s is for interpolating x00-x01 and x10-x11, and t is for the second
623// interpolation. 625// interpolation.
624template <typename X> 626template <typename X>
625inline auto BilinearInterp(const X& x00, const X& x01, const X& x10, const X& x11, const float s, 627constexpr auto BilinearInterp(const X& x00, const X& x01, const X& x10, const X& x11, const float s,
626 const float t) { 628 const float t) {
627 auto y0 = Lerp(x00, x01, s); 629 auto y0 = Lerp(x00, x01, s);
628 auto y1 = Lerp(x10, x11, s); 630 auto y1 = Lerp(x10, x11, s);
629 return Lerp(y0, y1, t); 631 return Lerp(y0, y1, t);
@@ -631,42 +633,42 @@ inline auto BilinearInterp(const X& x00, const X& x01, const X& x10, const X& x1
631 633
632// Utility vector factories 634// Utility vector factories
633template <typename T> 635template <typename T>
634static inline Vec2<T> MakeVec(const T& x, const T& y) { 636constexpr Vec2<T> MakeVec(const T& x, const T& y) {
635 return Vec2<T>{x, y}; 637 return Vec2<T>{x, y};
636} 638}
637 639
638template <typename T> 640template <typename T>
639static inline Vec3<T> MakeVec(const T& x, const T& y, const T& z) { 641constexpr Vec3<T> MakeVec(const T& x, const T& y, const T& z) {
640 return Vec3<T>{x, y, z}; 642 return Vec3<T>{x, y, z};
641} 643}
642 644
643template <typename T> 645template <typename T>
644static inline Vec4<T> MakeVec(const T& x, const T& y, const Vec2<T>& zw) { 646constexpr Vec4<T> MakeVec(const T& x, const T& y, const Vec2<T>& zw) {
645 return MakeVec(x, y, zw[0], zw[1]); 647 return MakeVec(x, y, zw[0], zw[1]);
646} 648}
647 649
648template <typename T> 650template <typename T>
649static inline Vec3<T> MakeVec(const Vec2<T>& xy, const T& z) { 651constexpr Vec3<T> MakeVec(const Vec2<T>& xy, const T& z) {
650 return MakeVec(xy[0], xy[1], z); 652 return MakeVec(xy[0], xy[1], z);
651} 653}
652 654
653template <typename T> 655template <typename T>
654static inline Vec3<T> MakeVec(const T& x, const Vec2<T>& yz) { 656constexpr Vec3<T> MakeVec(const T& x, const Vec2<T>& yz) {
655 return MakeVec(x, yz[0], yz[1]); 657 return MakeVec(x, yz[0], yz[1]);
656} 658}
657 659
658template <typename T> 660template <typename T>
659static inline Vec4<T> MakeVec(const T& x, const T& y, const T& z, const T& w) { 661constexpr Vec4<T> MakeVec(const T& x, const T& y, const T& z, const T& w) {
660 return Vec4<T>{x, y, z, w}; 662 return Vec4<T>{x, y, z, w};
661} 663}
662 664
663template <typename T> 665template <typename T>
664static inline Vec4<T> MakeVec(const Vec2<T>& xy, const T& z, const T& w) { 666constexpr Vec4<T> MakeVec(const Vec2<T>& xy, const T& z, const T& w) {
665 return MakeVec(xy[0], xy[1], z, w); 667 return MakeVec(xy[0], xy[1], z, w);
666} 668}
667 669
668template <typename T> 670template <typename T>
669static inline Vec4<T> MakeVec(const T& x, const Vec2<T>& yz, const T& w) { 671constexpr Vec4<T> MakeVec(const T& x, const Vec2<T>& yz, const T& w) {
670 return MakeVec(x, yz[0], yz[1], w); 672 return MakeVec(x, yz[0], yz[1], w);
671} 673}
672 674
@@ -674,17 +676,17 @@ static inline Vec4<T> MakeVec(const T& x, const Vec2<T>& yz, const T& w) {
674// Even if someone wanted to use an odd object like Vec2<Vec2<T>>, the compiler would error 676// Even if someone wanted to use an odd object like Vec2<Vec2<T>>, the compiler would error
675// out soon enough due to misuse of the returned structure. 677// out soon enough due to misuse of the returned structure.
676template <typename T> 678template <typename T>
677static inline Vec4<T> MakeVec(const Vec2<T>& xy, const Vec2<T>& zw) { 679constexpr Vec4<T> MakeVec(const Vec2<T>& xy, const Vec2<T>& zw) {
678 return MakeVec(xy[0], xy[1], zw[0], zw[1]); 680 return MakeVec(xy[0], xy[1], zw[0], zw[1]);
679} 681}
680 682
681template <typename T> 683template <typename T>
682static inline Vec4<T> MakeVec(const Vec3<T>& xyz, const T& w) { 684constexpr Vec4<T> MakeVec(const Vec3<T>& xyz, const T& w) {
683 return MakeVec(xyz[0], xyz[1], xyz[2], w); 685 return MakeVec(xyz[0], xyz[1], xyz[2], w);
684} 686}
685 687
686template <typename T> 688template <typename T>
687static inline Vec4<T> MakeVec(const T& x, const Vec3<T>& yzw) { 689constexpr Vec4<T> MakeVec(const T& x, const Vec3<T>& yzw) {
688 return MakeVec(x, yzw[0], yzw[1], yzw[2]); 690 return MakeVec(x, yzw[0], yzw[1], yzw[2]);
689} 691}
690 692
diff --git a/src/common/x64/xbyak_util.h b/src/common/x64/xbyak_util.h
index 0f52f704b..ec76e0a47 100644
--- a/src/common/x64/xbyak_util.h
+++ b/src/common/x64/xbyak_util.h
@@ -34,7 +34,7 @@ inline bool IsWithin2G(const Xbyak::CodeGenerator& code, uintptr_t target) {
34 34
35template <typename T> 35template <typename T>
36inline void CallFarFunction(Xbyak::CodeGenerator& code, const T f) { 36inline void CallFarFunction(Xbyak::CodeGenerator& code, const T f) {
37 static_assert(std::is_pointer<T>(), "Argument must be a (function) pointer."); 37 static_assert(std::is_pointer_v<T>, "Argument must be a (function) pointer.");
38 size_t addr = reinterpret_cast<size_t>(f); 38 size_t addr = reinterpret_cast<size_t>(f);
39 if (IsWithin2G(code, addr)) { 39 if (IsWithin2G(code, addr)) {
40 code.call(f); 40 code.call(f);