From 83dc9d54d2710121822dc93f1fe10ffe638c5394 Mon Sep 17 00:00:00 2001 From: AyiStar Date: Mon, 4 Dec 2023 09:14:29 +0000 Subject: [PATCH] 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);