Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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