From e536c892c4db4f3e621d4a02585a2aa8ad722455 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 7 Dec 2011 21:07:45 +0100 Subject: [PATCH] Added Math::pow() and Math::log() functions. Math::pow() is templated on exponent, so it should be as fast as possible (without recursion). --- src/CMakeLists.txt | 2 +- src/Math/CMakeLists.txt | 4 ++++ src/Math/Math.cpp | 27 +++++++++++++++++++++++++++ src/Math/Math.h | 22 ++++++++++++++++++++++ src/Math/Test/CMakeLists.txt | 1 + src/Math/Test/MathTest.cpp | 10 ++++++++++ src/Math/Test/MathTest.h | 2 ++ 7 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/Math/Math.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4cb717232..2fcf351e2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -20,7 +20,7 @@ set(Magnum_SRCS ) add_library(Magnum SHARED ${Magnum_SRCS}) -target_link_libraries(Magnum CorradePluginManager CorradeUtility ${OPENGL_gl_LIBRARY} ${GLEW_LIBRARY}) +target_link_libraries(Magnum CorradePluginManager CorradeUtility MagnumMath ${OPENGL_gl_LIBRARY} ${GLEW_LIBRARY}) install(TARGETS Magnum DESTINATION ${MAGNUM_LIBRARY_INSTALL_DIR}) diff --git a/src/Math/CMakeLists.txt b/src/Math/CMakeLists.txt index 6879e1b55..1ce5fe56a 100644 --- a/src/Math/CMakeLists.txt +++ b/src/Math/CMakeLists.txt @@ -1,5 +1,9 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}) +add_library(MagnumMath STATIC + Math.cpp +) + if(BUILD_TESTS) enable_testing() add_subdirectory(Test) diff --git a/src/Math/Math.cpp b/src/Math/Math.cpp new file mode 100644 index 000000000..0c74ebbbc --- /dev/null +++ b/src/Math/Math.cpp @@ -0,0 +1,27 @@ +/* + Copyright © 2010, 2011 Vladimír Vondruš + + This file is part of Magnum. + + Magnum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 3 + only, as published by the Free Software Foundation. + + Magnum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License version 3 for more details. +*/ + +#include "Math.h" + +namespace Magnum { namespace Math { + +size_t log(size_t base, size_t number) { + size_t log = 0; + while(number /= base) + ++log; + return log; +} + +}} diff --git a/src/Math/Math.h b/src/Math/Math.h index 107c6b2aa..0c82b0279 100644 --- a/src/Math/Math.h +++ b/src/Math/Math.h @@ -15,6 +15,8 @@ GNU Lesser General Public License version 3 for more details. */ +#include + /** @file * @brief Math constants and utilities */ @@ -27,6 +29,26 @@ namespace Magnum { namespace Math { /** @brief Maximal tolerance when comparing floats */ #define EPSILON 1.0e-6 +/** + * @brief Integral power + * + * Returns integral power of base to the exponent. + */ +template inline constexpr size_t pow(size_t base) { + return base*pow(base); +} + +#ifndef DOXYGEN_GENERATING_OUTPUT +template<> inline constexpr size_t pow<0>(size_t base) { return 1; } +#endif + +/** + * @brief Integral logarithm + * + * Returns integral logarithm of given number with given base. + */ +size_t log(size_t base, size_t number); + /** * @brief Angle in degrees * diff --git a/src/Math/Test/CMakeLists.txt b/src/Math/Test/CMakeLists.txt index e1812efa1..4badfbaf6 100644 --- a/src/Math/Test/CMakeLists.txt +++ b/src/Math/Test/CMakeLists.txt @@ -15,4 +15,5 @@ corrade_add_test(Matrix4Test Matrix4Test.h Matrix4Test.cpp) target_link_libraries(Matrix4Test ${CORRADE_UTILITY_LIBRARY}) corrade_add_test(MathTest MathTest.h MathTest.cpp) +target_link_libraries(MathTest MagnumMath) corrade_add_test(GeometryUtilsTest GeometryUtilsTest.h GeometryUtilsTest.cpp) diff --git a/src/Math/Test/MathTest.cpp b/src/Math/Test/MathTest.cpp index dfce8d3df..460afb5a8 100644 --- a/src/Math/Test/MathTest.cpp +++ b/src/Math/Test/MathTest.cpp @@ -32,4 +32,14 @@ void MathTest::degrad() { QVERIFY(deg(90) == PI/2); } +void MathTest::pow() { + QCOMPARE(Math::pow<10>(2), 1024ul); + QCOMPARE(Math::pow<0>(3), 1ul); +} + +void MathTest::log() { + QCOMPARE(Math::log(2, 256), 8ul); + QCOMPARE(Math::log(256, 2), 0ul); +} + }}} diff --git a/src/Math/Test/MathTest.h b/src/Math/Test/MathTest.h index 41388f489..88b32e977 100644 --- a/src/Math/Test/MathTest.h +++ b/src/Math/Test/MathTest.h @@ -24,6 +24,8 @@ class MathTest: public QObject { private slots: void degrad(); + void pow(); + void log(); }; }}}