forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FixedPoint.h
492 lines (441 loc) · 13.8 KB
/
FixedPoint.h
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
* Copyright (c) 2021, Leon Albrecht <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Concepts.h>
#include <AK/Format.h>
#include <AK/IntegralMath.h>
#include <AK/NumericLimits.h>
#include <AK/Types.h>
#ifndef KERNEL
# include <AK/Math.h>
#endif
#ifndef __SIZEOF_INT128__
# include <AK/UFixedBigInt.h>
# include <AK/UFixedBigIntDivision.h>
#endif
// Solaris' definition of signbit in math_c99.h conflicts with our implementation.
#ifdef AK_OS_SOLARIS
# undef signbit
#endif
namespace AK {
// FIXME: this always uses round to nearest break-tie to even
// FIXME: use the Integral concept to constrain Underlying
template<size_t precision, typename Underlying>
class FixedPoint {
using This = FixedPoint<precision, Underlying>;
constexpr static Underlying radix_mask = (static_cast<Underlying>(1) << precision) - 1;
template<size_t P, typename U>
friend class FixedPoint;
public:
constexpr FixedPoint() = default;
template<Integral I>
constexpr FixedPoint(I value)
: m_value(static_cast<Underlying>(value) << precision)
{
}
#ifndef KERNEL
template<FloatingPoint F>
FixedPoint(F value)
: m_value(round_to<Underlying>(value * (static_cast<Underlying>(1) << precision)))
{
}
#endif
template<size_t P, typename U>
explicit constexpr FixedPoint(FixedPoint<P, U> const& other)
: m_value(other.template cast_to<precision, Underlying>().m_value)
{
}
#ifndef KERNEL
template<FloatingPoint F>
explicit ALWAYS_INLINE operator F() const
{
return (F)m_value * pow<F>(0.5, precision);
}
#endif
template<Integral I>
explicit constexpr operator I() const
{
return trunc().raw() >> precision;
}
static constexpr This create_raw(Underlying value)
{
This t {};
t.raw() = value;
return t;
}
constexpr Underlying raw() const
{
return m_value;
}
constexpr Underlying& raw()
{
return m_value;
}
constexpr This fract() const
{
return create_raw(m_value & radix_mask);
}
constexpr This clamp(This minimum, This maximum) const
{
if (*this < minimum)
return minimum;
if (*this > maximum)
return maximum;
return *this;
}
constexpr This rint() const
{
// Note: Round fair, break tie to even
Underlying value = m_value >> precision;
// Note: For negative numbers the ordering are reversed,
// and they were already decremented by the shift, so we need to
// add 1 when we see a fract values behind the `.5`s place set,
// because that means they are smaller than .5
// fract(m_value) >= .5?
if (m_value & (static_cast<Underlying>(1) << (precision - 1))) {
// fract(m_value) > .5?
if (m_value & (radix_mask >> 1)) {
// yes: round "up";
value += 1;
} else {
// no: round to even;
value += value & 1;
}
}
return value;
}
constexpr This floor() const
{
return create_raw(m_value & ~radix_mask);
}
constexpr This ceil() const
{
return create_raw((m_value & ~radix_mask)
+ (m_value & radix_mask ? static_cast<Underlying>(1) << precision : 0));
}
constexpr This trunc() const
{
return create_raw((m_value & ~radix_mask)
+ ((m_value & radix_mask)
? (m_value > 0 ? 0 : (static_cast<Underlying>(1) << precision))
: 0));
}
constexpr Underlying lrint() const { return rint().raw() >> precision; }
constexpr Underlying lfloor() const { return m_value >> precision; }
constexpr Underlying lceil() const
{
return (m_value >> precision)
+ (m_value & radix_mask ? 1 : 0);
}
constexpr Underlying ltrunc() const
{
return (m_value >> precision)
+ ((m_value & radix_mask)
? m_value > 0 ? 0 : 1
: 0);
}
// http://www.claysturner.com/dsp/BinaryLogarithm.pdf
constexpr This log2() const
{
// 0.5
This b = create_raw(static_cast<Underlying>(1) << (precision - 1));
This y = 0;
This x = *this;
// FIXME: There's no negative infinity.
if (x.raw() <= 0)
return create_raw(NumericLimits<Underlying>::min());
if (x != 1) {
i32 shift_amount = AK::log2<Underlying>(x.raw()) - precision;
if (shift_amount > 0)
x >>= shift_amount;
else
x <<= -shift_amount;
y += shift_amount;
}
for (size_t i = 0; i < precision; ++i) {
x *= x;
if (x >= 2) {
x >>= 1;
y += b;
}
b >>= 1;
}
return y;
}
constexpr bool signbit() const
requires(IsSigned<Underlying>)
{
return m_value >> (sizeof(Underlying) * 8 - 1);
}
constexpr This operator-() const
requires(IsSigned<Underlying>)
{
return create_raw(-m_value);
}
constexpr This operator+(This const& other) const
{
return create_raw(m_value + other.m_value);
}
constexpr This operator-(This const& other) const
{
return create_raw(m_value - other.m_value);
}
constexpr This operator*(This const& other) const
{
#ifdef __SIZEOF_INT128__
// FIXME: Figure out a nicer way to use more narrow types and avoid __int128
using MulRes = Conditional<sizeof(Underlying) < sizeof(i64), i64, __int128>;
MulRes value = raw();
value *= other.raw();
This ret = create_raw(value >> precision);
#else
// Note: We sign extend the raw value to a u128 to emulate the signed multiplication
// done in the version above
// FIXME: Provide narrower intermediate results types
u128 value = { (u64)(i64)raw(), ~0ull * (raw() < 0) };
value *= (u64)(i64)other.raw();
This ret = create_raw((value >> precision).low());
#endif
// Rounding:
// If last bit cut off is 1:
if (value & (static_cast<Underlying>(1) << (precision - 1))) {
// If the bit after is 1 as well
if (value & (radix_mask >> 1)) {
// We round away from 0
ret.raw() += 1;
} else {
// Otherwise we round to the next even value
// Which means we add the least significant bit of the raw return value
ret.raw() += ret.raw() & 1;
}
}
return ret;
}
constexpr This operator/(This const& other) const
{
#ifdef __SIZEOF_INT128__
// FIXME: Figure out a nicer way to use more narrow types and avoid __int128
using DivRes = Conditional<sizeof(Underlying) < sizeof(i64), i64, __int128>;
DivRes value = raw();
value <<= precision;
value /= other.raw();
return create_raw(value);
#else
// Note: We sign extend the raw value to a u128 to emulate the wide division
// done in the version above
if constexpr (sizeof(Underlying) > sizeof(u32)) {
u128 value = { (u64)(i64)raw(), ~0ull * (raw() < 0) };
value <<= precision;
value /= (u64)(i64)other.raw();
return create_raw(value.low());
}
// FIXME: Maybe allow going even narrower
using DivRes = Conditional<sizeof(Underlying) < sizeof(i32), i32, i64>;
DivRes value = raw();
value <<= precision;
value /= other.raw();
return create_raw(value);
#endif
}
template<Integral I>
constexpr This operator+(I other) const
{
return create_raw(m_value + (static_cast<Underlying>(other) << precision));
}
template<Integral I>
constexpr This operator-(I other) const
{
return create_raw(m_value - (static_cast<Underlying>(other) << precision));
}
template<Integral I>
constexpr This operator*(I other) const
{
return create_raw(m_value * other);
}
template<Integral I>
constexpr This operator/(I other) const
{
return create_raw(m_value / other);
}
template<Integral I>
constexpr This operator>>(I other) const
{
return create_raw(m_value >> other);
}
template<Integral I>
constexpr This operator<<(I other) const
{
return create_raw(m_value << other);
}
This& operator+=(This const& other)
{
m_value += other.raw();
return *this;
}
This& operator-=(This const& other)
{
m_value -= other.raw();
return *this;
}
This& operator*=(This const& other)
{
*this = *this * other;
return *this;
}
This& operator/=(This const& other)
{
*this = *this / other;
return *this;
}
template<Integral I>
This& operator+=(I other)
{
m_value += static_cast<Underlying>(other) << precision;
return *this;
}
template<Integral I>
This& operator-=(I other)
{
m_value -= static_cast<Underlying>(other) << precision;
return *this;
}
template<Integral I>
This& operator*=(I other)
{
m_value *= other;
return *this;
}
template<Integral I>
This& operator/=(I other)
{
m_value /= other;
return *this;
}
template<Integral I>
This& operator>>=(I other)
{
m_value >>= other;
return *this;
}
template<Integral I>
This& operator<<=(I other)
{
m_value <<= other;
return *this;
}
bool operator==(This const& other) const { return raw() == other.raw(); }
bool operator!=(This const& other) const { return raw() != other.raw(); }
bool operator>(This const& other) const { return raw() > other.raw(); }
bool operator>=(This const& other) const { return raw() >= other.raw(); }
bool operator<(This const& other) const { return raw() < other.raw(); }
bool operator<=(This const& other) const { return raw() <= other.raw(); }
// FIXME: There are probably better ways to do these
template<Integral I>
bool operator==(I other) const
{
return m_value >> precision == other && !(m_value & radix_mask);
}
template<Integral I>
bool operator!=(I other) const
{
return (m_value >> precision) != other || m_value & radix_mask;
}
template<Integral I>
bool operator>(I other) const
{
return !(*this <= other);
}
template<Integral I>
bool operator>=(I other) const
{
return !(*this < other);
}
template<Integral I>
bool operator<(I other) const
{
return (m_value >> precision) < other || m_value < (static_cast<Underlying>(other) << precision);
}
template<Integral I>
bool operator<=(I other) const
{
return *this < other || *this == other;
}
// Casting from a float should be faster than casting to a float
template<FloatingPoint F>
bool operator==(F other) const { return *this == (This)other; }
template<FloatingPoint F>
bool operator!=(F other) const { return *this != (This)other; }
template<FloatingPoint F>
bool operator>(F other) const { return *this > (This)other; }
template<FloatingPoint F>
bool operator>=(F other) const { return *this >= (This)other; }
template<FloatingPoint F>
bool operator<(F other) const { return *this < (This)other; }
template<FloatingPoint F>
bool operator<=(F other) const { return *this <= (This)other; }
template<size_t P, typename U>
operator FixedPoint<P, U>() const
{
return cast_to<P, U>();
}
private:
template<size_t P, typename U>
constexpr FixedPoint<P, U> cast_to() const
{
U raw_value = static_cast<U>(m_value >> precision) << P;
if constexpr (precision > P)
raw_value |= (m_value & radix_mask) >> (precision - P);
else if constexpr (precision < P)
raw_value |= static_cast<U>(m_value & radix_mask) << (P - precision);
else
raw_value |= m_value & radix_mask;
return FixedPoint<P, U>::create_raw(raw_value);
}
Underlying m_value;
};
template<size_t precision, typename Underlying>
struct Formatter<FixedPoint<precision, Underlying>> : StandardFormatter {
Formatter() = default;
explicit Formatter(StandardFormatter formatter)
: StandardFormatter(formatter)
{
}
ErrorOr<void> format(FormatBuilder& builder, FixedPoint<precision, Underlying> value)
{
u8 base;
bool upper_case = false;
if (m_mode == Mode::Default || m_mode == Mode::FixedPoint) {
base = 10;
} else if (m_mode == Mode::Hexfloat) {
base = 16;
} else if (m_mode == Mode::HexfloatUppercase) {
base = 16;
upper_case = true;
} else if (m_mode == Mode::Binary) {
base = 2;
} else if (m_mode == Mode::BinaryUppercase) {
base = 2;
upper_case = true;
} else if (m_mode == Mode::Octal) {
base = 8;
} else {
VERIFY_NOT_REACHED();
}
m_width = m_width.value_or(0);
m_precision = m_precision.value_or(6);
bool is_negative = false;
if constexpr (IsSigned<Underlying>)
is_negative = value < 0;
i64 integer = value.ltrunc();
constexpr u64 one = static_cast<Underlying>(1) << precision;
u64 fraction_raw = value.raw() & (one - 1);
return builder.put_fixed_point(is_negative, integer, fraction_raw, one, precision, base, upper_case, m_zero_pad, m_use_separator, m_align, m_width.value(), m_precision.value(), m_fill, m_sign_mode);
}
};
}
#if USING_AK_GLOBALLY
using AK::FixedPoint;
#endif