Skip to content

Commit 95ddc65

Browse files
committed
simd testing
1 parent 979f050 commit 95ddc65

7 files changed

Lines changed: 200 additions & 13 deletions

File tree

benchmark/main.cpp

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <benchmark/benchmark.h>
22
#include <vector_math/matrix4.hpp>
3+
#include <vector_math/matrix4f.hpp>
34

45
#include <glm/vec3.hpp> // glm::vec3
56
#include <glm/vec4.hpp> // glm::vec4
@@ -8,6 +9,8 @@
89
#include <glm/ext/matrix_clip_space.hpp> // glm::perspective
910
#include <glm/ext/scalar_constants.hpp>
1011

12+
using namespace systems::leal::vector_math;
13+
1114
// dummy
1215
static void BM_StringCreation(benchmark::State& state) {
1316
for (auto _ : state)
@@ -18,47 +21,92 @@ BENCHMARK(BM_StringCreation);
1821
///////////////////////
1922
// Matrix4 benchmark //
2023
///////////////////////
21-
static void BM_MatrixCreation(benchmark::State& state) {
24+
static void BM_Matrix4Creation(benchmark::State& state) {
2225
for (auto _ : state){
2326
benchmark::DoNotOptimize(systems::leal::vector_math::Matrix4<float>());
2427
}
2528
}
26-
BENCHMARK(BM_MatrixCreation);
29+
BENCHMARK(BM_Matrix4Creation);
2730

28-
static void BM_MatrixIdentity(benchmark::State& state) {
31+
static void BM_Matrix4Identity(benchmark::State& state) {
2932
for (auto _ : state){
3033
benchmark::DoNotOptimize(systems::leal::vector_math::Matrix4<float>::identity());
3134
}
3235
}
33-
BENCHMARK(BM_MatrixIdentity);
36+
BENCHMARK(BM_Matrix4Identity);
3437

35-
static void BM_MatrixMultiply(benchmark::State& state) {
38+
static void BM_Matrix4Multiply(benchmark::State& state) {
3639
auto m1 = systems::leal::vector_math::Matrix4<float>::identity();
3740
auto m2 = systems::leal::vector_math::Matrix4<float>(1);
3841
for (auto _ : state){
3942
benchmark::DoNotOptimize(m1 * m2);
4043
}
4144
}
42-
BENCHMARK(BM_MatrixMultiply);
45+
BENCHMARK(BM_Matrix4Multiply);
4346

44-
static void BM_MatrixByVector(benchmark::State& state) {
47+
static void BM_Matrix4ByVector(benchmark::State& state) {
4548
auto m1 = systems::leal::vector_math::Matrix4<float>::identity();
4649
auto v1 = systems::leal::vector_math::Vector4<float>(1,2,3,4);
4750
for (auto _ : state){
4851
benchmark::DoNotOptimize(m1 * v1);
4952
}
5053
}
51-
BENCHMARK(BM_MatrixByVector);
54+
BENCHMARK(BM_Matrix4ByVector);
5255

53-
static void BM_LookAt(benchmark::State& state) {
56+
static void BM_Matrix4LookAt(benchmark::State& state) {
5457
auto eye = systems::leal::vector_math::Vector3<float>(0,0,0);
5558
auto target = systems::leal::vector_math::Vector3<float>(0,0,1000);
5659
auto up = systems::leal::vector_math::Vector3<float>(0,1,0);
5760
for (auto _ : state){
5861
benchmark::DoNotOptimize(systems::leal::vector_math::Matrix4<float>::lookAt(eye, target, up));
5962
}
6063
}
61-
BENCHMARK(BM_LookAt);
64+
BENCHMARK(BM_Matrix4LookAt);
65+
66+
////////////////////////
67+
// Matrix4f benchmark //
68+
////////////////////////
69+
static void BM_Matrix4fCreation(benchmark::State& state) {
70+
for (auto _ : state){
71+
benchmark::DoNotOptimize(systems::leal::vector_math::Matrix4f());
72+
}
73+
}
74+
BENCHMARK(BM_Matrix4fCreation);
75+
76+
static void BM_Matrix4fIdentity(benchmark::State& state) {
77+
for (auto _ : state){
78+
benchmark::DoNotOptimize(systems::leal::vector_math::Matrix4f::identity());
79+
}
80+
}
81+
BENCHMARK(BM_Matrix4fIdentity);
82+
83+
static void BM_Matrix4fMultiply(benchmark::State& state) {
84+
systems::leal::vector_math::Matrix4f m1 = systems::leal::vector_math::Matrix4f::identity();
85+
systems::leal::vector_math::Matrix4f m2 = systems::leal::vector_math::Matrix4f(1);
86+
for (auto _ : state){
87+
benchmark::DoNotOptimize(m1 * m2);
88+
}
89+
}
90+
BENCHMARK(BM_Matrix4fMultiply);
91+
92+
static void BM_Matrix4fByVector(benchmark::State& state) {
93+
Matrix4f m1 = systems::leal::vector_math::Matrix4f::identity();
94+
Vector4f v1 = systems::leal::vector_math::Vector4f(1,2,3,4);
95+
for (auto _ : state){
96+
benchmark::DoNotOptimize(m1 * v1);
97+
}
98+
}
99+
BENCHMARK(BM_Matrix4fByVector);
100+
101+
static void BM_Matrix4fLookAt(benchmark::State& state) {
102+
auto eye = systems::leal::vector_math::Vector3<float>(0,0,0);
103+
auto target = systems::leal::vector_math::Vector3<float>(0,0,1000);
104+
auto up = systems::leal::vector_math::Vector3<float>(0,1,0);
105+
for (auto _ : state){
106+
benchmark::DoNotOptimize(systems::leal::vector_math::Matrix4f::lookAt(eye, target, up));
107+
}
108+
}
109+
BENCHMARK(BM_Matrix4fLookAt);
62110

63111
///////////////////////////
64112
// GLM Matrix4 benchmark //

inc/vector_math/matrix4.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace systems::leal::vector_math
2424
///////////////
2525
// operators //
2626
///////////////
27-
Matrix4<DATA_TYPE> operator*(const Matrix4<DATA_TYPE> rhs) const;
27+
Matrix4<DATA_TYPE> operator*(const Matrix4<DATA_TYPE> &rhs) const;
2828
Matrix4<DATA_TYPE> operator*(DATA_TYPE scalar) const;
2929
Vector4<DATA_TYPE> operator*(const Vector4<DATA_TYPE> &vector) const;
3030

@@ -59,7 +59,7 @@ namespace systems::leal::vector_math
5959
}
6060

6161
template <class DATA_TYPE>
62-
Matrix4<DATA_TYPE> Matrix4<DATA_TYPE>::operator*(const Matrix4<DATA_TYPE> rhs) const {
62+
Matrix4<DATA_TYPE> Matrix4<DATA_TYPE>::operator*(const Matrix4<DATA_TYPE> &rhs) const {
6363
auto toReturn = ((Mat<DATA_TYPE,4,4> *)this)->operator*(rhs);
6464
return *(Matrix4<DATA_TYPE> *)&toReturn;
6565
}

inc/vector_math/matrix4f.hpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#pragma once
2+
3+
#include <intrin.h>
4+
#include <vector_math/matrix4.hpp>
5+
#include <vector_math/vector4f.hpp>
6+
7+
namespace systems::leal::vector_math
8+
{
9+
class alignas(float) Matrix4f : public Matrix4<float> {
10+
public:
11+
//////////////////
12+
// constructors //
13+
//////////////////
14+
Matrix4f():Matrix4<float>() {}
15+
Matrix4f(float value):Matrix4<float>(value) {}
16+
Matrix4f(float buffer[16]):Matrix4<float>(buffer) {}
17+
18+
///////////////
19+
// operators //
20+
///////////////
21+
Matrix4f operator*(const Matrix4f &rhs) const;
22+
Vector4f operator*(const Vector4f &rhs) const;
23+
24+
/////////////
25+
// statics //
26+
/////////////
27+
static Matrix4f identity();
28+
29+
};
30+
31+
Matrix4f Matrix4f::operator*(const Matrix4f &rhs) const {
32+
//auto toReturn = ((Mat<float,4,4> *)this)->operator*(rhs);
33+
34+
Matrix4f toReturn;
35+
auto transposed = rhs.transpose();
36+
alignas(float) float result[4];
37+
38+
const float *plhs = this->data;
39+
for (int c=0; c<4; c++) {
40+
const float *prhs = transposed.data;
41+
__m128 x = _mm_load_ps(plhs);
42+
for (int r=0; r<4; r++) {
43+
__m128 y = _mm_load_ps(prhs);
44+
__m128 z =_mm_mul_ps(x,y);
45+
_mm_store_ps(result, z);
46+
47+
float value=0;
48+
for (int e=0; e<4; e++) {
49+
value += result[e];
50+
}
51+
toReturn.data[4*r + c] = value;
52+
53+
prhs += 4;
54+
}
55+
plhs += 4;
56+
}
57+
58+
//__m128 y = _mm_load_ps(rhs.data);
59+
//__m128 z =_mm_mul_ps(x,y);
60+
//__m256 z = _mm256_add_ps(x,y);
61+
62+
//alignas(float) float result[8];
63+
//_mm256_store_ps(result, z);
64+
65+
66+
return toReturn;
67+
}
68+
69+
Vector4f Matrix4f::operator*(const Vector4f &rhs) const {
70+
//auto toReturn = ((Mat<float,4,4> *)this)->operator*(rhs);
71+
72+
Vector4f toReturn;
73+
alignas(float) float result[4];
74+
75+
const float *plhs = this->data;
76+
const float *prhs = rhs.data;
77+
__m128 y = _mm_load_ps(prhs);
78+
for (int r=0; r<4; r++) {
79+
__m128 x = _mm_load_ps(plhs);
80+
__m128 z =_mm_mul_ps(x,y);
81+
_mm_store_ps(result, z);
82+
83+
float value=0;
84+
for (int e=0; e<4; e++) {
85+
value += result[e];
86+
}
87+
toReturn.data[r] = value;
88+
89+
plhs += 4;
90+
}
91+
92+
//__m128 y = _mm_load_ps(rhs.data);
93+
//__m128 z =_mm_mul_ps(x,y);
94+
//__m256 z = _mm256_add_ps(x,y);
95+
96+
//alignas(float) float result[8];
97+
//_mm256_store_ps(result, z);
98+
99+
100+
return toReturn;
101+
}
102+
103+
Matrix4f Matrix4f::identity()
104+
{
105+
float data[] = {
106+
1.0,0.0,0.0,0.0,
107+
0.0,1.0,0.0,0.0,
108+
0.0,0.0,1.0,0.0,
109+
0.0,0.0,0.0,1.0,
110+
};
111+
return Matrix4f(data);
112+
}
113+
114+
}

inc/vector_math/vec.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace systems::leal::vector_math
99
{
1010

1111
template <class DATA_TYPE, uint32_t SIZE>
12-
class Vec
12+
class alignas(DATA_TYPE) Vec
1313
{
1414
public:
1515
typedef DATA_TYPE DataType;

inc/vector_math/vector4f.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include <vector_math/vector4.hpp>
4+
5+
namespace systems::leal::vector_math {
6+
7+
class Vector4f:public Vector4<float> {
8+
public:
9+
Vector4f() = default;
10+
Vector4f(float x, float y, float z, float w) {
11+
this->data[0] = x;
12+
this->data[1] = y;
13+
this->data[2] = z;
14+
this->data[3] = w;
15+
}
16+
17+
};
18+
19+
}

launch_benchmark.bat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cmake -B build/windows/benchmark -S . -DVECTOR_MATH_BUILD_BENCHMARK=ON -DCMAKE_BUILD_TYPE=Release
2+
msbuild build/windows/benchmark/vector_math.sln /p:Configuration=Release
3+
.\build\windows\benchmark\Release\vector_math_benchmark.exe

launch_test.bat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cmake -B build/windows/test -S . -DVECTOR_MATH_BUILD_TEST=ON
2+
msbuild build/windows/test/vector_math.sln /p:Configuration=Release
3+
.\build\windows\test\Release\vector_math_test.exe

0 commit comments

Comments
 (0)