Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

192 righe
5.2 KiB

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