From 86905e2590305733824ba2f30f2a426e80b80a1c Mon Sep 17 00:00:00 2001 From: Tom Englund Date: Fri, 11 Jul 2025 15:26:50 +0200 Subject: [PATCH] mat3x3: use float versions of sin/cos reduce dereferencing use the sinf, cosf versions instead of casting the double one to float. reduce the derefencing in multiply by pointing to the data directly. --- src/math/Mat3x3.cpp | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/math/Mat3x3.cpp b/src/math/Mat3x3.cpp index 58e2167..fdd2aa1 100644 --- a/src/math/Mat3x3.cpp +++ b/src/math/Mat3x3.cpp @@ -93,7 +93,7 @@ Mat3x3& Mat3x3::transform(eTransform transform) { } Mat3x3& Mat3x3::rotate(float rot) { - multiply(std::array{(float)cos(rot), (float)-sin(rot), 0.0f, (float)sin(rot), (float)cos(rot), 0.0f, 0.0f, 0.0f, 1.0f}); + multiply(std::array{cosf(rot), -sinf(rot), 0.0f, sinf(rot), cosf(rot), 0.0f, 0.0f, 0.0f, 1.0f}); return *this; } @@ -117,19 +117,22 @@ Mat3x3& Mat3x3::transpose() { } Mat3x3& Mat3x3::multiply(const Mat3x3& other) { + const float* m1 = matrix.data(); // Pointer to current matrix + const float* m2 = other.matrix.data(); // Pointer to the other matrix + std::array product; - product[0] = matrix[0] * other.matrix[0] + matrix[1] * other.matrix[3] + matrix[2] * other.matrix[6]; - product[1] = matrix[0] * other.matrix[1] + matrix[1] * other.matrix[4] + matrix[2] * other.matrix[7]; - product[2] = matrix[0] * other.matrix[2] + matrix[1] * other.matrix[5] + matrix[2] * other.matrix[8]; + product[0] = m1[0] * m2[0] + m1[1] * m2[3] + m1[2] * m2[6]; + product[1] = m1[0] * m2[1] + m1[1] * m2[4] + m1[2] * m2[7]; + product[2] = m1[0] * m2[2] + m1[1] * m2[5] + m1[2] * m2[8]; - product[3] = matrix[3] * other.matrix[0] + matrix[4] * other.matrix[3] + matrix[5] * other.matrix[6]; - product[4] = matrix[3] * other.matrix[1] + matrix[4] * other.matrix[4] + matrix[5] * other.matrix[7]; - product[5] = matrix[3] * other.matrix[2] + matrix[4] * other.matrix[5] + matrix[5] * other.matrix[8]; + product[3] = m1[3] * m2[0] + m1[4] * m2[3] + m1[5] * m2[6]; + product[4] = m1[3] * m2[1] + m1[4] * m2[4] + m1[5] * m2[7]; + product[5] = m1[3] * m2[2] + m1[4] * m2[5] + m1[5] * m2[8]; - product[6] = matrix[6] * other.matrix[0] + matrix[7] * other.matrix[3] + matrix[8] * other.matrix[6]; - product[7] = matrix[6] * other.matrix[1] + matrix[7] * other.matrix[4] + matrix[8] * other.matrix[7]; - product[8] = matrix[6] * other.matrix[2] + matrix[7] * other.matrix[5] + matrix[8] * other.matrix[8]; + product[6] = m1[6] * m2[0] + m1[7] * m2[3] + m1[8] * m2[6]; + product[7] = m1[6] * m2[1] + m1[7] * m2[4] + m1[8] * m2[7]; + product[8] = m1[6] * m2[2] + m1[7] * m2[5] + m1[8] * m2[8]; matrix = product; return *this;