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

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