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.
 
 
 

219 lines
6.4 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. #include "core.h"
  15. #include "loldebug.h"
  16. using namespace std;
  17. using namespace lol;
  18. static size_t const MATRIX_TABLE_SIZE = 64 * 1024;
  19. static size_t const MATRIX_RUNS = 100;
  20. static size_t const HALF_TABLE_SIZE = 1024 * 1024;
  21. static size_t const HALF_RUNS = 50;
  22. static void bench_matrix(int mode);
  23. static void bench_half(int mode);
  24. int main(int argc, char **argv)
  25. {
  26. Log::Info("----------------------------\n");
  27. Log::Info(" Float matrices [-2.0, 2.0]\n");
  28. Log::Info("----------------------------\n");
  29. bench_matrix(1);
  30. Log::Info("-------------------------------------\n");
  31. Log::Info(" Half precision floats (random bits)\n");
  32. Log::Info("-------------------------------------\n");
  33. bench_half(1);
  34. Log::Info("-----------------------------------\n");
  35. Log::Info(" Half precision floats [-2.0, 2.0]\n");
  36. Log::Info("-----------------------------------\n");
  37. bench_half(2);
  38. return EXIT_SUCCESS;
  39. }
  40. static void bench_matrix(int mode)
  41. {
  42. float result[5] = { 0.0f };
  43. Timer timer;
  44. /* Set up tables */
  45. mat4 *pm = new mat4[MATRIX_TABLE_SIZE + 1];
  46. float *pf = new float[MATRIX_TABLE_SIZE];
  47. for (size_t run = 0; run < MATRIX_RUNS; run++)
  48. {
  49. switch (mode)
  50. {
  51. case 1:
  52. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  53. for (int j = 0; j < 4; j++)
  54. for (int k = 0; k < 4; k++)
  55. pm[i][j][k] = RandF(-2.0f, 2.0f);
  56. break;
  57. }
  58. /* Copy matrices */
  59. timer.GetMs();
  60. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  61. pm[i] = pm[i + 1];
  62. result[0] += timer.GetMs();
  63. /* Determinant */
  64. timer.GetMs();
  65. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  66. pf[i] = pm[i].det();
  67. result[1] += timer.GetMs();
  68. /* Multiply matrices */
  69. timer.GetMs();
  70. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  71. pm[i] *= pm[i + 1];
  72. result[2] += timer.GetMs();
  73. /* Add matrices */
  74. timer.GetMs();
  75. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  76. pm[i] += pm[i + 1];
  77. result[3] += timer.GetMs();
  78. /* Invert matrix */
  79. timer.GetMs();
  80. for (size_t i = 0; i < MATRIX_TABLE_SIZE; i++)
  81. pm[i] = pm[i].invert();
  82. result[4] += timer.GetMs();
  83. }
  84. delete[] pm;
  85. delete[] pf;
  86. for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
  87. result[i] *= 1000000.0f / (MATRIX_TABLE_SIZE * MATRIX_RUNS);
  88. Log::Info(" ns/elem\n");
  89. Log::Info("mat4 = mat4 %7.3f\n", result[0]);
  90. Log::Info("float = mat4.det() %7.3f\n", result[1]);
  91. Log::Info("mat4 *= mat4 %7.3f\n", result[2]);
  92. Log::Info("mat4 += mat4 %7.3f\n", result[3]);
  93. Log::Info("mat4 = mat4.invert() %7.3f\n", result[4]);
  94. }
  95. static void bench_half(int mode)
  96. {
  97. float result[10] = { 0.0f };
  98. Timer timer;
  99. /* Set up tables */
  100. float *pf = new float[HALF_TABLE_SIZE + 1];
  101. half *ph = new half[HALF_TABLE_SIZE + 1];
  102. for (size_t run = 0; run < HALF_RUNS; run++)
  103. {
  104. switch (mode)
  105. {
  106. case 1:
  107. for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++)
  108. ph[i] = half::makebits(rand());
  109. break;
  110. case 2:
  111. for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++)
  112. ph[i] = RandF(-2.0f, 2.0f);
  113. break;
  114. }
  115. /* Copy float */
  116. timer.GetMs();
  117. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  118. pf[i] = pf[i + 1];
  119. result[0] += timer.GetMs();
  120. /* Convert half to float (array) */
  121. timer.GetMs();
  122. half::convert(pf, ph, HALF_TABLE_SIZE);
  123. result[1] += timer.GetMs();
  124. /* Convert half to float (fast) */
  125. timer.GetMs();
  126. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  127. pf[i] = (float)ph[i];
  128. result[2] += timer.GetMs();
  129. /* Add a half to every float */
  130. timer.GetMs();
  131. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  132. pf[i] += ph[i];
  133. result[3] += timer.GetMs();
  134. /* Copy half */
  135. timer.GetMs();
  136. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  137. ph[i] = ph[i + 1];
  138. result[4] += timer.GetMs();
  139. /* Change sign of every half */
  140. timer.GetMs();
  141. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  142. ph[i] = -ph[i];
  143. result[5] += timer.GetMs();
  144. /* Convert float to half (array) */
  145. timer.GetMs();
  146. half::convert(ph, pf, HALF_TABLE_SIZE);
  147. result[6] += timer.GetMs();
  148. /* Convert float to half (fast) */
  149. timer.GetMs();
  150. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  151. ph[i] = (half)pf[i];
  152. result[7] += timer.GetMs();
  153. /* Convert float to half (accurate) */
  154. timer.GetMs();
  155. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  156. ph[i] = half::makeaccurate(pf[i]);
  157. result[8] += timer.GetMs();
  158. /* Add a float to every half */
  159. timer.GetMs();
  160. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  161. ph[i] += pf[i];
  162. result[9] += timer.GetMs();
  163. }
  164. delete[] pf;
  165. delete[] ph;
  166. for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
  167. result[i] *= 1000000.0f / (HALF_TABLE_SIZE * HALF_RUNS);
  168. Log::Info(" ns/elem\n");
  169. Log::Info("float = float %7.3f\n", result[0]);
  170. Log::Info("float = half (array) %7.3f\n", result[1]);
  171. Log::Info("float = half (fast) %7.3f\n", result[2]);
  172. Log::Info("float += half %7.3f\n", result[3]);
  173. Log::Info("half = half %7.3f\n", result[4]);
  174. Log::Info("half = -half %7.3f\n", result[5]);
  175. Log::Info("half = float (array) %7.3f\n", result[6]);
  176. Log::Info("half = float (fast) %7.3f\n", result[7]);
  177. Log::Info("half = float (accurate) %7.3f\n", result[8]);
  178. Log::Info("half += float %7.3f\n", result[9]);
  179. }