Skip to content

Commit 1b49d85

Browse files
authored
Merge pull request #739 from cppalliance/simplify_bid
Reduce language requirement for constexpr BID conversions
2 parents 6e237c6 + 71fe572 commit 1b49d85

File tree

6 files changed

+111
-55
lines changed

6 files changed

+111
-55
lines changed

doc/decimal/conversions.adoc

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,54 @@ struct uint128
2929
3030
// ----- BID Conversions -----
3131
32-
BOOST_DECIMAL_CXX20_CONSTEXPR std::uint32_t to_bid_d32(decimal32 val) noexcept;
32+
constexpr std::uint32_t to_bid_d32(decimal32 val) noexcept;
3333
34-
BOOST_DECIMAL_CXX20_CONSTEXPR std::uint32_t to_bid_d32f(decimal32_fast val) noexcept;
34+
constexpr decimal32 from_bid_d32(std::uint32_t bits) noexcept;
3535
36-
BOOST_DECIMAL_CXX20_CONSTEXPR std::uint64_t to_bid_d64(decimal64 val) noexcept;
36+
constexpr std::uint32_t to_bid_d32f(decimal32_fast val) noexcept;
3737
38-
BOOST_DECIMAL_CXX20_CONSTEXPR std::uint64_t to_bid_d64f(decimal64_fast val) noexcept;
38+
constexpr decimal32_fast from_bid_d32f(std::uint32_t bits) noexcept;
3939
40-
BOOST_DECIMAL_CXX20_CONSTEXPR detail::uint128 to_bid_d128(decimal128 val) noexcept;
40+
constexpr std::uint64_t to_bid_d64(decimal64 val) noexcept;
41+
42+
constexpr decimal64 from_bid_d64(std::uint64_t bits) noexcept;
43+
44+
constexpr std::uint64_t to_bid_d64f(decimal64_fast val) noexcept;
45+
46+
constexpr decimal64_fast from_bid_d64f(std::uint64_t bits) noexcept;
47+
48+
constexpr detail::uint128 to_bid_d128(decimal128 val) noexcept;
49+
50+
constexpr decimal128 from_bid_d128(detail::uint128 bits) noexcept;
51+
52+
// Automatic detection if your platform has built-in unsigned __int128 or not to enable/disable the overload
53+
#ifdef BOOST_DECIMAL_HAS_INT128
54+
55+
constexpr decimal128 from_bid_d128(unsigned __int128 bits) noexcept;
56+
57+
#endif // BOOST_DECIMAL_HAS_INT128
58+
59+
constexpr detail::uint128 to_bid_d128f(decimal128_fast val) noexcept;
60+
61+
constexpr decimal128 from_bid_d128f(detail::uint128 bits) noexcept;
62+
63+
#ifdef BOOST_DECIMAL_HAS_INT128
64+
65+
constexpr decimal128 from_bid_d128f(unsigned __int128 bits) noexcept;
66+
67+
#endif // BOOST_DECIMAL_HAS_INT128
4168
4269
template <typename T>
43-
BOOST_DECIMAL_CXX20_CONSTEXPR auto to_bid(T val) noexcept;
70+
constexpr auto to_bid(T val) noexcept;
4471
4572
template <typename T = decimal32_fast>
46-
BOOST_DECIMAL_CXX20_CONSTEXPR T from_bid(std::uint32_t bits) noexcept;
73+
constexpr T from_bid(std::uint32_t bits) noexcept;
4774
4875
template <typename T = decimal64_fast>
49-
BOOST_DECIMAL_CXX20_CONSTEXPR T from_bid(std::uint64_t bits) noexcept;
76+
constexpr T from_bid(std::uint64_t bits) noexcept;
5077
5178
template <typename T = decimal128>
52-
BOOST_DECIMAL_CXX20_CONSTEXPR T from_bid(detail::uint128 bits) noexcept;
79+
constexpr T from_bid(detail::uint128 bits) noexcept;
5380
5481
// ----- DPD Conversions -----
5582

include/boost/decimal/bid_conversion.hpp

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,149 +20,162 @@ namespace decimal {
2020
# pragma GCC diagnostic ignored "-Wconversion"
2121
#endif
2222

23-
BOOST_DECIMAL_CXX20_CONSTEXPR auto to_bid_d32(decimal32 val) noexcept -> std::uint32_t
23+
constexpr auto to_bid_d32(decimal32 val) noexcept -> std::uint32_t
2424
{
25-
const auto bits {detail::bit_cast<std::uint32_t>(val)};
26-
return bits;
25+
return val.bits_;
2726
}
2827

29-
BOOST_DECIMAL_CXX20_CONSTEXPR auto from_bid_d32(std::uint32_t bits) noexcept -> decimal32
28+
constexpr auto from_bid_d32(std::uint32_t bits) noexcept -> decimal32
3029
{
31-
const auto val {detail::bit_cast<decimal32>(bits)};
32-
return val;
30+
return from_bits(bits);
3331
}
3432

35-
BOOST_DECIMAL_CXX20_CONSTEXPR auto to_bid_d32f(decimal32_fast val) noexcept -> std::uint32_t
33+
constexpr auto to_bid_d32f(decimal32_fast val) noexcept -> std::uint32_t
3634
{
3735
const decimal32 compliant_val {val};
38-
const auto bits {detail::bit_cast<std::uint32_t>(compliant_val)};
39-
return bits;
36+
return to_bid_d32(compliant_val);
4037
}
4138

42-
BOOST_DECIMAL_CXX20_CONSTEXPR auto from_bid_d32f(std::uint32_t bits) noexcept -> decimal32_fast
39+
constexpr auto from_bid_d32f(std::uint32_t bits) noexcept -> decimal32_fast
4340
{
44-
const auto compliant_val {detail::bit_cast<decimal32>(bits)};
41+
const auto compliant_val {from_bid_d32(bits)};
4542
const decimal32_fast val {compliant_val};
4643
return val;
4744
}
4845

49-
BOOST_DECIMAL_CXX20_CONSTEXPR auto to_bid_d64(decimal64 val) noexcept -> std::uint64_t
46+
constexpr auto to_bid_d64(decimal64 val) noexcept -> std::uint64_t
5047
{
51-
const auto bits {detail::bit_cast<std::uint64_t>(val)};
52-
return bits;
48+
return val.bits_;
5349
}
5450

55-
BOOST_DECIMAL_CXX20_CONSTEXPR auto from_bid_d64(std::uint64_t bits) noexcept -> decimal64
51+
constexpr auto from_bid_d64(std::uint64_t bits) noexcept -> decimal64
5652
{
57-
const auto val {detail::bit_cast<decimal64>(bits)};
58-
return val;
53+
return from_bits(bits);
5954
}
6055

61-
BOOST_DECIMAL_CXX20_CONSTEXPR auto to_bid_d64f(decimal64_fast val) noexcept -> std::uint64_t
56+
constexpr auto to_bid_d64f(decimal64_fast val) noexcept -> std::uint64_t
6257
{
6358
const decimal64 compliant_val {val};
64-
const auto bits {detail::bit_cast<std::uint64_t>(compliant_val)};
65-
return bits;
59+
return to_bid_d64(compliant_val);
6660
}
6761

68-
BOOST_DECIMAL_CXX20_CONSTEXPR auto from_bid_d64f(std::uint64_t bits) noexcept -> decimal64_fast
62+
constexpr auto from_bid_d64f(std::uint64_t bits) noexcept -> decimal64_fast
6963
{
70-
const auto compliant_val {detail::bit_cast<decimal64>(bits)};
64+
const auto compliant_val {from_bid_d64(bits)};
7165
const decimal64_fast val {compliant_val};
7266
return val;
7367
}
7468

75-
BOOST_DECIMAL_CXX20_CONSTEXPR auto to_bid_d128(decimal128 val) noexcept -> detail::uint128
69+
constexpr auto to_bid_d128(decimal128 val) noexcept -> detail::uint128
7670
{
77-
const auto bits {detail::bit_cast<detail::uint128>(val)};
78-
return bits;
71+
return val.bits_;
7972
}
8073

81-
BOOST_DECIMAL_CXX20_CONSTEXPR auto from_bid_d128(detail::uint128 bits) noexcept -> decimal128
74+
constexpr auto from_bid_d128(detail::uint128 bits) noexcept -> decimal128
8275
{
83-
const auto val {detail::bit_cast<decimal128>(bits)};
84-
return val;
76+
return from_bits(bits);
77+
}
78+
79+
#ifdef BOOST_DECIMAL_HAS_INT128
80+
constexpr auto from_bits_d128(detail::uint128_t bits) noexcept -> decimal128
81+
{
82+
return from_bits(bits);
8583
}
84+
#endif
8685

87-
BOOST_DECIMAL_CXX20_CONSTEXPR auto to_bid_d128f(decimal128_fast val) noexcept -> detail::uint128
86+
constexpr auto to_bid_d128f(decimal128_fast val) noexcept -> detail::uint128
8887
{
8988
const decimal128 compliant_val {val};
90-
const auto bits {detail::bit_cast<detail::uint128>(compliant_val)};
91-
return bits;
89+
return to_bid_d128(compliant_val);
90+
}
91+
92+
constexpr auto from_bid_d128f(detail::uint128 bits) noexcept -> decimal128_fast
93+
{
94+
const auto compliant_val {from_bid_d128(bits)};
95+
const decimal128_fast val {compliant_val};
96+
return val;
9297
}
9398

94-
BOOST_DECIMAL_CXX20_CONSTEXPR auto from_bid_d128f(detail::uint128 bits) noexcept -> decimal128_fast
99+
#ifdef BOOST_DECIMAL_HAS_INT128
100+
constexpr auto from_bid_d128f(detail::uint128_t bits) noexcept -> decimal128_fast
95101
{
96-
const auto compliant_val {detail::bit_cast<decimal128>(bits)};
102+
const auto compliant_val {from_bid_d128(bits)};
97103
const decimal128_fast val {compliant_val};
98104
return val;
99105
}
106+
#endif
100107

101-
BOOST_DECIMAL_CXX20_CONSTEXPR auto to_bid(decimal32 val) noexcept -> std::uint32_t
108+
constexpr auto to_bid(decimal32 val) noexcept -> std::uint32_t
102109
{
103110
return to_bid_d32(val);
104111
}
105112

106-
BOOST_DECIMAL_CXX20_CONSTEXPR auto to_bid(decimal32_fast val) noexcept -> std::uint32_t
113+
constexpr auto to_bid(decimal32_fast val) noexcept -> std::uint32_t
107114
{
108115
return to_bid_d32f(val);
109116
}
110117

111-
BOOST_DECIMAL_CXX20_CONSTEXPR auto to_bid(decimal64 val) noexcept -> std::uint64_t
118+
constexpr auto to_bid(decimal64 val) noexcept -> std::uint64_t
112119
{
113120
return to_bid_d64(val);
114121
}
115122

116-
BOOST_DECIMAL_CXX20_CONSTEXPR auto to_bid(decimal64_fast val) noexcept -> std::uint64_t
123+
constexpr auto to_bid(decimal64_fast val) noexcept -> std::uint64_t
117124
{
118125
return to_bid_d64f(val);
119126
}
120127

121-
BOOST_DECIMAL_CXX20_CONSTEXPR auto to_bid(decimal128 val) noexcept -> detail::uint128
128+
constexpr auto to_bid(decimal128 val) noexcept -> detail::uint128
122129
{
123130
return to_bid_d128(val);
124131
}
125132

126-
BOOST_DECIMAL_CXX20_CONSTEXPR auto to_bid(decimal128_fast val) noexcept -> detail::uint128
133+
constexpr auto to_bid(decimal128_fast val) noexcept -> detail::uint128
127134
{
128135
return to_bid_d128f(val);
129136
}
130137

138+
template <typename T>
139+
constexpr auto to_bid(T val) noexcept
140+
{
141+
return to_bid(val);
142+
}
143+
131144
template <typename T = decimal32_fast>
132-
BOOST_DECIMAL_CXX20_CONSTEXPR auto from_bid(std::uint32_t bits) noexcept
145+
constexpr auto from_bid(std::uint32_t bits) noexcept
133146
BOOST_DECIMAL_REQUIRES(detail::is_decimal_floating_point_v, T)
134147
{
135148
return from_bid_d32f(bits);
136149
}
137150

138151
template <>
139-
BOOST_DECIMAL_CXX20_CONSTEXPR auto from_bid<decimal32>(std::uint32_t bits) noexcept -> decimal32
152+
constexpr auto from_bid<decimal32>(std::uint32_t bits) noexcept -> decimal32
140153
{
141154
return from_bid_d32(bits);
142155
}
143156

144157
template <typename T = decimal64_fast>
145-
BOOST_DECIMAL_CXX20_CONSTEXPR auto from_bid(std::uint64_t bits) noexcept
158+
constexpr auto from_bid(std::uint64_t bits) noexcept
146159
BOOST_DECIMAL_REQUIRES(detail::is_decimal_floating_point_v, T)
147160
{
148161
return from_bid_d64f(bits);
149162
}
150163

151164
template <>
152-
BOOST_DECIMAL_CXX20_CONSTEXPR auto from_bid<decimal64>(std::uint64_t bits) noexcept -> decimal64
165+
constexpr auto from_bid<decimal64>(std::uint64_t bits) noexcept -> decimal64
153166
{
154167
return from_bid_d64(bits);
155168
}
156169

157170
template <typename T = decimal128_fast>
158-
BOOST_DECIMAL_CXX20_CONSTEXPR auto from_bid(detail::uint128 bits) noexcept
171+
constexpr auto from_bid(detail::uint128 bits) noexcept
159172
BOOST_DECIMAL_REQUIRES(detail::is_decimal_floating_point_v, T)
160173
{
161174
return from_bid_d128f(bits);
162175
}
163176

164177
template <>
165-
BOOST_DECIMAL_CXX20_CONSTEXPR auto from_bid<decimal128>(detail::uint128 bits) noexcept -> decimal128
178+
constexpr auto from_bid<decimal128>(detail::uint128 bits) noexcept -> decimal128
166179
{
167180
return from_bid_d128(bits);
168181
}

include/boost/decimal/decimal128.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,14 @@ BOOST_DECIMAL_EXPORT class decimal128 final
228228

229229
friend constexpr auto not_finite(decimal128 rhs) noexcept -> bool;
230230

231+
friend constexpr auto to_bid_d128(decimal128 val) noexcept -> detail::uint128;
232+
233+
friend constexpr auto from_bid_d128(detail::uint128 bits) noexcept -> decimal128;
234+
235+
#ifdef BOOST_DECIMAL_HAS_INT128
236+
friend constexpr auto from_bid_d128(detail::uint128_t bits) noexcept -> decimal128;
237+
#endif
238+
231239
public:
232240
// 3.2.4.1 construct/copy/destroy
233241
constexpr decimal128() noexcept = default;

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+
friend constexpr auto to_bid_d32(decimal32 val) noexcept -> std::uint32_t;
222+
223+
friend constexpr auto from_bid_d32(std::uint32_t bits) noexcept -> decimal32;
224+
221225
template <typename DecimalType>
222226
friend constexpr auto to_dpd_d32(DecimalType val) noexcept
223227
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_decimal_floating_point_v, DecimalType, std::uint32_t);

include/boost/decimal/decimal64.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ BOOST_DECIMAL_EXPORT class decimal64 final
225225
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE DecimalType>
226226
friend constexpr auto sequential_less_impl(DecimalType lhs, DecimalType rhs) noexcept -> bool;
227227

228+
friend constexpr auto to_bid_d64(decimal64 val) noexcept -> std::uint64_t;
229+
230+
friend constexpr auto from_bid_d64(std::uint64_t bits) noexcept -> decimal64;
231+
228232
public:
229233
// 3.2.3.1 construct/copy/destroy
230234
constexpr decimal64() noexcept = default;

test/test_bid_conversions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void test()
1818
for (std::size_t i {}; i < 1024; ++i)
1919
{
2020
const T val {dist(rng)};
21-
const auto bits {to_bid(val)};
21+
const auto bits {to_bid<T>(val)};
2222
const T return_val {from_bid<T>(bits)};
2323
BOOST_TEST_EQ(val, return_val);
2424
}

0 commit comments

Comments
 (0)