From 6124ad3c27063f1540984c167601d9896d4c8419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 11 Apr 2013 17:18:04 +0200 Subject: [PATCH] Math: added sqrtInverted() function. There are SIMD instructions for that, so why not have it in place. --- src/Math/Functions.h | 25 ++++++++++++++++++++++++- src/Math/Test/FunctionsTest.cpp | 7 +++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Math/Functions.h b/src/Math/Functions.h index fb4bafd1b..d3a9be345 100644 --- a/src/Math/Functions.h +++ b/src/Math/Functions.h @@ -197,7 +197,11 @@ template Vector abs(const Vector& a } #endif -/** @brief Square root */ +/** +@brief Square root + +@see sqrtInverted(), Vector::length() +*/ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T sqrt(const T& a); #else @@ -212,6 +216,25 @@ template Vector sqrt(const Vector& } #endif +/** +@brief Inverse square root + +@see sqrt() +*/ +#ifdef DOXYGEN_GENERATING_OUTPUT +template inline T sqrtInverted(const T& a); +#else +template inline typename std::enable_if::value, T>::type sqrtInverted(T a) { + return T(1)/std::sqrt(a); +} +template Vector sqrtInverted(const Vector& a) { + Vector out; + for(std::size_t i = 0; i != size; ++i) + out[i] = T(1)/std::sqrt(a[i]); + return out; +} +#endif + /** @brief Clamp value diff --git a/src/Math/Test/FunctionsTest.cpp b/src/Math/Test/FunctionsTest.cpp index 93c09d86b..cda253c74 100644 --- a/src/Math/Test/FunctionsTest.cpp +++ b/src/Math/Test/FunctionsTest.cpp @@ -38,6 +38,7 @@ class FunctionsTest: public Corrade::TestSuite::Tester { void sign(); void abs(); void sqrt(); + void sqrtInverted(); void clamp(); void lerp(); void normalizeUnsigned(); @@ -70,6 +71,7 @@ FunctionsTest::FunctionsTest() { &FunctionsTest::sign, &FunctionsTest::abs, &FunctionsTest::sqrt, + &FunctionsTest::sqrtInverted, &FunctionsTest::clamp, &FunctionsTest::lerp, &FunctionsTest::normalizeUnsigned, @@ -116,6 +118,11 @@ void FunctionsTest::sqrt() { CORRADE_COMPARE(Math::sqrt(Vector3i(256, 1, 0)), Vector3i(16, 1, 0)); } +void FunctionsTest::sqrtInverted() { + CORRADE_COMPARE(Math::sqrtInverted(16.0f), 0.25f); + CORRADE_COMPARE(Math::sqrtInverted(Vector3(1.0f, 4.0f, 16.0f)), Vector3(1.0f, 0.5f, 0.25f)); +} + void FunctionsTest::clamp() { CORRADE_COMPARE(Math::clamp(0.5f, -1.0f, 5.0f), 0.5f); CORRADE_COMPARE(Math::clamp(-1.6f, -1.0f, 5.0f), -1.0f);