From 226d52f240396c19539a22f1a8a7c2b00ad7f702 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Wed, 1 May 2024 09:15:03 -0700 Subject: [PATCH] Added bx::sinCosApprox bench. --- include/bx/inline/math.inl | 6 +- include/bx/math.h | 2 +- tests/math_bench.cpp | 123 +++++++++++++++++++++++++++---------- tests/math_test.cpp | 4 +- 4 files changed, 95 insertions(+), 40 deletions(-) diff --git a/include/bx/inline/math.inl b/include/bx/inline/math.inl index bb34d48..826b8f9 100644 --- a/include/bx/inline/math.inl +++ b/include/bx/inline/math.inl @@ -162,7 +162,7 @@ namespace bx return _a * _a; } - inline void sinCosApprox(float _a, float* _outSinApprox, float* _outCos) + inline void sinCosApprox(float& _outSinApprox, float& _outCos, float _a) { const float aa = _a - floor(_a*kInvPi2)*kPi2; const float absA = abs(aa); @@ -172,8 +172,8 @@ namespace bx const float tmp1 = aa > 0.0f && aa < kPi ? 1.0f : -1.0f; const float sinA = mul(tmp0, tmp1); - *_outSinApprox = sinA; - *_outCos = cosA; + _outSinApprox = sinA; + _outCos = cosA; } inline BX_CONST_FUNC float sin(float _a) diff --git a/include/bx/math.h b/include/bx/math.h index 0030c23..a7b5e2b 100644 --- a/include/bx/math.h +++ b/include/bx/math.h @@ -208,7 +208,7 @@ namespace bx /// @remarks The function calculates cosine, and then approximates sine based on the cosine /// result. Therefore calculation of sine is less accurate than calling `bx::sin` function. /// - void sinCosApprox(float _a, float* _outSinApprox, float* _outCos); + void sinCosApprox(float& _outSinApprox, float& _outCos, float _a); /// Returns the sine of the argument _a. /// diff --git a/tests/math_bench.cpp b/tests/math_bench.cpp index 399162f..48cec9f 100644 --- a/tests/math_bench.cpp +++ b/tests/math_bench.cpp @@ -38,72 +38,127 @@ float rsqrt(float _a) return 1.0f/::sqrtf(_a); } +float sinCosNonApproxBench() +{ + bx::WriterI* writer = bx::getStdOut(); + int64_t elapsed = -bx::getHPCounter(); + + float result = 0.0f; + const float max = 1389.0f; + + for (float xx = 0.0f; xx < max; xx += 0.1f) + { + float ss, cc; + ss = bx::sin(xx); + cc = bx::cos(xx); + + result += ss + cc; + } + + bx::Error err; + + elapsed += bx::getHPCounter(); + bx::write(writer, &err, "%-20s: %15f\n", "sinCosNonApproxBench", double(elapsed) ); + + return result; +} + +float sinCosApproxBench() +{ + bx::WriterI* writer = bx::getStdOut(); + int64_t elapsed = -bx::getHPCounter(); + + float result = 0.0f; + const float max = 1389.0f; + + for (float xx = 0.0f; xx < max; xx += 0.1f) + { + float ss, cc; + bx::sinCosApprox(ss, cc, xx); + + result += ss + cc; + } + + bx::Error err; + + elapsed += bx::getHPCounter(); + bx::write(writer, &err, "%-20s: %15f\n", "sinCosApprox", double(elapsed) ); + + return result; +} + +float g_result; // trick compiler to not discard results + void math_bench() { bx::WriterI* writer = bx::getStdOut(); bx::Error err; bx::write(writer, &err, "Math bench\n\n"); - mathTest< ::sqrtf >(" ::sqrtf"); - mathTest("bx::sqrtRef"); - mathTest("bx::sqrtSimd"); - mathTest("bx::sqrt"); + g_result += mathTest< ::sqrtf >(" ::sqrtf"); + g_result += mathTest("bx::sqrtRef"); + g_result += mathTest("bx::sqrtSimd"); + g_result += mathTest("bx::sqrt"); bx::write(writer, &err, "\n"); - mathTest< ::rsqrt >(" ::rsqrtf"); - mathTest("bx::rsqrtRef"); - mathTest("bx::rsqrtSimd"); - mathTest("bx::rsqrt"); + g_result += mathTest< ::rsqrt >(" ::rsqrtf"); + g_result += mathTest("bx::rsqrtRef"); + g_result += mathTest("bx::rsqrtSimd"); + g_result += mathTest("bx::rsqrt"); bx::write(writer, &err, "\n"); - mathTest< ::sinf >(" ::sinf"); - mathTest("bx::sin"); + g_result += sinCosNonApproxBench(); + g_result += sinCosApproxBench(); bx::write(writer, &err, "\n"); - mathTest< ::sinhf>(" ::sinhf"); - mathTest("bx::sinh"); + g_result += mathTest< ::sinf >(" ::sinf"); + g_result += mathTest("bx::sin"); bx::write(writer, &err, "\n"); - mathTest< ::asinf>(" ::asinf"); - mathTest("bx::asin"); + g_result += mathTest< ::sinhf>(" ::sinhf"); + g_result += mathTest("bx::sinh"); bx::write(writer, &err, "\n"); - mathTest< ::cosf >(" ::cosf"); - mathTest("bx::cos"); + g_result += mathTest< ::asinf>(" ::asinf"); + g_result += mathTest("bx::asin"); bx::write(writer, &err, "\n"); - mathTest< ::coshf>(" ::coshf"); - mathTest("bx::cosh"); + g_result += mathTest< ::cosf >(" ::cosf"); + g_result += mathTest("bx::cos"); bx::write(writer, &err, "\n"); - mathTest< ::acosf>(" ::acosf"); - mathTest("bx::acos"); + g_result += mathTest< ::coshf>(" ::coshf"); + g_result += mathTest("bx::cosh"); bx::write(writer, &err, "\n"); - mathTest< ::tanf >(" ::tanf"); - mathTest("bx::tan"); + g_result += mathTest< ::acosf>(" ::acosf"); + g_result += mathTest("bx::acos"); bx::write(writer, &err, "\n"); - mathTest< ::tanhf>(" ::tanhf"); - mathTest("bx::tanh"); + g_result += mathTest< ::tanf >(" ::tanf"); + g_result += mathTest("bx::tan"); bx::write(writer, &err, "\n"); - mathTest< ::atanf>(" ::atanf"); - mathTest("bx::atan"); + g_result += mathTest< ::tanhf>(" ::tanhf"); + g_result += mathTest("bx::tanh"); bx::write(writer, &err, "\n"); - mathTest< ::expf>(" ::expf"); - mathTest("bx::exp"); + g_result += mathTest< ::atanf>(" ::atanf"); + g_result += mathTest("bx::atan"); bx::write(writer, &err, "\n"); - mathTest< ::exp2f>(" ::exp2f"); - mathTest("bx::exp2"); + g_result += mathTest< ::expf>(" ::expf"); + g_result += mathTest("bx::exp"); bx::write(writer, &err, "\n"); - mathTest< ::logf >(" ::logf"); - mathTest("bx::log"); + g_result += mathTest< ::exp2f>(" ::exp2f"); + g_result += mathTest("bx::exp2"); bx::write(writer, &err, "\n"); - mathTest< ::log2f>(" ::log2f"); - mathTest("bx::log2"); + g_result += mathTest< ::logf >(" ::logf"); + g_result += mathTest("bx::log"); + + bx::write(writer, &err, "\n"); + g_result += mathTest< ::log2f>(" ::log2f"); + g_result += mathTest("bx::log2"); } diff --git a/tests/math_test.cpp b/tests/math_test.cpp index 55b7ae7..a1d6235 100644 --- a/tests/math_test.cpp +++ b/tests/math_test.cpp @@ -449,7 +449,7 @@ TEST_CASE("sinCos", "[math][libm]") for (float xx = -100.0f; xx < 100.0f; xx += 0.1f) { float ss, cc; - bx::sinCosApprox(xx, &ss, &cc); + bx::sinCosApprox(ss, cc, xx); bx::write(writer, &err, "sinCos(%f) == sin %f (expected: %f)\n", xx, ss, ::sinf(xx) ); bx::write(writer, &err, "sinCos(%f) == cos %f (expected: %f)\n", xx, cc, ::cosf(xx) ); @@ -461,7 +461,7 @@ TEST_CASE("sinCos", "[math][libm]") for (float xx = -bx::kPi2; xx < bx::kPi2; xx += 0.0001f) { float ss, cc; - bx::sinCosApprox(xx, &ss, &cc); + bx::sinCosApprox(ss, cc, xx); bx::write(writer, &err, "sinCos(%f) == sin %f (expected: %f)\n", xx, ss, ::sinf(xx) ); bx::write(writer, &err, "sinCos(%f) == cos %f (expected: %f)\n", xx, cc, ::cosf(xx) );