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

Add in the initial support for DPD conversions #738

Merged
merged 21 commits into from
Sep 30, 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
21 changes: 18 additions & 3 deletions doc/decimal/conversions.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ https://www.boost.org/LICENSE_1_0.txt
////

[#conversions]
= Fast Type Conversions
= Bit Conversions
:idprefix: conversions_

Since we have non-IEEE 754 compliant types we offer a set of functions that allow their conversion to and from the IEEE 754 compliant BID layout.
These functions allow lossless conversion with more compact storage.
IEEE 754 specifies two different encodings for decimal floating point types: Binary Integer Significand Field (BID), and Densely Packed Decimal Significand Field (DPD).
Internally this library is implemented in the BID format for the IEEE-754 compliant types.
Should the user want to capture the bit format in BID or convert to DPD we offer a family of conversion functions: `to_bid`, `from_bid`, `to_dpd`, and `from_dpd` that allow conversion to or from the bit strings regardless of encoding.

[source, c++]
----
Expand All @@ -26,6 +27,8 @@ struct uint128

} // namespace detail

// ----- BID Conversions -----

BOOST_DECIMAL_CXX20_CONSTEXPR std::uint32_t to_bid_d32(decimal32 val) noexcept;

BOOST_DECIMAL_CXX20_CONSTEXPR std::uint32_t to_bid_d32f(decimal32_fast val) noexcept;
Expand All @@ -48,6 +51,18 @@ BOOST_DECIMAL_CXX20_CONSTEXPR T from_bid(std::uint64_t bits) noexcept;
template <typename T = decimal128>
BOOST_DECIMAL_CXX20_CONSTEXPR T from_bid(detail::uint128 bits) noexcept;

// ----- DPD Conversions -----

constexpr std::uint32_t to_dpd_d32(decimal32 val) noexcept;

constexpr std::uint32_t to_dpd_d32f(decimal32_fast val) noexcept;

template <typename T>
constexpr auto to_dpd(T val) noexcept;

template <typename T = decimal32_fast>
constexpr T from_dpd(std::uint32_t bits) noexcept;

} // namespace decimal
} // namespace boost
----
1 change: 1 addition & 0 deletions include/boost/decimal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <boost/decimal/detail/io.hpp>
#include <boost/decimal/cstdio.hpp>
#include <boost/decimal/bid_conversion.hpp>
#include <boost/decimal/dpd_conversion.hpp>

#if defined(__clang__) && !defined(__GNUC__)
# pragma clang diagnostic pop
Expand Down
4 changes: 4 additions & 0 deletions include/boost/decimal/decimal32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ BOOST_DECIMAL_EXPORT class decimal32 final // NOLINT(cppcoreguidelines-special-m
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE DecimalType>
friend constexpr auto sequential_less_impl(DecimalType lhs, DecimalType rhs) noexcept -> bool;

template <typename DecimalType>
friend constexpr auto to_dpd_d32(DecimalType val) noexcept
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_decimal_floating_point_v, DecimalType, std::uint32_t);

public:
// 3.2.2.1 construct/copy/destroy:
constexpr decimal32() noexcept = default;
Expand Down
4 changes: 4 additions & 0 deletions include/boost/decimal/decimal32_fast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ class decimal32_fast final
-> std::enable_if_t<(detail::is_decimal_floating_point_v<Decimal1> &&
detail::is_decimal_floating_point_v<Decimal2>), bool>;

template <typename DecimalType>
friend constexpr auto to_dpd_d32(DecimalType val) noexcept
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_decimal_floating_point_v, DecimalType, std::uint32_t);

public:
constexpr decimal32_fast() noexcept {}

Expand Down
Loading
Loading