From a43149ec90bab79277ed67af00d9d53e1460c2b9 Mon Sep 17 00:00:00 2001 From: Amy Retzerau Date: Mon, 22 Dec 2025 13:22:56 +0100 Subject: [PATCH] feat: improved fastmath perf --- fastmath.hpp | 47 +++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/fastmath.hpp b/fastmath.hpp index 9b33226..e904eaa 100644 --- a/fastmath.hpp +++ b/fastmath.hpp @@ -26,62 +26,57 @@ struct decimal { constexpr decimal(double i) : i(TO_INT(i)) {} constexpr decimal(int32_t i) : i(i) {} - friend std::ostream &operator<<(std::ostream &os, const decimal &d) { + inline friend std::ostream &operator<<(std::ostream &os, const decimal &d) { return (os << TO_FLOAT(d.i)); } - friend decimal operator+(const decimal &d1, const decimal &d2) { + inline friend decimal operator+(const decimal &d1, const decimal &d2) { return {d1.i + d2.i}; } - decimal &operator+=(const decimal &d) { return (*this) = {i + d.i}; } + inline decimal &operator+=(const decimal &d) { return (*this) = {i + d.i}; } - friend decimal operator-(const decimal &d1, const decimal &d2) { + inline friend decimal operator-(const decimal &d1, const decimal &d2) { return {d1.i - d2.i}; } - friend decimal operator-(const decimal &d) { return {-d.i}; } + inline friend decimal operator-(const decimal &d) { return {-d.i}; } - friend decimal operator*(const decimal &d1, const decimal &d2) { + inline friend decimal operator*(const decimal &d1, const decimal &d2) { return {MUL_F(d1.i, d2.i)}; } - decimal &operator*=(const decimal &d) { return (*this) = {MUL_F(i, d.i)}; } + inline decimal &operator*=(const decimal &d) { + return (*this) = {MUL_F(i, d.i)}; + } - friend decimal operator/(const decimal &d1, const decimal &d2) { + inline friend decimal operator/(const decimal &d1, const decimal &d2) { return {DIV_F(d1.i, d2.i)}; } - friend bool operator<(const decimal &d1, const decimal &d2) { + inline friend bool operator<(const decimal &d1, const decimal &d2) { return d1.i < d2.i; } - friend bool operator>(const decimal &d1, const decimal &d2) { + inline friend bool operator>(const decimal &d1, const decimal &d2) { return d1.i > d2.i; } - friend bool operator<=(const decimal &d1, const decimal &d2) { + inline friend bool operator<=(const decimal &d1, const decimal &d2) { return d1.i <= d2.i; } - friend bool operator>=(const decimal &d1, const decimal &d2) { + inline friend bool operator>=(const decimal &d1, const decimal &d2) { return d1.i >= d2.i; } - friend bool operator==(const decimal &d1, const decimal &d2) { + inline friend bool operator==(const decimal &d1, const decimal &d2) { return d1.i == d2.i; } - friend bool operator!=(const decimal &d1, const decimal &d2) { + inline friend bool operator!=(const decimal &d1, const decimal &d2) { return d1.i != d2.i; } - decimal &operator=(decimal const &in) { - if (this != &in) { - std::destroy_at(this); - std::construct_at(this, in); - } - return *this; - } - decimal sqrt() { return {((int32_t)sqrtf(i)) << HALF_SHIFT}; } + inline decimal sqrt() { return {((int32_t)sqrtf(i)) << HALF_SHIFT}; } - float to_float() { return TO_FLOAT(i); } - bool isSmall() { return (abs(i) < (1 << (HALF_SHIFT - 1))); } + inline float to_float() { return TO_FLOAT(i); } + inline bool isSmall() { return (abs(i) < (1 << (HALF_SHIFT - 1))); } }; template struct vec { @@ -165,10 +160,10 @@ template struct vec { return v * d; } - decimal operator*(const vec &vec) { + friend decimal operator*(const vec &v1, const vec &v2) { decimal res = {0}; for (int i = 0; i < n; i++) { - res += vec.v[i] * v[i]; + res += v1.v[i] * v2.v[i]; } return res; }