Browse Source

utils moved to math

Kajetan Johannes Hammerle 2 years ago
parent
commit
44c2c77e81
10 changed files with 96 additions and 95 deletions
  1. 2 2
      Main.cpp
  2. 29 0
      math/Math.h
  3. 1 1
      meson.build
  4. 52 0
      tests/MathTests.cpp
  5. 8 0
      tests/MathTests.h
  6. 0 47
      tests/UtilsTests.cpp
  7. 0 8
      tests/UtilsTests.h
  8. 2 2
      utils/BitArray.cpp
  9. 2 2
      utils/HashMap.h
  10. 0 33
      utils/Utils.h

+ 2 - 2
Main.cpp

@@ -12,6 +12,7 @@
 #include "tests/HashMapTests.h"
 #include "tests/ImageReaderTests.h"
 #include "tests/ListTests.h"
+#include "tests/MathTests.h"
 #include "tests/MatrixStackTests.h"
 #include "tests/MatrixTests.h"
 #include "tests/NetworkTests.h"
@@ -24,7 +25,6 @@
 #include "tests/StringBufferTests.h"
 #include "tests/TypedBufferTests.h"
 #include "tests/UniquePointerTests.h"
-#include "tests/UtilsTests.h"
 #include "tests/VectorTests.h"
 #include "utils/Logger.h"
 #include "wrapper/GL.h"
@@ -63,7 +63,7 @@ int main(int argAmount, char** args) {
     PlaneTests::test();
     FrustumTests::test();
     QuaternionTests::test();
-    UtilsTests::test();
+    MathTests::test();
     ColorTests::test();
     ClockTests::test();
     ImageReaderTests::test(args[1]);

+ 29 - 0
math/Math.h

@@ -11,4 +11,33 @@
 void sincosf(float a, float* s, float* c);
 #endif
 
+namespace Math {
+    template<typename T>
+    T interpolate(const T& a, const T& b, float f) {
+        return a * (1.0f - f) + b * f;
+    }
+
+    template<typename T>
+    int popCount(const T& t) {
+        static constexpr int map[16] = {0, 1, 1, 2, 1, 2, 2, 3,
+                                        1, 2, 2, 3, 2, 3, 3, 4};
+        int sum = 0;
+        for(int i = 0; i < static_cast<int>(sizeof(T) * 8); i += 4) {
+            sum += map[(t >> i) & 0xF];
+        }
+        return sum;
+    }
+
+    constexpr int roundUpLog2(int i) {
+        if(i <= 0) {
+            return 0;
+        }
+        int c = 1;
+        while(((i - 1) >> c) > 0) {
+            c++;
+        }
+        return c;
+    }
+}
+
 #endif

+ 1 - 1
meson.build

@@ -51,7 +51,7 @@ src_tests = [
     'tests/Test.cpp',
     'tests/TypedBufferTests.cpp',
     'tests/UniquePointerTests.cpp',
-    'tests/UtilsTests.cpp',
+    'tests/MathTests.cpp',
     'tests/VectorTests.cpp',
     'tests/ComponentsTests.cpp',
 ]

+ 52 - 0
tests/MathTests.cpp

@@ -0,0 +1,52 @@
+#include "tests/MathTests.h"
+#include "math/Math.h"
+#include "tests/Test.h"
+
+const float eps = 0.0001f;
+
+static void testInterpolate(Test& test) {
+    test.checkFloat(7.5f, Math::interpolate(5.0f, 10.0f, 0.5f), eps,
+                    "interpolate 1");
+    test.checkFloat(-2.0, Math::interpolate(-10.0f, 10.0f, 0.4f), eps,
+                    "interpolate 2");
+    test.checkFloat(10.0f, Math::interpolate(-3.0f, 10.0f, 1.0f), eps,
+                    "interpolate 3");
+    test.checkFloat(7.0f, Math::interpolate(7.0f, 10.0f, 0.0f), eps,
+                    "interpolate 4");
+    test.checkFloat(6.0f, Math::interpolate(0.0f, 10.0f, 0.6f), eps,
+                    "interpolate 5");
+}
+
+static void testPopCount(Test& test) {
+    test.checkEqual(4, Math::popCount(0xF), "pop count 1");
+    test.checkEqual(0, Math::popCount(0x0), "pop count 2");
+    test.checkEqual(2, Math::popCount(0x6), "pop count 3");
+    test.checkEqual(7, Math::popCount(0x7F), "pop count 4");
+    test.checkEqual(3, Math::popCount(0x2A), "pop count 5");
+    test.checkEqual(32, Math::popCount(0xFFFFFFFF), "pop count 6");
+    test.checkEqual(64, Math::popCount(0xFFFFFFFFFFFFFFFFL), "pop count 7");
+    test.checkEqual(44, Math::popCount(0xFFFF0FFFFFFF), "pop count 8");
+    test.checkEqual(32, Math::popCount(-1), "pop count 9");
+}
+
+static void testRoundUpLog2(Test& test) {
+    test.checkEqual(0, Math::roundUpLog2(-5), "round up log2 1");
+    test.checkEqual(0, Math::roundUpLog2(0), "round up log2 2");
+    test.checkEqual(1, Math::roundUpLog2(1), "round up log2 3");
+    test.checkEqual(1, Math::roundUpLog2(2), "round up log2 4");
+    test.checkEqual(2, Math::roundUpLog2(3), "round up log2 5");
+    test.checkEqual(2, Math::roundUpLog2(4), "round up log2 6");
+    test.checkEqual(3, Math::roundUpLog2(5), "round up log2 7");
+    test.checkEqual(4, Math::roundUpLog2(10), "round up log2 8");
+    test.checkEqual(5, Math::roundUpLog2(20), "round up log2 9");
+    test.checkEqual(16, Math::roundUpLog2(35345), "round up log2 10");
+    test.checkEqual(31, Math::roundUpLog2(0x7FFFFFFF), "round up log2 11");
+}
+
+void MathTests::test() {
+    Test test("Utils");
+    testInterpolate(test);
+    testPopCount(test);
+    testRoundUpLog2(test);
+    test.finalize();
+}

+ 8 - 0
tests/MathTests.h

@@ -0,0 +1,8 @@
+#ifndef MATHTESTS_H
+#define MATHTESTS_H
+
+namespace MathTests {
+    void test();
+}
+
+#endif

+ 0 - 47
tests/UtilsTests.cpp

@@ -1,47 +0,0 @@
-#include "tests/UtilsTests.h"
-#include "tests/Test.h"
-#include "utils/Utils.h"
-
-const float eps = 0.0001f;
-
-static void testInterpolate(Test& test) {
-    test.checkFloat(7.5f, Utils::interpolate(5.0f, 10.0f, 0.5f), eps, "interpolate 1");
-    test.checkFloat(-2.0, Utils::interpolate(-10.0f, 10.0f, 0.4f), eps, "interpolate 2");
-    test.checkFloat(10.0f, Utils::interpolate(-3.0f, 10.0f, 1.0f), eps, "interpolate 3");
-    test.checkFloat(7.0f, Utils::interpolate(7.0f, 10.0f, 0.0f), eps, "interpolate 4");
-    test.checkFloat(6.0f, Utils::interpolate(0.0f, 10.0f, 0.6f), eps, "interpolate 5");
-}
-
-static void testPopCount(Test& test) {
-    test.checkEqual(4, Utils::popCount(0xF), "pop count 1");
-    test.checkEqual(0, Utils::popCount(0x0), "pop count 2");
-    test.checkEqual(2, Utils::popCount(0x6), "pop count 3");
-    test.checkEqual(7, Utils::popCount(0x7F), "pop count 4");
-    test.checkEqual(3, Utils::popCount(0x2A), "pop count 5");
-    test.checkEqual(32, Utils::popCount(0xFFFFFFFF), "pop count 6");
-    test.checkEqual(64, Utils::popCount(0xFFFFFFFFFFFFFFFFL), "pop count 7");
-    test.checkEqual(44, Utils::popCount(0xFFFF0FFFFFFF), "pop count 8");
-    test.checkEqual(32, Utils::popCount(-1), "pop count 9");
-}
-
-static void testRoundUpLog2(Test& test) {
-    test.checkEqual(0, Utils::roundUpLog2(-5), "round up log2 1");
-    test.checkEqual(0, Utils::roundUpLog2(0), "round up log2 2");
-    test.checkEqual(1, Utils::roundUpLog2(1), "round up log2 3");
-    test.checkEqual(1, Utils::roundUpLog2(2), "round up log2 4");
-    test.checkEqual(2, Utils::roundUpLog2(3), "round up log2 5");
-    test.checkEqual(2, Utils::roundUpLog2(4), "round up log2 6");
-    test.checkEqual(3, Utils::roundUpLog2(5), "round up log2 7");
-    test.checkEqual(4, Utils::roundUpLog2(10), "round up log2 8");
-    test.checkEqual(5, Utils::roundUpLog2(20), "round up log2 9");
-    test.checkEqual(16, Utils::roundUpLog2(35345), "round up log2 10");
-    test.checkEqual(31, Utils::roundUpLog2(0x7FFFFFFF), "round up log2 11");
-}
-
-void UtilsTests::test() {
-    Test test("Utils");
-    testInterpolate(test);
-    testPopCount(test);
-    testRoundUpLog2(test);
-    test.finalize();
-}

+ 0 - 8
tests/UtilsTests.h

@@ -1,8 +0,0 @@
-#ifndef UTILSTESTS_H
-#define UTILSTESTS_H
-
-namespace UtilsTests {
-    void test();
-}
-
-#endif

+ 2 - 2
utils/BitArray.cpp

@@ -1,5 +1,5 @@
 #include "utils/BitArray.h"
-#include "utils/Utils.h"
+#include "math/Math.h"
 
 static int roundUpDivide(int a, int b) {
     if(a % b == 0) {
@@ -9,7 +9,7 @@ static int roundUpDivide(int a, int b) {
 }
 
 static constexpr int INT_BITS = sizeof(int) * 8;
-static constexpr int DIVIDE_BITS = Utils::roundUpLog2(INT_BITS);
+static constexpr int DIVIDE_BITS = Math::roundUpLog2(INT_BITS);
 
 static int readBits(const int* data, int index, int bits) {
     int dataIndexA = (index * bits) >> DIVIDE_BITS;

+ 2 - 2
utils/HashMap.h

@@ -1,11 +1,11 @@
 #ifndef HASHMAP_H
 #define HASHMAP_H
 
+#include "math/Math.h"
 #include "utils/Array.h"
 #include "utils/List.h"
 #include "utils/StringBuffer.h"
 #include "utils/Types.h"
-#include "utils/Utils.h"
 
 template<typename K, typename V>
 struct HashMap final {
@@ -218,7 +218,7 @@ private:
 
 public:
     HashMap(int minCapacity = 8) : elements(0) {
-        nodes.resize(1 << Utils::roundUpLog2(minCapacity));
+        nodes.resize(1 << Math::roundUpLog2(minCapacity));
     }
 
     template<typename... Args>

+ 0 - 33
utils/Utils.h

@@ -1,33 +0,0 @@
-#ifndef UTILS_H
-#define UTILS_H
-
-namespace Utils {
-    template<typename T>
-    T interpolate(const T& a, const T& b, float f) {
-        return a * (1.0f - f) + b * f;
-    }
-
-    template<typename T>
-    int popCount(const T& t) {
-        static constexpr int map[16] = {0, 1, 1, 2, 1, 2, 2, 3,
-                                        1, 2, 2, 3, 2, 3, 3, 4};
-        int sum = 0;
-        for(int i = 0; i < static_cast<int>(sizeof(T) * 8); i += 4) {
-            sum += map[(t >> i) & 0xF];
-        }
-        return sum;
-    }
-
-    constexpr int roundUpLog2(int i) {
-        if(i <= 0) {
-            return 0;
-        }
-        int c = 1;
-        while(((i - 1) >> c) > 0) {
-            c++;
-        }
-        return c;
-    }
-}
-
-#endif