Skip to content

Commit e6fa5ac

Browse files
Fix erfc implementation
Kudos to Joël Falcou for helping out on that one. Fix #936
1 parent ac04b31 commit e6fa5ac

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

include/xsimd/arch/generic/xsimd_generic_math.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,16 +470,18 @@ namespace xsimd
470470
batch_type x = abs(self);
471471
auto test0 = self < batch_type(0.);
472472
batch_type r1(0.);
473+
auto test1 = 3.f * x < 2.f;
473474
batch_type z = x / (batch_type(1.) + x);
474-
if (any(3.f * x < 2.f))
475+
if (any(test1))
475476
{
476477
r1 = detail::erf_kernel<batch_type>::erfc3(z);
478+
if (all(test1))
479+
return select(test0, batch_type(2.) - r1, r1);
477480
}
478-
else
479-
{
480-
z -= batch_type(0.4f);
481-
r1 = exp(-x * x) * detail::erf_kernel<batch_type>::erfc2(z);
482-
}
481+
482+
z -= batch_type(0.4f);
483+
batch_type r2 = exp(-x * x) * detail::erf_kernel<batch_type>::erfc2(z);
484+
r1 = select(test1, r1, r2);
483485
#ifndef XSIMD_NO_INFINITIES
484486
r1 = select(x == constants::infinity<batch_type>(), batch_type(0.), r1);
485487
#endif

test/test_xsimd_api.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,11 @@ struct xsimd_api_float_types_functions
538538
void test_erfc()
539539
{
540540
// FIXME: can we do better?
541-
value_type val(0);
542-
CHECK_EQ(extract(xsimd::erfc(T(val))), doctest::Approx(std::erfc(val)).epsilon(10e-7));
541+
for (float f : { 0.f, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f })
542+
{
543+
value_type val(f);
544+
CHECK_EQ(extract(xsimd::erfc(T(val))), doctest::Approx(std::erfc(val)).epsilon(10e-8));
545+
}
543546
}
544547
void test_fabs()
545548
{

0 commit comments

Comments
 (0)