Skip to content

Commit 6e237c6

Browse files
authored
Merge pull request #738 from cppalliance/dpd
Add in the initial support for DPD conversions
2 parents 6725d33 + db3e924 commit 6e237c6

File tree

7 files changed

+620
-3
lines changed

7 files changed

+620
-3
lines changed

doc/decimal/conversions.adoc

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ https://www.boost.org/LICENSE_1_0.txt
55
////
66

77
[#conversions]
8-
= Fast Type Conversions
8+
= Bit Conversions
99
:idprefix: conversions_
1010

11-
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.
12-
These functions allow lossless conversion with more compact storage.
11+
IEEE 754 specifies two different encodings for decimal floating point types: Binary Integer Significand Field (BID), and Densely Packed Decimal Significand Field (DPD).
12+
Internally this library is implemented in the BID format for the IEEE-754 compliant types.
13+
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.
1314

1415
[source, c++]
1516
----
@@ -26,6 +27,8 @@ struct uint128
2627
2728
} // namespace detail
2829
30+
// ----- BID Conversions -----
31+
2932
BOOST_DECIMAL_CXX20_CONSTEXPR std::uint32_t to_bid_d32(decimal32 val) noexcept;
3033
3134
BOOST_DECIMAL_CXX20_CONSTEXPR std::uint32_t to_bid_d32f(decimal32_fast val) noexcept;
@@ -48,6 +51,18 @@ BOOST_DECIMAL_CXX20_CONSTEXPR T from_bid(std::uint64_t bits) noexcept;
4851
template <typename T = decimal128>
4952
BOOST_DECIMAL_CXX20_CONSTEXPR T from_bid(detail::uint128 bits) noexcept;
5053
54+
// ----- DPD Conversions -----
55+
56+
constexpr std::uint32_t to_dpd_d32(decimal32 val) noexcept;
57+
58+
constexpr std::uint32_t to_dpd_d32f(decimal32_fast val) noexcept;
59+
60+
template <typename T>
61+
constexpr auto to_dpd(T val) noexcept;
62+
63+
template <typename T = decimal32_fast>
64+
constexpr T from_dpd(std::uint32_t bits) noexcept;
65+
5166
} // namespace decimal
5267
} // namespace boost
5368
----

include/boost/decimal.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <boost/decimal/detail/io.hpp>
4141
#include <boost/decimal/cstdio.hpp>
4242
#include <boost/decimal/bid_conversion.hpp>
43+
#include <boost/decimal/dpd_conversion.hpp>
4344

4445
#if defined(__clang__) && !defined(__GNUC__)
4546
# pragma clang diagnostic pop

include/boost/decimal/decimal32.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ BOOST_DECIMAL_EXPORT class decimal32 final // NOLINT(cppcoreguidelines-special-m
218218
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE DecimalType>
219219
friend constexpr auto sequential_less_impl(DecimalType lhs, DecimalType rhs) noexcept -> bool;
220220

221+
template <typename DecimalType>
222+
friend constexpr auto to_dpd_d32(DecimalType val) noexcept
223+
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_decimal_floating_point_v, DecimalType, std::uint32_t);
224+
221225
public:
222226
// 3.2.2.1 construct/copy/destroy:
223227
constexpr decimal32() noexcept = default;

include/boost/decimal/decimal32_fast.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ class decimal32_fast final
114114
-> std::enable_if_t<(detail::is_decimal_floating_point_v<Decimal1> &&
115115
detail::is_decimal_floating_point_v<Decimal2>), bool>;
116116

117+
template <typename DecimalType>
118+
friend constexpr auto to_dpd_d32(DecimalType val) noexcept
119+
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_decimal_floating_point_v, DecimalType, std::uint32_t);
120+
117121
public:
118122
constexpr decimal32_fast() noexcept {}
119123

0 commit comments

Comments
 (0)