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.

126 rader
3.7 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. #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());
  33. break;
  34. case 2:
  35. for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++)
  36. ph[i] = RandF(-2.0f, 2.0f);
  37. break;
  38. }
  39. /* Copy float */
  40. timer.Get();
  41. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  42. pf[i] = pf[i + 1];
  43. result[0] += timer.Get();
  44. /* Convert half to float (array) */
  45. timer.Get();
  46. half::convert(pf, ph, HALF_TABLE_SIZE);
  47. result[1] += timer.Get();
  48. /* Convert half to float (fast) */
  49. timer.Get();
  50. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  51. pf[i] = (float)ph[i];
  52. result[2] += timer.Get();
  53. /* Add a half to every float */
  54. timer.Get();
  55. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  56. pf[i] += ph[i];
  57. result[3] += timer.Get();
  58. /* Copy half */
  59. timer.Get();
  60. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  61. ph[i] = ph[i + 1];
  62. result[4] += timer.Get();
  63. /* Change sign of every half */
  64. timer.Get();
  65. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  66. ph[i] = -ph[i];
  67. result[5] += timer.Get();
  68. /* Convert float to half (array) */
  69. timer.Get();
  70. half::convert(ph, pf, HALF_TABLE_SIZE);
  71. result[6] += timer.Get();
  72. /* Convert float to half (fast) */
  73. timer.Get();
  74. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  75. ph[i] = (half)pf[i];
  76. result[7] += timer.Get();
  77. /* Convert float to half (accurate) */
  78. timer.Get();
  79. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  80. ph[i] = half::makeaccurate(pf[i]);
  81. result[8] += timer.Get();
  82. /* Add a float to every half */
  83. timer.Get();
  84. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  85. ph[i] += pf[i];
  86. result[9] += timer.Get();
  87. }
  88. delete[] pf;
  89. delete[] ph;
  90. for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
  91. result[i] *= 1e9f / (HALF_TABLE_SIZE * HALF_RUNS);
  92. Log::Info(" ns/elem\n");
  93. Log::Info("float = float %7.3f\n", result[0]);
  94. Log::Info("float = half (array) %7.3f\n", result[1]);
  95. Log::Info("float = half (fast) %7.3f\n", result[2]);
  96. Log::Info("float += half %7.3f\n", result[3]);
  97. Log::Info("half = half %7.3f\n", result[4]);
  98. Log::Info("half = -half %7.3f\n", result[5]);
  99. Log::Info("half = float (array) %7.3f\n", result[6]);
  100. Log::Info("half = float (fast) %7.3f\n", result[7]);
  101. Log::Info("half = float (accurate) %7.3f\n", result[8]);
  102. Log::Info("half += float %7.3f\n", result[9]);
  103. }