Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

193 rader
5.3 KiB

  1. //
  2. // Lol Engine — Benchmark program
  3. //
  4. // Copyright © 2005—2015 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // This program is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #if HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include <cstdio>
  16. #if HAVE_FASTMATH_H
  17. # include <fastmath.h>
  18. #endif
  19. #include <lol/engine.h>
  20. using namespace lol;
  21. static size_t const TRIG_TABLE_SIZE = 128 * 1024;
  22. static size_t const TRIG_RUNS = 50;
  23. void bench_trig(int mode)
  24. {
  25. float result[12] = { 0.0f };
  26. Timer timer;
  27. /* Set up tables */
  28. float *pf = new float[TRIG_TABLE_SIZE];
  29. float *pf2 = new float[TRIG_TABLE_SIZE];
  30. float *pf3 = new float[TRIG_TABLE_SIZE];
  31. for (size_t run = 0; run < TRIG_RUNS; run++)
  32. {
  33. switch (mode)
  34. {
  35. case 1:
  36. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  37. pf[i] = rand(-1e5f, 1e5f);
  38. break;
  39. case 2:
  40. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  41. pf[i] = rand(-F_PI, F_PI);
  42. break;
  43. case 3:
  44. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  45. pf[i] = rand(-1e-2f, 1e-2f);
  46. break;
  47. }
  48. /* Sin */
  49. timer.Get();
  50. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  51. #if __GNUC__ && !__SNC__
  52. pf2[i] = __builtin_sinf(pf[i]);
  53. #else
  54. pf2[i] = sinf(pf[i]);
  55. #endif
  56. result[0] += timer.Get();
  57. /* Fast sin */
  58. timer.Get();
  59. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  60. #if HAVE_FASTMATH_H && !__native_client__ && !EMSCRIPTEN
  61. pf2[i] = f_sinf(pf[i]);
  62. #else
  63. pf2[i] = sinf(pf[i]);
  64. #endif
  65. result[1] += timer.Get();
  66. /* Lol sin */
  67. timer.Get();
  68. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  69. pf2[i] = lol_sin(pf[i]);
  70. result[2] += timer.Get();
  71. /* Cos */
  72. timer.Get();
  73. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  74. #if __GNUC__ && !__SNC__
  75. pf2[i] = __builtin_cosf(pf[i]);
  76. #else
  77. pf2[i] = cosf(pf[i]);
  78. #endif
  79. result[3] += timer.Get();
  80. /* Fast cos */
  81. timer.Get();
  82. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  83. #if HAVE_FASTMATH_H && !__native_client__ && !EMSCRIPTEN
  84. pf2[i] = f_cosf(pf[i]);
  85. #else
  86. pf2[i] = cosf(pf[i]);
  87. #endif
  88. result[4] += timer.Get();
  89. /* Lol cos */
  90. timer.Get();
  91. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  92. pf2[i] = lol_cos(pf[i]);
  93. result[5] += timer.Get();
  94. /* Sin & cos */
  95. timer.Get();
  96. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  97. {
  98. #if __GNUC__ && !__SNC__
  99. pf2[i] = __builtin_sinf(pf[i]);
  100. pf3[i] = __builtin_cosf(pf[i]);
  101. #else
  102. pf2[i] = sinf(pf[i]);
  103. pf3[i] = cosf(pf[i]);
  104. #endif
  105. }
  106. result[6] += timer.Get();
  107. /* Fast sin & cos */
  108. timer.Get();
  109. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  110. {
  111. #if HAVE_FASTMATH_H && !__native_client__ && !EMSCRIPTEN
  112. pf2[i] = f_sinf(pf[i]);
  113. pf3[i] = f_cosf(pf[i]);
  114. #else
  115. pf2[i] = sinf(pf[i]);
  116. pf3[i] = cosf(pf[i]);
  117. #endif
  118. }
  119. result[7] += timer.Get();
  120. /* Lol sincos */
  121. timer.Get();
  122. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  123. lol_sincos(pf[i], &pf2[i], &pf3[i]);
  124. result[8] += timer.Get();
  125. /* Tan */
  126. timer.Get();
  127. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  128. #if __GNUC__ && !__SNC__
  129. pf2[i] = __builtin_tanf(pf[i]);
  130. #else
  131. pf2[i] = tanf(pf[i]);
  132. #endif
  133. result[9] += timer.Get();
  134. /* Fast tan */
  135. timer.Get();
  136. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  137. #if HAVE_FASTMATH_H && !__native_client__ && !EMSCRIPTEN
  138. pf2[i] = f_tanf(pf[i]);
  139. #else
  140. pf2[i] = tanf(pf[i]);
  141. #endif
  142. result[10] += timer.Get();
  143. /* Lol tan */
  144. timer.Get();
  145. for (size_t i = 0; i < TRIG_TABLE_SIZE; i++)
  146. pf2[i] = lol_tan(pf[i]);
  147. result[11] += timer.Get();
  148. }
  149. delete[] pf;
  150. delete[] pf2;
  151. delete[] pf3;
  152. for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
  153. result[i] *= 1e9f / (TRIG_TABLE_SIZE * TRIG_RUNS);
  154. msg::info(" ns/elem\n");
  155. msg::info("float = sinf(float) %7.3f\n", result[0]);
  156. msg::info("float = f_sinf(float) %7.3f\n", result[1]);
  157. msg::info("float = lol_sin(float) %7.3f\n", result[2]);
  158. msg::info("float = cosf(float) %7.3f\n", result[3]);
  159. msg::info("float = f_cosf(float) %7.3f\n", result[4]);
  160. msg::info("float = lol_cos(float) %7.3f\n", result[5]);
  161. msg::info("float = sinf,cosf(float) %7.3f\n", result[6]);
  162. msg::info("float = f_sinf,f_cosf(float) %7.3f\n", result[7]);
  163. msg::info("float = lol_sincos(float) %7.3f\n", result[8]);
  164. msg::info("float = tanf(float) %7.3f\n", result[9]);
  165. msg::info("float = f_tanf(float) %7.3f\n", result[10]);
  166. msg::info("float = lol_tanf(float) %7.3f\n", result[11]);
  167. }