From 0a6c454abb3f1c830036e95c76bbf776845e105d Mon Sep 17 00:00:00 2001 From: AyiStar Date: Thu, 30 Nov 2023 23:28:20 +0800 Subject: [PATCH 1/2] try adding two math constants E and PI for FixedPoint --- libopenage/util/fixed_point.h | 25 +++++++++++++++++++++---- libopenage/util/fixed_point_test.cpp | 9 +++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/libopenage/util/fixed_point.h b/libopenage/util/fixed_point.h index 1e27d15082..3623091218 100644 --- a/libopenage/util/fixed_point.h +++ b/libopenage/util/fixed_point.h @@ -53,8 +53,8 @@ constexpr static /** - * Helper function that performs either a safe shift-right (amount > 0), - * or a safe shift-left (amount < 0). + * Helper function that performs either a safe shift-right (amount < 0), + * or a safe shift-left (amount >= 0). */ template constexpr static @@ -169,6 +169,17 @@ class FixedPoint { return FixedPoint::from_int(0); } + /** + * Constants + */ + static constexpr FixedPoint e() { + return from_fixedpoint(FixedPoint::from_raw_value(6267931151224907085ll)); + } + + static constexpr FixedPoint pi() { + return from_fixedpoint(FixedPoint::from_raw_value(7244019458077122842ll)); + } + /** * Factory function to get a fixed-point number from an integer. */ @@ -193,10 +204,16 @@ class FixedPoint { /** * Factory function to get a fixed-point number from a fixed-point number of different type. */ - template + template other_fractional_bits)>::type* = nullptr> + static constexpr FixedPoint from_fixedpoint(const FixedPoint &other) { + return FixedPoint::from_raw_value( + safe_shift(static_cast(other.get_raw_value()))); + } + + template ::type* = nullptr> static constexpr FixedPoint from_fixedpoint(const FixedPoint &other) { return FixedPoint::from_raw_value( - safe_shift(other.get_raw_value())); + static_cast(other.get_raw_value() / safe_shiftleft(1))); } /** diff --git a/libopenage/util/fixed_point_test.cpp b/libopenage/util/fixed_point_test.cpp index e5728f518c..72a0a0f088 100644 --- a/libopenage/util/fixed_point_test.cpp +++ b/libopenage/util/fixed_point_test.cpp @@ -6,6 +6,7 @@ #include "../testing/testing.h" #include "stringformatter.h" +#include "math_constants.h" namespace openage { namespace util { @@ -98,6 +99,14 @@ void fixed_point() { std::stringstream sstr("1234.5678"); sstr >> e; TESTEQUALS_FLOAT(e.to_double(), 1234.5678, 1e-7); + + TESTEQUALS_FLOAT(TestType::e().to_double(), math::E, 1e-7); + TESTEQUALS_FLOAT(TestType::pi().to_double(), math::PI, 1e-7); + + using TestTypeShort = FixedPoint; + TESTEQUALS_FLOAT(TestTypeShort::e().to_double(), math::E, 1e-3); + TESTEQUALS_FLOAT(TestTypeShort::pi().to_double(), math::PI, 1e-3); + } }}} // openage::util::tests From 83dc9d54d2710121822dc93f1fe10ffe638c5394 Mon Sep 17 00:00:00 2001 From: AyiStar Date: Mon, 4 Dec 2023 09:14:29 +0000 Subject: [PATCH 2/2] Complete full set of math constants --- libopenage/util/fixed_point.h | 59 +++++++++++++++++++++++++++- libopenage/util/fixed_point_test.cpp | 15 +++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/libopenage/util/fixed_point.h b/libopenage/util/fixed_point.h index 3623091218..39717540bf 100644 --- a/libopenage/util/fixed_point.h +++ b/libopenage/util/fixed_point.h @@ -170,16 +170,73 @@ class FixedPoint { } /** - * Constants + * Math constants represented in FixedPoint */ + // naming, definition and value are kept compatible with `math_constants.h` static constexpr FixedPoint e() { return from_fixedpoint(FixedPoint::from_raw_value(6267931151224907085ll)); } + static constexpr FixedPoint log2e() { + return from_fixedpoint(FixedPoint::from_raw_value(3326628274461080622ll)); + } + + static constexpr FixedPoint log10e() { + return from_fixedpoint(FixedPoint::from_raw_value(1001414895036696345ll)); + } + + static constexpr FixedPoint ln2() { + return from_fixedpoint(FixedPoint::from_raw_value(1598288580650331957ll)); + } + + static constexpr FixedPoint ln10() { + return from_fixedpoint(FixedPoint::from_raw_value(5309399739799983627ll)); + } + static constexpr FixedPoint pi() { return from_fixedpoint(FixedPoint::from_raw_value(7244019458077122842ll)); } + static constexpr FixedPoint pi_2() { + return from_fixedpoint(FixedPoint::from_raw_value(3622009729038561421ll)); + } + + static constexpr FixedPoint pi_4() { + return from_fixedpoint(FixedPoint::from_raw_value(1811004864519280710ll)); + } + + static constexpr FixedPoint inv_pi() { + return from_fixedpoint(FixedPoint::from_raw_value(733972625820500306ll)); + } + + static constexpr FixedPoint inv2_pi() { + return from_fixedpoint(FixedPoint::from_raw_value(1467945251641000613ll)); + } + + static constexpr FixedPoint inv2_sqrt_pi() { + return from_fixedpoint(FixedPoint::from_raw_value(2601865214189558307ll)); + } + + static constexpr FixedPoint tau() { + return from_fixedpoint(FixedPoint::from_raw_value(7244019458077122842ll)); + } + + static constexpr FixedPoint degs_per_rad() { + return from_fixedpoint(FixedPoint::from_raw_value(40244552544872904ll)); + } + + static constexpr FixedPoint rads_per_deg() { + return from_fixedpoint(FixedPoint::from_raw_value(8257192040480628449ll)); + } + + static constexpr FixedPoint sqrt_2() { + return from_fixedpoint(FixedPoint::from_raw_value(3260954456333195553ll)); + } + + static constexpr FixedPoint inv_sqrt_2() { + return from_fixedpoint(FixedPoint::from_raw_value(1630477228166597776ll)); + } + /** * Factory function to get a fixed-point number from an integer. */ diff --git a/libopenage/util/fixed_point_test.cpp b/libopenage/util/fixed_point_test.cpp index 72a0a0f088..7a7fb2bc0c 100644 --- a/libopenage/util/fixed_point_test.cpp +++ b/libopenage/util/fixed_point_test.cpp @@ -101,7 +101,22 @@ void fixed_point() { TESTEQUALS_FLOAT(e.to_double(), 1234.5678, 1e-7); TESTEQUALS_FLOAT(TestType::e().to_double(), math::E, 1e-7); + TESTEQUALS_FLOAT(TestType::log2e().to_double(), math::LOG2E, 1e-7); + TESTEQUALS_FLOAT(TestType::log10e().to_double(), math::LOG10E, 1e-7); + TESTEQUALS_FLOAT(TestType::ln2().to_double(), math::LN2, 1e-7); + TESTEQUALS_FLOAT(TestType::ln10().to_double(), math::LN10, 1e-7); TESTEQUALS_FLOAT(TestType::pi().to_double(), math::PI, 1e-7); + TESTEQUALS_FLOAT(TestType::pi_2().to_double(), math::PI_2, 1e-7); + TESTEQUALS_FLOAT(TestType::pi_4().to_double(), math::PI_4, 1e-7); + TESTEQUALS_FLOAT(TestType::inv_pi().to_double(), math::INV_PI, 1e-7); + TESTEQUALS_FLOAT(TestType::inv2_pi().to_double(), math::INV2_PI, 1e-7); + TESTEQUALS_FLOAT(TestType::inv2_sqrt_pi().to_double(), math::INV2_SQRT_PI, 1e-7); + TESTEQUALS_FLOAT(TestType::tau().to_double(), math::TAU, 1e-7); + TESTEQUALS_FLOAT(TestType::degs_per_rad().to_double(), math::DEGSPERRAD, 1e-7); + TESTEQUALS_FLOAT(TestType::rads_per_deg().to_double(), math::RADSPERDEG, 1e-7); + TESTEQUALS_FLOAT(TestType::sqrt_2().to_double(), math::SQRT_2, 1e-7); + TESTEQUALS_FLOAT(TestType::inv_sqrt_2().to_double(), math::INV_SQRT_2, 1e-7); + using TestTypeShort = FixedPoint; TESTEQUALS_FLOAT(TestTypeShort::e().to_double(), math::E, 1e-3);