Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion hwy/contrib/math/math-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2660,7 +2660,10 @@ HWY_INLINE V Expm1(const D d, V x) {

// Reduce, approximate, and then reconstruct.
const V y = impl.ExpPoly(d, impl.ExpReduce(d, x, q));
const V z = IfThenElse(Lt(Abs(x), kLn2Over2), y,
// Reapply the sign of x on the polynomial path so a negative-zero input is
// preserved: expm1(-0) = -0 (C11 F.10.3.3), which the polynomial drops when
// it forms 0.5 * (+0) + (-0) = +0.
const V z = IfThenElse(Lt(Abs(x), kLn2Over2), Or(y, And(x, kNegZero)),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again for fixing! Rather than Or+And, please use CopySign(y, x) - this is more efficient on some platforms.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once again thanks for fixing! Rather than Or+And, please use CopySign(y, x) - this is more efficient on some platforms.

Sub(impl.LoadExpShortRange(d, Add(y, kOne), q), kOne));
return IfThenElse(Lt(x, kLowerBound), kNegOne, z);
}
Expand Down
20 changes: 20 additions & 0 deletions hwy/contrib/math/math_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,25 @@ HWY_NOINLINE void TestAllPow() {
ForFloat3264Types(ForPartialVectors<TestPow>());
}

// expm1(+/-0) must preserve the sign of zero (C11 F.10.3.3). The ULP comparator
// used by the other math tests treats +0 == -0, so check the bit pattern.
struct TestExpm1SignedZero {
template <class T, class D>
HWY_NOINLINE void operator()(T, D d) {
using TU = MakeUnsigned<T>;
const T pos0 = ConvertScalarTo<T>(0.0);
const T neg0 = ConvertScalarTo<T>(-0.0);
const T got_pos = GetLane(CallExpm1(d, Set(d, pos0)));
const T got_neg = GetLane(CallExpm1(d, Set(d, neg0)));
HWY_ASSERT_EQ(BitCastScalar<TU>(pos0), BitCastScalar<TU>(got_pos));
HWY_ASSERT_EQ(BitCastScalar<TU>(neg0), BitCastScalar<TU>(got_neg));
}
};

HWY_NOINLINE void TestAllExpm1SignedZero() {
ForFloat3264Types(ForPartialVectors<TestExpm1SignedZero>());
}

} // namespace
// NOLINTNEXTLINE(google-readability-namespace-comments)
} // namespace HWY_NAMESPACE
Expand All @@ -213,6 +232,7 @@ HWY_EXPORT_AND_TEST_P(HwyMathTest, TestAllCbrt);
HWY_EXPORT_AND_TEST_P(HwyMathTest, TestAllTgamma);
HWY_EXPORT_AND_TEST_P(HwyMathTest, TestAllLogGamma);
HWY_EXPORT_AND_TEST_P(HwyMathTest, TestAllPow);
HWY_EXPORT_AND_TEST_P(HwyMathTest, TestAllExpm1SignedZero);
HWY_AFTER_TEST();
} // namespace
} // namespace hwy
Expand Down
Loading