1 // Copyright 2022 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "ShaderCore.hpp"
16 #include "Reactor/Reactor.hpp"
17
18 #include "benchmark/benchmark.h"
19
20 #include <vector>
21
22 BENCHMARK_MAIN();
23
24 // Macro that creates a lambda wrapper around the input overloaded function,
25 // creating a non-overload based on the args. This is useful for passing
26 // overloaded functions as template arguments.
27 // See https://stackoverflow.com/questions/25871381/c-overloaded-function-as-template-argument
28 #define LIFT(fname) \
29 [](auto &&...args) -> decltype(auto) { \
30 return fname(std::forward<decltype(args)>(args)...); \
31 }
32
33 namespace sw {
34
35 template<typename Func, class... Args>
Transcendental1(benchmark::State & state,Func func,Args &&...args)36 static void Transcendental1(benchmark::State &state, Func func, Args &&...args)
37 {
38 const int REPS = state.range(0);
39
40 FunctionT<void(float *, float *)> function;
41 {
42 Pointer<SIMD::Float> r = Pointer<Float>(function.Arg<0>());
43 Pointer<SIMD::Float> a = Pointer<Float>(function.Arg<1>());
44
45 for(int i = 0; i < REPS; i++)
46 {
47 r[i] = func(a[i], args...);
48 }
49 }
50
51 auto routine = function("one");
52
53 std::vector<float> r(REPS * SIMD::Width);
54 std::vector<float> a(REPS * SIMD::Width, 1.0f);
55
56 for(auto _ : state)
57 {
58 routine(r.data(), a.data());
59 }
60 }
61
62 template<typename Func, class... Args>
Transcendental2(benchmark::State & state,Func func,Args &&...args)63 static void Transcendental2(benchmark::State &state, Func func, Args &&...args)
64 {
65 const int REPS = state.range(0);
66
67 FunctionT<void(float *, float *, float *)> function;
68 {
69 Pointer<SIMD::Float> r = Pointer<Float>(function.Arg<0>());
70 Pointer<SIMD::Float> a = Pointer<Float>(function.Arg<1>());
71 Pointer<SIMD::Float> b = Pointer<Float>(function.Arg<2>());
72
73 for(int i = 0; i < REPS; i++)
74 {
75 r[i] = func(a[i], b[i], args...);
76 }
77 }
78
79 auto routine = function("two");
80
81 std::vector<float> r(REPS * SIMD::Width);
82 std::vector<float> a(REPS * SIMD::Width, 0.456f);
83 std::vector<float> b(REPS * SIMD::Width, 0.789f);
84
85 for(auto _ : state)
86 {
87 routine(r.data(), a.data(), b.data());
88 }
89 }
90
91 // No operation; just copy the input to the output, for use as a baseline.
Nop(RValue<SIMD::Float> x)92 static SIMD::Float Nop(RValue<SIMD::Float> x)
93 {
94 return x;
95 }
96
97 static const int REPS = 10;
98
99 BENCHMARK_CAPTURE(Transcendental1, Nop, Nop)->Arg(REPS);
100
101 BENCHMARK_CAPTURE(Transcendental1, rr_Sin, LIFT(rr::Sin))->Arg(REPS);
102 BENCHMARK_CAPTURE(Transcendental1, sw_Sin_highp, LIFT(sw::Sin), false /* relaxedPrecision */)->Arg(REPS);
103 BENCHMARK_CAPTURE(Transcendental1, sw_Sin_mediump, LIFT(sw::Sin), true /* relaxedPrecision */)->Arg(REPS);
104 BENCHMARK_CAPTURE(Transcendental1, rr_Cos, LIFT(rr::Cos))->Arg(REPS);
105 BENCHMARK_CAPTURE(Transcendental1, sw_Cos_highp, LIFT(sw::Cos), false /* relaxedPrecision */)->Arg(REPS);
106 BENCHMARK_CAPTURE(Transcendental1, sw_Cos_mediump, LIFT(sw::Cos), true /* relaxedPrecision */)->Arg(REPS);
107 BENCHMARK_CAPTURE(Transcendental1, rr_Tan, LIFT(rr::Tan))->Arg(REPS);
108 BENCHMARK_CAPTURE(Transcendental1, sw_Tan_highp, LIFT(sw::Tan), false /* relaxedPrecision */)->Arg(REPS);
109 BENCHMARK_CAPTURE(Transcendental1, sw_Tan_mediump, LIFT(sw::Tan), true /* relaxedPrecision */)->Arg(REPS);
110
111 BENCHMARK_CAPTURE(Transcendental1, rr_Asin, LIFT(rr::Asin))->Arg(REPS);
112 BENCHMARK_CAPTURE(Transcendental1, sw_Asin_highp, LIFT(sw::Asin), false /* relaxedPrecision */)->Arg(REPS);
113 BENCHMARK_CAPTURE(Transcendental1, sw_Asin_mediump, LIFT(sw::Asin), true /* relaxedPrecision */)->Arg(REPS);
114 BENCHMARK_CAPTURE(Transcendental1, rr_Acos, LIFT(rr::Acos))->Arg(REPS);
115 BENCHMARK_CAPTURE(Transcendental1, sw_Acos_highp, LIFT(sw::Acos), false /* relaxedPrecision */)->Arg(REPS);
116 BENCHMARK_CAPTURE(Transcendental1, sw_Acos_mediump, LIFT(sw::Acos), true /* relaxedPrecision */)->Arg(REPS);
117
118 BENCHMARK_CAPTURE(Transcendental1, rr_Atan, LIFT(rr::Atan))->Arg(REPS);
119 BENCHMARK_CAPTURE(Transcendental1, sw_Atan_highp, LIFT(sw::Atan), false /* relaxedPrecision */)->Arg(REPS);
120 BENCHMARK_CAPTURE(Transcendental1, sw_Atan_mediump, LIFT(sw::Atan), true /* relaxedPrecision */)->Arg(REPS);
121 BENCHMARK_CAPTURE(Transcendental1, rr_Sinh, LIFT(rr::Sinh))->Arg(REPS);
122 BENCHMARK_CAPTURE(Transcendental1, sw_Sinh_highp, LIFT(sw::Sinh), false /* relaxedPrecision */)->Arg(REPS);
123 BENCHMARK_CAPTURE(Transcendental1, sw_Sinh_mediump, LIFT(sw::Sinh), true /* relaxedPrecision */)->Arg(REPS);
124 BENCHMARK_CAPTURE(Transcendental1, rr_Cosh, LIFT(rr::Cosh))->Arg(REPS);
125 BENCHMARK_CAPTURE(Transcendental1, sw_Cosh_highp, LIFT(sw::Cosh), false /* relaxedPrecision */)->Arg(REPS);
126 BENCHMARK_CAPTURE(Transcendental1, sw_Cosh_mediump, LIFT(sw::Cosh), true /* relaxedPrecision */)->Arg(REPS);
127 BENCHMARK_CAPTURE(Transcendental1, rr_Tanh, LIFT(rr::Tanh))->Arg(REPS);
128 BENCHMARK_CAPTURE(Transcendental1, sw_Tanh_highp, LIFT(sw::Tanh), false /* relaxedPrecision */)->Arg(REPS);
129 BENCHMARK_CAPTURE(Transcendental1, sw_Tanh_mediump, LIFT(sw::Tanh), true /* relaxedPrecision */)->Arg(REPS);
130
131 BENCHMARK_CAPTURE(Transcendental1, rr_Asinh, LIFT(rr::Asinh))->Arg(REPS);
132 BENCHMARK_CAPTURE(Transcendental1, sw_Asinh_highp, LIFT(sw::Asinh), false /* relaxedPrecision */)->Arg(REPS);
133 BENCHMARK_CAPTURE(Transcendental1, sw_Asinh_mediump, LIFT(sw::Asinh), true /* relaxedPrecision */)->Arg(REPS);
134 BENCHMARK_CAPTURE(Transcendental1, rr_Acosh, LIFT(rr::Acosh))->Arg(REPS);
135 BENCHMARK_CAPTURE(Transcendental1, sw_Acosh_highp, LIFT(sw::Acosh), false /* relaxedPrecision */)->Arg(REPS);
136 BENCHMARK_CAPTURE(Transcendental1, sw_Acosh_mediump, LIFT(sw::Acosh), true /* relaxedPrecision */)->Arg(REPS);
137 BENCHMARK_CAPTURE(Transcendental1, rr_Atanh, LIFT(rr::Atanh))->Arg(REPS);
138 BENCHMARK_CAPTURE(Transcendental1, sw_Atanh_highp, LIFT(sw::Atanh), false /* relaxedPrecision */)->Arg(REPS);
139 BENCHMARK_CAPTURE(Transcendental1, sw_Atanh_mediump, LIFT(sw::Atanh), true /* relaxedPrecision */)->Arg(REPS);
140 BENCHMARK_CAPTURE(Transcendental2, rr_Atan2, LIFT(rr::Atan2))->Arg(REPS);
141 BENCHMARK_CAPTURE(Transcendental2, sw_Atan2_highp, LIFT(sw::Atan2), false /* relaxedPrecision */)->Arg(REPS);
142 BENCHMARK_CAPTURE(Transcendental2, sw_Atan2_mediump, LIFT(sw::Atan2), true /* relaxedPrecision */)->Arg(REPS);
143
144 BENCHMARK_CAPTURE(Transcendental2, rr_Pow, LIFT(rr::Pow))->Arg(REPS);
145 BENCHMARK_CAPTURE(Transcendental2, sw_Pow_highp, LIFT(sw::Pow<sw::Highp>))->Arg(REPS);
146 BENCHMARK_CAPTURE(Transcendental2, sw_Pow_mediump, LIFT(sw::Pow<sw::Mediump>))->Arg(REPS);
147 BENCHMARK_CAPTURE(Transcendental1, rr_Exp, LIFT(rr::Exp))->Arg(REPS);
148 BENCHMARK_CAPTURE(Transcendental1, sw_Exp_highp, LIFT(sw::Exp), false /* relaxedPrecision */)->Arg(REPS);
149 BENCHMARK_CAPTURE(Transcendental1, sw_Exp_mediump, LIFT(sw::Exp), true /* relaxedPrecision */)->Arg(REPS);
150 BENCHMARK_CAPTURE(Transcendental1, rr_Log, LIFT(rr::Log))->Arg(REPS);
151 BENCHMARK_CAPTURE(Transcendental1, sw_Log_highp, LIFT(sw::Log), false /* relaxedPrecision */)->Arg(REPS);
152 BENCHMARK_CAPTURE(Transcendental1, sw_Log_mediump, LIFT(sw::Log), true /* relaxedPrecision */)->Arg(REPS);
153 BENCHMARK_CAPTURE(Transcendental1, rr_Exp2, LIFT(rr::Exp2))->Arg(REPS);
154 BENCHMARK_CAPTURE(Transcendental1, sw_Exp2_highp, LIFT(sw::Exp2), false /* relaxedPrecision */)->Arg(REPS);
155 BENCHMARK_CAPTURE(Transcendental1, sw_Exp2_mediump, LIFT(sw::Exp2), true /* relaxedPrecision */)->Arg(REPS);
156 BENCHMARK_CAPTURE(Transcendental1, rr_Log2, LIFT(rr::Log2))->Arg(REPS);
157 BENCHMARK_CAPTURE(Transcendental1, sw_Log2_highp, LIFT(sw::Log2), false /* relaxedPrecision */)->Arg(REPS);
158 BENCHMARK_CAPTURE(Transcendental1, sw_Log2_mediump, LIFT(sw::Log2), true /* relaxedPrecision */)->Arg(REPS);
159
160 } // namespace sw