2021-08-20 15:35:03 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Concepts.h>
|
2023-01-18 21:48:02 +00:00
|
|
|
#include <AK/Format.h>
|
2022-02-14 23:38:54 +00:00
|
|
|
#include <AK/IntegralMath.h>
|
|
|
|
#include <AK/NumericLimits.h>
|
2021-08-20 15:35:03 +00:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
2022-04-09 20:31:54 +00:00
|
|
|
#ifndef KERNEL
|
|
|
|
# include <AK/Math.h>
|
|
|
|
#endif
|
|
|
|
|
2023-02-25 20:16:03 +00:00
|
|
|
// Solaris' definition of signbit in math_c99.h conflicts with our implementation.
|
|
|
|
#ifdef AK_OS_SOLARIS
|
|
|
|
# undef signbit
|
|
|
|
#endif
|
|
|
|
|
2021-08-20 15:35:03 +00:00
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
// FIXME: this always uses round to nearest break-tie to even
|
2021-12-28 01:18:53 +00:00
|
|
|
// FIXME: use the Integral concept to constrain Underlying
|
|
|
|
template<size_t precision, typename Underlying>
|
2021-08-20 15:35:03 +00:00
|
|
|
class FixedPoint {
|
|
|
|
using This = FixedPoint<precision, Underlying>;
|
2022-01-21 04:29:30 +00:00
|
|
|
constexpr static Underlying radix_mask = (static_cast<Underlying>(1) << precision) - 1;
|
2021-08-20 15:35:03 +00:00
|
|
|
|
2022-01-21 04:31:13 +00:00
|
|
|
template<size_t P, typename U>
|
|
|
|
friend class FixedPoint;
|
|
|
|
|
2021-08-20 15:35:03 +00:00
|
|
|
public:
|
|
|
|
constexpr FixedPoint() = default;
|
|
|
|
template<Integral I>
|
|
|
|
constexpr FixedPoint(I value)
|
2022-01-21 04:29:30 +00:00
|
|
|
: m_value(static_cast<Underlying>(value) << precision)
|
2021-08-20 15:35:03 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-01 23:34:01 +00:00
|
|
|
#ifndef KERNEL
|
2021-08-20 15:35:03 +00:00
|
|
|
template<FloatingPoint F>
|
2023-03-01 23:34:01 +00:00
|
|
|
FixedPoint(F value)
|
|
|
|
: m_value(round_to<Underlying>(value * (static_cast<Underlying>(1) << precision)))
|
2021-08-20 15:35:03 +00:00
|
|
|
{
|
|
|
|
}
|
2023-03-01 23:34:01 +00:00
|
|
|
#endif
|
2021-08-20 15:35:03 +00:00
|
|
|
|
2022-01-21 04:31:13 +00:00
|
|
|
template<size_t P, typename U>
|
|
|
|
explicit constexpr FixedPoint(FixedPoint<P, U> const& other)
|
|
|
|
: m_value(other.template cast_to<precision, Underlying>().m_value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-09 20:31:54 +00:00
|
|
|
#ifndef KERNEL
|
2021-08-20 15:35:03 +00:00
|
|
|
template<FloatingPoint F>
|
|
|
|
explicit ALWAYS_INLINE operator F() const
|
|
|
|
{
|
|
|
|
return (F)m_value * pow<F>(0.5, precision);
|
|
|
|
}
|
2022-04-09 20:31:54 +00:00
|
|
|
#endif
|
|
|
|
|
2021-08-20 15:35:03 +00:00
|
|
|
template<Integral I>
|
|
|
|
explicit constexpr operator I() const
|
|
|
|
{
|
|
|
|
I value = m_value >> precision;
|
|
|
|
// fract(m_value) >= .5?
|
|
|
|
if (m_value & (1u << (precision - 1))) {
|
|
|
|
// fract(m_value) > .5?
|
|
|
|
if (m_value & (radix_mask >> 2u)) {
|
|
|
|
// yes: round "up";
|
|
|
|
value += (m_value > 0 ? 1 : -1);
|
|
|
|
} else {
|
|
|
|
// no: round to even;
|
|
|
|
value += value & 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2023-01-23 15:48:49 +00:00
|
|
|
static constexpr This create_raw(Underlying value)
|
|
|
|
{
|
|
|
|
This t {};
|
|
|
|
t.raw() = value;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2021-08-20 15:35:03 +00:00
|
|
|
constexpr Underlying raw() const
|
|
|
|
{
|
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
constexpr Underlying& raw()
|
|
|
|
{
|
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr This fract() const
|
|
|
|
{
|
|
|
|
return create_raw(m_value & radix_mask);
|
|
|
|
}
|
|
|
|
|
2023-02-09 14:35:29 +00:00
|
|
|
constexpr This clamp(This minimum, This maximum) const
|
|
|
|
{
|
|
|
|
if (*this < minimum)
|
|
|
|
return minimum;
|
|
|
|
if (*this > maximum)
|
|
|
|
return maximum;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-08-20 15:35:03 +00:00
|
|
|
constexpr This round() const
|
|
|
|
{
|
|
|
|
return This { static_cast<Underlying>(*this) };
|
|
|
|
}
|
|
|
|
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 ? 1 << precision : 0));
|
|
|
|
}
|
|
|
|
constexpr This trunk() const
|
|
|
|
{
|
|
|
|
return create_raw((m_value & ~radix_mask)
|
|
|
|
+ ((m_value & radix_mask)
|
|
|
|
? (m_value > 0 ? 0 : (1 << precision))
|
|
|
|
: 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr Underlying lround() const { return static_cast<Underlying>(*this); }
|
|
|
|
constexpr Underlying lfloor() const { return m_value >> precision; }
|
|
|
|
constexpr Underlying lceil() const
|
|
|
|
{
|
|
|
|
return (m_value >> precision)
|
|
|
|
+ (m_value & radix_mask ? 1 : 0);
|
|
|
|
}
|
|
|
|
constexpr Underlying ltrunk() const
|
|
|
|
{
|
|
|
|
return (m_value >> precision)
|
|
|
|
+ ((m_value & radix_mask)
|
|
|
|
? m_value > 0 ? 0 : 1
|
|
|
|
: 0);
|
|
|
|
}
|
|
|
|
|
2022-02-14 23:38:54 +00:00
|
|
|
// http://www.claysturner.com/dsp/BinaryLogarithm.pdf
|
|
|
|
constexpr This log2() const
|
|
|
|
{
|
|
|
|
// 0.5
|
|
|
|
This b = create_raw(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;
|
|
|
|
}
|
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
constexpr bool signbit() const
|
|
|
|
requires(IsSigned<Underlying>)
|
2021-08-20 15:35:03 +00:00
|
|
|
{
|
|
|
|
return m_value >> (sizeof(Underlying) * 8 - 1);
|
|
|
|
}
|
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
constexpr This operator-() const
|
|
|
|
requires(IsSigned<Underlying>)
|
2021-08-20 15:35:03 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2023-07-23 17:42:19 +00:00
|
|
|
// FIXME: Figure out a 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);
|
|
|
|
// Rounding:
|
|
|
|
// If last bit cut off is 1:
|
2021-08-20 15:35:03 +00:00
|
|
|
if (value & (1u << (precision - 1))) {
|
2023-07-23 17:42:19 +00:00
|
|
|
// If the bit after is 1 as well
|
2021-08-20 15:35:03 +00:00
|
|
|
if (value & (radix_mask >> 2u)) {
|
2023-07-23 17:42:19 +00:00
|
|
|
// We round away from 0
|
|
|
|
ret.raw() += 1;
|
2021-08-20 15:35:03 +00:00
|
|
|
} else {
|
2023-07-23 17:42:19 +00:00
|
|
|
// 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;
|
2021-08-20 15:35:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
constexpr This operator/(This const& other) const
|
|
|
|
{
|
|
|
|
// FIXME: Better rounding?
|
|
|
|
return create_raw((m_value / other.m_value) << (precision));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<Integral I>
|
|
|
|
constexpr This operator+(I other) const
|
|
|
|
{
|
|
|
|
return create_raw(m_value + (other << precision));
|
|
|
|
}
|
|
|
|
template<Integral I>
|
|
|
|
constexpr This operator-(I other) const
|
|
|
|
{
|
|
|
|
return create_raw(m_value - (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);
|
|
|
|
}
|
2022-02-14 23:35:20 +00:00
|
|
|
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);
|
|
|
|
}
|
2021-08-20 15:35:03 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2023-07-23 17:42:19 +00:00
|
|
|
*this = *this * other;
|
2021-08-20 15:35:03 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
This& operator/=(This const& other)
|
|
|
|
{
|
|
|
|
// FIXME: See above
|
|
|
|
m_value /= other.raw();
|
|
|
|
m_value <<= precision;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<Integral I>
|
|
|
|
This& operator+=(I other)
|
|
|
|
{
|
|
|
|
m_value += other << precision;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template<Integral I>
|
|
|
|
This& operator-=(I other)
|
|
|
|
{
|
|
|
|
m_value -= 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;
|
|
|
|
}
|
2022-02-14 23:35:20 +00:00
|
|
|
template<Integral I>
|
|
|
|
This& operator>>=(I other)
|
|
|
|
{
|
|
|
|
m_value >>= other;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template<Integral I>
|
|
|
|
This& operator<<=(I other)
|
|
|
|
{
|
|
|
|
m_value <<= other;
|
|
|
|
return *this;
|
|
|
|
}
|
2021-08-20 15:35:03 +00:00
|
|
|
|
|
|
|
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(); }
|
|
|
|
|
2023-04-12 17:32:21 +00:00
|
|
|
// FIXME: There are probably better ways to do these
|
2021-08-20 15:35:03 +00:00
|
|
|
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
|
|
|
|
{
|
2022-02-14 23:32:55 +00:00
|
|
|
return !(*this <= other);
|
2021-08-20 15:35:03 +00:00
|
|
|
}
|
|
|
|
template<Integral I>
|
|
|
|
bool operator>=(I other) const
|
|
|
|
{
|
2022-02-14 23:32:55 +00:00
|
|
|
return !(*this < other);
|
2021-08-20 15:35:03 +00:00
|
|
|
}
|
|
|
|
template<Integral I>
|
|
|
|
bool operator<(I other) const
|
|
|
|
{
|
2022-02-14 23:32:55 +00:00
|
|
|
return (m_value >> precision) < other || m_value < (other << precision);
|
2021-08-20 15:35:03 +00:00
|
|
|
}
|
|
|
|
template<Integral I>
|
|
|
|
bool operator<=(I other) const
|
|
|
|
{
|
2022-02-14 23:32:55 +00:00
|
|
|
return *this < other || *this == other;
|
2021-08-20 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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; }
|
|
|
|
|
2022-01-21 04:31:13 +00:00
|
|
|
template<size_t P, typename U>
|
|
|
|
operator FixedPoint<P, U>() const
|
|
|
|
{
|
|
|
|
return cast_to<P, U>();
|
|
|
|
}
|
|
|
|
|
2021-08-20 15:35:03 +00:00
|
|
|
private:
|
2022-01-21 04:31:13 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-08-20 15:35:03 +00:00
|
|
|
Underlying m_value;
|
|
|
|
};
|
|
|
|
|
2023-01-18 21:48:02 +00:00
|
|
|
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;
|
|
|
|
FormatBuilder::RealNumberDisplayMode real_number_display_mode = FormatBuilder::RealNumberDisplayMode::General;
|
|
|
|
if (m_mode == Mode::Default || m_mode == Mode::FixedPoint) {
|
|
|
|
base = 10;
|
|
|
|
upper_case = false;
|
|
|
|
if (m_mode == Mode::FixedPoint)
|
|
|
|
real_number_display_mode = FormatBuilder::RealNumberDisplayMode::FixedPoint;
|
|
|
|
} else if (m_mode == Mode::Hexfloat) {
|
|
|
|
base = 16;
|
|
|
|
upper_case = false;
|
|
|
|
} else if (m_mode == Mode::HexfloatUppercase) {
|
|
|
|
base = 16;
|
|
|
|
upper_case = true;
|
|
|
|
} else {
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_width = m_width.value_or(0);
|
|
|
|
m_precision = m_precision.value_or(6);
|
|
|
|
|
2023-02-18 16:31:59 +00:00
|
|
|
bool is_negative = false;
|
|
|
|
if constexpr (IsSigned<Underlying>)
|
|
|
|
is_negative = value < 0;
|
|
|
|
|
2023-01-18 21:48:02 +00:00
|
|
|
i64 integer = value.ltrunk();
|
|
|
|
constexpr u64 one = static_cast<Underlying>(1) << precision;
|
|
|
|
u64 fraction_raw = value.raw() & (one - 1);
|
2023-03-06 17:31:39 +00:00
|
|
|
return builder.put_fixed_point(is_negative, integer, fraction_raw, one, base, upper_case, m_zero_pad, m_use_separator, m_align, m_width.value(), m_precision.value(), m_fill, m_sign_mode, real_number_display_mode);
|
2023-01-18 21:48:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-20 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2021-08-20 15:35:03 +00:00
|
|
|
using AK::FixedPoint;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|