1 | // |
---|
2 | // Lol Engine - Benchmark program |
---|
3 | // |
---|
4 | // Copyright: (c) 2005-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
9 | // |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <cstdio> |
---|
16 | |
---|
17 | #if defined HAVE_FASTMATH_H |
---|
18 | # include <fastmath.h> |
---|
19 | #endif |
---|
20 | |
---|
21 | #include "core.h" |
---|
22 | #include "loldebug.h" |
---|
23 | |
---|
24 | using namespace std; |
---|
25 | using namespace lol; |
---|
26 | |
---|
27 | static size_t const TRIG_TABLE_SIZE = 128 * 1024; |
---|
28 | static size_t const TRIG_RUNS = 50; |
---|
29 | |
---|
30 | static size_t const MATRIX_TABLE_SIZE = 64 * 1024; |
---|
31 | static size_t const MATRIX_RUNS = 100; |
---|
32 | |
---|
33 | static size_t const HALF_TABLE_SIZE = 1024 * 1024; |
---|
34 | static size_t const HALF_RUNS = 50; |
---|
35 | |
---|
36 | static void bench_trig(int mode); |
---|
37 | static void bench_matrix(int mode); |
---|
38 | static void bench_half(int mode); |
---|
39 | |
---|
40 | int main(int argc, char **argv) |
---|
41 | { |
---|
42 | Log::Info("--------------------------\n"); |
---|
43 | Log::Info(" Trigonometry [-1e5, 1e5]\n"); |
---|
44 | Log::Info("--------------------------\n"); |
---|
45 | bench_trig(1); |
---|
46 | |
---|
47 | Log::Info("------------------------\n"); |
---|
48 | Log::Info(" Trigonometry [-pi, pi]\n"); |
---|
49 | Log::Info("------------------------\n"); |
---|
50 | bench_trig(2); |
---|
51 | |
---|
52 | Log::Info("----------------------------\n"); |
---|
53 | Log::Info(" Trigonometry [-1e-2, 1e-2]\n"); |
---|
54 | Log::Info("----------------------------\n"); |
---|
55 | bench_trig(3); |
---|
56 | |
---|
57 | Log::Info("----------------------------\n"); |
---|
58 | Log::Info(" Float matrices [-2.0, 2.0]\n"); |
---|
59 | Log::Info("----------------------------\n"); |
---|
60 | bench_matrix(1); |
---|
61 | |
---|
62 | Log::Info("-------------------------------------\n"); |
---|
63 | Log::Info(" Half precision floats (random bits)\n"); |
---|
64 | Log::Info("-------------------------------------\n"); |
---|
65 | bench_half(1); |
---|
66 | |
---|
67 | Log::Info("-----------------------------------\n"); |
---|
68 | Log::Info(" Half precision floats [-2.0, 2.0]\n"); |
---|
69 | Log::Info("-----------------------------------\n"); |
---|
70 | bench_half(2); |
---|
71 | |
---|
72 | return EXIT_SUCCESS; |
---|
73 | } |
---|
74 | |
---|
75 | static void bench_trig(int mode) |
---|
76 | { |
---|
77 | float result[5] = { 0.0f }; |
---|
78 | Timer timer; |
---|
79 | |
---|
80 | /* Set up tables */ |
---|
81 | float *pf = new float[TRIG_TABLE_SIZE]; |
---|
82 | float *pf2 = new float[TRIG_TABLE_SIZE]; |
---|
83 | |
---|
84 | for (size_t run = 0; run < TRIG_RUNS; run++) |
---|
85 | { |
---|
86 | switch (mode) |
---|
87 | { |
---|
88 | case 1: |
---|
89 | for (size_t i = 0; i < TRIG_TABLE_SIZE; i++) |
---|
90 | pf[i] = RandF(-1e5f, 1e5f); |
---|
91 | break; |
---|
92 | case 2: |
---|
93 | for (size_t i = 0; i < TRIG_TABLE_SIZE; i++) |
---|
94 | pf[i] = RandF(-M_PI, M_PI); |
---|
95 | break; |
---|
96 | case 3: |
---|
97 | for (size_t i = 0; i < TRIG_TABLE_SIZE; i++) |
---|
98 | pf[i] = RandF(-1e-2f, 1e-2f); |
---|
99 | break; |
---|
100 | } |
---|
101 | |
---|
102 | /* Sin */ |
---|
103 | timer.GetMs(); |
---|
104 | for (size_t i = 0; i < TRIG_TABLE_SIZE; i++) |
---|
105 | pf2[i] = __builtin_sinf(pf[i]); |
---|
106 | result[0] += timer.GetMs(); |
---|
107 | |
---|
108 | /* Fast sin */ |
---|
109 | timer.GetMs(); |
---|
110 | for (size_t i = 0; i < TRIG_TABLE_SIZE; i++) |
---|
111 | #if defined HAVE_FASTMATH_H |
---|
112 | pf2[i] = f_sinf(pf[i]); |
---|
113 | #else |
---|
114 | pf2[i] = sinf(pf[i]); |
---|
115 | #endif |
---|
116 | result[1] += timer.GetMs(); |
---|
117 | |
---|
118 | /* Lol sin */ |
---|
119 | timer.GetMs(); |
---|
120 | for (size_t i = 0; i < TRIG_TABLE_SIZE; i++) |
---|
121 | pf2[i] = lol_sin(pf[i]); |
---|
122 | result[2] += timer.GetMs(); |
---|
123 | |
---|
124 | /* Cos */ |
---|
125 | timer.GetMs(); |
---|
126 | for (size_t i = 0; i < TRIG_TABLE_SIZE; i++) |
---|
127 | pf2[i] = __builtin_cosf(pf[i]); |
---|
128 | result[3] += timer.GetMs(); |
---|
129 | |
---|
130 | /* Tan */ |
---|
131 | timer.GetMs(); |
---|
132 | for (size_t i = 0; i < TRIG_TABLE_SIZE; i++) |
---|
133 | pf2[i] = __builtin_tanf(pf[i]); |
---|
134 | result[4] += timer.GetMs(); |
---|
135 | } |
---|
136 | |
---|
137 | delete[] pf; |
---|
138 | delete[] pf2; |
---|
139 | |
---|
140 | for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++) |
---|
141 | result[i] *= 1000000.0f / (TRIG_TABLE_SIZE * TRIG_RUNS); |
---|
142 | |
---|
143 | Log::Info(" ns/elem\n"); |
---|
144 | Log::Info("float = sinf(float) %7.3f\n", result[0]); |
---|
145 | Log::Info("float = fastsinf(float) %7.3f\n", result[1]); |
---|
146 | Log::Info("float = lol_sinf(float) %7.3f\n", result[2]); |
---|
147 | Log::Info("float = cosf(float) %7.3f\n", result[3]); |
---|
148 | Log::Info("float = tanf(float) %7.3f\n", result[4]); |
---|
149 | } |
---|
150 | |
---|
151 | static void bench_matrix(int mode) |
---|
152 | { |
---|
153 | float result[5] = { 0.0f }; |
---|
154 | Timer timer; |
---|
155 | |
---|
156 | /* Set up tables */ |
---|
157 | mat4 *pm = new mat4[MATRIX_TABLE_SIZE + 1]; |
---|
158 | float *pf = new float[MATRIX_TABLE_SIZE]; |
---|
159 | |
---|
160 | for (size_t run = 0; run < MATRIX_RUNS; run++) |
---|
161 | { |
---|
162 | switch (mode) |
---|
163 | { |
---|
164 | case 1: |
---|
165 | for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++) |
---|
166 | for (int j = 0; j < 4; j++) |
---|
167 | for (int k = 0; k < 4; k++) |
---|
168 | pm[i][j][k] = RandF(-2.0f, 2.0f); |
---|
169 | break; |
---|
170 | } |
---|
171 | |
---|
172 | /* Copy matrices */ |
---|
173 | timer.GetMs(); |
---|
174 | for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++) |
---|
175 | pm[i] = pm[i + 1]; |
---|
176 | result[0] += timer.GetMs(); |
---|
177 | |
---|
178 | /* Determinant */ |
---|
179 | timer.GetMs(); |
---|
180 | for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++) |
---|
181 | pf[i] = pm[i].det(); |
---|
182 | result[1] += timer.GetMs(); |
---|
183 | |
---|
184 | /* Multiply matrices */ |
---|
185 | timer.GetMs(); |
---|
186 | for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++) |
---|
187 | pm[i] *= pm[i + 1]; |
---|
188 | result[2] += timer.GetMs(); |
---|
189 | |
---|
190 | /* Add matrices */ |
---|
191 | timer.GetMs(); |
---|
192 | for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++) |
---|
193 | pm[i] += pm[i + 1]; |
---|
194 | result[3] += timer.GetMs(); |
---|
195 | |
---|
196 | /* Invert matrix */ |
---|
197 | timer.GetMs(); |
---|
198 | for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++) |
---|
199 | pm[i] = pm[i].invert(); |
---|
200 | result[4] += timer.GetMs(); |
---|
201 | } |
---|
202 | |
---|
203 | delete[] pm; |
---|
204 | delete[] pf; |
---|
205 | |
---|
206 | for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++) |
---|
207 | result[i] *= 1000000.0f / (MATRIX_TABLE_SIZE * MATRIX_RUNS); |
---|
208 | |
---|
209 | Log::Info(" ns/elem\n"); |
---|
210 | Log::Info("mat4 = mat4 %7.3f\n", result[0]); |
---|
211 | Log::Info("float = mat4.det() %7.3f\n", result[1]); |
---|
212 | Log::Info("mat4 *= mat4 %7.3f\n", result[2]); |
---|
213 | Log::Info("mat4 += mat4 %7.3f\n", result[3]); |
---|
214 | Log::Info("mat4 = mat4.invert() %7.3f\n", result[4]); |
---|
215 | } |
---|
216 | |
---|
217 | static void bench_half(int mode) |
---|
218 | { |
---|
219 | float result[10] = { 0.0f }; |
---|
220 | Timer timer; |
---|
221 | |
---|
222 | /* Set up tables */ |
---|
223 | float *pf = new float[HALF_TABLE_SIZE + 1]; |
---|
224 | half *ph = new half[HALF_TABLE_SIZE + 1]; |
---|
225 | |
---|
226 | for (size_t run = 0; run < HALF_RUNS; run++) |
---|
227 | { |
---|
228 | switch (mode) |
---|
229 | { |
---|
230 | case 1: |
---|
231 | for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++) |
---|
232 | ph[i] = half::makebits(rand()); |
---|
233 | break; |
---|
234 | case 2: |
---|
235 | for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++) |
---|
236 | ph[i] = RandF(-2.0f, 2.0f); |
---|
237 | break; |
---|
238 | } |
---|
239 | |
---|
240 | /* Copy float */ |
---|
241 | timer.GetMs(); |
---|
242 | for (size_t i = 0; i < HALF_TABLE_SIZE; i++) |
---|
243 | pf[i] = pf[i + 1]; |
---|
244 | result[0] += timer.GetMs(); |
---|
245 | |
---|
246 | /* Convert half to float (array) */ |
---|
247 | timer.GetMs(); |
---|
248 | half::convert(pf, ph, HALF_TABLE_SIZE); |
---|
249 | result[1] += timer.GetMs(); |
---|
250 | |
---|
251 | /* Convert half to float (fast) */ |
---|
252 | timer.GetMs(); |
---|
253 | for (size_t i = 0; i < HALF_TABLE_SIZE; i++) |
---|
254 | pf[i] = (float)ph[i]; |
---|
255 | result[2] += timer.GetMs(); |
---|
256 | |
---|
257 | /* Add a half to every float */ |
---|
258 | timer.GetMs(); |
---|
259 | for (size_t i = 0; i < HALF_TABLE_SIZE; i++) |
---|
260 | pf[i] += ph[i]; |
---|
261 | result[3] += timer.GetMs(); |
---|
262 | |
---|
263 | /* Copy half */ |
---|
264 | timer.GetMs(); |
---|
265 | for (size_t i = 0; i < HALF_TABLE_SIZE; i++) |
---|
266 | ph[i] = ph[i + 1]; |
---|
267 | result[4] += timer.GetMs(); |
---|
268 | |
---|
269 | /* Change sign of every half */ |
---|
270 | timer.GetMs(); |
---|
271 | for (size_t i = 0; i < HALF_TABLE_SIZE; i++) |
---|
272 | ph[i] = -ph[i]; |
---|
273 | result[5] += timer.GetMs(); |
---|
274 | |
---|
275 | /* Convert float to half (array) */ |
---|
276 | timer.GetMs(); |
---|
277 | half::convert(ph, pf, HALF_TABLE_SIZE); |
---|
278 | result[6] += timer.GetMs(); |
---|
279 | |
---|
280 | /* Convert float to half (fast) */ |
---|
281 | timer.GetMs(); |
---|
282 | for (size_t i = 0; i < HALF_TABLE_SIZE; i++) |
---|
283 | ph[i] = (half)pf[i]; |
---|
284 | result[7] += timer.GetMs(); |
---|
285 | |
---|
286 | /* Convert float to half (accurate) */ |
---|
287 | timer.GetMs(); |
---|
288 | for (size_t i = 0; i < HALF_TABLE_SIZE; i++) |
---|
289 | ph[i] = half::makeaccurate(pf[i]); |
---|
290 | result[8] += timer.GetMs(); |
---|
291 | |
---|
292 | /* Add a float to every half */ |
---|
293 | timer.GetMs(); |
---|
294 | for (size_t i = 0; i < HALF_TABLE_SIZE; i++) |
---|
295 | ph[i] += pf[i]; |
---|
296 | result[9] += timer.GetMs(); |
---|
297 | } |
---|
298 | |
---|
299 | delete[] pf; |
---|
300 | delete[] ph; |
---|
301 | |
---|
302 | for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++) |
---|
303 | result[i] *= 1000000.0f / (HALF_TABLE_SIZE * HALF_RUNS); |
---|
304 | |
---|
305 | Log::Info(" ns/elem\n"); |
---|
306 | Log::Info("float = float %7.3f\n", result[0]); |
---|
307 | Log::Info("float = half (array) %7.3f\n", result[1]); |
---|
308 | Log::Info("float = half (fast) %7.3f\n", result[2]); |
---|
309 | Log::Info("float += half %7.3f\n", result[3]); |
---|
310 | Log::Info("half = half %7.3f\n", result[4]); |
---|
311 | Log::Info("half = -half %7.3f\n", result[5]); |
---|
312 | Log::Info("half = float (array) %7.3f\n", result[6]); |
---|
313 | Log::Info("half = float (fast) %7.3f\n", result[7]); |
---|
314 | Log::Info("half = float (accurate) %7.3f\n", result[8]); |
---|
315 | Log::Info("half += float %7.3f\n", result[9]); |
---|
316 | } |
---|
317 | |
---|