Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

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