您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

127 行
3.6 KiB

  1. //
  2. // Lol Engine — Benchmark program
  3. //
  4. // Copyright: © 2005—2015 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the Do What The Fuck You Want To
  8. // Public License, Version 2, as published by Sam Hocevar. See
  9. // http://www.wtfpl.net/ for more details.
  10. //
  11. #if HAVE_CONFIG_H
  12. # include "config.h"
  13. #endif
  14. #include <cstdio>
  15. #include <lol/engine.h>
  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. msg::info(" ns/elem\n");
  94. msg::info("float = half (array) %7.3f\n", result[0]);
  95. msg::info("float = half (fast) %7.3f\n", result[1]);
  96. msg::info("float = float %7.3f\n", result[2]);
  97. msg::info("float += half %7.3f\n", result[3]);
  98. msg::info("half = half %7.3f\n", result[4]);
  99. msg::info("half = -half %7.3f\n", result[5]);
  100. msg::info("half = float (array) %7.3f\n", result[6]);
  101. msg::info("half = float (fast) %7.3f\n", result[7]);
  102. msg::info("half = float (accurate) %7.3f\n", result[8]);
  103. msg::info("half += float %7.3f\n", result[9]);
  104. }