25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

half.cpp 3.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // Lol Engine — Benchmark program
  3. //
  4. // Copyright © 2005—2015 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. 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. }