You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

318 lines
9.2 KiB

  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. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cstdio>
  14. #if defined HAVE_FASTMATH_H
  15. # include <fastmath.h>
  16. #endif
  17. #include "core.h"
  18. #include "loldebug.h"
  19. using namespace std;
  20. using namespace lol;
  21. static size_t const TRIG_TABLE_SIZE = 128 * 1024;
  22. static size_t const TRIG_RUNS = 50;
  23. static size_t const MATRIX_TABLE_SIZE = 64 * 1024;
  24. static size_t const MATRIX_RUNS = 100;
  25. static size_t const HALF_TABLE_SIZE = 1024 * 1024;
  26. static size_t const HALF_RUNS = 50;
  27. static void bench_trig(int mode);
  28. static void bench_matrix(int mode);
  29. static void bench_half(int mode);
  30. int main(int argc, char **argv)
  31. {
  32. Log::Info("--------------------------\n");
  33. Log::Info(" Trigonometry [-1e5, 1e5]\n");
  34. Log::Info("--------------------------\n");
  35. bench_trig(1);
  36. Log::Info("------------------------\n");
  37. Log::Info(" Trigonometry [-pi, pi]\n");
  38. Log::Info("------------------------\n");
  39. bench_trig(2);
  40. Log::Info("----------------------------\n");
  41. Log::Info(" Trigonometry [-1e-2, 1e-2]\n");
  42. Log::Info("----------------------------\n");
  43. bench_trig(3);
  44. Log::Info("----------------------------\n");
  45. Log::Info(" Float matrices [-2.0, 2.0]\n");
  46. Log::Info("----------------------------\n");
  47. bench_matrix(1);
  48. Log::Info("-------------------------------------\n");
  49. Log::Info(" Half precision floats (random bits)\n");
  50. Log::Info("-------------------------------------\n");
  51. bench_half(1);
  52. Log::Info("-----------------------------------\n");
  53. Log::Info(" Half precision floats [-2.0, 2.0]\n");
  54. Log::Info("-----------------------------------\n");
  55. bench_half(2);
  56. return EXIT_SUCCESS;
  57. }
  58. static void bench_trig(int mode)
  59. {
  60. float result[5] = { 0.0f };
  61. Timer timer;
  62. /* Set up tables */
  63. float *pf = new float[TRIG_TABLE_SIZE];
  64. float *pf2 = new float[TRIG_TABLE_SIZE];
  65. for (size_t run = 0; run < TRIG_RUNS; run++)
  66. {
  67. switch (mode)
  68. {
  69. case 1:
  70. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  71. pf[i] = RandF(-1e5f, 1e5f);
  72. break;
  73. case 2:
  74. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  75. pf[i] = RandF(-M_PI, M_PI);
  76. break;
  77. case 3:
  78. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  79. pf[i] = RandF(-1e-2f, 1e-2f);
  80. break;
  81. }
  82. /* Sin */
  83. timer.GetMs();
  84. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  85. pf2[i] = __builtin_sinf(pf[i]);
  86. result[0] += timer.GetMs();
  87. /* Fast sin */
  88. timer.GetMs();
  89. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  90. #if defined HAVE_FASTMATH_H
  91. pf2[i] = f_sinf(pf[i]);
  92. #else
  93. pf2[i] = sinf(pf[i]);
  94. #endif
  95. result[1] += timer.GetMs();
  96. /* Lol sin */
  97. timer.GetMs();
  98. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  99. pf2[i] = lol_sin(pf[i]);
  100. result[2] += timer.GetMs();
  101. /* Cos */
  102. timer.GetMs();
  103. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  104. pf2[i] = __builtin_cosf(pf[i]);
  105. result[3] += timer.GetMs();
  106. /* Tan */
  107. timer.GetMs();
  108. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  109. pf2[i] = __builtin_tanf(pf[i]);
  110. result[4] += timer.GetMs();
  111. }
  112. delete[] pf;
  113. delete[] pf2;
  114. for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
  115. result[i] *= 1000000.0f / (TRIG_TABLE_SIZE * TRIG_RUNS);
  116. Log::Info(" ns/elem\n");
  117. Log::Info("float = sinf(float) %7.3f\n", result[0]);
  118. Log::Info("float = fastsinf(float) %7.3f\n", result[1]);
  119. Log::Info("float = lol_sinf(float) %7.3f\n", result[2]);
  120. Log::Info("float = cosf(float) %7.3f\n", result[3]);
  121. Log::Info("float = tanf(float) %7.3f\n", result[4]);
  122. }
  123. static void bench_matrix(int mode)
  124. {
  125. float result[5] = { 0.0f };
  126. Timer timer;
  127. /* Set up tables */
  128. mat4 *pm = new mat4[MATRIX_TABLE_SIZE + 1];
  129. float *pf = new float[MATRIX_TABLE_SIZE];
  130. for (size_t run = 0; run < MATRIX_RUNS; run++)
  131. {
  132. switch (mode)
  133. {
  134. case 1:
  135. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  136. for (int j = 0; j < 4; j++)
  137. for (int k = 0; k < 4; k++)
  138. pm[i][j][k] = RandF(-2.0f, 2.0f);
  139. break;
  140. }
  141. /* Copy matrices */
  142. timer.GetMs();
  143. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  144. pm[i] = pm[i + 1];
  145. result[0] += timer.GetMs();
  146. /* Determinant */
  147. timer.GetMs();
  148. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  149. pf[i] = pm[i].det();
  150. result[1] += timer.GetMs();
  151. /* Multiply matrices */
  152. timer.GetMs();
  153. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  154. pm[i] *= pm[i + 1];
  155. result[2] += timer.GetMs();
  156. /* Add matrices */
  157. timer.GetMs();
  158. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  159. pm[i] += pm[i + 1];
  160. result[3] += timer.GetMs();
  161. /* Invert matrix */
  162. timer.GetMs();
  163. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  164. pm[i] = pm[i].invert();
  165. result[4] += timer.GetMs();
  166. }
  167. delete[] pm;
  168. delete[] pf;
  169. for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
  170. result[i] *= 1000000.0f / (MATRIX_TABLE_SIZE * MATRIX_RUNS);
  171. Log::Info(" ns/elem\n");
  172. Log::Info("mat4 = mat4 %7.3f\n", result[0]);
  173. Log::Info("float = mat4.det() %7.3f\n", result[1]);
  174. Log::Info("mat4 *= mat4 %7.3f\n", result[2]);
  175. Log::Info("mat4 += mat4 %7.3f\n", result[3]);
  176. Log::Info("mat4 = mat4.invert() %7.3f\n", result[4]);
  177. }
  178. static void bench_half(int mode)
  179. {
  180. float result[10] = { 0.0f };
  181. Timer timer;
  182. /* Set up tables */
  183. float *pf = new float[HALF_TABLE_SIZE + 1];
  184. half *ph = new half[HALF_TABLE_SIZE + 1];
  185. for (size_t run = 0; run < HALF_RUNS; run++)
  186. {
  187. switch (mode)
  188. {
  189. case 1:
  190. for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++)
  191. ph[i] = half::makebits(rand());
  192. break;
  193. case 2:
  194. for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++)
  195. ph[i] = RandF(-2.0f, 2.0f);
  196. break;
  197. }
  198. /* Copy float */
  199. timer.GetMs();
  200. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  201. pf[i] = pf[i + 1];
  202. result[0] += timer.GetMs();
  203. /* Convert half to float (array) */
  204. timer.GetMs();
  205. half::convert(pf, ph, HALF_TABLE_SIZE);
  206. result[1] += timer.GetMs();
  207. /* Convert half to float (fast) */
  208. timer.GetMs();
  209. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  210. pf[i] = (float)ph[i];
  211. result[2] += timer.GetMs();
  212. /* Add a half to every float */
  213. timer.GetMs();
  214. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  215. pf[i] += ph[i];
  216. result[3] += timer.GetMs();
  217. /* Copy half */
  218. timer.GetMs();
  219. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  220. ph[i] = ph[i + 1];
  221. result[4] += timer.GetMs();
  222. /* Change sign of every half */
  223. timer.GetMs();
  224. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  225. ph[i] = -ph[i];
  226. result[5] += timer.GetMs();
  227. /* Convert float to half (array) */
  228. timer.GetMs();
  229. half::convert(ph, pf, HALF_TABLE_SIZE);
  230. result[6] += timer.GetMs();
  231. /* Convert float to half (fast) */
  232. timer.GetMs();
  233. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  234. ph[i] = (half)pf[i];
  235. result[7] += timer.GetMs();
  236. /* Convert float to half (accurate) */
  237. timer.GetMs();
  238. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  239. ph[i] = half::makeaccurate(pf[i]);
  240. result[8] += timer.GetMs();
  241. /* Add a float to every half */
  242. timer.GetMs();
  243. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  244. ph[i] += pf[i];
  245. result[9] += timer.GetMs();
  246. }
  247. delete[] pf;
  248. delete[] ph;
  249. for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
  250. result[i] *= 1000000.0f / (HALF_TABLE_SIZE * HALF_RUNS);
  251. Log::Info(" ns/elem\n");
  252. Log::Info("float = float %7.3f\n", result[0]);
  253. Log::Info("float = half (array) %7.3f\n", result[1]);
  254. Log::Info("float = half (fast) %7.3f\n", result[2]);
  255. Log::Info("float += half %7.3f\n", result[3]);
  256. Log::Info("half = half %7.3f\n", result[4]);
  257. Log::Info("half = -half %7.3f\n", result[5]);
  258. Log::Info("half = float (array) %7.3f\n", result[6]);
  259. Log::Info("half = float (fast) %7.3f\n", result[7]);
  260. Log::Info("half = float (accurate) %7.3f\n", result[8]);
  261. Log::Info("half += float %7.3f\n", result[9]);
  262. }