]> git.sesse.net Git - interpreter_trials/blob - interpreter_trials.cpp
Add a tail-call variant with state as global variables in locked registers.
[interpreter_trials] / interpreter_trials.cpp
1 #include <benchmark/benchmark.h>
2 #include <iostream>
3
4 #include "bytecode.h"
5 #include "interpreter.h"
6
7
8 template<typename RunLoop>
9 void run(RunLoop run_loop)
10 {
11     auto [res, cycles] = run_loop(fibonacci, 46);
12     if(res != 1836311903)
13     {
14         throw std::runtime_error("Invalid result " + std::to_string(res));
15     }
16     if(cycles != 481)
17     {
18         throw std::runtime_error("Invalid cycle count " + std::to_string(cycles));
19
20     }
21 }
22
23 static void interpreter1_baseline(benchmark::State& state) {
24     for (auto _ : state)
25     {
26         run(interpreter1::interpreter_run);
27     }
28 }
29 BENCHMARK(interpreter1_baseline);
30
31 static void interpreter2_state_as_parameters(benchmark::State& state) {
32     for (auto _ : state)
33     {
34         run(interpreter2::interpreter_run);
35     }
36 }
37 BENCHMARK(interpreter2_state_as_parameters);
38
39 static void interpreter3_cycle_count_in_op_funs(benchmark::State& state) {
40     for (auto _ : state)
41     {
42         run(interpreter3::interpreter_run);
43     }
44 }
45 BENCHMARK(interpreter3_cycle_count_in_op_funs);
46
47 static void interpreter4_pc_in_registers(benchmark::State& state) {
48     for (auto _ : state)
49     {
50         run(interpreter4::interpreter_run);
51     }
52 }
53 BENCHMARK(interpreter4_pc_in_registers);
54
55 static void interpreter5_pc_flags_in_registers(benchmark::State& state) {
56     for (auto _ : state)
57     {
58         run(interpreter5::interpreter_run);
59     }
60 }
61 BENCHMARK(interpreter5_pc_flags_in_registers);
62
63 static void interpreter6_pc_flags_cycle_count_in_registers(benchmark::State& state) {
64     for (auto _ : state)
65     {
66         run(interpreter6::interpreter_run);
67     }
68 }
69 BENCHMARK(interpreter6_pc_flags_cycle_count_in_registers);
70
71 static void interpreter7_tail_calls(benchmark::State& state) {
72     for (auto _ : state)
73     {
74         run(interpreter7::interpreter_run);
75     }
76 }
77 BENCHMARK(interpreter7_tail_calls);
78
79 static void interpreter8_dispatch_table_in_registers(benchmark::State& state) {
80     for (auto _ : state)
81     {
82         run(interpreter8::interpreter_run);
83     }
84 }
85 BENCHMARK(interpreter8_dispatch_table_in_registers);
86
87 static void interpreter9_separate_flags(benchmark::State& state) {
88     for (auto _ : state)
89     {
90         run(interpreter9::interpreter_run);
91     }
92 }
93 BENCHMARK(interpreter9_separate_flags);
94
95 static void interpreter10_only_4_parameters(benchmark::State& state) {
96     for (auto _ : state)
97     {
98         run(interpreter10::interpreter_run);
99     }
100 }
101 BENCHMARK(interpreter10_only_4_parameters);
102
103 static void interpreter11_computed_goto(benchmark::State& state) {
104     for (auto _ : state)
105     {
106         run(interpreter11::interpreter_run);
107     }
108 }
109 BENCHMARK(interpreter11_computed_goto);
110
111 static void interpreter12_tail_calls_locked_reg(benchmark::State& state) {
112     for (auto _ : state)
113     {
114         run(interpreter12::interpreter_run);
115     }
116 }
117 BENCHMARK(interpreter12_tail_calls_locked_reg);
118
119 BENCHMARK_MAIN();