Skip to content

Commit e9b0200

Browse files
committed
Add handling of non-finite numbers in from_dpd
1 parent f8dc430 commit e9b0200

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

include/boost/decimal/dpd_conversion.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#ifndef BOOST_DECIMAL_BUILD_MODULE
1313
#include <cstdint>
14+
#include <limits>
1415
#endif
1516

1617
namespace boost {
@@ -427,6 +428,24 @@ template <typename DecimalType = decimal32_fast>
427428
constexpr auto from_dpd_d32(std::uint32_t dpd) noexcept
428429
BOOST_DECIMAL_REQUIRES(detail::is_decimal_floating_point_v, DecimalType)
429430
{
431+
// First we check for non-finite values
432+
// Since they are in the same initial format as BID it's easy to check with our existing masks
433+
if ((dpd & detail::d32_inf_mask) == detail::d32_inf_mask)
434+
{
435+
if ((dpd & detail::d32_snan_mask) == detail::d32_snan_mask)
436+
{
437+
return std::numeric_limits<DecimalType>::signaling_NaN();
438+
}
439+
else if ((dpd & detail::d32_nan_mask) == detail::d32_nan_mask)
440+
{
441+
return std::numeric_limits<DecimalType>::quiet_NaN();
442+
}
443+
else
444+
{
445+
return std::numeric_limits<DecimalType>::infinity();
446+
}
447+
}
448+
430449
// The bit lengths are the same as used in the standard bid format
431450
const auto sign {(dpd & detail::d32_sign_mask) != 0};
432451
const auto combination_field_bits {(dpd & detail::d32_combination_field_mask) >> 26U};

0 commit comments

Comments
 (0)