Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

lol-bench.cpp 11 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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[12] = { 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. float *pf3 = new float[TRIG_TABLE_SIZE];
  66. for (size_t run = 0; run < TRIG_RUNS; run++)
  67. {
  68. switch (mode)
  69. {
  70. case 1:
  71. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  72. pf[i] = RandF(-1e5f, 1e5f);
  73. break;
  74. case 2:
  75. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  76. pf[i] = RandF(-M_PI, M_PI);
  77. break;
  78. case 3:
  79. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  80. pf[i] = RandF(-1e-2f, 1e-2f);
  81. break;
  82. }
  83. /* Sin */
  84. timer.GetMs();
  85. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  86. pf2[i] = __builtin_sinf(pf[i]);
  87. result[0] += timer.GetMs();
  88. /* Fast sin */
  89. timer.GetMs();
  90. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  91. #if defined HAVE_FASTMATH_H
  92. pf2[i] = f_sinf(pf[i]);
  93. #else
  94. pf2[i] = sinf(pf[i]);
  95. #endif
  96. result[1] += timer.GetMs();
  97. /* Lol sin */
  98. timer.GetMs();
  99. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  100. pf2[i] = lol_sin(pf[i]);
  101. result[2] += timer.GetMs();
  102. /* Cos */
  103. timer.GetMs();
  104. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  105. pf2[i] = __builtin_cosf(pf[i]);
  106. result[3] += timer.GetMs();
  107. /* Fast cos */
  108. timer.GetMs();
  109. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  110. #if defined HAVE_FASTMATH_H
  111. pf2[i] = f_cosf(pf[i]);
  112. #else
  113. pf2[i] = cosf(pf[i]);
  114. #endif
  115. result[4] += timer.GetMs();
  116. /* Lol cos */
  117. timer.GetMs();
  118. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  119. pf2[i] = lol_cos(pf[i]);
  120. result[5] += timer.GetMs();
  121. /* Sin & cos */
  122. timer.GetMs();
  123. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  124. {
  125. pf2[i] = __builtin_sinf(pf[i]);
  126. pf3[i] = __builtin_cosf(pf[i]);
  127. }
  128. result[6] += timer.GetMs();
  129. /* Fast sin & cos */
  130. timer.GetMs();
  131. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  132. {
  133. #if defined HAVE_FASTMATH_H
  134. pf2[i] = f_sinf(pf[i]);
  135. pf3[i] = f_cosf(pf[i]);
  136. #else
  137. pf2[i] = sinf(pf[i]);
  138. pf3[i] = cosf(pf[i]);
  139. #endif
  140. }
  141. result[7] += timer.GetMs();
  142. /* Lol sincos */
  143. timer.GetMs();
  144. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  145. lol_sincos(pf[i], &pf2[i], &pf3[i]);
  146. result[8] += timer.GetMs();
  147. /* Tan */
  148. timer.GetMs();
  149. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  150. pf2[i] = __builtin_tanf(pf[i]);
  151. result[9] += timer.GetMs();
  152. /* Fast tan */
  153. timer.GetMs();
  154. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  155. #if defined HAVE_FASTMATH_H
  156. pf2[i] = f_tanf(pf[i]);
  157. #else
  158. pf2[i] = tanf(pf[i]);
  159. #endif
  160. result[10] += timer.GetMs();
  161. /* Lol tan */
  162. timer.GetMs();
  163. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  164. pf2[i] = lol_tan(pf[i]);
  165. result[11] += timer.GetMs();
  166. }
  167. delete[] pf;
  168. delete[] pf2;
  169. delete[] pf3;
  170. for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
  171. result[i] *= 1000000.0f / (TRIG_TABLE_SIZE * TRIG_RUNS);
  172. Log::Info(" ns/elem\n");
  173. Log::Info("float = sinf(float) %7.3f\n", result[0]);
  174. Log::Info("float = f_sinf(float) %7.3f\n", result[1]);
  175. Log::Info("float = lol_sin(float) %7.3f\n", result[2]);
  176. Log::Info("float = cosf(float) %7.3f\n", result[3]);
  177. Log::Info("float = f_cosf(float) %7.3f\n", result[4]);
  178. Log::Info("float = lol_cos(float) %7.3f\n", result[5]);
  179. Log::Info("float = sinf,cosf(float) %7.3f\n", result[6]);
  180. Log::Info("float = f_sinf,f_cosf(float) %7.3f\n", result[7]);
  181. Log::Info("float = lol_sincos(float) %7.3f\n", result[8]);
  182. Log::Info("float = tanf(float) %7.3f\n", result[9]);
  183. Log::Info("float = f_tanf(float) %7.3f\n", result[10]);
  184. Log::Info("float = lol_tanf(float) %7.3f\n", result[11]);
  185. }
  186. static void bench_matrix(int mode)
  187. {
  188. float result[5] = { 0.0f };
  189. Timer timer;
  190. /* Set up tables */
  191. mat4 *pm = new mat4[MATRIX_TABLE_SIZE + 1];
  192. float *pf = new float[MATRIX_TABLE_SIZE];
  193. for (size_t run = 0; run < MATRIX_RUNS; run++)
  194. {
  195. switch (mode)
  196. {
  197. case 1:
  198. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  199. for (int j = 0; j < 4; j++)
  200. for (int k = 0; k < 4; k++)
  201. pm[i][j][k] = RandF(-2.0f, 2.0f);
  202. break;
  203. }
  204. /* Copy matrices */
  205. timer.GetMs();
  206. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  207. pm[i] = pm[i + 1];
  208. result[0] += timer.GetMs();
  209. /* Determinant */
  210. timer.GetMs();
  211. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  212. pf[i] = pm[i].det();
  213. result[1] += timer.GetMs();
  214. /* Multiply matrices */
  215. timer.GetMs();
  216. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  217. pm[i] *= pm[i + 1];
  218. result[2] += timer.GetMs();
  219. /* Add matrices */
  220. timer.GetMs();
  221. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  222. pm[i] += pm[i + 1];
  223. result[3] += timer.GetMs();
  224. /* Invert matrix */
  225. timer.GetMs();
  226. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  227. pm[i] = pm[i].invert();
  228. result[4] += timer.GetMs();
  229. }
  230. delete[] pm;
  231. delete[] pf;
  232. for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
  233. result[i] *= 1000000.0f / (MATRIX_TABLE_SIZE * MATRIX_RUNS);
  234. Log::Info(" ns/elem\n");
  235. Log::Info("mat4 = mat4 %7.3f\n", result[0]);
  236. Log::Info("float = mat4.det() %7.3f\n", result[1]);
  237. Log::Info("mat4 *= mat4 %7.3f\n", result[2]);
  238. Log::Info("mat4 += mat4 %7.3f\n", result[3]);
  239. Log::Info("mat4 = mat4.invert() %7.3f\n", result[4]);
  240. }
  241. static void bench_half(int mode)
  242. {
  243. float result[10] = { 0.0f };
  244. Timer timer;
  245. /* Set up tables */
  246. float *pf = new float[HALF_TABLE_SIZE + 1];
  247. half *ph = new half[HALF_TABLE_SIZE + 1];
  248. for (size_t run = 0; run < HALF_RUNS; run++)
  249. {
  250. switch (mode)
  251. {
  252. case 1:
  253. for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++)
  254. ph[i] = half::makebits(rand());
  255. break;
  256. case 2:
  257. for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++)
  258. ph[i] = RandF(-2.0f, 2.0f);
  259. break;
  260. }
  261. /* Copy float */
  262. timer.GetMs();
  263. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  264. pf[i] = pf[i + 1];
  265. result[0] += timer.GetMs();
  266. /* Convert half to float (array) */
  267. timer.GetMs();
  268. half::convert(pf, ph, HALF_TABLE_SIZE);
  269. result[1] += timer.GetMs();
  270. /* Convert half to float (fast) */
  271. timer.GetMs();
  272. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  273. pf[i] = (float)ph[i];
  274. result[2] += timer.GetMs();
  275. /* Add a half to every float */
  276. timer.GetMs();
  277. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  278. pf[i] += ph[i];
  279. result[3] += timer.GetMs();
  280. /* Copy half */
  281. timer.GetMs();
  282. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  283. ph[i] = ph[i + 1];
  284. result[4] += timer.GetMs();
  285. /* Change sign of every half */
  286. timer.GetMs();
  287. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  288. ph[i] = -ph[i];
  289. result[5] += timer.GetMs();
  290. /* Convert float to half (array) */
  291. timer.GetMs();
  292. half::convert(ph, pf, HALF_TABLE_SIZE);
  293. result[6] += timer.GetMs();
  294. /* Convert float to half (fast) */
  295. timer.GetMs();
  296. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  297. ph[i] = (half)pf[i];
  298. result[7] += timer.GetMs();
  299. /* Convert float to half (accurate) */
  300. timer.GetMs();
  301. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  302. ph[i] = half::makeaccurate(pf[i]);
  303. result[8] += timer.GetMs();
  304. /* Add a float to every half */
  305. timer.GetMs();
  306. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  307. ph[i] += pf[i];
  308. result[9] += timer.GetMs();
  309. }
  310. delete[] pf;
  311. delete[] ph;
  312. for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
  313. result[i] *= 1000000.0f / (HALF_TABLE_SIZE * HALF_RUNS);
  314. Log::Info(" ns/elem\n");
  315. Log::Info("float = float %7.3f\n", result[0]);
  316. Log::Info("float = half (array) %7.3f\n", result[1]);
  317. Log::Info("float = half (fast) %7.3f\n", result[2]);
  318. Log::Info("float += half %7.3f\n", result[3]);
  319. Log::Info("half = half %7.3f\n", result[4]);
  320. Log::Info("half = -half %7.3f\n", result[5]);
  321. Log::Info("half = float (array) %7.3f\n", result[6]);
  322. Log::Info("half = float (fast) %7.3f\n", result[7]);
  323. Log::Info("half = float (accurate) %7.3f\n", result[8]);
  324. Log::Info("half += float %7.3f\n", result[9]);
  325. }