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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <lol/main.h>
  15. using namespace lol;
  16. static size_t const HALF_TABLE_SIZE = 1024 * 1024;
  17. static size_t const HALF_RUNS = 50;
  18. void bench_half(int mode)
  19. {
  20. float result[10] = { 0.0f };
  21. Timer timer;
  22. /* Set up tables */
  23. float *pf = new float[HALF_TABLE_SIZE + 1];
  24. half *ph = new half[HALF_TABLE_SIZE + 1];
  25. for (size_t run = 0; run < HALF_RUNS; run++)
  26. {
  27. switch (mode)
  28. {
  29. case 1:
  30. for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++)
  31. ph[i] = half::makebits(rand<uint16_t>());
  32. break;
  33. case 2:
  34. default:
  35. for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++)
  36. ph[i] = rand(-2.0f, 2.0f);
  37. break;
  38. }
  39. /* Convert half to float (array) */
  40. timer.Get();
  41. half::convert(pf, ph, HALF_TABLE_SIZE);
  42. result[0] += timer.Get();
  43. /* Convert half to float (fast) */
  44. timer.Get();
  45. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  46. pf[i] = (float)ph[i];
  47. result[1] += timer.Get();
  48. /* Copy float */
  49. timer.Get();
  50. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  51. pf[i] = pf[i + 1];
  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 = half (array) %7.3f\n", result[0]);
  94. Log::Info("float = half (fast) %7.3f\n", result[1]);
  95. Log::Info("float = float %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. }