math_bench.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright 2010-2023 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx/blob/master/LICENSE
  4. */
  5. #include <bx/math.h>
  6. #include <bx/timer.h>
  7. #include <bx/file.h>
  8. #include <math.h>
  9. typedef float (*MathFn)(float);
  10. template<MathFn mfn>
  11. float mathTest(const char* _name)
  12. {
  13. bx::WriterI* writer = bx::getStdOut();
  14. int64_t elapsed = -bx::getHPCounter();
  15. float result = 0.0f;
  16. const float max = 1389.0f;
  17. for (float xx = 0.0f; xx < max; xx += 0.1f)
  18. {
  19. result += mfn(xx);
  20. }
  21. bx::Error err;
  22. elapsed += bx::getHPCounter();
  23. bx::write(writer, &err, "%-20s: %15f\n", _name, double(elapsed) );
  24. return result;
  25. }
  26. float rsqrt(float _a)
  27. {
  28. return 1.0f/::sqrtf(_a);
  29. }
  30. void math_bench()
  31. {
  32. bx::WriterI* writer = bx::getStdOut();
  33. bx::Error err;
  34. bx::write(writer, &err, "Math bench\n\n");
  35. mathTest< ::sqrtf >(" ::sqrtf");
  36. mathTest<bx::sqrtRef >("bx::sqrtRef");
  37. mathTest<bx::sqrtSimd >("bx::sqrtSimd");
  38. mathTest<bx::sqrt >("bx::sqrt");
  39. bx::write(writer, &err, "\n");
  40. mathTest< ::rsqrt >(" ::rsqrtf");
  41. mathTest<bx::rsqrtRef >("bx::rsqrtRef");
  42. mathTest<bx::rsqrtSimd>("bx::rsqrtSimd");
  43. mathTest<bx::rsqrt >("bx::rsqrt");
  44. bx::write(writer, &err, "\n");
  45. mathTest< ::sinf >(" ::sinf");
  46. mathTest<bx::sin >("bx::sin");
  47. bx::write(writer, &err, "\n");
  48. mathTest< ::sinhf>(" ::sinhf");
  49. mathTest<bx::sinh >("bx::sinh");
  50. bx::write(writer, &err, "\n");
  51. mathTest< ::asinf>(" ::asinf");
  52. mathTest<bx::asin >("bx::asin");
  53. bx::write(writer, &err, "\n");
  54. mathTest< ::cosf >(" ::cosf");
  55. mathTest<bx::cos >("bx::cos");
  56. bx::write(writer, &err, "\n");
  57. mathTest< ::coshf>(" ::coshf");
  58. mathTest<bx::cosh >("bx::cosh");
  59. bx::write(writer, &err, "\n");
  60. mathTest< ::acosf>(" ::acosf");
  61. mathTest<bx::acos >("bx::acos");
  62. bx::write(writer, &err, "\n");
  63. mathTest< ::tanf >(" ::tanf");
  64. mathTest<bx::tan >("bx::tan");
  65. bx::write(writer, &err, "\n");
  66. mathTest< ::tanhf>(" ::tanhf");
  67. mathTest<bx::tanh >("bx::tanh");
  68. bx::write(writer, &err, "\n");
  69. mathTest< ::atanf>(" ::atanf");
  70. mathTest<bx::atan >("bx::atan");
  71. bx::write(writer, &err, "\n");
  72. mathTest< ::expf>(" ::expf");
  73. mathTest<bx::exp >("bx::exp");
  74. bx::write(writer, &err, "\n");
  75. mathTest< ::exp2f>(" ::exp2f");
  76. mathTest<bx::exp2 >("bx::exp2");
  77. bx::write(writer, &err, "\n");
  78. mathTest< ::logf >(" ::logf");
  79. mathTest<bx::log >("bx::log");
  80. bx::write(writer, &err, "\n");
  81. mathTest< ::log2f>(" ::log2f");
  82. mathTest<bx::log2 >("bx::log2");
  83. }