xref: /aosp_15_r20/external/swiftshader/tests/ReactorBenchmarks/ReactorBenchmarks.cpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1 // Copyright 2020 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 "Coroutine.hpp"
16 #include "Reactor.hpp"
17 
18 #include "benchmark/benchmark.h"
19 
20 using namespace rr;
21 
22 BENCHMARK_MAIN();
23 
24 class Coroutines : public benchmark::Fixture
25 {
26 public:
SetUp(const::benchmark::State & state)27 	void SetUp(const ::benchmark::State &state) {}
28 
TearDown(const::benchmark::State & state)29 	void TearDown(const ::benchmark::State &state) {}
30 };
31 
BENCHMARK_DEFINE_F(Coroutines,Fibonacci)32 BENCHMARK_DEFINE_F(Coroutines, Fibonacci)
33 (benchmark::State &state)
34 {
35 	if(!Caps::coroutinesSupported())
36 	{
37 		state.SkipWithError("Coroutines are not supported");
38 		return;
39 	}
40 
41 	Coroutine<int()> function;
42 	{
43 		Yield(Int(0));
44 		Yield(Int(1));
45 		Int current = 1;
46 		Int next = 1;
47 		While(true)
48 		{
49 			Yield(next);
50 			auto tmp = current + next;
51 			current = next;
52 			next = tmp;
53 		}
54 	}
55 
56 	auto coroutine = function();
57 
58 	const auto iterations = state.range(0);
59 
60 	int out = 0;
61 	for(auto _ : state)
62 	{
63 		for(int64_t i = 0; i < iterations; i++)
64 		{
65 			coroutine->await(out);
66 		}
67 	}
68 }
69 
70 BENCHMARK_REGISTER_F(Coroutines, Fibonacci)->RangeMultiplier(8)->Range(1, 0x1000000)->ArgName("iterations");
71 
72 // Macro that creates a lambda wrapper around the input overloaded function,
73 // creating a non-overload based on the args. This is useful for passing
74 // overloaded functions as template arguments.
75 // See https://stackoverflow.com/questions/25871381/c-overloaded-function-as-template-argument
76 #define LIFT(fname)                                          \
77 	[](auto &&...args) -> decltype(auto) {                   \
78 		return fname(std::forward<decltype(args)>(args)...); \
79 	}
80 
81 template<typename Func, class... Args>
Transcedental1(benchmark::State & state,Func func,Args &&...args)82 static void Transcedental1(benchmark::State &state, Func func, Args &&...args)
83 {
84 	FunctionT<float(float)> function;
85 	{
86 		Float a = function.Arg<0>();
87 		Float4 v{ a };
88 		Float4 r = func(v, args...);
89 		Return(Float(r.x));
90 	}
91 
92 	auto routine = function("one");
93 
94 	for(auto _ : state)
95 	{
96 		routine(1.f);
97 	}
98 }
99 
100 template<typename Func, class... Args>
Transcedental2(benchmark::State & state,Func func,Args &&...args)101 static void Transcedental2(benchmark::State &state, Func func, Args &&...args)
102 {
103 	FunctionT<float(float, float)> function;
104 	{
105 		Float a = function.Arg<0>();
106 		Float b = function.Arg<1>();
107 		Float4 v1{ a };
108 		Float4 v2{ b };
109 		Float4 r = func(v1, v2, args...);
110 		Return(Float(r.x));
111 	}
112 
113 	auto routine = function("two");
114 
115 	for(auto _ : state)
116 	{
117 		routine(0.456f, 0.789f);
118 	}
119 }
120 
121 BENCHMARK_CAPTURE(Transcedental1, rr_Sin, Sin);
122 BENCHMARK_CAPTURE(Transcedental1, rr_Cos, Cos);
123 BENCHMARK_CAPTURE(Transcedental1, rr_Tan, Tan);
124 
125 BENCHMARK_CAPTURE(Transcedental1, rr_Asin, Asin);
126 BENCHMARK_CAPTURE(Transcedental1, rr_Acos, Acos);
127 
128 BENCHMARK_CAPTURE(Transcedental1, rr_Atan, Atan);
129 BENCHMARK_CAPTURE(Transcedental1, rr_Sinh, Sinh);
130 BENCHMARK_CAPTURE(Transcedental1, rr_Cosh, Cosh);
131 BENCHMARK_CAPTURE(Transcedental1, rr_Tanh, Tanh);
132 
133 BENCHMARK_CAPTURE(Transcedental1, rr_Asinh, Asinh);
134 BENCHMARK_CAPTURE(Transcedental1, rr_Acosh, Acosh);
135 BENCHMARK_CAPTURE(Transcedental1, rr_Atanh, Atanh);
136 BENCHMARK_CAPTURE(Transcedental2, rr_Atan2, Atan2);
137 
138 BENCHMARK_CAPTURE(Transcedental2, rr_Pow, Pow);
139 BENCHMARK_CAPTURE(Transcedental1, rr_Exp, Exp);
140 BENCHMARK_CAPTURE(Transcedental1, rr_Log, Log);
141 BENCHMARK_CAPTURE(Transcedental1, rr_Exp2, LIFT(Exp2));
142 BENCHMARK_CAPTURE(Transcedental1, rr_Log2, LIFT(Log2));
143