-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalize.cpp
123 lines (102 loc) · 3.21 KB
/
normalize.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <benchmark/benchmark.h>
#include <xmmintrin.h>
#include <immintrin.h>
#include <cmath>
struct Vec4 {
float x, y, z, w;
};
Vec4 normalize_sisd(Vec4 a) {
const float square_magnitude = a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w;
const float magnitude = std::sqrt(square_magnitude);
return Vec4{
a.x / magnitude,
a.y / magnitude,
a.z / magnitude,
a.w / magnitude,
};
}
Vec4 normalize_sisd_fast(Vec4 a) {
const float square_magnitude = a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w;
const float inv_magnitude = 1.0f/std::sqrt(square_magnitude);
return Vec4{
a.x * inv_magnitude,
a.y * inv_magnitude,
a.z * inv_magnitude,
a.w * inv_magnitude,
};
}
Vec4 normalize_sisd_fast_cheat(Vec4 a) {
const float square_magnitude = a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w;
const __m128 sqr_mag = _mm_set1_ps(square_magnitude);
const __m128 inv_mag = _mm_rsqrt_ps(sqr_mag);
const float inv_magnitude = _mm_cvtss_f32(inv_mag);
return Vec4{
a.x * inv_magnitude,
a.y * inv_magnitude,
a.z * inv_magnitude,
a.w * inv_magnitude,
};
}
Vec4 normalize_simd(Vec4 a) {
const __m128 xmm_a = _mm_load_ps(&a.x);
const __m128 square_magnitude = _mm_dp_ps(xmm_a, xmm_a, 0xff);
const __m128 magnitude = _mm_sqrt_ps(square_magnitude);
const __m128 normalized_a = _mm_div_ps(xmm_a, magnitude);
Vec4 res;
_mm_store_ps(&res.x, normalized_a);
return res;
}
Vec4 normalize_simd_fast(Vec4 a) {
const __m128 xmm_a = _mm_load_ps(&a.x);
const __m128 square_magnitude = _mm_dp_ps(xmm_a, xmm_a, 0xff);
const __m128 inv_magnitude = _mm_rsqrt_ps(square_magnitude);
const __m128 normalized_a = _mm_mul_ps(xmm_a, inv_magnitude);
Vec4 res;
_mm_store_ps(&res.x, normalized_a);
return res;
}
static void normalize_sisd_bench(benchmark::State& state) {
for (auto _ : state) {
Vec4 a = {1.0f, 0.5f, 0.72f, 5.0f};
benchmark::DoNotOptimize(a);
Vec4 res = normalize_sisd(a);
benchmark::DoNotOptimize(res);
}
}
BENCHMARK(normalize_sisd_bench);
static void normalize_sisd_fast_bench(benchmark::State& state) {
for (auto _ : state) {
Vec4 a = {1.0f, 0.5f, 0.72f, 5.0f};
benchmark::DoNotOptimize(a);
Vec4 res = normalize_sisd_fast(a);
benchmark::DoNotOptimize(res);
}
}
BENCHMARK(normalize_sisd_fast_bench);
static void normalize_sisd_fast_cheat_bench(benchmark::State& state) {
for (auto _ : state) {
Vec4 a = {1.0f, 0.5f, 0.72f, 5.0f};
benchmark::DoNotOptimize(a);
Vec4 res = normalize_sisd_fast_cheat(a);
benchmark::DoNotOptimize(res);
}
}
BENCHMARK(normalize_sisd_fast_cheat_bench);
static void normalize_simd_bench(benchmark::State& state) {
for (auto _ : state) {
Vec4 a = {1.0f, 0.5f, 0.72f, 5.0f};
benchmark::DoNotOptimize(a);
Vec4 res = normalize_simd(a);
benchmark::DoNotOptimize(res);
}
}
BENCHMARK(normalize_simd_bench);
static void normalize_simd_fast_bench(benchmark::State& state) {
for (auto _ : state) {
Vec4 a = {1.0f, 0.5f, 0.72f, 5.0f};
benchmark::DoNotOptimize(a);
Vec4 res = normalize_simd_fast(a);
benchmark::DoNotOptimize(res);
}
}
BENCHMARK(normalize_simd_fast_bench);