Browse Source

Math: added sqrtInverted() function.

There are SIMD instructions for that, so why not have it in place.
pull/278/head
Vladimír Vondruš 13 years ago
parent
commit
6124ad3c27
  1. 25
      src/Math/Functions.h
  2. 7
      src/Math/Test/FunctionsTest.cpp

25
src/Math/Functions.h

@ -197,7 +197,11 @@ template<std::size_t size, class T> Vector<size, T> abs(const Vector<size, T>& a
}
#endif
/** @brief Square root */
/**
@brief Square root
@see sqrtInverted(), Vector::length()
*/
#ifdef DOXYGEN_GENERATING_OUTPUT
template<class T> inline T sqrt(const T& a);
#else
@ -212,6 +216,25 @@ template<std::size_t size, class T> Vector<size, T> sqrt(const Vector<size, T>&
}
#endif
/**
@brief Inverse square root
@see sqrt()
*/
#ifdef DOXYGEN_GENERATING_OUTPUT
template<class T> inline T sqrtInverted(const T& a);
#else
template<class T> inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type sqrtInverted(T a) {
return T(1)/std::sqrt(a);
}
template<std::size_t size, class T> Vector<size, T> sqrtInverted(const Vector<size, T>& a) {
Vector<size, T> out;
for(std::size_t i = 0; i != size; ++i)
out[i] = T(1)/std::sqrt(a[i]);
return out;
}
#endif
/**
@brief Clamp value

7
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);

Loading…
Cancel
Save