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.

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