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.

198 rader
5.5 KiB

  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 && !defined __native_client__
  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 && !defined __native_client__
  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 && !defined __native_client__
  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 && !defined __native_client__
  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. }