Skip to content

Commit

Permalink
Merge pull request #518 from cppalliance/normalize
Browse files Browse the repository at this point in the history
  • Loading branch information
mborland authored May 1, 2024
2 parents 47cb147 + dc6068d commit b2798ed
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 131 deletions.
110 changes: 55 additions & 55 deletions doc/decimal/benchmarks.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ An example on Linux with b2: `../../../b2 cxxstd=20 toolset=gcc-13 define=BOOST_

== Comparisons

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

=== M1 macOS Results
Expand All @@ -32,20 +32,20 @@ Run using a Macbook pro with M1 pro chipset running macOS Sonoma 14.4.1 and home
|===
| Type | Runtime (us) | Ratio to `double`
| `float`
| 9032
| 1.589
| 8764
| 1.577
| `double`
| 5684
| 5559
| 1.000
| `decimal32`
| 285,453
| 50.2204
| 276,124
| 49.672
| `decimal64`
| 352,644
| 62.042
| 355,999
| 64.760
| `decimal128`
| 15,355,817
| 2701.590
| 989,028
| 177.915
|===

== Basic Operations
Expand All @@ -62,83 +62,83 @@ Run using a Macbook pro with M1 pro chipset running macOS Sonoma 14.4.1 and home
|===
| Type | Runtime (us) | Ratio to `double`
| `float`
| 1641
| 0.965
| 2113
| 0.739
| `double`
| 1708
| 2860
| 1.000
| `decimal32`
| 378,252
| 221.459
| 353,836
| 123.719
| `decimal64`
| 589,313
| 345.031
| 409,098
| 143.041
| `decimal128`
| 13,829,995
| 8097.190
| 2,418,039
| 845.468
|===

==== Subtraction

|===
| Type | Runtime (us) | Ratio to `double`
| `float`
| 3633
| 2.221
| 1782
| 1.061
| `double`
| 1636
| 1680
| 1.000
| `decimal32`
| 307,765
| 188.120
| 293,927
| 174.957
| `decimal64`
| 461,442
| 282.055
| 329,425
| 196.086
| `decimal128`
| 11,449,306
| 6998.350
| 1,527,261
| 909.084
|===

==== Multiplication

|===
| Type | Runtime (us) | Ratio to `double`
| `float`
| 1678
| 0.523
| 1691
| 0.979
| `double`
| 3209
| 1728
| 1.000
| `decimal32`
| 310,543
| 96.773
| 309,117
| 178.887
| `decimal64`
| 570,938
| 177.918
| 408,010
| 236.117
| `decimal128`
| 9,434,297
| 2939.95
| 2,506,105
| 1450.292
|===

==== Division

|===
| Type | Runtime (us) | Ratio to `double`
| `float`
| 2019
| 0.565
| 2058
| 0.846
| `double`
| 3572
| 2434
| 1.000
| `decimal32`
| 322,116
| 90.178
| 304,852
| 125.247
| `decimal64`
| 734,173
| 205.536
| 519,990
| 213.636
| `decimal128`
| 14,592,284
| 4085.19
| 3,534,909
| 1452.304
|===

== Selected Special Functions
Expand All @@ -155,20 +155,20 @@ Run using a Macbook pro with M1 pro chipset running macOS Sonoma 14.4.1 and home
|===
| Type | Runtime (us) | Ratio to `double`
| `float`
| 1904
| 0.565
| 2021
| 0.626
| `double`
| 3746
| 3229
| 1.000
| `decimal32`
| 5,050,241
| 1341.72
| 4,826,066
| 1494.601
| `decimal64`
| 12,084,821
| 3210.630
| 7,780,637
| 2409.612
| `decimal128`
| 275,779,340
| 73267.60
| 100,269,145
| 31052.693
|===

== `<charconv>`
Expand Down
12 changes: 6 additions & 6 deletions include/boost/decimal/charconv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <boost/decimal/detail/cmath/frexp10.hpp>
#include <boost/decimal/detail/attributes.hpp>
#include <boost/decimal/detail/countl.hpp>
#include <boost/decimal/detail/remove_trailing_zeros.hpp>

#ifndef BOOST_DECIMAL_BUILD_MODULE
#include <cstdint>
Expand Down Expand Up @@ -412,12 +413,11 @@ BOOST_DECIMAL_CONSTEXPR auto to_chars_fixed_impl(char* first, char* last, const
// In general formatting we remove trailing 0s
if (fmt == chars_format::general)
{
while (significand % 10 == 0)
{
significand /= 10;
++exponent;
--num_dig;
}

const auto zeros_removal {remove_trailing_zeros(significand)};
significand = zeros_removal.trimmed_number;
exponent += static_cast<int>(zeros_removal.number_of_removed_zeros);
num_dig -= static_cast<int>(zeros_removal.number_of_removed_zeros);
}
}

Expand Down
82 changes: 13 additions & 69 deletions include/boost/decimal/detail/normalize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,87 +9,31 @@
#include <boost/decimal/detail/integer_search_trees.hpp>
#include <boost/decimal/detail/fenv_rounding.hpp>
#include <boost/decimal/detail/attributes.hpp>
#include <boost/decimal/detail/remove_trailing_zeros.hpp>

namespace boost {
namespace decimal {
namespace detail {

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

if (digits < detail::precision_v<TargetDecimalType>)
if (digits < target_precision)
{
while (digits < detail::precision_v<TargetDecimalType>)
{
significand *= 10;
--exp;
++digits;
}
const auto zeros_needed {target_precision - digits};
significand *= pow10(static_cast<T1>(zeros_needed));
exp -= zeros_needed;
}
else if (digits > detail::precision_v<TargetDecimalType>)
else if (digits > target_precision)
{
while (digits > detail::precision_v<TargetDecimalType> + 1)
{
significand /= 10;

#if ((defined(__GNUC__) && (__GNUC__ > 12)) && !defined(__clang__))
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Waggressive-loop-optimizations"
#endif

++exp;

#if ((defined(__GNUC__) && (__GNUC__ > 12)) && !defined(__clang__))
# pragma GCC diagnostic pop
#endif

--digits;
}

exp += detail::fenv_round<TargetDecimalType>(significand, significand < 0);
}
}

template <typename TargetDecimalType = decimal32, typename T1, typename T2,
std::enable_if_t<std::is_same<TargetDecimalType, decimal128>::value, bool> = true>
constexpr auto normalize(T1& significand, T2& exp) noexcept
{
auto digits {num_digits(significand)};

if (digits < detail::precision_v<decimal128>)
{
while (digits < detail::precision_v<decimal128>)
{
significand *= UINT64_C(10);
--exp;
++digits;
}
}

else if (digits > detail::precision_v<TargetDecimalType>)
{
while (digits > detail::precision_v<TargetDecimalType> + 1)
{
significand /= 10;

#if ((defined(__GNUC__) && (__GNUC__ > 12)) && !defined(__clang__))
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Waggressive-loop-optimizations"
#endif

++exp;

#if ((defined(__GNUC__) && (__GNUC__ > 12)) && !defined(__clang__))
# pragma GCC diagnostic pop
#endif

--digits;
}

const auto excess_digits {digits - (target_precision + 1)};
significand /= pow10(static_cast<T1>(excess_digits));
exp += excess_digits;
// Perform final rounding according to the fenv rounding mode
exp += detail::fenv_round<TargetDecimalType>(significand, significand < 0);
}
}
Expand Down
Loading

0 comments on commit b2798ed

Please sign in to comment.