You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lol-bench.cpp 3.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #include <cstdio>
  14. #include "core.h"
  15. #include "loldebug.h"
  16. using namespace std;
  17. using namespace lol;
  18. static size_t const HALF_TABLE_SIZE = 1024 * 1024;
  19. static size_t const HALF_RUNS = 100;
  20. static void bench_half(int mode);
  21. int main(int argc, char **argv)
  22. {
  23. Log::Info("-----------------------------------\n");
  24. Log::Info("Half precision floats (random bits)\n");
  25. Log::Info("-----------------------------------\n");
  26. bench_half(1);
  27. Log::Info("---------------------------------\n");
  28. Log::Info("Half precision floats [-2.0, 2.0]\n");
  29. Log::Info("---------------------------------\n");
  30. bench_half(2);
  31. return EXIT_SUCCESS;
  32. }
  33. static void bench_half(int mode)
  34. {
  35. float result[8] = { 0.0f };
  36. Timer timer;
  37. /* Set up tables */
  38. float *pf = new float[HALF_TABLE_SIZE];
  39. half *ph = new half[HALF_TABLE_SIZE];
  40. switch (mode)
  41. {
  42. case 1:
  43. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  44. ph[i] = half::makebits(rand());
  45. break;
  46. case 2:
  47. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  48. ph[i] = RandF(-2.0f, 2.0f);
  49. break;
  50. }
  51. for (size_t run = 0; run < HALF_RUNS; run++)
  52. {
  53. /* Convert half to float (array) */
  54. timer.GetMs();
  55. half::convert(pf, ph, HALF_TABLE_SIZE);
  56. result[0] += timer.GetMs();
  57. /* Convert half to float (fast) */
  58. timer.GetMs();
  59. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  60. pf[i] = (float)ph[i];
  61. result[1] += timer.GetMs();
  62. /* Convert float to half (array) */
  63. timer.GetMs();
  64. half::convert(ph, pf, HALF_TABLE_SIZE);
  65. result[2] += timer.GetMs();
  66. /* Convert float to half (fast) */
  67. timer.GetMs();
  68. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  69. ph[i] = (half)pf[i];
  70. result[3] += timer.GetMs();
  71. /* Convert float to half (accurate) */
  72. timer.GetMs();
  73. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  74. ph[i] = half::makeaccurate(pf[i]);
  75. result[4] += timer.GetMs();
  76. /* Change sign of every half */
  77. timer.GetMs();
  78. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  79. ph[i] = -ph[i];
  80. result[5] += timer.GetMs();
  81. /* Add a half to every float */
  82. timer.GetMs();
  83. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  84. pf[i] += ph[i];
  85. result[6] += timer.GetMs();
  86. /* Add a float to every half */
  87. timer.GetMs();
  88. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  89. ph[i] += pf[i];
  90. result[7] += timer.GetMs();
  91. }
  92. delete[]pf;
  93. delete[]ph;
  94. for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
  95. result[i] *= 1000000.0f / (HALF_TABLE_SIZE * HALF_RUNS);
  96. Log::Info(" ns/elem\n");
  97. Log::Info("float[] = half[] %7.3f\n", result[0]);
  98. Log::Info("float = half %7.3f\n", result[1]);
  99. Log::Info("half[] = float[] %7.3f\n", result[2]);
  100. Log::Info("half = float (fast) %7.3f\n", result[3]);
  101. Log::Info("half = float (accurate) %7.3f\n", result[4]);
  102. Log::Info("half = -half %7.3f\n", result[5]);
  103. Log::Info("float += half %7.3f\n", result[6]);
  104. Log::Info("half += float %7.3f\n", result[7]);
  105. }