Skip to content

Commit c24dd80

Browse files
authored
Merge pull request #1159 from cppalliance/refactor_quantum
2 parents c9e2349 + 2791693 commit c24dd80

File tree

9 files changed

+50
-93
lines changed

9 files changed

+50
-93
lines changed

include/boost/decimal/charconv.hpp

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <boost/decimal/detail/countl.hpp>
2727
#include <boost/decimal/detail/remove_trailing_zeros.hpp>
2828
#include <boost/decimal/detail/promotion.hpp>
29-
#include <boost/decimal/detail/quantum_preservation_format.hpp>
3029

3130
#ifndef BOOST_DECIMAL_BUILD_MODULE
3231
#include <cstdint>
@@ -1110,9 +1109,19 @@ constexpr auto to_chars_hex_impl(char* first, char* last, const TargetDecimalTyp
11101109
return to_chars_integer_impl<std::uint32_t>(first, last, static_cast<std::uint32_t>(abs_exp));
11111110
}
11121111

1112+
#ifdef _MSC_VER
1113+
# pragma warning(push)
1114+
# pragma warning(disable: 4702) // Unreachable code
1115+
#endif
1116+
11131117
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetDecimalType>
11141118
constexpr auto to_chars_cohort_preserving_scientific(char* first, char* last, const TargetDecimalType& value) noexcept -> to_chars_result
11151119
{
1120+
BOOST_DECIMAL_IF_CONSTEXPR (detail::is_fast_type_v<TargetDecimalType>)
1121+
{
1122+
return {last, std::errc::invalid_argument};
1123+
}
1124+
11161125
using unsigned_integer = typename TargetDecimalType::significand_type;
11171126

11181127
const auto fp = fpclassify(value);
@@ -1171,11 +1180,6 @@ constexpr auto to_chars_cohort_preserving_scientific(char* first, char* last, co
11711180
return to_chars_integer_impl<std::uint32_t>(first, last, abs_exp);
11721181
}
11731182

1174-
#ifdef _MSC_VER
1175-
# pragma warning(push)
1176-
# pragma warning(disable: 4702) // Unreachable code
1177-
#endif
1178-
11791183
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetDecimalType>
11801184
constexpr auto to_chars_impl(char* first, char* last, const TargetDecimalType& value, const chars_format fmt = chars_format::general, const int local_precision = -1) noexcept -> to_chars_result
11811185
{
@@ -1212,6 +1216,20 @@ constexpr auto to_chars_impl(char* first, char* last, const TargetDecimalType& v
12121216
return to_chars_scientific_impl(first, last, value, fmt);
12131217
case chars_format::hex:
12141218
return to_chars_hex_impl(first, last, value);
1219+
case chars_format::cohort_preserving_scientific:
1220+
BOOST_DECIMAL_IF_CONSTEXPR (detail::is_fast_type_v<TargetDecimalType>)
1221+
{
1222+
// Fast types have no concept of cohorts
1223+
return {last, std::errc::invalid_argument};
1224+
}
1225+
1226+
if (local_precision != -1)
1227+
{
1228+
// Precision and cohort preservation are mutually exclusive options
1229+
return {last, std::errc::invalid_argument};
1230+
}
1231+
1232+
return to_chars_cohort_preserving_scientific(first, last, value);
12151233
// LCOV_EXCL_START
12161234
default:
12171235
BOOST_DECIMAL_UNREACHABLE;
@@ -1243,34 +1261,6 @@ constexpr auto to_chars_impl(char* first, char* last, const TargetDecimalType& v
12431261
return to_chars_scientific_impl(first, last, value, fmt, local_precision); // LCOV_EXCL_LINE
12441262
}
12451263

1246-
// TODO(mborland): Remove once other modes are inplace
1247-
#ifdef _MSC_VER
1248-
# pragma warning(push)
1249-
# pragma warning(disable: 4065)
1250-
#endif
1251-
1252-
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetDecimalType>
1253-
constexpr auto to_chars_impl(char* first, char* last, const TargetDecimalType& value, const chars_format fmt, const quantum_preservation method) noexcept -> to_chars_result
1254-
{
1255-
// No quantum preservation is the same thing as regular to_chars
1256-
if (method == quantum_preservation::off)
1257-
{
1258-
return to_chars_impl(first, last, value, fmt);
1259-
}
1260-
1261-
// Sanity check our bounds
1262-
if (BOOST_DECIMAL_UNLIKELY(first >= last))
1263-
{
1264-
return {last, std::errc::invalid_argument};
1265-
}
1266-
1267-
switch (fmt)
1268-
{
1269-
default:
1270-
return to_chars_cohort_preserving_scientific(first, last, value);
1271-
}
1272-
}
1273-
12741264
#ifdef _MSC_VER
12751265
# pragma warning(pop)
12761266
#endif
@@ -1300,13 +1290,6 @@ constexpr auto to_chars(char* first, char* last, const TargetDecimalType& value,
13001290
return detail::to_chars_impl(first, last, value, fmt, precision);
13011291
}
13021292

1303-
BOOST_DECIMAL_EXPORT template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetDecimalType>
1304-
constexpr auto to_chars(char* first, char* last, const TargetDecimalType& value, const chars_format fmt, const quantum_preservation method) noexcept -> to_chars_result
1305-
{
1306-
static_assert(detail::is_ieee_type_v<TargetDecimalType>, "Fast types are automatically normalized, so they have no concept of quantum preservation");
1307-
return detail::to_chars_impl(first, last, value, fmt, method);
1308-
}
1309-
13101293
#ifdef BOOST_DECIMAL_HAS_STD_CHARCONV
13111294

13121295
BOOST_DECIMAL_EXPORT template <typename DecimalType>

include/boost/decimal/decimal128_t.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ BOOST_DECIMAL_EXPORT class decimal128_t final
207207

208208
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetDecimalType>
209209
friend constexpr auto detail::to_chars_hex_impl(char* first, char* last, const TargetDecimalType& value) noexcept -> to_chars_result;
210-
210+
211211
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetDecimalType>
212212
friend constexpr auto detail::to_chars_cohort_preserving_scientific(char* first, char* last, const TargetDecimalType& value) noexcept -> to_chars_result;
213213

include/boost/decimal/decimal_fast128_t.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ constexpr auto to_chars_fixed_impl(char* first, char* last, const TargetDecimalT
7070
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetDecimalType>
7171
constexpr auto to_chars_hex_impl(char* first, char* last, const TargetDecimalType& value) noexcept -> to_chars_result;
7272

73+
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetDecimalType>
74+
constexpr auto to_chars_cohort_preserving_scientific(char* first, char* last, const TargetDecimalType& value) noexcept -> to_chars_result;
75+
7376
} // namespace detail
7477

7578
#ifdef _MSC_VER
@@ -185,6 +188,9 @@ BOOST_DECIMAL_EXPORT class decimal_fast128_t final
185188
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetDecimalType>
186189
friend constexpr auto detail::to_chars_hex_impl(char* first, char* last, const TargetDecimalType& value) noexcept -> to_chars_result;
187190

191+
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetDecimalType>
192+
friend constexpr auto detail::to_chars_cohort_preserving_scientific(char* first, char* last, const TargetDecimalType& value) noexcept -> to_chars_result;
193+
188194
#if !defined(BOOST_DECIMAL_DISABLE_CLIB)
189195
constexpr decimal_fast128_t(const char* str, std::size_t len);
190196
#endif

include/boost/decimal/decimal_fast32_t.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ constexpr auto to_chars_fixed_impl(char* first, char* last, const TargetDecimalT
6565
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetDecimalType>
6666
constexpr auto to_chars_hex_impl(char* first, char* last, const TargetDecimalType& value) noexcept -> to_chars_result;
6767

68+
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetDecimalType>
69+
constexpr auto to_chars_cohort_preserving_scientific(char* first, char* last, const TargetDecimalType& value) noexcept -> to_chars_result;
70+
6871
template <bool checked, BOOST_DECIMAL_DECIMAL_FLOATING_TYPE T>
6972
constexpr auto d32_fma_impl(T x, T y, T z) noexcept -> T;
7073

@@ -182,6 +185,9 @@ BOOST_DECIMAL_EXPORT class decimal_fast32_t final
182185
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetDecimalType>
183186
friend constexpr auto detail::to_chars_hex_impl(char* first, char* last, const TargetDecimalType& value) noexcept -> to_chars_result;
184187

188+
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetDecimalType>
189+
friend constexpr auto detail::to_chars_cohort_preserving_scientific(char* first, char* last, const TargetDecimalType& value) noexcept -> to_chars_result;
190+
185191
template <typename DecimalType, typename T>
186192
friend constexpr auto detail::generic_div_impl(const T& lhs, const T& rhs) noexcept -> DecimalType;
187193

include/boost/decimal/decimal_fast64_t.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ constexpr auto to_chars_fixed_impl(char* first, char* last, const TargetDecimalT
6767
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetDecimalType>
6868
constexpr auto to_chars_hex_impl(char* first, char* last, const TargetDecimalType& value) noexcept -> to_chars_result;
6969

70+
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetDecimalType>
71+
constexpr auto to_chars_cohort_preserving_scientific(char* first, char* last, const TargetDecimalType& value) noexcept -> to_chars_result;
72+
7073
template <bool checked, BOOST_DECIMAL_DECIMAL_FLOATING_TYPE T>
7174
constexpr auto d64_fma_impl(T x, T y, T z) noexcept -> T;
7275

@@ -189,6 +192,9 @@ BOOST_DECIMAL_EXPORT class decimal_fast64_t final
189192
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetDecimalType>
190193
friend constexpr auto detail::to_chars_hex_impl(char* first, char* last, const TargetDecimalType& value) noexcept -> to_chars_result;
191194

195+
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetDecimalType>
196+
friend constexpr auto detail::to_chars_cohort_preserving_scientific(char* first, char* last, const TargetDecimalType& value) noexcept -> to_chars_result;
197+
192198
template <bool checked, BOOST_DECIMAL_DECIMAL_FLOATING_TYPE T>
193199
friend constexpr auto detail::d64_fma_impl(T x, T y, T z) noexcept -> T;
194200

include/boost/decimal/detail/chars_format.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ BOOST_DECIMAL_EXPORT enum class chars_format : unsigned
1717
scientific = 1 << 0,
1818
fixed = 1 << 1,
1919
hex = 1 << 2,
20+
cohort_preserving_scientific = 1 << 3,
2021
general = fixed | scientific
2122
};
2223

include/boost/decimal/detail/concepts.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,11 @@ concept fast_decimal_floating_point_type = boost::decimal::detail::is_fast_type_
267267
#define BOOST_DECIMAL_OUTPUT_ITER(I, T) boost::decimal::detail::concepts::output_iterator<I, T>
268268
#define BOOST_DECIMAL_REQUIRES_ITER(X) requires X
269269

270-
#define BOOST_DECIMAL_REQUIRES(X, T) -> T requires X<T>
271-
#define BOOST_DECIMAL_REQUIRES_TWO(X1, T1, X2, T2) -> detail::promote_args_t<T1, T2> requires X1<T1> && X2<T2>
272-
#define BOOST_DECIMAL_REQUIRES_TWO_RETURN(X1, T1, X2, T2, ReturnType) -> ReturnType requires X1<T1> && X2<T2>
273-
#define BOOST_DECIMAL_REQUIRES_THREE(X1, T1, X2, T2, X3, T3) -> detail::promote_args_t<T1, T2, T3> requires X1<T1> && X2<T2> && X3<T3>
274-
#define BOOST_DECIMAL_REQUIRES_RETURN(X, T, ReturnType) -> ReturnType requires X<T>
270+
#define BOOST_DECIMAL_REQUIRES(X, T) -> T requires (X<T>)
271+
#define BOOST_DECIMAL_REQUIRES_TWO(X1, T1, X2, T2) -> detail::promote_args_t<T1, T2> requires (X1<T1> && X2<T2>)
272+
#define BOOST_DECIMAL_REQUIRES_TWO_RETURN(X1, T1, X2, T2, ReturnType) -> ReturnType requires (X1<T1> && X2<T2>)
273+
#define BOOST_DECIMAL_REQUIRES_THREE(X1, T1, X2, T2, X3, T3) -> detail::promote_args_t<T1, T2, T3> requires (X1<T1> && X2<T2> && X3<T3>)
274+
#define BOOST_DECIMAL_REQUIRES_RETURN(X, T, ReturnType) -> ReturnType requires (X<T>)
275275

276276
#ifdef BOOST_DECIMAL_EXEC_COMPATIBLE
277277
#include <execution>

include/boost/decimal/detail/quantum_preservation_format.hpp

Lines changed: 0 additions & 29 deletions
This file was deleted.

test/test_to_chars_quantum.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,12 @@ void test_to_chars_scientific(const ResultsType& decimals, const StringsType& st
2222
for (std::size_t i {}; i < decimals.size(); ++i)
2323
{
2424
char buffer[64] {};
25-
const auto r {to_chars(buffer, buffer + sizeof(buffer), decimals[i], chars_format::scientific, quantum_preservation::on)};
25+
const auto r {to_chars(buffer, buffer + sizeof(buffer), decimals[i], chars_format::cohort_preserving_scientific)};
2626
BOOST_TEST(r);
2727
*r.ptr = '\0';
2828

2929
BOOST_TEST_CSTR_EQ(buffer, strings[i]);
3030
}
31-
32-
for (std::size_t i {}; i < decimals.size(); ++i)
33-
{
34-
char quantum_buffer[64] {};
35-
const auto r_quantum {to_chars(quantum_buffer, quantum_buffer + sizeof(quantum_buffer), decimals[i], chars_format::scientific, quantum_preservation::off)};
36-
BOOST_TEST(r_quantum);
37-
*r_quantum.ptr = '\0';
38-
39-
char buffer[64] {};
40-
const auto r {to_chars(buffer, buffer + sizeof(buffer), decimals[i], chars_format::scientific)};
41-
BOOST_TEST(r);
42-
*r.ptr = '\0';
43-
44-
BOOST_TEST_CSTR_EQ(quantum_buffer, buffer);
45-
BOOST_TEST_CSTR_EQ(quantum_buffer, strings[0]);
46-
}
4731
}
4832

4933
template <typename T>

0 commit comments

Comments
 (0)