forked from pcsx-redux/nugget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixed-point.hh
478 lines (416 loc) · 15.8 KB
/
fixed-point.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
MIT License
Copyright (c) 2023 PCSX-Redux authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <EASTL/functional.h>
#include <stdint.h>
#include <compare>
#include <concepts>
#include <type_traits>
namespace psyqo {
namespace FixedPointInternals {
uint32_t iDiv(uint64_t rem, uint32_t base, unsigned scale);
int32_t dDiv(int32_t a, int32_t b, unsigned scale);
void printInt(uint32_t value, const eastl::function<void(char)>&, unsigned scale);
} // namespace FixedPointInternals
/**
* @brief Fixed point number type.
*
* @details This is a fixed point number type with a configurable number
* of fractional bits. The default is 12 fractional bits, which is
* suitable for representing 3D coordinates in the PSX's 20.12 fixed
* point format. The number of fractional bits can be changed by
* specifying a different value for the template parameter
* `precisionBits`. The underlying integer type can also be changed
* by specifying a different type for the template parameter `T`.
* The default is `int32_t`, but `int16_t`, `int8_t`, `uint32_t`,
* `uint16_t`, and `uint8_t` are also supported. The template
* behaves as a value type, and supports most of the usual arithmetic
* operators. Its size is the same as the underlying integer type.
*
* @tparam precisionBits The number of fractional bits to use.
* @tparam T The underlying integer type to use.
* @tparam Scale The scale of the fixed point number. The default is
* 2 raised to the power of `precisionBits`, but this can be changed
* to produce fixed point numbers with a different range. A scale
* which is not a power of 2 will produce slower codegen. This is
* used for the external representation of the fixed point number.
*/
template <unsigned precisionBits = 12, std::integral T = int32_t, unsigned Scale = 1 << precisionBits>
requires((precisionBits > 0) && (precisionBits < 32) && ((sizeof(T) == 4) || (sizeof(T) == 2) || sizeof(T) == 1))
class FixedPoint {
using signedUpType = std::conditional<sizeof(T) == 4, int64_t, int32_t>::type;
using unsignedUpType = std::conditional<sizeof(T) == 4, uint64_t, uint32_t>::type;
using upType = std::conditional<std::is_signed<T>::value, signedUpType, unsignedUpType>::type;
public:
/**
* @brief The raw value of the fixed point number.
*
* @details This is the raw value of the fixed point number, and
* can be used to access the underlying integer type directly, which
* can be useful for some operations.
*/
T value;
T raw() const { return value; }
/**
* @brief The scale of the fixed point number.
*
*/
static constexpr unsigned scale = Scale;
/**
* @brief Constructs a fixed point number from an integer and a
* fraction. Specifying a fraction different from 0 when the
* scale is not a power of 2 is undefined behavior.
*/
explicit constexpr FixedPoint(T integer, T fraction) : value(integer * scale + fraction) {
static_assert(sizeof(FixedPoint) == sizeof(T));
}
/**
* @brief Constructs a fixed point number from a floating point
* number.
*
* @details Note that this is a `consteval` function, so it can
* only be used with compile-time constants. This is intentional,
* as the conversion from floating point to fixed point is
* basically impossible to do at runtime without using floating
* point arithmetic, which is not available on the PSX.
*/
consteval FixedPoint(long double ld) {
bool negative = ld < 0;
T integer = negative ? -ld : ld;
T fraction = ld * scale - integer * scale + (negative ? -0.5 : 0.5);
value = integer * scale + fraction;
}
constexpr FixedPoint() : value(0) {}
constexpr FixedPoint(const FixedPoint&) = default;
constexpr FixedPoint(FixedPoint&&) = default;
constexpr FixedPoint& operator=(const FixedPoint&) = default;
enum Raw { RAW };
constexpr FixedPoint(T raw, Raw) : value(raw) {}
/**
* @brief Construct a new Fixed Point number from a different
* fixed point number.
*/
template <unsigned otherPrecisionBits = 12, std::integral U = int32_t>
explicit FixedPoint(FixedPoint<otherPrecisionBits, U> other) {
if constexpr (precisionBits == otherPrecisionBits) {
value = T(other.value);
} else if constexpr (precisionBits > otherPrecisionBits) {
value = T(other.value << (precisionBits - otherPrecisionBits));
} else if constexpr (precisionBits < otherPrecisionBits) {
value = T(other.value >> (otherPrecisionBits - precisionBits));
}
}
/**
* @brief Returns the integer part of the fixed point number.
*
* @details This returns the integer part of the fixed point,
* rounded to the nearest integer. Note that this is not the same
* as truncating the fixed point number, as it rounds to the
* nearest integer, rather than towards zero.
*
* @tparam factor The factor to scale the integer part by. This
* defaults to 1, which means that the integer part is returned
* as-is. It can be used to return the integer part scaled by
* some factor, which can be useful for some operations.
* The codegen for this will be bad if the factor is not a
* power of 2.
* @return constexpr T The integer part of the fixed point number.
*/
template <size_t factor = 1>
constexpr T integer() const {
if constexpr (std::is_signed<T>::value) {
if (value < 0) {
return T(value - scale / (2 * factor)) / T(scale / factor);
}
}
return T(value + scale / (2 * factor)) / T(scale / factor);
}
template <std::integral U>
constexpr U integer() const {
if constexpr (std::is_signed<T>::value) {
if (value < 0) {
return U((value - scale / 2) / scale);
}
}
return U(value + scale / 2) / U(scale);
}
/**
* @brief Returns the floor of the fixed point number.
*
* @details This returns the largest integer less than or equal
* to the fixed point number. For example, floor of 3.7 is 3,
* floor of -3.7 is -4.
*
* @tparam U The type to return the floor value in. Defaults to
* the underlying type T.
* @return constexpr U The floor value of the fixed point number.
*/
template <std::integral U = T>
constexpr U floor() const {
if constexpr (std::is_signed<T>::value) {
if (value < 0) {
return U(value - scale + 1) / U(scale);
}
}
return U(value) / U(scale);
}
/**
* @brief Returns the ceiling of the fixed point number.
*
* @details This returns the smallest integer greater than or
* equal to the fixed point number. For example, ceil of 3.2
* is 4, ceil of -3.2 is -3.
*
* @tparam U The type to return the ceiling value in. Defaults
* to the underlying type T.
* @return constexpr U The ceiling value of the fixed point number.
*/
template <std::integral U = T>
constexpr U ceil() const {
if constexpr (std::is_signed<T>::value) {
if (value < 0) {
return U(value) / U(scale);
}
}
return U(value + scale - 1) / U(scale);
}
/**
* @brief Prints out the fixed point number.
*
* @details This prints out the fixed point number using the provided
* function for emitting characters out. Note that the formatting is
* pretty basic for now, and only supports printing out the number in
* decimal format, with no padding or precision setting. Maximum
* displayed precision is 5 decimal places.
*
* @param charPrinter A function that prints a single character, to
* be used to print the fixed point number.
*/
void print(const eastl::function<void(char)>& charPrinter) const {
T copy = value;
if constexpr (std::is_signed<T>::value) {
if (copy < 0) {
charPrinter('-');
copy = -copy;
}
}
FixedPointInternals::printInt(copy, charPrinter, scale);
}
constexpr FixedPoint abs() const {
FixedPoint ret = *this;
if constexpr (std::is_signed<T>::value) {
if (ret.value < 0) {
ret.value = -ret.value;
}
}
return ret;
}
constexpr FixedPoint operator+(FixedPoint other) const {
FixedPoint ret = *this;
ret.value += other.value;
return ret;
}
template <std::integral U>
constexpr FixedPoint operator+(U other) const {
FixedPoint ret = *this;
ret.value += other * scale;
return ret;
}
constexpr FixedPoint operator-(FixedPoint other) const {
FixedPoint ret = *this;
ret.value -= other.value;
return ret;
}
template <std::integral U>
constexpr FixedPoint operator-(U other) const {
FixedPoint ret = *this;
ret.value -= other * scale;
return ret;
}
constexpr FixedPoint operator*(FixedPoint other) const {
upType t = value;
t *= other.value;
t /= scale;
FixedPoint ret;
ret.value = t;
return ret;
}
template <std::integral U>
constexpr FixedPoint operator*(U other) const {
FixedPoint ret = *this;
ret.value *= other;
return ret;
}
constexpr FixedPoint operator/(FixedPoint other) const {
FixedPoint ret;
if constexpr (sizeof(T) == 4) {
if constexpr (std::is_signed<T>::value) {
ret.value = FixedPointInternals::dDiv(value, other.value, scale);
} else if constexpr (!std::is_signed<T>::value) {
ret.value = FixedPointInternals::iDiv(value, other.value, scale);
}
} else if constexpr (sizeof(T) < 4) {
upType t = value;
t *= scale;
t /= other.value;
ret.value = t;
}
return ret;
}
template <std::integral U>
constexpr FixedPoint operator/(U other) const {
FixedPoint ret = *this;
ret.value /= other;
return ret;
}
constexpr FixedPoint operator-() const {
FixedPoint ret = *this;
ret.value = -ret.value;
return ret;
}
constexpr FixedPoint& operator+=(FixedPoint other) {
value += other.value;
return *this;
}
template <std::integral U>
constexpr FixedPoint& operator+=(U other) {
value += other * scale;
return *this;
}
constexpr FixedPoint& operator-=(FixedPoint other) {
value -= other.value;
return *this;
}
template <std::integral U>
constexpr FixedPoint& operator-=(U other) {
value -= other * scale;
return *this;
}
constexpr FixedPoint& operator*=(FixedPoint other) {
upType t = value;
t *= other.value;
t /= scale;
value = t;
return *this;
}
template <std::integral U>
constexpr FixedPoint& operator*=(U other) {
value *= other;
return *this;
}
constexpr FixedPoint& operator/=(FixedPoint other) {
if constexpr (sizeof(T) == 4) {
if constexpr (std::is_signed<T>::value) {
value = FixedPointInternals::dDiv(value, other.value, scale);
} else if constexpr (!std::is_signed<T>::value) {
value = FixedPointInternals::iDiv(value, other.value, scale);
}
} else if constexpr (sizeof(T) == 2) {
upType t = value;
t *= scale;
t /= other.value;
value = t;
}
return *this;
}
template <std::integral U>
constexpr FixedPoint& operator/=(U other) {
value /= other;
return *this;
}
auto operator<=>(const FixedPoint& other) const = default;
constexpr FixedPoint operator<<(unsigned shift) const {
FixedPoint ret = *this;
ret.value <<= shift;
return ret;
}
constexpr FixedPoint operator>>(unsigned shift) const {
FixedPoint ret = *this;
ret.value >>= shift;
return ret;
}
constexpr FixedPoint& operator<<=(unsigned shift) {
value <<= shift;
return *this;
}
constexpr FixedPoint& operator>>=(unsigned shift) {
value >>= shift;
return *this;
}
constexpr FixedPoint operator++() {
value += scale;
return *this;
}
constexpr FixedPoint operator++(int) {
FixedPoint ret = *this;
value += scale;
return ret;
}
constexpr FixedPoint operator--() {
value -= scale;
return *this;
}
constexpr FixedPoint operator--(int) {
FixedPoint ret = *this;
value -= scale;
return ret;
}
constexpr bool operator!() const { return value == 0; }
};
template <unsigned precisionBits = 12, std::integral T = int32_t, unsigned scale = 1 << precisionBits,
std::integral U = int32_t>
constexpr FixedPoint<precisionBits, T, scale> operator+(U a, FixedPoint<precisionBits, T, scale> b) {
return b + a;
}
template <unsigned precisionBits = 12, std::integral T = int32_t, unsigned scale = 1 << precisionBits,
std::integral U = int32_t>
constexpr FixedPoint<precisionBits, T, scale> operator-(U a, FixedPoint<precisionBits, T, scale> b) {
return -b + a;
}
template <unsigned precisionBits = 12, std::integral T = int32_t, unsigned scale = 1 << precisionBits,
std::integral U = int32_t>
constexpr FixedPoint<precisionBits, T, scale> operator*(U a, FixedPoint<precisionBits, T, scale> b) {
return b * a;
}
template <unsigned precisionBits = 12, std::integral T = int32_t, unsigned scale = 1 << precisionBits,
std::integral U = int32_t>
constexpr FixedPoint<precisionBits, T, scale> operator/(U a, FixedPoint<precisionBits, T, scale> b) {
FixedPoint<precisionBits, T, scale> ret;
if constexpr (sizeof(T) == 4) {
if constexpr (std::is_signed<T>::value || std::is_signed<U>::value) {
ret.value = FixedPointInternals::dDiv(a * FixedPoint<precisionBits, T>::scale, b.raw(), scale);
} else if constexpr (!std::is_signed<T>::value && !std::is_signed<U>::value) {
ret.value = FixedPointInternals::iDiv(a * FixedPoint<precisionBits, T>::scale, b.raw(), scale);
}
} else if constexpr (sizeof(T) == 2) {
ret.value = a * FixedPoint<precisionBits, T>::scale / b.raw();
}
return ret;
}
namespace fixed_point_literals {
/**
* @brief User-defined literal for constructing a 20.12 fixed point number.
*
* @param value The value to construct the fixed point number from.
* @return consteval FixedPoint<> The constructed fixed point number.
*/
consteval FixedPoint<> operator""_fp(long double value) { return value; }
} // namespace fixed_point_literals
} // namespace psyqo