AK: Remove unused FixedPoint
This commit is contained in:
parent
fb5b78bfea
commit
9ebcc67144
4 changed files with 0 additions and 789 deletions
482
AK/FixedPoint.h
482
AK/FixedPoint.h
|
|
@ -1,482 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Concepts.h>
|
||||
#include <AK/Format.h>
|
||||
#include <AK/IntegralMath.h>
|
||||
#include <AK/Math.h>
|
||||
#include <AK/NumericLimits.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
#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)
|
||||
{
|
||||
}
|
||||
|
||||
template<FloatingPoint F>
|
||||
FixedPoint(F value)
|
||||
: m_value(round_to<Underlying>(value * (static_cast<Underlying>(1) << precision)))
|
||||
{
|
||||
}
|
||||
|
||||
template<size_t P, typename U>
|
||||
explicit constexpr FixedPoint(FixedPoint<P, U> const& other)
|
||||
: m_value(other.template cast_to<precision, Underlying>().m_value)
|
||||
{
|
||||
}
|
||||
|
||||
template<FloatingPoint F>
|
||||
explicit ALWAYS_INLINE operator F() const
|
||||
{
|
||||
return (F)m_value * pow<F>(0.5, precision);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -120,9 +120,6 @@ class Badge;
|
|||
template<typename T>
|
||||
class FixedArray;
|
||||
|
||||
template<size_t precision, typename Underlying = i32>
|
||||
class FixedPoint;
|
||||
|
||||
template<typename>
|
||||
class Function;
|
||||
|
||||
|
|
|
|||
|
|
@ -27,7 +27,6 @@ set(AK_TEST_SOURCES
|
|||
TestEnumerate.cpp
|
||||
TestFind.cpp
|
||||
TestFixedArray.cpp
|
||||
TestFixedPoint.cpp
|
||||
TestFunction.cpp
|
||||
TestFlyString.cpp
|
||||
TestFormat.cpp
|
||||
|
|
|
|||
|
|
@ -1,303 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2021, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibTest/TestCase.h>
|
||||
|
||||
#include <AK/FixedPoint.h>
|
||||
#include <AK/NumericLimits.h>
|
||||
|
||||
using AK::FixedPoint;
|
||||
|
||||
using Type = FixedPoint<4>;
|
||||
|
||||
TEST_CASE(arithmetic)
|
||||
{
|
||||
EXPECT_EQ(
|
||||
Type(0.5) + Type(0.5),
|
||||
Type(1));
|
||||
EXPECT_EQ(
|
||||
Type(1) + Type(0.5),
|
||||
Type(1.5));
|
||||
EXPECT_EQ(
|
||||
(float)(Type(1) + Type(0.5)),
|
||||
1.5f);
|
||||
// FIXME: Test for rounded multiply
|
||||
EXPECT_EQ(
|
||||
Type((int)1) * Type(0.5),
|
||||
Type(0.5));
|
||||
EXPECT_EQ(Type(0.125) * Type(3.75),
|
||||
Type(0.125 * 3.75));
|
||||
EXPECT_EQ(Type(0.125) * Type(-3.75),
|
||||
Type(0.125 * -3.75));
|
||||
|
||||
EXPECT_EQ(
|
||||
Type((int)1) / Type(0.5),
|
||||
Type(2));
|
||||
}
|
||||
|
||||
TEST_CASE(rounding)
|
||||
{
|
||||
EXPECT_EQ(Type(0.5).rint(), Type(0));
|
||||
EXPECT_EQ(Type(0.5).floor(), Type(0));
|
||||
EXPECT_EQ(Type(0.5).ceil(), Type(1));
|
||||
EXPECT_EQ(Type(0.75).trunc(), Type(0));
|
||||
|
||||
EXPECT_EQ(Type(1.5).rint(), Type(2));
|
||||
EXPECT_EQ(Type(1.5).floor(), Type(1));
|
||||
EXPECT_EQ(Type(1.5).ceil(), Type(2));
|
||||
EXPECT_EQ(Type(1.25).trunc(), Type(1));
|
||||
|
||||
EXPECT_EQ(Type(-0.5).rint(), Type(0));
|
||||
EXPECT_EQ(Type(-0.5).floor(), Type(-1));
|
||||
EXPECT_EQ(Type(-0.5).ceil(), Type(0));
|
||||
EXPECT_EQ(Type(-0.75).trunc(), Type(0));
|
||||
|
||||
EXPECT_EQ(Type(-1.5).rint(), Type(-2));
|
||||
EXPECT_EQ(Type(-1.5).floor(), Type(-2));
|
||||
EXPECT_EQ(Type(-1.5).ceil(), Type(-1));
|
||||
EXPECT_EQ(Type(-1.25).trunc(), Type(-1));
|
||||
|
||||
EXPECT_EQ(Type(2.75).rint(), Type(3));
|
||||
EXPECT_EQ(Type(-1.25).rint(), Type(-1));
|
||||
|
||||
EXPECT_EQ(Type(0.5).lrint(), 0);
|
||||
EXPECT_EQ(Type(0.5).lfloor(), 0);
|
||||
EXPECT_EQ(Type(0.5).lceil(), 1);
|
||||
EXPECT_EQ(Type(0.5).ltrunc(), 0);
|
||||
|
||||
EXPECT_EQ(Type(1.5).lrint(), 2);
|
||||
EXPECT_EQ(Type(1.5).lfloor(), 1);
|
||||
EXPECT_EQ(Type(1.5).lceil(), 2);
|
||||
EXPECT_EQ(Type(1.5).ltrunc(), 1);
|
||||
|
||||
EXPECT_EQ(Type(-0.5).lrint(), 0);
|
||||
EXPECT_EQ(Type(-0.5).lfloor(), -1);
|
||||
EXPECT_EQ(Type(-0.5).lceil(), 0);
|
||||
EXPECT_EQ(Type(-0.5).ltrunc(), 0);
|
||||
|
||||
EXPECT_EQ(Type(-1.5).lrint(), -2);
|
||||
EXPECT_EQ(Type(-1.5).lfloor(), -2);
|
||||
EXPECT_EQ(Type(-1.5).lceil(), -1);
|
||||
EXPECT_EQ(Type(-1.5).ltrunc(), -1);
|
||||
|
||||
EXPECT_EQ(Type(-1.6).rint(), -2);
|
||||
EXPECT_EQ(Type(-1.4).rint(), -1);
|
||||
EXPECT_EQ(Type(1.6).rint(), 2);
|
||||
EXPECT_EQ(Type(1.4).rint(), 1);
|
||||
|
||||
// Check that sRGB TRC curve parameters match the s15fixed16 values stored in Gimp's built-in profile.
|
||||
// (This only requires that the FixedPoint<> constructor rounds before truncating to the fixed-point value,
|
||||
// as it should anyways.)
|
||||
using S15Fixed16 = FixedPoint<16, i32>;
|
||||
EXPECT_EQ(S15Fixed16(2.4).raw(), 0x26666);
|
||||
EXPECT_EQ(S15Fixed16(1 / 1.055).raw(), 0xf2a7);
|
||||
EXPECT_EQ(S15Fixed16(0.055 / 1.055).raw(), 0xd59);
|
||||
EXPECT_EQ(S15Fixed16(1 / 12.92).raw(), 0x13d0);
|
||||
EXPECT_EQ(S15Fixed16(0.04045).raw(), 0xa5b);
|
||||
}
|
||||
|
||||
TEST_CASE(logarithm)
|
||||
{
|
||||
EXPECT_EQ(Type(0).log2().raw(), NumericLimits<int>::min());
|
||||
EXPECT_EQ(Type(1).log2(), Type(0));
|
||||
EXPECT_EQ(Type(2).log2(), Type(1));
|
||||
EXPECT_EQ(Type(8).log2(), Type(3));
|
||||
EXPECT_EQ(Type(0.5).log2(), Type(-1));
|
||||
|
||||
EXPECT_EQ(Type(22.627416997969520780827019587355).log2(), Type(4.4375));
|
||||
EXPECT_EQ(Type(3088).log2(), Type(11.592457037268080419637304576833));
|
||||
}
|
||||
|
||||
TEST_CASE(comparison)
|
||||
{
|
||||
EXPECT(Type(0) < 1);
|
||||
EXPECT(Type(0) <= 1);
|
||||
EXPECT(Type(0) <= 0);
|
||||
EXPECT(Type(-10) <= -10);
|
||||
|
||||
EXPECT(Type(4.25) > 4);
|
||||
EXPECT(Type(4.25) >= 4);
|
||||
EXPECT(Type(4.25) <= 5);
|
||||
EXPECT(Type(4.25) < 5);
|
||||
EXPECT(Type(1.5) > 1);
|
||||
|
||||
EXPECT(!(FixedPoint<4, u8>(2) > 128));
|
||||
EXPECT(!(FixedPoint<4, u8>(2) >= 128));
|
||||
|
||||
EXPECT(Type(-6.25) < -6);
|
||||
EXPECT(Type(-6.25) <= -6);
|
||||
EXPECT(Type(-6.75) > -7);
|
||||
EXPECT(Type(-6.75) >= -7);
|
||||
|
||||
EXPECT(Type(17) == 17);
|
||||
EXPECT(Type(-8) != -9);
|
||||
}
|
||||
|
||||
TEST_CASE(cast)
|
||||
{
|
||||
FixedPoint<16, u32> downcast_value1(FixedPoint<32, u64>(123.4567));
|
||||
EXPECT((double)downcast_value1 >= 123.4566 && (double)downcast_value1 <= 123.4568);
|
||||
static FixedPoint<32, u64> const value1(321.7654);
|
||||
downcast_value1 = value1;
|
||||
EXPECT((double)downcast_value1 >= 321.7653 && (double)downcast_value1 <= 321.7655);
|
||||
FixedPoint<6, u32> downcast_value2(FixedPoint<32, u64>(4567.123456));
|
||||
EXPECT((double)downcast_value2 >= 4567.1 && (double)downcast_value2 <= 4567.2);
|
||||
downcast_value2 = FixedPoint<32, u64>(7654.654321);
|
||||
EXPECT((double)downcast_value2 >= 7654.64 && (double)downcast_value2 <= 7654.66);
|
||||
|
||||
EXPECT((double)downcast_value2 >= 7654.64 && (double)downcast_value2 <= 7654.66);
|
||||
FixedPoint<6, u32> downcast_value3(FixedPoint<32, u64>(4567.987654));
|
||||
EXPECT((double)downcast_value3 >= 4567.9 && (double)downcast_value3 <= 4567.99);
|
||||
downcast_value3 = FixedPoint<32, u64>(7654.456789);
|
||||
EXPECT((double)downcast_value3 >= 7654.45 && (double)downcast_value3 <= 7654.46);
|
||||
|
||||
FixedPoint<32, u64> upcast_value1(FixedPoint<16, u32>(123.4567));
|
||||
EXPECT((double)upcast_value1 >= 123.4566 && (double)upcast_value1 <= 123.4568);
|
||||
upcast_value1 = FixedPoint<16, u32>(321.7654);
|
||||
EXPECT((double)upcast_value1 >= 321.7653 && (double)upcast_value1 <= 321.7655);
|
||||
FixedPoint<32, u64> upcast_value2(FixedPoint<6, u32>(4567.123456));
|
||||
EXPECT((double)upcast_value2 >= 4567.1 && (double)upcast_value2 <= 4567.2);
|
||||
upcast_value2 = FixedPoint<6, u32>(7654.654321);
|
||||
EXPECT((double)upcast_value2 >= 7654.64 && (double)upcast_value2 <= 7654.66);
|
||||
FixedPoint<32, u64> upcast_value3(FixedPoint<6, u32>(4567.987654));
|
||||
EXPECT((double)upcast_value3 >= 4567.9 && (double)upcast_value3 <= 4567.99);
|
||||
upcast_value3 = FixedPoint<6, u32>(7654.456789);
|
||||
EXPECT((double)upcast_value3 >= 7654.45 && (double)upcast_value3 <= 7654.46);
|
||||
}
|
||||
|
||||
TEST_CASE(formatter)
|
||||
{
|
||||
EXPECT_EQ(ByteString::formatted("{}", FixedPoint<16>(123.456)), "123.455993"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{}", FixedPoint<16>(-123.456)), "-123.455993"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{}", FixedPoint<4>(123.456)), "123.4375"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{}", FixedPoint<4>(-123.456)), "-123.4375"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{}", FixedPoint<16> {}), "0"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{}", FixedPoint<16>(0.1)), "0.100006"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{}", FixedPoint<16>(0.02)), "0.020004"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{}", FixedPoint<16>(0.003)), "0.003005"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{}", FixedPoint<16>(0.0004)), "0.000396"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{}", FixedPoint<16>(0.0000000005)), "0"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{}", FixedPoint<16>(-0.1)), "-0.100006"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{}", FixedPoint<16>(-0.02)), "-0.020004"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{}", FixedPoint<16>(-0.0000000005)), "0"sv);
|
||||
|
||||
EXPECT_EQ(ByteString::formatted("{}", Type(-1)), "-1"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{}", Type(-2)), "-2"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{}", Type(-3)), "-3"sv);
|
||||
|
||||
// exact representation
|
||||
EXPECT_EQ(ByteString::formatted("{:.30}", FixedPoint<16>(123.456)), "123.45599365234375"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30}", FixedPoint<16>(-0.1)), "-0.100006103515625"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30}", FixedPoint<16>(-0.02)), "-0.0200042724609375"sv);
|
||||
|
||||
// maximum fraction per precision; 1 - 2^-precision
|
||||
EXPECT_EQ(ByteString::formatted("{:0.20}", AK::FixedPoint<7, u64>::create_raw((1ull << 7) - 1)), "0.99218750000000000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.20}", AK::FixedPoint<8, u64>::create_raw((1ull << 8) - 1)), "0.99609375000000000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.20}", AK::FixedPoint<9, u64>::create_raw((1ull << 9) - 1)), "0.99804687500000000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.20}", AK::FixedPoint<10, u64>::create_raw((1ull << 10) - 1)), "0.99902343750000000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.20}", AK::FixedPoint<11, u64>::create_raw((1ull << 11) - 1)), "0.99951171875000000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.20}", AK::FixedPoint<12, u64>::create_raw((1ull << 12) - 1)), "0.99975585937500000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.20}", AK::FixedPoint<13, u64>::create_raw((1ull << 13) - 1)), "0.99987792968750000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.20}", AK::FixedPoint<14, u64>::create_raw((1ull << 14) - 1)), "0.99993896484375000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.20}", AK::FixedPoint<15, u64>::create_raw((1ull << 15) - 1)), "0.99996948242187500000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.20}", AK::FixedPoint<16, u64>::create_raw((1ull << 16) - 1)), "0.99998474121093750000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.20}", AK::FixedPoint<17, u64>::create_raw((1ull << 17) - 1)), "0.99999237060546875000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.20}", AK::FixedPoint<18, u64>::create_raw((1ull << 18) - 1)), "0.99999618530273437500"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.20}", AK::FixedPoint<19, u64>::create_raw((1ull << 19) - 1)), "0.99999809265136718750"sv);
|
||||
// maximum factor and precision >= 20 bits/digits will overflow u64: (5^20)*(2^20 - 1) > 2^64
|
||||
// EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<20, u64>::create_raw((1ull << 20) - 1)), "0.99999904632568359375"sv);
|
||||
|
||||
// minimum fraction per precision; 2^-precision
|
||||
EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<7, u64>::create_raw(1)), "0.007812500000000000000000000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<8, u64>::create_raw(1)), "0.003906250000000000000000000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<9, u64>::create_raw(1)), "0.001953125000000000000000000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<10, u64>::create_raw(1)), "0.000976562500000000000000000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<11, u64>::create_raw(1)), "0.000488281250000000000000000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<12, u64>::create_raw(1)), "0.000244140625000000000000000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<13, u64>::create_raw(1)), "0.000122070312500000000000000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<14, u64>::create_raw(1)), "0.000061035156250000000000000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<15, u64>::create_raw(1)), "0.000030517578125000000000000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<16, u64>::create_raw(1)), "0.000015258789062500000000000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<17, u64>::create_raw(1)), "0.000007629394531250000000000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<18, u64>::create_raw(1)), "0.000003814697265625000000000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<19, u64>::create_raw(1)), "0.000001907348632812500000000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<20, u64>::create_raw(1)), "0.000000953674316406250000000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<21, u64>::create_raw(1)), "0.000000476837158203125000000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<22, u64>::create_raw(1)), "0.000000238418579101562500000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<23, u64>::create_raw(1)), "0.000000119209289550781250000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<24, u64>::create_raw(1)), "0.000000059604644775390625000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<25, u64>::create_raw(1)), "0.000000029802322387695312500000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<26, u64>::create_raw(1)), "0.000000014901161193847656250000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<27, u64>::create_raw(1)), "0.000000007450580596923828125000"sv);
|
||||
// minimum factor and precision >= 28 bits/digits will overflow u64: (5^28)*(1) > 2^64
|
||||
// EXPECT_EQ(ByteString::formatted("{:0.30}", AK::FixedPoint<28, u64>::create_raw(1)), "0.000000003725290298461914062500"sv);
|
||||
|
||||
EXPECT_EQ(ByteString::formatted("{:a}", FixedPoint<16>(42.42)), "2a.6b85"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:o}", FixedPoint<16>(42.42)), "52.327024"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:b}", FixedPoint<16>(42.42)), "101010.01101"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.10a}", FixedPoint<16>(69.69)), "45.b0a4000000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.10o}", FixedPoint<16>(69.69)), "105.5412200000"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:0.10b}", FixedPoint<16>(69.69)), "1000101.1011000010"sv);
|
||||
|
||||
EXPECT_EQ(ByteString::formatted("{:.30o}", AK::FixedPoint<13, u64>::create_raw(1)), "0.00004"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30b}", AK::FixedPoint<13, u64>::create_raw(1)), "0.0000000000001"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30o}", AK::FixedPoint<21, u64>::create_raw(0211234567)), "21.1234567"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30b}", AK::FixedPoint<13, u64>::create_raw(0b110011011010110)), "11.001101101011"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30o}", AK::FixedPoint<11, u64>::create_raw((1ull << 11) - 1)), "0.7776"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30b}", AK::FixedPoint<11, u64>::create_raw((1ull << 11) - 1)), "0.11111111111"sv);
|
||||
|
||||
// maximum fraction per precision rendered in hexadecimal; 1 - 2^-precision; no overflow
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<7, u64>::create_raw((1ull << 7) - 1)), "0.fe"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<8, u64>::create_raw((1ull << 8) - 1)), "0.ff"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<9, u64>::create_raw((1ull << 9) - 1)), "0.ff8"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<10, u64>::create_raw((1ull << 10) - 1)), "0.ffc"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<11, u64>::create_raw((1ull << 11) - 1)), "0.ffe"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<12, u64>::create_raw((1ull << 12) - 1)), "0.fff"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<13, u64>::create_raw((1ull << 13) - 1)), "0.fff8"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<14, u64>::create_raw((1ull << 14) - 1)), "0.fffc"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<15, u64>::create_raw((1ull << 15) - 1)), "0.fffe"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<16, u64>::create_raw((1ull << 16) - 1)), "0.ffff"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<17, u64>::create_raw((1ull << 17) - 1)), "0.ffff8"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<18, u64>::create_raw((1ull << 18) - 1)), "0.ffffc"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<19, u64>::create_raw((1ull << 19) - 1)), "0.ffffe"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<20, u64>::create_raw((1ull << 20) - 1)), "0.fffff"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<21, u64>::create_raw((1ull << 21) - 1)), "0.fffff8"sv);
|
||||
// ...skip some precisions
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<56, u64>::create_raw((1ull << 56) - 1)), "0.ffffffffffffff"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<57, u64>::create_raw((1ull << 57) - 1)), "0.ffffffffffffff8"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<58, u64>::create_raw((1ull << 58) - 1)), "0.ffffffffffffffc"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<59, u64>::create_raw((1ull << 59) - 1)), "0.ffffffffffffffe"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<60, u64>::create_raw((1ull << 60) - 1)), "0.fffffffffffffff"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<61, u64>::create_raw((1ull << 61) - 1)), "0.fffffffffffffff8"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<62, u64>::create_raw((1ull << 62) - 1)), "0.fffffffffffffffc"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<63, u64>::create_raw((1ull << 63) - 1)), "0.fffffffffffffffe"sv);
|
||||
|
||||
// minimum fraction per precision rendered in hexadecimal; 2^-precision; no overflow
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<7, u64>::create_raw(1)), "0.02"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<8, u64>::create_raw(1)), "0.01"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<9, u64>::create_raw(1)), "0.008"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<10, u64>::create_raw(1)), "0.004"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<11, u64>::create_raw(1)), "0.002"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<12, u64>::create_raw(1)), "0.001"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<13, u64>::create_raw(1)), "0.0008"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<14, u64>::create_raw(1)), "0.0004"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<15, u64>::create_raw(1)), "0.0002"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<16, u64>::create_raw(1)), "0.0001"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<17, u64>::create_raw(1)), "0.00008"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<18, u64>::create_raw(1)), "0.00004"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<19, u64>::create_raw(1)), "0.00002"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<20, u64>::create_raw(1)), "0.00001"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<21, u64>::create_raw(1)), "0.000008"sv);
|
||||
// ..skip some precisions
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<56, u64>::create_raw(1)), "0.00000000000001"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<57, u64>::create_raw(1)), "0.000000000000008"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<58, u64>::create_raw(1)), "0.000000000000004"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<59, u64>::create_raw(1)), "0.000000000000002"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<60, u64>::create_raw(1)), "0.000000000000001"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<61, u64>::create_raw(1)), "0.0000000000000008"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<62, u64>::create_raw(1)), "0.0000000000000004"sv);
|
||||
EXPECT_EQ(ByteString::formatted("{:.30a}", AK::FixedPoint<63, u64>::create_raw(1)), "0.0000000000000002"sv);
|
||||
}
|
||||
Loading…
Reference in a new issue