-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunion.cpp
104 lines (88 loc) · 2.52 KB
/
union.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
#include <benchmark/benchmark.h>
#include <xmmintrin.h>
struct Vec4 {
Vec4() : Vec4(0.0f, 0.0f, 0.0f, 0.0f) {}
Vec4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
float x, y, z, w;
};
union Vec4Union {
Vec4Union() : Vec4Union(0.0f, 0.0f, 0.0f, 0.0f) {}
Vec4Union(__m128 xmm) : xmm(xmm) {}
Vec4Union(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
__m128 xmm;
struct {
float x, y, z, w;
};
};
static_assert(sizeof(Vec4) == sizeof(Vec4Union), "");
Vec4 add_sisd(Vec4 a, Vec4 b) {
return Vec4{
a.x + b.x,
a.y + b.y,
a.z + b.z,
a.w + b.w,
};
}
Vec4Union add_sisd_union(Vec4Union a, Vec4Union b) {
return Vec4Union{
a.x + b.x,
a.y + b.y,
a.z + b.z,
a.w + b.w,
};
}
Vec4 add_simd(Vec4 a, Vec4 b) {
const __m128 xmm_a = _mm_load_ps(&a.x);
const __m128 xmm_b = _mm_load_ps(&b.x);
const __m128 xmm_res = _mm_add_ps(xmm_a, xmm_b);
Vec4 res;
_mm_store_ps(&res.x, xmm_res);
return res;
}
Vec4Union add_simd_union(Vec4Union a, Vec4Union b) {
return Vec4Union{_mm_add_ps(a.xmm, b.xmm)};
}
static void union_add_sisd_bench(benchmark::State& state) {
for (auto _ : state) {
Vec4 a = {1.0f, 0.5f, 0.72f, 5.0f};
Vec4 b = {1.5f, 1.9f, 0.0f, 12.0f};
benchmark::DoNotOptimize(a);
benchmark::DoNotOptimize(b);
Vec4 res = add_sisd(a, b);
benchmark::DoNotOptimize(res);
}
}
BENCHMARK(union_add_sisd_bench);
static void union_add_sisd_union_bench(benchmark::State& state) {
for (auto _ : state) {
Vec4Union a = {1.0f, 0.5f, 0.72f, 5.0f};
Vec4Union b = {1.5f, 1.9f, 0.0f, 12.0f};
benchmark::DoNotOptimize(a);
benchmark::DoNotOptimize(b);
Vec4Union res = add_sisd_union(a, b);
benchmark::DoNotOptimize(res);
}
}
BENCHMARK(union_add_sisd_union_bench);
static void union_add_simd_bench(benchmark::State& state) {
for (auto _ : state) {
Vec4 a = {1.0f, 0.5f, 0.72f, 5.0f};
Vec4 b = {1.5f, 1.9f, 0.0f, 12.0f};
benchmark::DoNotOptimize(a);
benchmark::DoNotOptimize(b);
Vec4 res = add_simd(a, b);
benchmark::DoNotOptimize(res);
}
}
BENCHMARK(union_add_simd_bench);
static void union_add_simd_union_bench(benchmark::State& state) {
for (auto _ : state) {
Vec4Union a = {1.0f, 0.5f, 0.72f, 5.0f};
Vec4Union b = {1.5f, 1.9f, 0.0f, 12.0f};
benchmark::DoNotOptimize(a);
benchmark::DoNotOptimize(b);
Vec4Union res = add_simd_union(a, b);
benchmark::DoNotOptimize(res);
}
}
BENCHMARK(union_add_simd_bench);