Skip to content

Commit a6fab8e

Browse files
authored
Merge pull request #548 from cppalliance/expm1_further
Fix #537 via expm1 64/128 and other small repairs
2 parents 43a9bf1 + be8d7f5 commit a6fab8e

File tree

9 files changed

+329
-236
lines changed

9 files changed

+329
-236
lines changed

include/boost/decimal/detail/cmath/exp.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
// Copyright 2023 Matt Borland
2-
// Copyright 2023 Christopher Kormanyos
1+
// Copyright 2023 - 2024 Matt Borland
2+
// Copyright 2023 - 2024 Christopher Kormanyos
33
// Distributed under the Boost Software License, Version 1.0.
44
// https://www.boost.org/LICENSE_1_0.txt
55

66
#ifndef BOOST_DECIMAL_DETAIL_CMATH_EXP_HPP
77
#define BOOST_DECIMAL_DETAIL_CMATH_EXP_HPP
88

99
#include <boost/decimal/fwd.hpp> // NOLINT(llvm-include-order)
10-
#include <boost/decimal/detail/cmath/impl/exp_impl.hpp>
10+
#include <boost/decimal/detail/cmath/impl/expm1_impl.hpp>
1111
#include <boost/decimal/detail/cmath/impl/pow_impl.hpp>
1212
#include <boost/decimal/detail/type_traits.hpp>
1313
#include <boost/decimal/detail/concepts.hpp>
@@ -76,7 +76,7 @@ constexpr auto exp_impl(T x) noexcept
7676
x -= numbers::ln2_v<T> * nf2;
7777
}
7878

79-
result = detail::exp_pade_appxroximant_or_series(x);
79+
result = fma(x, detail::expm1_series_expansion(x), one);
8080

8181
if (nf2 > 0)
8282
{

include/boost/decimal/detail/cmath/expm1.hpp

Lines changed: 4 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
#define BOOST_DECIMAL_DETAIL_CMATH_EXPM1_HPP
88

99
#include <boost/decimal/fwd.hpp> // NOLINT(llvm-include-order)
10-
#include <boost/decimal/detail/type_traits.hpp>
10+
#include <boost/decimal/detail/cmath/impl/expm1_impl.hpp>
1111
#include <boost/decimal/detail/concepts.hpp>
1212
#include <boost/decimal/detail/config.hpp>
13+
#include <boost/decimal/detail/type_traits.hpp>
1314
#include <boost/decimal/numbers.hpp>
1415

1516
#ifndef BOOST_DECIMAL_BUILD_MODULE
16-
#include <array>
17+
#include <cmath>
1718
#include <type_traits>
1819
#endif
1920

@@ -70,66 +71,7 @@ constexpr auto expm1_impl(T x) noexcept
7071
}
7172
else
7273
{
73-
// Specifically derive a polynomial expansion for Exp[x] - 1 for this work.
74-
// Table[{x, Exp[x] - 1}, {x, -Log[2], Log[2], 1/60}]
75-
// N[%, 48]
76-
// Fit[%, {x, x^2, x^3, x^4, x^5, x^6, x^7, x^8, x^9, x^10, x^11, x^12, x^13, x^14}, x]
77-
78-
// 0.1000000000000000003213692169066381945529176657E+01 x
79-
// + 0.4999999999999999998389405741198241227273662223E+00 x^2
80-
// + 0.1666666666666664035765593562709186076539985328E+00 x^3
81-
// + 0.4166666666666666934614928838666442575683452206E-01 x^4
82-
// + 0.8333333333339521841328202617206868796855583809E-02 x^5
83-
// + 0.1388888888888953513176946682731620625302469979E-02 x^6
84-
// + 0.1984126983488689186859793276462049624439889135E-03 x^7
85-
// + 0.2480158730001499149369647648735612017495156006E-04 x^8
86-
// + 0.2755732258782898252481007286813761544775538366E-05 x^9
87-
// + 0.2755732043147979013276287368071846972098889744E-06 x^10
88-
// + 0.2505116286861719378770371641094067075234027345E-07 x^11
89-
// + 0.2087632598463662328337672597832718168295672334E-08 x^12
90-
// + 0.1619385892296180390338553597911165126625722485E-09 x^13
91-
// + 0.1154399218598221557485183048765404442959841646E-10 x^14
92-
93-
using coefficient_array_type = std::array<T, static_cast<std::size_t>(UINT8_C(14))>;
94-
95-
#if (defined(__clang__) && (__clang__ < 6))
96-
# pragma clang diagnostic push
97-
# pragma clang diagnostic ignored "-Wmissing-braces"
98-
#endif
99-
100-
constexpr auto coefficient_table =
101-
coefficient_array_type
102-
{
103-
T { UINT64_C(100000000000000000), -17 - 0 }, // * x
104-
T { UINT64_C(500000000000000000), -18 - 0 }, // * x^2
105-
T { UINT64_C(166666666666666404), -18 - 0 }, // * x^3
106-
T { UINT64_C(416666666666666693), -18 - 1 }, // * x^4
107-
T { UINT64_C(833333333333952184), -18 - 2 }, // * x^5
108-
T { UINT64_C(138888888888895351), -18 - 2 }, // * x^6
109-
T { UINT64_C(198412698348868919), -18 - 3 }, // * x^7
110-
T { UINT64_C(248015873000149915), -18 - 4 }, // * x^8
111-
T { UINT64_C(275573225878289825), -18 - 5 }, // * x^9
112-
T { UINT64_C(275573204314797901), -18 - 6 }, // * x^10
113-
T { UINT64_C(250511628686171938), -18 - 7 }, // * x^11
114-
T { UINT64_C(208763259846366233), -18 - 8 }, // * x^12
115-
T { UINT64_C(161938589229618039), -18 - 9 }, // * x^13
116-
T { UINT64_C(115439921859822156), -18 - 10 } // * x^14
117-
};
118-
119-
#if (defined(__clang__) && (__clang__ < 6))
120-
# pragma clang diagnostic pop
121-
#endif
122-
123-
auto rit = coefficient_table.crbegin() + static_cast<std::size_t>((sizeof(T) == 4U) ? 5U : 0U);
124-
125-
result = *rit;
126-
127-
while(rit != coefficient_table.crend())
128-
{
129-
result = fma(result, x, *rit++);
130-
}
131-
132-
result *= x;
74+
result = x * detail::expm1_series_expansion(x);
13375
}
13476
}
13577

include/boost/decimal/detail/cmath/ilogb.hpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,30 @@ BOOST_DECIMAL_EXPORT template <typename T>
2222
constexpr auto ilogb(T d) noexcept
2323
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_decimal_floating_point_v, T, int)
2424
{
25-
const auto fpc_d = fpclassify(d);
25+
const auto fpc = fpclassify(d);
2626

27-
if (fpc_d == FP_ZERO)
27+
int result { };
28+
29+
if (fpc == FP_ZERO)
2830
{
29-
return FP_ILOGB0;
31+
result = static_cast<int>(FP_ILOGB0);
3032
}
31-
else if (fpc_d == FP_INFINITE)
33+
else if (fpc == FP_INFINITE)
3234
{
33-
return INT_MAX;
35+
result = static_cast<int>(INT_MAX);
3436
}
35-
else if (fpc_d == FP_NAN)
37+
else if (fpc == FP_NAN)
3638
{
37-
return FP_ILOGBNAN;
39+
result = static_cast<int>(FP_ILOGBNAN);
3840
}
41+
else
42+
{
43+
const auto offset = detail::num_digits(d.full_significand()) - 1;
3944

40-
const auto offset = detail::num_digits(d.full_significand()) - 1;
41-
42-
const auto expval = static_cast<int>(static_cast<int>(d.unbiased_exponent()) + offset);
45+
result = static_cast<int>(static_cast<int>(d.unbiased_exponent()) + offset);
46+
}
4347

44-
return expval;
48+
return result;
4549
}
4650

4751
} // namespace decimal

include/boost/decimal/detail/cmath/impl/exp_impl.hpp

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

0 commit comments

Comments
 (0)