You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

110 lines
3.0 KiB

  1. //
  2. // Lol Engine — Benchmark program
  3. //
  4. // Copyright © 2005—2020 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. #include <lol/msg>
  18. using namespace lol;
  19. static size_t const HALF_TABLE_SIZE = 1024 * 1024;
  20. static size_t const HALF_RUNS = 50;
  21. void bench_half(int mode)
  22. {
  23. float result[7] = { 0.0f };
  24. lol::timer timer;
  25. /* Set up tables */
  26. float *pf = new float[HALF_TABLE_SIZE + 1];
  27. half *ph = new half[HALF_TABLE_SIZE + 1];
  28. for (size_t run = 0; run < HALF_RUNS; run++)
  29. {
  30. switch (mode)
  31. {
  32. case 1:
  33. for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++)
  34. ph[i] = half::frombits(rand<uint16_t>());
  35. break;
  36. case 2:
  37. default:
  38. for (size_t i = 0; i < HALF_TABLE_SIZE + 1; i++)
  39. ph[i] = rand(-2.0f, 2.0f);
  40. break;
  41. }
  42. /* Convert half to float */
  43. timer.get();
  44. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  45. pf[i] = (float)ph[i];
  46. result[0] += timer.get();
  47. /* Copy float */
  48. timer.get();
  49. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  50. pf[i] = pf[i + 1];
  51. result[1] += timer.get();
  52. /* Add a half to every float */
  53. timer.get();
  54. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  55. pf[i] += ph[i];
  56. result[2] += timer.get();
  57. /* Copy half */
  58. timer.get();
  59. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  60. ph[i] = ph[i + 1];
  61. result[3] += timer.get();
  62. /* Change sign of every half */
  63. timer.get();
  64. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  65. ph[i] = -ph[i];
  66. result[4] += timer.get();
  67. /* Convert float to half (accurate) */
  68. timer.get();
  69. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  70. ph[i] = half(pf[i]);
  71. result[5] += timer.get();
  72. /* Add a float to every half */
  73. timer.get();
  74. for (size_t i = 0; i < HALF_TABLE_SIZE; i++)
  75. ph[i] += pf[i];
  76. result[6] += timer.get();
  77. }
  78. delete[] pf;
  79. delete[] ph;
  80. for (size_t i = 0; i < sizeof(result) / sizeof(*result); i++)
  81. result[i] *= 1e9f / (HALF_TABLE_SIZE * HALF_RUNS);
  82. msg::info(" ns/elem\n");
  83. msg::info("float = half %7.3f\n", result[0]);
  84. msg::info("float = float %7.3f\n", result[1]);
  85. msg::info("float += half %7.3f\n", result[2]);
  86. msg::info("half = half %7.3f\n", result[3]);
  87. msg::info("half = -half %7.3f\n", result[4]);
  88. msg::info("half = float %7.3f\n", result[5]);
  89. msg::info("half += float %7.3f\n", result[6]);
  90. }