Browse Source

use sincosf again with fallback

Kajetan Johannes Hammerle 2 years ago
parent
commit
54a8f9ffe2
9 changed files with 83 additions and 58 deletions
  1. 0 1
      math/Frustum.cpp
  2. 17 0
      math/Math.h
  3. 0 8
      math/MathConstants.h
  4. 4 5
      math/Matrix.cpp
  5. 3 5
      math/Quaternion.cpp
  6. 6 7
      math/Vector.cpp
  7. 1 2
      math/Vector.h
  8. 51 27
      tests/MatrixTests.cpp
  9. 1 3
      tests/VectorTests.cpp

+ 0 - 1
math/Frustum.cpp

@@ -1,5 +1,4 @@
 #include "math/Frustum.h"
-#include "math/MathConstants.h"
 
 Frustum::Frustum(float fieldOfView, float nearClip, float farClip)
     : fieldOfView(fieldOfView), nearClip(nearClip), farClip(farClip) {

+ 17 - 0
math/Math.h

@@ -0,0 +1,17 @@
+#ifndef MATH_H
+#define MATH_H
+
+#include <cmath>
+
+#ifndef M_PI
+#define M_PI 3.14159265358979323846
+#endif
+
+#ifndef _GNU_SOURCE
+void sincosf(float a, float* s, float* c) {
+    *s = sinf(a);
+    *c = cosf(a);
+}
+#endif
+
+#endif

+ 0 - 8
math/MathConstants.h

@@ -1,8 +0,0 @@
-#ifndef MATH_CONSTANTS_H
-#define MATH_CONSTANTS_H
-
-#ifndef M_PI
-#define M_PI 3.14159265358979323846
-#endif
-
-#endif

+ 4 - 5
math/Matrix.cpp

@@ -1,7 +1,5 @@
-#include <cmath>
-
-#include "math/MathConstants.h"
 #include "math/Matrix.h"
+#include "math/Math.h"
 
 Matrix::Matrix() {
     data[0] = Vector4(1.0f, 0.0f, 0.0f, 0.0f);
@@ -93,8 +91,9 @@ Matrix& Matrix::translateTo(const Vector3& v) {
 
 Matrix& Matrix::rotate(float degrees, int a, int b) {
     degrees *= (M_PI / 180.0f);
-    float sin = sinf(degrees);
-    float cos = cosf(degrees);
+    float sin = 0.0f;
+    float cos = 0.0f;
+    sincosf(degrees, &sin, &cos);
     Vector4 v = data[a];
     data[a] = cos * data[a] - sin * data[b];
     data[b] = sin * v + cos * data[b];

+ 3 - 5
math/Quaternion.cpp

@@ -1,6 +1,3 @@
-#include <cmath>
-
-#include "math/MathConstants.h"
 #include "math/Quaternion.h"
 
 Quaternion::Quaternion() : w(1.0f) {
@@ -9,8 +6,9 @@ Quaternion::Quaternion() : w(1.0f) {
 Quaternion::Quaternion(const Vector3& axis, float angle) : xyz(axis) {
     angle *= (M_PI / 360.0f);
     xyz.normalize();
-    w = cosf(angle);
-    xyz *= sinf(angle);
+    float factor = 0.0f;
+    sincosf(angle, &factor, &w);
+    xyz *= factor;
 }
 
 Quaternion Quaternion::lerp(float f, const Quaternion& other) const {

+ 6 - 7
math/Vector.cpp

@@ -1,6 +1,3 @@
-#include <cmath>
-
-#include "math/MathConstants.h"
 #include "math/Vector.h"
 
 template<>
@@ -8,11 +5,13 @@ Vector<3>& Vector<3>::setAngles(float lengthAngle, float widthAngle) {
     lengthAngle *= (M_PI / 180.0f);
     widthAngle *= (M_PI / 180.0f);
 
-    float sinWidth = sinf(widthAngle);
-    float cosWidth = cosf(widthAngle);
+    float sinWidth = 0.0f;
+    float cosWidth = 0.0f;
+    sincosf(widthAngle, &sinWidth, &cosWidth);
 
-    float sinLength = sinf(lengthAngle);
-    float cosLength = cosf(lengthAngle);
+    float sinLength = 0.0f;
+    float cosLength = 0.0f;
+    sincosf(lengthAngle, &sinLength, &cosLength);
 
     return *this =
                Vector<3>(cosWidth * cosLength, sinWidth, -sinLength * cosWidth);

+ 1 - 2
math/Vector.h

@@ -1,8 +1,7 @@
 #ifndef VECTOR_H
 #define VECTOR_H
 
-#include <cmath>
-
+#include "math/Math.h"
 #include "utils/StringBuffer.h"
 
 template<int N>

+ 51 - 27
tests/MatrixTests.cpp

@@ -1,16 +1,17 @@
-#include <cmath>
-
 #include "tests/MatrixTests.h"
+#include "math/Matrix.h"
 #include "tests/Test.h"
 #include "utils/StringBuffer.h"
-#include "math/Matrix.h"
 
 const float eps = 0.0001f;
 
 template<int N>
-static void compareVectors(Test& test, const Vector<N>& wanted, const Vector<N>& actual, const char* text) {
+static void compareVectors(Test& test, const Vector<N>& wanted,
+                           const Vector<N>& actual, const char* text) {
     for(int i = 0; i < N; i++) {
-        test.checkFloat(wanted[i], actual[i], eps, StringBuffer<50>(text).append(" (").append(i).append(")"));
+        test.checkFloat(
+            wanted[i], actual[i], eps,
+            StringBuffer<50>(text).append(" (").append(i).append(")"));
     }
 }
 
@@ -20,7 +21,8 @@ static void testInit(Test& test) {
     for(int i = 0; i < 16; i++) {
         int x = i % 4;
         int y = i / 4;
-        test.checkEqual(static_cast<float> (x == y), data[i], StringBuffer<50>("init ").append(i));
+        test.checkEqual(static_cast<float>(x == y), data[i],
+                        StringBuffer<50>("init ").append(i));
     }
 }
 
@@ -44,44 +46,51 @@ static void testTranspose(Test& test) {
     }
     const float* mp2 = m2.getValues();
     for(int i = 0; i < 16; i++) {
-        test.checkEqual(mp[i], mp2[i], StringBuffer<50>("transpose ").append(i));
+        test.checkEqual(mp[i], mp2[i],
+                        StringBuffer<50>("transpose ").append(i));
     }
 }
 
 static void testScale(Test& test) {
     Matrix m;
     m.scale(Vector3(2.0f, 3.0f, 4.0f));
-    compareVectors(test, Vector3(-8.0f, 18.0f, 28.0f), m * Vector3(-4.0f, 6.0f, 7.0f), "scale");
+    compareVectors(test, Vector3(-8.0f, 18.0f, 28.0f),
+                   m * Vector3(-4.0f, 6.0f, 7.0f), "scale");
 }
 
 static void testUniformScale(Test& test) {
     Matrix m;
     m.scale(2.0f);
-    compareVectors(test, Vector3(-8.0f, 12.0f, 14.0f), m * Vector3(-4.0f, 6.0f, 7.0f), "uniform scale");
+    compareVectors(test, Vector3(-8.0f, 12.0f, 14.0f),
+                   m * Vector3(-4.0f, 6.0f, 7.0f), "uniform scale");
 }
 
 static void testTranslateX(Test& test) {
     Matrix m;
     m.translateX(5.0f);
-    compareVectors(test, Vector3(1.0f, 6.0f, 7.0f), m * Vector3(-4.0f, 6.0f, 7.0f), "translate x");
+    compareVectors(test, Vector3(1.0f, 6.0f, 7.0f),
+                   m * Vector3(-4.0f, 6.0f, 7.0f), "translate x");
 }
 
 static void testTranslateY(Test& test) {
     Matrix m;
     m.translateY(6.0f);
-    compareVectors(test, Vector3(-4.0f, 12.0f, 7.0f), m * Vector3(-4.0f, 6.0f, 7.0f), "translate y");
+    compareVectors(test, Vector3(-4.0f, 12.0f, 7.0f),
+                   m * Vector3(-4.0f, 6.0f, 7.0f), "translate y");
 }
 
 static void testTranslateZ(Test& test) {
     Matrix m;
     m.translateZ(7.0f);
-    compareVectors(test, Vector3(-4.0f, 6.0f, 14.0f), m * Vector3(-4.0f, 6.0f, 7.0f), "translate z");
+    compareVectors(test, Vector3(-4.0f, 6.0f, 14.0f),
+                   m * Vector3(-4.0f, 6.0f, 7.0f), "translate z");
 }
 
 static void testTranslate(Test& test) {
     Matrix m;
     m.translate(Vector3(1.0f, 2.0f, 3.0f));
-    compareVectors(test, Vector3(-3.0f, 8.0f, 10.0f), m * Vector3(-4.0f, 6.0f, 7.0f), "translate");
+    compareVectors(test, Vector3(-3.0f, 8.0f, 10.0f),
+                   m * Vector3(-4.0f, 6.0f, 7.0f), "translate");
 }
 
 static void testCombination(Test& test) {
@@ -93,7 +102,8 @@ static void testCombination(Test& test) {
     m.translate(Vector3(-4.0f, 2.0f, 3.0f));
     m.scale(Vector3(2.0f, 3.0f, 4.0f));
     m.scale(0.5f);
-    compareVectors(test, Vector3(-1.0f, 9.0f, 16.0f), m * Vector3(1.0f, 1.0f, 1.0f), "combination");
+    compareVectors(test, Vector3(-1.0f, 9.0f, 16.0f),
+                   m * Vector3(1.0f, 1.0f, 1.0f), "combination");
 }
 
 static void testMatrixCombination(Test& test) {
@@ -109,31 +119,41 @@ static void testMatrixCombination(Test& test) {
     c.translate(Vector3(-1.0f, -2.0f, -3.0f));
     c *= b * a;
 
-    compareVectors(test, Vector3(9.0f, 11.0f, 13.0f), c * Vector3(1.0f, 1.0f, 1.0f), "combination");
+    compareVectors(test, Vector3(9.0f, 11.0f, 13.0f),
+                   c * Vector3(1.0f, 1.0f, 1.0f), "combination");
 }
 
 static void testRotateX(Test& test) {
     Matrix m;
     m.rotateX(90);
-    compareVectors(test, Vector3(1.0f, 0.0f, 0.0f), m * Vector3(1.0f, 0.0f, 0.0f), "rotate x 1");
-    compareVectors(test, Vector3(0.0f, 0.0f, 1.0f), m * Vector3(0.0f, 1.0f, 0.0f), "rotate x 2");
-    compareVectors(test, Vector3(0.0f, -1.0f, 0.0f), m * Vector3(0.0f, 0.0f, 1.0f), "rotate x 3");
+    compareVectors(test, Vector3(1.0f, 0.0f, 0.0f),
+                   m * Vector3(1.0f, 0.0f, 0.0f), "rotate x 1");
+    compareVectors(test, Vector3(0.0f, 0.0f, 1.0f),
+                   m * Vector3(0.0f, 1.0f, 0.0f), "rotate x 2");
+    compareVectors(test, Vector3(0.0f, -1.0f, 0.0f),
+                   m * Vector3(0.0f, 0.0f, 1.0f), "rotate x 3");
 }
 
 static void testRotateY(Test& test) {
     Matrix m;
     m.rotateY(90);
-    compareVectors(test, Vector3(0.0f, 0.0f, -1.0f), m * Vector3(1.0f, 0.0f, 0.0f), "rotate y 1");
-    compareVectors(test, Vector3(0.0f, 1.0f, 0.0f), m * Vector3(0.0f, 1.0f, 0.0f), "rotate y 2");
-    compareVectors(test, Vector3(1.0f, 0.0f, 0.0f), m * Vector3(0.0f, 0.0f, 1.0f), "rotate y 3");
+    compareVectors(test, Vector3(0.0f, 0.0f, -1.0f),
+                   m * Vector3(1.0f, 0.0f, 0.0f), "rotate y 1");
+    compareVectors(test, Vector3(0.0f, 1.0f, 0.0f),
+                   m * Vector3(0.0f, 1.0f, 0.0f), "rotate y 2");
+    compareVectors(test, Vector3(1.0f, 0.0f, 0.0f),
+                   m * Vector3(0.0f, 0.0f, 1.0f), "rotate y 3");
 }
 
 static void testRotateZ(Test& test) {
     Matrix m;
     m.rotateZ(90);
-    compareVectors(test, Vector3(0.0f, 1.0f, 0.0f), m * Vector3(1.0f, 0.0f, 0.0f), "rotate z 1");
-    compareVectors(test, Vector3(-1.0f, 0.0f, 0.0f), m * Vector3(0.0f, 1.0f, 0.0f), "rotate z 2");
-    compareVectors(test, Vector3(0.0f, 0.0f, 1.0f), m * Vector3(0.0f, 0.0f, 1.0f), "rotate z 3");
+    compareVectors(test, Vector3(0.0f, 1.0f, 0.0f),
+                   m * Vector3(1.0f, 0.0f, 0.0f), "rotate z 1");
+    compareVectors(test, Vector3(-1.0f, 0.0f, 0.0f),
+                   m * Vector3(0.0f, 1.0f, 0.0f), "rotate z 2");
+    compareVectors(test, Vector3(0.0f, 0.0f, 1.0f),
+                   m * Vector3(0.0f, 0.0f, 1.0f), "rotate z 3");
 }
 
 static void testToString(Test& test) {
@@ -144,8 +164,11 @@ static void testToString(Test& test) {
     m.set(2, Vector4(9.0f, 10.0f, 11.0f, 12.0f));
     m.set(3, Vector4(13.0f, 14.0f, 15.0f, 16.0f));
     s.append(m);
-    test.checkEqual(StringBuffer<200>("[[1.00, 2.00, 3.00, 4.00], [5.00, 6.00, 7.00, 8.00], "
-            "[9.00, 10.00, 11.00, 12.00], [13.00, 14.00, 15.00, 16.00]]"), s, "to string");
+    test.checkEqual(
+        StringBuffer<200>(
+            "[[1.00, 2.00, 3.00, 4.00], [5.00, 6.00, 7.00, 8.00], "
+            "[9.00, 10.00, 11.00, 12.00], [13.00, 14.00, 15.00, 16.00]]"),
+        s, "to string");
 }
 
 static void testQuaternionMatrix(Test& test) {
@@ -164,7 +187,8 @@ static void testQuaternionMatrix(Test& test) {
     check.translate(Vector3(1.0f, 2.0f, 3.0f));
 
     for(int i = 0; i < 16; i++) {
-        test.checkFloat(check.getValues()[i], m.getValues()[i], eps, "mul matrix");
+        test.checkFloat(check.getValues()[i], m.getValues()[i], eps,
+                        "mul matrix");
     }
 }
 

+ 1 - 3
tests/VectorTests.cpp

@@ -1,8 +1,6 @@
-#include <cmath>
-
+#include "tests/VectorTests.h"
 #include "math/Vector.h"
 #include "tests/Test.h"
-#include "tests/VectorTests.h"
 #include "utils/StringBuffer.h"
 
 const float eps = 0.0001f;