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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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[7] = { 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::frombits(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 */
  42. timer.get();
  43. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  44. pf[i] = (float)ph[i];
  45. result[0] += timer.get();
  46. /* Copy float */
  47. timer.get();
  48. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  49. pf[i] = pf[i + 1];
  50. result[1] += timer.get();
  51. /* Add a half to every float */
  52. timer.get();
  53. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  54. pf[i] += ph[i];
  55. result[2] += timer.get();
  56. /* Copy half */
  57. timer.get();
  58. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  59. ph[i] = ph[i + 1];
  60. result[3] += timer.get();
  61. /* Change sign of every half */
  62. timer.get();
  63. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  64. ph[i] = -ph[i];
  65. result[4] += timer.get();
  66. /* Convert float to half (accurate) */
  67. timer.get();
  68. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  69. ph[i] = half(pf[i]);
  70. result[5] += timer.get();
  71. /* Add a float to every half */
  72. timer.get();
  73. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  74. ph[i] += pf[i];
  75. result[6] += timer.get();
  76. }
  77. delete[] pf;
  78. delete[] ph;
  79. for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
  80. result[i] *= 1e9f / (HALF_TABLE_SIZE * HALF_RUNS);
  81. msg::info(" ns/elem\n");
  82. msg::info("float = half %7.3f\n", result[0]);
  83. msg::info("float = float %7.3f\n", result[1]);
  84. msg::info("float += half %7.3f\n", result[2]);
  85. msg::info("half = half %7.3f\n", result[3]);
  86. msg::info("half = -half %7.3f\n", result[4]);
  87. msg::info("half = float %7.3f\n", result[5]);
  88. msg::info("half += float %7.3f\n", result[6]);
  89. }