Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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