math_bench.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright 2010-2018 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  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. elapsed += bx::getHPCounter();
  22. bx::writePrintf(writer, "%-20s: %15f\n", _name, double(elapsed) );
  23. return result;
  24. }
  25. float rsqrt(float _a)
  26. {
  27. return 1.0f/::sqrtf(_a);
  28. }
  29. void math_bench()
  30. {
  31. bx::WriterI* writer = bx::getStdOut();
  32. bx::writePrintf(writer, "Math bench\n\n");
  33. mathTest< ::sqrtf >(" ::sqrtf");
  34. mathTest<bx::sqrtRef >("bx::sqrtRef");
  35. mathTest<bx::sqrtSimd >("bx::sqrtSimd");
  36. mathTest<bx::sqrt >("bx::sqrt");
  37. bx::writePrintf(writer, "\n");
  38. mathTest< ::rsqrt >(" ::rsqrtf");
  39. mathTest<bx::rsqrtRef >("bx::rsqrtRef");
  40. mathTest<bx::rsqrtSimd>("bx::rsqrtSimd");
  41. mathTest<bx::rsqrt >("bx::rsqrt");
  42. bx::writePrintf(writer, "\n");
  43. mathTest< ::sinf >(" ::sinf");
  44. mathTest<bx::sin >("bx::sin");
  45. bx::writePrintf(writer, "\n");
  46. mathTest< ::asinf>(" ::asinf");
  47. mathTest<bx::asin >("bx::asin");
  48. bx::writePrintf(writer, "\n");
  49. mathTest< ::cosf >(" ::cosf");
  50. mathTest<bx::cos >("bx::cos");
  51. bx::writePrintf(writer, "\n");
  52. mathTest< ::acosf>(" ::acosf");
  53. mathTest<bx::acos >("bx::acos");
  54. bx::writePrintf(writer, "\n");
  55. mathTest< ::tanf >(" ::tanf");
  56. mathTest<bx::tan >("bx::tan");
  57. bx::writePrintf(writer, "\n");
  58. mathTest< ::atanf>(" ::atanf");
  59. mathTest<bx::atan >("bx::atan");
  60. }