From 5520001117ecca277a12d43461cfa1180af5178f Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 19 Apr 2020 03:16:34 -0400 Subject: [PATCH 1/4] General: Mark file-scope constexpr variables as inline Allows the compiler to unify all usages of these constants down to a single address if they're ODR used, rather than creating an individual copy within each translation unit. Unfortunately all file-scope variables don't have inline implied for them by default (only for static class member variables). Shrinks the size of the emitted binary a little bit. --- include/zeus/CAABox.hpp | 4 ++-- include/zeus/CColor.hpp | 24 ++++++++++++------------ include/zeus/CVector2f.hpp | 6 +++--- include/zeus/CVector3d.hpp | 2 +- include/zeus/CVector3f.hpp | 22 +++++++++++----------- 5 files changed, 29 insertions(+), 29 deletions(-) diff --git a/include/zeus/CAABox.hpp b/include/zeus/CAABox.hpp index dac571c..58c893f 100644 --- a/include/zeus/CAABox.hpp +++ b/include/zeus/CAABox.hpp @@ -309,8 +309,8 @@ public: return max[idx - 3]; } }; -constexpr CAABox skInvertedBox; -constexpr CAABox skNullBox(CVector3f{}, CVector3f{}); +constexpr inline CAABox skInvertedBox; +constexpr inline CAABox skNullBox(CVector3f{}, CVector3f{}); [[nodiscard]] inline bool operator==(const CAABox& left, const CAABox& right) { return (left.min == right.min && left.max == right.max); diff --git a/include/zeus/CColor.hpp b/include/zeus/CColor.hpp index ab64d47..c1a62d1 100644 --- a/include/zeus/CColor.hpp +++ b/include/zeus/CColor.hpp @@ -310,18 +310,18 @@ constexpr CVector4f& CVector4f::operator=(const CColor& other) { return *this; } -constexpr CColor skRed(1.f, 0.f, 0.f, 1.f); -constexpr CColor skBlack(0.f, 0.f, 0.f, 1.f); -constexpr CColor skBlue(0.f, 0.f, 1.f, 1.f); -constexpr CColor skCyan(0.f, 1.f, 1.f, 1.f); -constexpr CColor skGreen(0.f, 1.f, 0.f, 1.f); -constexpr CColor skGrey(0.5f, 0.5f, 0.5f, 1.f); -constexpr CColor skMagenta(1.f, 0.f, 1.f, 1.f); -constexpr CColor skOrange(1.f, 0.43f, 0.f, 1.f); -constexpr CColor skPurple(0.63f, 0.f, 1.f, 1.f); -constexpr CColor skYellow(1.f, 1.f, 0.f, 1.f); -constexpr CColor skWhite(1.f, 1.f, 1.f, 1.f); -constexpr CColor skClear(0.f, 0.f, 0.f, 0.f); +constexpr inline CColor skRed(1.f, 0.f, 0.f, 1.f); +constexpr inline CColor skBlack(0.f, 0.f, 0.f, 1.f); +constexpr inline CColor skBlue(0.f, 0.f, 1.f, 1.f); +constexpr inline CColor skCyan(0.f, 1.f, 1.f, 1.f); +constexpr inline CColor skGreen(0.f, 1.f, 0.f, 1.f); +constexpr inline CColor skGrey(0.5f, 0.5f, 0.5f, 1.f); +constexpr inline CColor skMagenta(1.f, 0.f, 1.f, 1.f); +constexpr inline CColor skOrange(1.f, 0.43f, 0.f, 1.f); +constexpr inline CColor skPurple(0.63f, 0.f, 1.f, 1.f); +constexpr inline CColor skYellow(1.f, 1.f, 0.f, 1.f); +constexpr inline CColor skWhite(1.f, 1.f, 1.f, 1.f); +constexpr inline CColor skClear(0.f, 0.f, 0.f, 0.f); [[nodiscard]] inline CColor operator+(float lhs, const CColor& rhs) { return CColor(simd(lhs) + rhs.mSimd).Clamp(); diff --git a/include/zeus/CVector2f.hpp b/include/zeus/CVector2f.hpp index 266cae1..b05c9d2 100644 --- a/include/zeus/CVector2f.hpp +++ b/include/zeus/CVector2f.hpp @@ -207,9 +207,9 @@ public: [[nodiscard]] simd::reference x() { return mSimd[0]; } [[nodiscard]] simd::reference y() { return mSimd[1]; } }; -constexpr CVector2f skOne2f(1.f); -constexpr CVector2f skNegOne2f(-1.f); -constexpr CVector2f skZero2f(0.f); +constexpr inline CVector2f skOne2f(1.f); +constexpr inline CVector2f skNegOne2f(-1.f); +constexpr inline CVector2f skZero2f(0.f); [[nodiscard]] inline CVector2f operator+(float lhs, const CVector2f& rhs) { return zeus::simd(lhs) + rhs.mSimd; } diff --git a/include/zeus/CVector3d.hpp b/include/zeus/CVector3d.hpp index 9b0918a..943caf0 100644 --- a/include/zeus/CVector3d.hpp +++ b/include/zeus/CVector3d.hpp @@ -79,7 +79,7 @@ public: }; inline CVector3f::CVector3f(const CVector3d& vec) : mSimd(vec.mSimd) {} -constexpr CVector3d skZero3d(0.0); +constexpr inline CVector3d skZero3d(0.0); [[nodiscard]] inline CVector3d operator+(double lhs, const CVector3d& rhs) { return zeus::simd(lhs) + rhs.mSimd; diff --git a/include/zeus/CVector3f.hpp b/include/zeus/CVector3f.hpp index bed85d3..c755065 100644 --- a/include/zeus/CVector3f.hpp +++ b/include/zeus/CVector3f.hpp @@ -209,17 +209,17 @@ public: [[nodiscard]] static inline CVector3f degToRad(const CVector3f& deg); }; -constexpr CVector3f skOne3f(1.f); -constexpr CVector3f skNegOne3f(-1.f); -constexpr CVector3f skZero3f(0.f); -constexpr CVector3f skForward(0.f, 1.f, 0.f); -constexpr CVector3f skBack(0.f, -1.f, 0.f); -constexpr CVector3f skLeft(-1.f, 0.f, 0.f); -constexpr CVector3f skRight(1.f, 0.f, 0.f); -constexpr CVector3f skUp(0.f, 0.f, 1.f); -constexpr CVector3f skDown(0.f, 0.f, -1.f); -constexpr CVector3f skRadToDegVec(180.f / M_PIF); -constexpr CVector3f skDegToRadVec(M_PIF / 180.f); +constexpr inline CVector3f skOne3f(1.f); +constexpr inline CVector3f skNegOne3f(-1.f); +constexpr inline CVector3f skZero3f(0.f); +constexpr inline CVector3f skForward(0.f, 1.f, 0.f); +constexpr inline CVector3f skBack(0.f, -1.f, 0.f); +constexpr inline CVector3f skLeft(-1.f, 0.f, 0.f); +constexpr inline CVector3f skRight(1.f, 0.f, 0.f); +constexpr inline CVector3f skUp(0.f, 0.f, 1.f); +constexpr inline CVector3f skDown(0.f, 0.f, -1.f); +constexpr inline CVector3f skRadToDegVec(180.f / M_PIF); +constexpr inline CVector3f skDegToRadVec(M_PIF / 180.f); [[nodiscard]] inline CVector3f operator+(float lhs, const CVector3f& rhs) { return zeus::simd(lhs) + rhs.mSimd; } From f99184556aa6cfbe6c196b2e2e9f51b79052f802 Mon Sep 17 00:00:00 2001 From: Luke Street Date: Sun, 19 Apr 2020 21:01:49 -0400 Subject: [PATCH 2/4] CVector3f/CVector2f: Fix clamp in getAngleDiff --- src/CVector2f.cpp | 2 +- src/CVector3f.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CVector2f.cpp b/src/CVector2f.cpp index a63768b..977e29f 100644 --- a/src/CVector2f.cpp +++ b/src/CVector2f.cpp @@ -14,7 +14,7 @@ float CVector2f::getAngleDiff(const CVector2f& a, const CVector2f& b) { return 0.f; float dot = a.dot(b); - return std::acos(std::clamp(-1.f, dot / (mag1 * mag2), 1.f)); + return std::acos(zeus::clamp(-1.f, dot / (mag1 * mag2), 1.f)); } CVector2f CVector2f::slerp(const CVector2f& a, const CVector2f& b, float t) { diff --git a/src/CVector3f.cpp b/src/CVector3f.cpp index 53cdb4b..9814d11 100644 --- a/src/CVector3f.cpp +++ b/src/CVector3f.cpp @@ -16,7 +16,7 @@ float CVector3f::getAngleDiff(const CVector3f& a, const CVector3f& b) { return 0.f; float dot = a.dot(b); - return std::acos(std::clamp(-1.f, dot / (mag1 * mag2), 1.f)); + return std::acos(zeus::clamp(-1.f, dot / (mag1 * mag2), 1.f)); } CVector3f CVector3f::slerp(const CVector3f& a, const CVector3f& b, CRelAngle clampAngle) { From 58c8a902aceabe0f9417f64c8d5e9c2cdfe70b48 Mon Sep 17 00:00:00 2001 From: Luke Street Date: Wed, 16 Sep 2020 00:41:47 -0400 Subject: [PATCH 3/4] CVector3f/CVector2f: Fix isZero --- include/zeus/CVector2f.hpp | 2 +- include/zeus/CVector3f.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/zeus/CVector2f.hpp b/include/zeus/CVector2f.hpp index b05c9d2..7929a5e 100644 --- a/include/zeus/CVector2f.hpp +++ b/include/zeus/CVector2f.hpp @@ -184,7 +184,7 @@ public: return std::fabs(x()) >= FLT_EPSILON || std::fabs(y()) >= FLT_EPSILON; } - [[nodiscard]] bool isZero() const { return magSquared() <= FLT_EPSILON; } + [[nodiscard]] bool isZero() const { return mSimd[0] == 0.f && mSimd[1] == 0.f; } [[nodiscard]] bool isEqu(const CVector2f& other, float epsilon = FLT_EPSILON) const { const CVector2f diffVec = other - *this; diff --git a/include/zeus/CVector3f.hpp b/include/zeus/CVector3f.hpp index c755065..f8f7ecd 100644 --- a/include/zeus/CVector3f.hpp +++ b/include/zeus/CVector3f.hpp @@ -162,7 +162,7 @@ public: return !(std::fabs(x()) < FLT_EPSILON && std::fabs(y()) < FLT_EPSILON && std::fabs(z()) < FLT_EPSILON); } - [[nodiscard]] bool isZero() const { return magSquared() <= FLT_EPSILON; } + [[nodiscard]] bool isZero() const { return mSimd[0] == 0.f && mSimd[1] == 0.f && mSimd[2] == 0.f; } void scaleToLength(float newLength) { float length = magSquared(); From eec855018a4e72c4f69ffb705b9d1d96854ad182 Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Sat, 19 Sep 2020 15:43:35 -0700 Subject: [PATCH 4/4] Add countLeadingZeros --- include/zeus/Math.hpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/include/zeus/Math.hpp b/include/zeus/Math.hpp index 32b4d87..3fdefd8 100644 --- a/include/zeus/Math.hpp +++ b/include/zeus/Math.hpp @@ -159,6 +159,26 @@ template return PopCount(static_cast::type>(e)); } +template +[[nodiscard]] typename std::enable_if::value && std::is_integral::value, int>::type +countLeadingZeros(U x) { +#if __GNUC__ >= 4 + return __builtin_clz(x); +#else + x = x | (x >> 1); + x = x | (x >> 2); + x = x | (x >> 4); + x = x | (x >> 8); + x = x | (x >> 16); + return PopCount(~x); +#endif +} + +template +[[nodiscard]] typename std::enable_if::value, int>::type countLeadingZeros(E e) { + return countLeadingZeros(static_cast::type>(e)); +} + [[nodiscard]] bool close_enough(const CVector3f& a, const CVector3f& b, float epsilon = FLT_EPSILON); [[nodiscard]] bool close_enough(const CVector2f& a, const CVector2f& b, float epsilon = FLT_EPSILON);