feat: improved fastmath perf
This commit is contained in:
47
fastmath.hpp
47
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 <int n, class Dev> struct vec {
|
||||
@@ -165,10 +160,10 @@ template <int n, class Dev> struct vec {
|
||||
return v * d;
|
||||
}
|
||||
|
||||
decimal operator*(const vec<n, Dev> &vec) {
|
||||
friend decimal operator*(const vec<n, Dev> &v1, const vec<n, Dev> &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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user