Skip to content

Commit b2798ed

Browse files
authored
Merge pull request #518 from cppalliance/normalize
2 parents 47cb147 + dc6068d commit b2798ed

File tree

6 files changed

+189
-131
lines changed

6 files changed

+189
-131
lines changed

doc/decimal/benchmarks.adoc

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ An example on Linux with b2: `../../../b2 cxxstd=20 toolset=gcc-13 define=BOOST_
2222

2323
== Comparisons
2424

25-
The benchmark for comparisons generates a random vector containing 2,000,000 elements and does operations `>`, `>=`, `<`, `<=`, `==`, and `!=` between `vec[i] and vec[i + 1]`.
25+
The benchmark for comparisons generates a random vector containing 2,000,000 elements and does operations `>`, `>=`, `<`, `\<=`, `==`, and `!=` between `vec[i] and vec[i + 1]`.
2626
This is repeated 5 times to generate stable results.
2727

2828
=== M1 macOS Results
@@ -32,20 +32,20 @@ Run using a Macbook pro with M1 pro chipset running macOS Sonoma 14.4.1 and home
3232
|===
3333
| Type | Runtime (us) | Ratio to `double`
3434
| `float`
35-
| 9032
36-
| 1.589
35+
| 8764
36+
| 1.577
3737
| `double`
38-
| 5684
38+
| 5559
3939
| 1.000
4040
| `decimal32`
41-
| 285,453
42-
| 50.2204
41+
| 276,124
42+
| 49.672
4343
| `decimal64`
44-
| 352,644
45-
| 62.042
44+
| 355,999
45+
| 64.760
4646
| `decimal128`
47-
| 15,355,817
48-
| 2701.590
47+
| 989,028
48+
| 177.915
4949
|===
5050

5151
== Basic Operations
@@ -62,83 +62,83 @@ Run using a Macbook pro with M1 pro chipset running macOS Sonoma 14.4.1 and home
6262
|===
6363
| Type | Runtime (us) | Ratio to `double`
6464
| `float`
65-
| 1641
66-
| 0.965
65+
| 2113
66+
| 0.739
6767
| `double`
68-
| 1708
68+
| 2860
6969
| 1.000
7070
| `decimal32`
71-
| 378,252
72-
| 221.459
71+
| 353,836
72+
| 123.719
7373
| `decimal64`
74-
| 589,313
75-
| 345.031
74+
| 409,098
75+
| 143.041
7676
| `decimal128`
77-
| 13,829,995
78-
| 8097.190
77+
| 2,418,039
78+
| 845.468
7979
|===
8080

8181
==== Subtraction
8282

8383
|===
8484
| Type | Runtime (us) | Ratio to `double`
8585
| `float`
86-
| 3633
87-
| 2.221
86+
| 1782
87+
| 1.061
8888
| `double`
89-
| 1636
89+
| 1680
9090
| 1.000
9191
| `decimal32`
92-
| 307,765
93-
| 188.120
92+
| 293,927
93+
| 174.957
9494
| `decimal64`
95-
| 461,442
96-
| 282.055
95+
| 329,425
96+
| 196.086
9797
| `decimal128`
98-
| 11,449,306
99-
| 6998.350
98+
| 1,527,261
99+
| 909.084
100100
|===
101101

102102
==== Multiplication
103103

104104
|===
105105
| Type | Runtime (us) | Ratio to `double`
106106
| `float`
107-
| 1678
108-
| 0.523
107+
| 1691
108+
| 0.979
109109
| `double`
110-
| 3209
110+
| 1728
111111
| 1.000
112112
| `decimal32`
113-
| 310,543
114-
| 96.773
113+
| 309,117
114+
| 178.887
115115
| `decimal64`
116-
| 570,938
117-
| 177.918
116+
| 408,010
117+
| 236.117
118118
| `decimal128`
119-
| 9,434,297
120-
| 2939.95
119+
| 2,506,105
120+
| 1450.292
121121
|===
122122

123123
==== Division
124124

125125
|===
126126
| Type | Runtime (us) | Ratio to `double`
127127
| `float`
128-
| 2019
129-
| 0.565
128+
| 2058
129+
| 0.846
130130
| `double`
131-
| 3572
131+
| 2434
132132
| 1.000
133133
| `decimal32`
134-
| 322,116
135-
| 90.178
134+
| 304,852
135+
| 125.247
136136
| `decimal64`
137-
| 734,173
138-
| 205.536
137+
| 519,990
138+
| 213.636
139139
| `decimal128`
140-
| 14,592,284
141-
| 4085.19
140+
| 3,534,909
141+
| 1452.304
142142
|===
143143

144144
== Selected Special Functions
@@ -155,20 +155,20 @@ Run using a Macbook pro with M1 pro chipset running macOS Sonoma 14.4.1 and home
155155
|===
156156
| Type | Runtime (us) | Ratio to `double`
157157
| `float`
158-
| 1904
159-
| 0.565
158+
| 2021
159+
| 0.626
160160
| `double`
161-
| 3746
161+
| 3229
162162
| 1.000
163163
| `decimal32`
164-
| 5,050,241
165-
| 1341.72
164+
| 4,826,066
165+
| 1494.601
166166
| `decimal64`
167-
| 12,084,821
168-
| 3210.630
167+
| 7,780,637
168+
| 2409.612
169169
| `decimal128`
170-
| 275,779,340
171-
| 73267.60
170+
| 100,269,145
171+
| 31052.693
172172
|===
173173

174174
== `<charconv>`

include/boost/decimal/charconv.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <boost/decimal/detail/cmath/frexp10.hpp>
2020
#include <boost/decimal/detail/attributes.hpp>
2121
#include <boost/decimal/detail/countl.hpp>
22+
#include <boost/decimal/detail/remove_trailing_zeros.hpp>
2223

2324
#ifndef BOOST_DECIMAL_BUILD_MODULE
2425
#include <cstdint>
@@ -412,12 +413,11 @@ BOOST_DECIMAL_CONSTEXPR auto to_chars_fixed_impl(char* first, char* last, const
412413
// In general formatting we remove trailing 0s
413414
if (fmt == chars_format::general)
414415
{
415-
while (significand % 10 == 0)
416-
{
417-
significand /= 10;
418-
++exponent;
419-
--num_dig;
420-
}
416+
417+
const auto zeros_removal {remove_trailing_zeros(significand)};
418+
significand = zeros_removal.trimmed_number;
419+
exponent += static_cast<int>(zeros_removal.number_of_removed_zeros);
420+
num_dig -= static_cast<int>(zeros_removal.number_of_removed_zeros);
421421
}
422422
}
423423

include/boost/decimal/detail/normalize.hpp

Lines changed: 13 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -9,87 +9,31 @@
99
#include <boost/decimal/detail/integer_search_trees.hpp>
1010
#include <boost/decimal/detail/fenv_rounding.hpp>
1111
#include <boost/decimal/detail/attributes.hpp>
12+
#include <boost/decimal/detail/remove_trailing_zeros.hpp>
1213

1314
namespace boost {
1415
namespace decimal {
1516
namespace detail {
1617

1718
// Converts the significand to full precision to remove the effects of cohorts
18-
template <typename TargetDecimalType = decimal32, typename T1, typename T2,
19-
std::enable_if_t<!std::is_same<TargetDecimalType, decimal128>::value, bool> = true>
19+
template <typename TargetDecimalType = decimal32, typename T1, typename T2>
2020
constexpr auto normalize(T1& significand, T2& exp) noexcept -> void
2121
{
22-
auto digits {num_digits(significand)};
22+
constexpr auto target_precision {detail::precision_v<TargetDecimalType>};
23+
const auto digits {num_digits(significand)};
2324

24-
if (digits < detail::precision_v<TargetDecimalType>)
25+
if (digits < target_precision)
2526
{
26-
while (digits < detail::precision_v<TargetDecimalType>)
27-
{
28-
significand *= 10;
29-
--exp;
30-
++digits;
31-
}
27+
const auto zeros_needed {target_precision - digits};
28+
significand *= pow10(static_cast<T1>(zeros_needed));
29+
exp -= zeros_needed;
3230
}
33-
else if (digits > detail::precision_v<TargetDecimalType>)
31+
else if (digits > target_precision)
3432
{
35-
while (digits > detail::precision_v<TargetDecimalType> + 1)
36-
{
37-
significand /= 10;
38-
39-
#if ((defined(__GNUC__) && (__GNUC__ > 12)) && !defined(__clang__))
40-
# pragma GCC diagnostic push
41-
# pragma GCC diagnostic ignored "-Waggressive-loop-optimizations"
42-
#endif
43-
44-
++exp;
45-
46-
#if ((defined(__GNUC__) && (__GNUC__ > 12)) && !defined(__clang__))
47-
# pragma GCC diagnostic pop
48-
#endif
49-
50-
--digits;
51-
}
52-
53-
exp += detail::fenv_round<TargetDecimalType>(significand, significand < 0);
54-
}
55-
}
56-
57-
template <typename TargetDecimalType = decimal32, typename T1, typename T2,
58-
std::enable_if_t<std::is_same<TargetDecimalType, decimal128>::value, bool> = true>
59-
constexpr auto normalize(T1& significand, T2& exp) noexcept
60-
{
61-
auto digits {num_digits(significand)};
62-
63-
if (digits < detail::precision_v<decimal128>)
64-
{
65-
while (digits < detail::precision_v<decimal128>)
66-
{
67-
significand *= UINT64_C(10);
68-
--exp;
69-
++digits;
70-
}
71-
}
72-
73-
else if (digits > detail::precision_v<TargetDecimalType>)
74-
{
75-
while (digits > detail::precision_v<TargetDecimalType> + 1)
76-
{
77-
significand /= 10;
78-
79-
#if ((defined(__GNUC__) && (__GNUC__ > 12)) && !defined(__clang__))
80-
# pragma GCC diagnostic push
81-
# pragma GCC diagnostic ignored "-Waggressive-loop-optimizations"
82-
#endif
83-
84-
++exp;
85-
86-
#if ((defined(__GNUC__) && (__GNUC__ > 12)) && !defined(__clang__))
87-
# pragma GCC diagnostic pop
88-
#endif
89-
90-
--digits;
91-
}
92-
33+
const auto excess_digits {digits - (target_precision + 1)};
34+
significand /= pow10(static_cast<T1>(excess_digits));
35+
exp += excess_digits;
36+
// Perform final rounding according to the fenv rounding mode
9337
exp += detail::fenv_round<TargetDecimalType>(significand, significand < 0);
9438
}
9539
}

0 commit comments

Comments
 (0)