選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

128 行
3.7 KiB

  1. //
  2. // Lol Engine — Benchmark program
  3. //
  4. // Copyright © 2005—2018 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // This program is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #if HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include <cstdio>
  16. #include <lol/engine.h>
  17. using namespace lol;
  18. static size_t const HALF_TABLE_SIZE = 1024 * 1024;
  19. static size_t const HALF_RUNS = 50;
  20. void bench_half(int mode)
  21. {
  22. float result[10] = { 0.0f };
  23. lol::timer timer;
  24. /* Set up tables */
  25. float *pf = new float[HALF_TABLE_SIZE + 1];
  26. half *ph = new half[HALF_TABLE_SIZE + 1];
  27. for (size_t run = 0; run < HALF_RUNS; run++)
  28. {
  29. switch (mode)
  30. {
  31. case 1:
  32. for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++)
  33. ph[i] = half::makebits(rand<uint16_t>());
  34. break;
  35. case 2:
  36. default:
  37. for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++)
  38. ph[i] = rand(-2.0f, 2.0f);
  39. break;
  40. }
  41. /* Convert half to float (array) */
  42. timer.get();
  43. half::convert(pf, ph, HALF_TABLE_SIZE);
  44. result[0] += timer.get();
  45. /* Convert half to float (fast) */
  46. timer.get();
  47. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  48. pf[i] = (float)ph[i];
  49. result[1] += timer.get();
  50. /* Copy float */
  51. timer.get();
  52. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  53. pf[i] = pf[i + 1];
  54. result[2] += timer.get();
  55. /* Add a half to every float */
  56. timer.get();
  57. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  58. pf[i] += ph[i];
  59. result[3] += timer.get();
  60. /* Copy half */
  61. timer.get();
  62. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  63. ph[i] = ph[i + 1];
  64. result[4] += timer.get();
  65. /* Change sign of every half */
  66. timer.get();
  67. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  68. ph[i] = -ph[i];
  69. result[5] += timer.get();
  70. /* Convert float to half (array) */
  71. timer.get();
  72. half::convert(ph, pf, HALF_TABLE_SIZE);
  73. result[6] += timer.get();
  74. /* Convert float to half (fast) */
  75. timer.get();
  76. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  77. ph[i] = (half)pf[i];
  78. result[7] += timer.get();
  79. /* Convert float to half (accurate) */
  80. timer.get();
  81. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  82. ph[i] = half::makeaccurate(pf[i]);
  83. result[8] += timer.get();
  84. /* Add a float to every half */
  85. timer.get();
  86. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  87. ph[i] += pf[i];
  88. result[9] += timer.get();
  89. }
  90. delete[] pf;
  91. delete[] ph;
  92. for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
  93. result[i] *= 1e9f / (HALF_TABLE_SIZE * HALF_RUNS);
  94. msg::info(" ns/elem\n");
  95. msg::info("float = half (array) %7.3f\n", result[0]);
  96. msg::info("float = half (fast) %7.3f\n", result[1]);
  97. msg::info("float = float %7.3f\n", result[2]);
  98. msg::info("float += half %7.3f\n", result[3]);
  99. msg::info("half = half %7.3f\n", result[4]);
  100. msg::info("half = -half %7.3f\n", result[5]);
  101. msg::info("half = float (array) %7.3f\n", result[6]);
  102. msg::info("half = float (fast) %7.3f\n", result[7]);
  103. msg::info("half = float (accurate) %7.3f\n", result[8]);
  104. msg::info("half += float %7.3f\n", result[9]);
  105. }