Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor decimal32 FMA implementation #555

Merged
merged 2 commits into from
May 14, 2024
Merged
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
65 changes: 0 additions & 65 deletions include/boost/decimal/decimal32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2400,71 +2400,6 @@ constexpr auto copysignd32(decimal32 mag, decimal32 sgn) noexcept -> decimal32
return mag;
}

constexpr auto fmad32(decimal32 x, decimal32 y, decimal32 z) noexcept -> decimal32
{
// First calculate x * y without rounding
constexpr decimal32 zero {0, 0};

const auto res {detail::check_non_finite(x, y)};
if (res != zero)
{
return res;
}

auto sig_lhs {x.full_significand()};
auto exp_lhs {x.biased_exponent()};
detail::normalize(sig_lhs, exp_lhs);

auto sig_rhs {y.full_significand()};
auto exp_rhs {y.biased_exponent()};
detail::normalize(sig_rhs, exp_rhs);

auto mul_result {mul_impl(sig_lhs, exp_lhs, x.isneg(), sig_rhs, exp_rhs, y.isneg())};
const decimal32 dec_result {mul_result.sig, mul_result.exp, mul_result.sign};

const auto res_add {detail::check_non_finite(dec_result, z)};
if (res_add != zero)
{
return res_add;
}

bool lhs_bigger {dec_result > z};
if (dec_result.isneg() && z.isneg())
{
lhs_bigger = !lhs_bigger;
}
bool abs_lhs_bigger {abs(dec_result) > abs(z)};

detail::normalize(mul_result.sig, mul_result.exp);

auto sig_z {z.full_significand()};
auto exp_z {z.biased_exponent()};
detail::normalize(sig_z, exp_z);
detail::decimal32_components z_components {sig_z, exp_z, z.isneg()};

if (!lhs_bigger)
{
detail::swap(mul_result, z_components);
abs_lhs_bigger = !abs_lhs_bigger;
}

detail::decimal32_components result {};

if (!mul_result.sign && z_components.sign)
{
result = sub_impl(mul_result.sig, mul_result.exp, mul_result.sign,
z_components.sig, z_components.exp, z_components.sign,
abs_lhs_bigger);
}
else
{
result = add_impl(mul_result.sig, mul_result.exp, mul_result.sign,
z_components.sig, z_components.exp, z_components.sign);
}

return {result.sig, result.exp, result.sign};
}

} // namespace decimal
} // namespace boost

Expand Down
69 changes: 69 additions & 0 deletions include/boost/decimal/detail/cmath/fma.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,75 @@
namespace boost {
namespace decimal {

constexpr auto fmad32(decimal32 x, decimal32 y, decimal32 z) noexcept -> decimal32
{
// First calculate x * y without rounding
constexpr decimal32 zero {0, 0};

const auto res {detail::check_non_finite(x, y)};
if (res != zero)
{
return res;
}

auto sig_lhs {x.full_significand()};
auto exp_lhs {x.biased_exponent()};
detail::normalize(sig_lhs, exp_lhs);

auto sig_rhs {y.full_significand()};
auto exp_rhs {y.biased_exponent()};
detail::normalize(sig_rhs, exp_rhs);

auto mul_result {mul_impl(sig_lhs, exp_lhs, x.isneg(), sig_rhs, exp_rhs, y.isneg())};
const decimal32 dec_result {mul_result.sig, mul_result.exp, mul_result.sign};

const auto res_add {detail::check_non_finite(dec_result, z)};
if (res_add != zero)
{
return res_add;
}

bool lhs_bigger {dec_result > z};
if (dec_result.isneg() && z.isneg())
{
lhs_bigger = !lhs_bigger;
}
bool abs_lhs_bigger {abs(dec_result) > abs(z)};

// To avoid the rounding step we promote the constituent pieces to the next higher type
detail::decimal64_components promoted_mul_result {static_cast<std::uint64_t>(mul_result.sig),
mul_result.exp, mul_result.sign};

detail::normalize<decimal64>(promoted_mul_result.sig, promoted_mul_result.exp);

auto sig_z {static_cast<std::uint64_t>(z.full_significand())};
auto exp_z {z.biased_exponent()};
detail::normalize<decimal64>(sig_z, exp_z);
detail::decimal64_components z_components {sig_z, exp_z, z.isneg()};

if (!lhs_bigger)
{
detail::swap(promoted_mul_result, z_components);
abs_lhs_bigger = !abs_lhs_bigger;
}

detail::decimal64_components result {};

if (!promoted_mul_result.sign && z_components.sign)
{
result = d64_sub_impl(promoted_mul_result.sig, promoted_mul_result.exp, promoted_mul_result.sign,
z_components.sig, z_components.exp, z_components.sign,
abs_lhs_bigger);
}
else
{
result = d64_add_impl(promoted_mul_result.sig, promoted_mul_result.exp, promoted_mul_result.sign,
z_components.sig, z_components.exp, z_components.sign);
}

return {result.sig, result.exp, result.sign};
}

BOOST_DECIMAL_EXPORT constexpr auto fma(decimal32 x, decimal32 y, decimal32 z) noexcept -> decimal32
{
return fmad32(x, y, z);
Expand Down
2 changes: 1 addition & 1 deletion test/test_cmath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ void test_fma()
BOOST_TEST_EQ(Dec(1, 0) + Dec(1, 0, true), Dec(0, 0));
BOOST_TEST_EQ(fma(Dec(1, -1), Dec(1, 1), Dec(1, 0, true)), Dec(0, 0));

std::uniform_real_distribution<double> dist(-1e10, 1e10);
std::uniform_real_distribution<double> dist(-1e3, 1e3);

constexpr auto max_iter {std::is_same<Dec, decimal128>::value ? N / 4 : N};
for (std::size_t n {}; n < max_iter; ++n)
Expand Down
Loading