mirror of
https://github.com/bkaradzic/bx.git
synced 2026-02-20 22:03:13 +01:00
Added bx::sinCosApprox bench.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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.
|
||||
///
|
||||
|
||||
@@ -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 >("bx::sqrtRef");
|
||||
mathTest<bx::sqrtSimd >("bx::sqrtSimd");
|
||||
mathTest<bx::sqrt >("bx::sqrt");
|
||||
g_result += mathTest< ::sqrtf >(" ::sqrtf");
|
||||
g_result += mathTest<bx::sqrtRef >("bx::sqrtRef");
|
||||
g_result += mathTest<bx::sqrtSimd >("bx::sqrtSimd");
|
||||
g_result += mathTest<bx::sqrt >("bx::sqrt");
|
||||
|
||||
bx::write(writer, &err, "\n");
|
||||
mathTest< ::rsqrt >(" ::rsqrtf");
|
||||
mathTest<bx::rsqrtRef >("bx::rsqrtRef");
|
||||
mathTest<bx::rsqrtSimd>("bx::rsqrtSimd");
|
||||
mathTest<bx::rsqrt >("bx::rsqrt");
|
||||
g_result += mathTest< ::rsqrt >(" ::rsqrtf");
|
||||
g_result += mathTest<bx::rsqrtRef >("bx::rsqrtRef");
|
||||
g_result += mathTest<bx::rsqrtSimd>("bx::rsqrtSimd");
|
||||
g_result += mathTest<bx::rsqrt >("bx::rsqrt");
|
||||
|
||||
bx::write(writer, &err, "\n");
|
||||
mathTest< ::sinf >(" ::sinf");
|
||||
mathTest<bx::sin >("bx::sin");
|
||||
g_result += sinCosNonApproxBench();
|
||||
g_result += sinCosApproxBench();
|
||||
|
||||
bx::write(writer, &err, "\n");
|
||||
mathTest< ::sinhf>(" ::sinhf");
|
||||
mathTest<bx::sinh >("bx::sinh");
|
||||
g_result += mathTest< ::sinf >(" ::sinf");
|
||||
g_result += mathTest<bx::sin >("bx::sin");
|
||||
|
||||
bx::write(writer, &err, "\n");
|
||||
mathTest< ::asinf>(" ::asinf");
|
||||
mathTest<bx::asin >("bx::asin");
|
||||
g_result += mathTest< ::sinhf>(" ::sinhf");
|
||||
g_result += mathTest<bx::sinh >("bx::sinh");
|
||||
|
||||
bx::write(writer, &err, "\n");
|
||||
mathTest< ::cosf >(" ::cosf");
|
||||
mathTest<bx::cos >("bx::cos");
|
||||
g_result += mathTest< ::asinf>(" ::asinf");
|
||||
g_result += mathTest<bx::asin >("bx::asin");
|
||||
|
||||
bx::write(writer, &err, "\n");
|
||||
mathTest< ::coshf>(" ::coshf");
|
||||
mathTest<bx::cosh >("bx::cosh");
|
||||
g_result += mathTest< ::cosf >(" ::cosf");
|
||||
g_result += mathTest<bx::cos >("bx::cos");
|
||||
|
||||
bx::write(writer, &err, "\n");
|
||||
mathTest< ::acosf>(" ::acosf");
|
||||
mathTest<bx::acos >("bx::acos");
|
||||
g_result += mathTest< ::coshf>(" ::coshf");
|
||||
g_result += mathTest<bx::cosh >("bx::cosh");
|
||||
|
||||
bx::write(writer, &err, "\n");
|
||||
mathTest< ::tanf >(" ::tanf");
|
||||
mathTest<bx::tan >("bx::tan");
|
||||
g_result += mathTest< ::acosf>(" ::acosf");
|
||||
g_result += mathTest<bx::acos >("bx::acos");
|
||||
|
||||
bx::write(writer, &err, "\n");
|
||||
mathTest< ::tanhf>(" ::tanhf");
|
||||
mathTest<bx::tanh >("bx::tanh");
|
||||
g_result += mathTest< ::tanf >(" ::tanf");
|
||||
g_result += mathTest<bx::tan >("bx::tan");
|
||||
|
||||
bx::write(writer, &err, "\n");
|
||||
mathTest< ::atanf>(" ::atanf");
|
||||
mathTest<bx::atan >("bx::atan");
|
||||
g_result += mathTest< ::tanhf>(" ::tanhf");
|
||||
g_result += mathTest<bx::tanh >("bx::tanh");
|
||||
|
||||
bx::write(writer, &err, "\n");
|
||||
mathTest< ::expf>(" ::expf");
|
||||
mathTest<bx::exp >("bx::exp");
|
||||
g_result += mathTest< ::atanf>(" ::atanf");
|
||||
g_result += mathTest<bx::atan >("bx::atan");
|
||||
|
||||
bx::write(writer, &err, "\n");
|
||||
mathTest< ::exp2f>(" ::exp2f");
|
||||
mathTest<bx::exp2 >("bx::exp2");
|
||||
g_result += mathTest< ::expf>(" ::expf");
|
||||
g_result += mathTest<bx::exp >("bx::exp");
|
||||
|
||||
bx::write(writer, &err, "\n");
|
||||
mathTest< ::logf >(" ::logf");
|
||||
mathTest<bx::log >("bx::log");
|
||||
g_result += mathTest< ::exp2f>(" ::exp2f");
|
||||
g_result += mathTest<bx::exp2 >("bx::exp2");
|
||||
|
||||
bx::write(writer, &err, "\n");
|
||||
mathTest< ::log2f>(" ::log2f");
|
||||
mathTest<bx::log2 >("bx::log2");
|
||||
g_result += mathTest< ::logf >(" ::logf");
|
||||
g_result += mathTest<bx::log >("bx::log");
|
||||
|
||||
bx::write(writer, &err, "\n");
|
||||
g_result += mathTest< ::log2f>(" ::log2f");
|
||||
g_result += mathTest<bx::log2 >("bx::log2");
|
||||
}
|
||||
|
||||
@@ -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) );
|
||||
|
||||
Reference in New Issue
Block a user