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 | #include "core.h" |
---|
18 | |
---|
19 | using namespace std; |
---|
20 | using namespace lol; |
---|
21 | |
---|
22 | static size_t const HALF_TABLE_SIZE = 1024 * 1024; |
---|
23 | static size_t const HALF_RUNS = 50; |
---|
24 | |
---|
25 | void bench_half(int mode) |
---|
26 | { |
---|
27 | float result[10] = { 0.0f }; |
---|
28 | Timer timer; |
---|
29 | |
---|
30 | /* Set up tables */ |
---|
31 | float *pf = new float[HALF_TABLE_SIZE + 1]; |
---|
32 | half *ph = new half[HALF_TABLE_SIZE + 1]; |
---|
33 | |
---|
34 | for (size_t run = 0; run < HALF_RUNS; run++) |
---|
35 | { |
---|
36 | switch (mode) |
---|
37 | { |
---|
38 | case 1: |
---|
39 | for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++) |
---|
40 | ph[i] = half::makebits(rand()); |
---|
41 | break; |
---|
42 | case 2: |
---|
43 | for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++) |
---|
44 | ph[i] = RandF(-2.0f, 2.0f); |
---|
45 | break; |
---|
46 | } |
---|
47 | |
---|
48 | /* Copy float */ |
---|
49 | timer.Get(); |
---|
50 | for (size_t i = 0; i < HALF_TABLE_SIZE; i++) |
---|
51 | pf[i] = pf[i + 1]; |
---|
52 | result[0] += timer.Get(); |
---|
53 | |
---|
54 | /* Convert half to float (array) */ |
---|
55 | timer.Get(); |
---|
56 | half::convert(pf, ph, HALF_TABLE_SIZE); |
---|
57 | result[1] += timer.Get(); |
---|
58 | |
---|
59 | /* Convert half to float (fast) */ |
---|
60 | timer.Get(); |
---|
61 | for (size_t i = 0; i < HALF_TABLE_SIZE; i++) |
---|
62 | pf[i] = (float)ph[i]; |
---|
63 | result[2] += timer.Get(); |
---|
64 | |
---|
65 | /* Add a half to every float */ |
---|
66 | timer.Get(); |
---|
67 | for (size_t i = 0; i < HALF_TABLE_SIZE; i++) |
---|
68 | pf[i] += ph[i]; |
---|
69 | result[3] += timer.Get(); |
---|
70 | |
---|
71 | /* Copy half */ |
---|
72 | timer.Get(); |
---|
73 | for (size_t i = 0; i < HALF_TABLE_SIZE; i++) |
---|
74 | ph[i] = ph[i + 1]; |
---|
75 | result[4] += timer.Get(); |
---|
76 | |
---|
77 | /* Change sign of every half */ |
---|
78 | timer.Get(); |
---|
79 | for (size_t i = 0; i < HALF_TABLE_SIZE; i++) |
---|
80 | ph[i] = -ph[i]; |
---|
81 | result[5] += timer.Get(); |
---|
82 | |
---|
83 | /* Convert float to half (array) */ |
---|
84 | timer.Get(); |
---|
85 | half::convert(ph, pf, HALF_TABLE_SIZE); |
---|
86 | result[6] += timer.Get(); |
---|
87 | |
---|
88 | /* Convert float to half (fast) */ |
---|
89 | timer.Get(); |
---|
90 | for (size_t i = 0; i < HALF_TABLE_SIZE; i++) |
---|
91 | ph[i] = (half)pf[i]; |
---|
92 | result[7] += timer.Get(); |
---|
93 | |
---|
94 | /* Convert float to half (accurate) */ |
---|
95 | timer.Get(); |
---|
96 | for (size_t i = 0; i < HALF_TABLE_SIZE; i++) |
---|
97 | ph[i] = half::makeaccurate(pf[i]); |
---|
98 | result[8] += timer.Get(); |
---|
99 | |
---|
100 | /* Add a float to every half */ |
---|
101 | timer.Get(); |
---|
102 | for (size_t i = 0; i < HALF_TABLE_SIZE; i++) |
---|
103 | ph[i] += pf[i]; |
---|
104 | result[9] += timer.Get(); |
---|
105 | } |
---|
106 | |
---|
107 | delete[] pf; |
---|
108 | delete[] ph; |
---|
109 | |
---|
110 | for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++) |
---|
111 | result[i] *= 1e9f / (HALF_TABLE_SIZE * HALF_RUNS); |
---|
112 | |
---|
113 | Log::Info(" ns/elem\n"); |
---|
114 | Log::Info("float = float %7.3f\n", result[0]); |
---|
115 | Log::Info("float = half (array) %7.3f\n", result[1]); |
---|
116 | Log::Info("float = half (fast) %7.3f\n", result[2]); |
---|
117 | Log::Info("float += half %7.3f\n", result[3]); |
---|
118 | Log::Info("half = half %7.3f\n", result[4]); |
---|
119 | Log::Info("half = -half %7.3f\n", result[5]); |
---|
120 | Log::Info("half = float (array) %7.3f\n", result[6]); |
---|
121 | Log::Info("half = float (fast) %7.3f\n", result[7]); |
---|
122 | Log::Info("half = float (accurate) %7.3f\n", result[8]); |
---|
123 | Log::Info("half += float %7.3f\n", result[9]); |
---|
124 | } |
---|
125 | |
---|