Skip to content

Commit fc7058c

Browse files
committed
Preliminary logarightmic funcs improve
1 parent 7fdcf98 commit fc7058c

File tree

6 files changed

+141
-97
lines changed

6 files changed

+141
-97
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ constexpr typename cosh_table_imp<b>::d128_coeffs_t cosh_table_imp<b>::d128_coef
9999
using cosh_table = cosh_detail::cosh_table_imp<true>;
100100

101101
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE T>
102-
constexpr auto cosh_series_expansion(T z) noexcept;
102+
constexpr auto cosh_series_expansion(T z2) noexcept;
103103

104104
template <>
105105
constexpr auto cosh_series_expansion<decimal32>(decimal32 z2) noexcept
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright 2023 - 2024 Matt Borland
2+
// Copyright 2023 - 2024 Christopher Kormanyos
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// https://www.boost.org/LICENSE_1_0.txt
5+
6+
#ifndef BOOST_DECIMAL_DETAIL_CMATH_IMPL_LOG_IMPL_HPP
7+
#define BOOST_DECIMAL_DETAIL_CMATH_IMPL_LOG_IMPL_HPP
8+
9+
#include <boost/decimal/detail/concepts.hpp>
10+
#include <boost/decimal/detail/cmath/impl/taylor_series_result.hpp>
11+
12+
#ifndef BOOST_DECIMAL_BUILD_MODULE
13+
#include <array>
14+
#include <cstddef>
15+
#include <cstdint>
16+
#endif
17+
18+
namespace boost {
19+
namespace decimal {
20+
namespace detail {
21+
22+
namespace log_detail {
23+
24+
template <bool b>
25+
struct log_table_imp
26+
{
27+
private:
28+
using d32_coeffs_t = std::array<decimal32, 8>;
29+
using d64_coeffs_t = std::array<decimal64, 11>;
30+
using d128_coeffs_t = std::array<decimal128, 17>;
31+
32+
public:
33+
static constexpr d32_coeffs_t d32_coeffs =
34+
{{
35+
// Series[Log[(1 + (z/2))/(1 - (z/2))], {z, 0, 17}]
36+
// (1), // * z
37+
::boost::decimal::decimal32 { UINT64_C(8333333333333333333), - 19 - 1 }, // * z^3
38+
::boost::decimal::decimal32 { UINT64_C(1250000000000000000), - 19 - 1 }, // * z^5
39+
::boost::decimal::decimal32 { UINT64_C(2232142857142857143), - 19 - 2 }, // * z^7
40+
::boost::decimal::decimal32 { UINT64_C(4340277777777777778), - 19 - 3 }, // * z^9
41+
::boost::decimal::decimal32 { UINT64_C(8877840909090909091), - 19 - 4 }, // * z^11
42+
::boost::decimal::decimal32 { UINT64_C(1878004807692307692), - 19 - 4 }, // * z^13
43+
::boost::decimal::decimal32 { UINT64_C(4069010416666666667), - 19 - 5 }, // * z^15
44+
::boost::decimal::decimal32 { UINT64_C(8975758272058823529), - 19 - 6 }, // * z^17
45+
}};
46+
47+
static constexpr d64_coeffs_t d64_coeffs =
48+
{{
49+
// Series[Log[(1 + (z/2))/(1 - (z/2))], {z, 0, 23}]
50+
// (1), // * z
51+
::boost::decimal::decimal64 { UINT64_C(8333333333333333333), - 19 - 1 }, // * z^3
52+
::boost::decimal::decimal64 { UINT64_C(1250000000000000000), - 19 - 1 }, // * z^5
53+
::boost::decimal::decimal64 { UINT64_C(2232142857142857143), - 19 - 2 }, // * z^7
54+
::boost::decimal::decimal64 { UINT64_C(4340277777777777778), - 19 - 3 }, // * z^9
55+
::boost::decimal::decimal64 { UINT64_C(8877840909090909091), - 19 - 4 }, // * z^11
56+
::boost::decimal::decimal64 { UINT64_C(1878004807692307692), - 19 - 4 }, // * z^13
57+
::boost::decimal::decimal64 { UINT64_C(4069010416666666667), - 19 - 5 }, // * z^15
58+
::boost::decimal::decimal64 { UINT64_C(8975758272058823529), - 19 - 6 }, // * z^17
59+
::boost::decimal::decimal64 { UINT64_C(2007735402960526316), - 19 - 6 }, // * z^19
60+
::boost::decimal::decimal64 { UINT64_C(4541306268601190476), - 19 - 7 }, // * z^21
61+
::boost::decimal::decimal64 { UINT64_C(1036602517832880435), - 19 - 7 }, // * z^23
62+
}};
63+
64+
static constexpr d128_coeffs_t d128_coeffs =
65+
{{
66+
// Series[Cosh[x], {x, 0, 34}]
67+
// (1), // * 1
68+
::boost::decimal::decimal128 { 5, -1 }, // * x^2
69+
::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(225875452601146), UINT64_C(13965751134118914724) }, -35 }, // * x^4
70+
::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(75291817533715), UINT64_C(10804165069276155440) }, -36 }, // * x^6
71+
::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(134449674167349), UINT64_C(4799281565792772746) }, -38 }, // * x^8
72+
::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(149388526852610), UINT64_C(5332535073103080820) }, -40 }, // * x^10
73+
::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(113173126403492), UINT64_C(11865690723015477068) }, -42 }, // * x^12
74+
::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(62183036485435), UINT64_C(9560282387433155251) }, -44 }, // * x^14
75+
::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(259095985355981), UINT64_C(6015479145837302244) }, -47 }, // * x^16
76+
::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(84671890639209), UINT64_C(10767230553416093986) }, -49 }, // * x^18
77+
::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(222820764840025), UINT64_C(4062785569898205740) }, -52 }, // * x^20
78+
::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(482296027792262), UINT64_C(7037075391028107068) }, -55 }, // * x^22
79+
::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(87372468802946), UINT64_C(1542176615384940434) }, -57 }, // * x^24
80+
::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(134419182773763), UINT64_C(3791559721646796942) }, -60 }, // * x^26
81+
::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(177803151817147), UINT64_C(1794430560736952558) }, -63 }, // * x^28
82+
::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(204371438870284), UINT64_C(366311534299067156) }, -66 }, // * x^30
83+
::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(206019595635366), UINT64_C(17625897212400736954) }, -69 }, // * x^32
84+
::boost::decimal::decimal128 { boost::decimal::detail::uint128 { UINT64_C(183618177928134), UINT64_C(9987905770721758456) }, -72 }, // * x^34
85+
}};
86+
};
87+
88+
#if !(defined(__cpp_inline_variables) && __cpp_inline_variables >= 201606L) && (!defined(_MSC_VER) || _MSC_VER != 1900)
89+
90+
template <bool b>
91+
constexpr typename log_table_imp<b>::d32_coeffs_t log_table_imp<b>::d32_coeffs;
92+
93+
template <bool b>
94+
constexpr typename log_table_imp<b>::d64_coeffs_t log_table_imp<b>::d64_coeffs;
95+
96+
template <bool b>
97+
constexpr typename log_table_imp<b>::d128_coeffs_t log_table_imp<b>::d128_coeffs;
98+
99+
#endif
100+
101+
} //namespace log_detail
102+
103+
using log_table = log_detail::log_table_imp<true>;
104+
105+
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE T>
106+
constexpr auto log_series_expansion(T z2) noexcept;
107+
108+
template <>
109+
constexpr auto log_series_expansion<decimal32>(decimal32 z2) noexcept
110+
{
111+
return taylor_series_result(z2, log_table::d32_coeffs);
112+
}
113+
114+
template <>
115+
constexpr auto log_series_expansion<decimal64>(decimal64 z2) noexcept
116+
{
117+
return taylor_series_result(z2, log_table::d64_coeffs);
118+
}
119+
120+
template <>
121+
constexpr auto log_series_expansion<decimal128>(decimal128 z2) noexcept
122+
{
123+
return taylor_series_result(z2, log_table::d128_coeffs);
124+
}
125+
126+
} //namespace detail
127+
} //namespace decimal
128+
} //namespace boost
129+
130+
#endif //BOOST_DECIMAL_DETAIL_CMATH_IMPL_LOG_IMPL_HPP

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ constexpr typename sinh_table_imp<b>::d128_coeffs_t sinh_table_imp<b>::d128_coef
9999
using sinh_table = sinh_detail::sinh_table_imp<true>;
100100

101101
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE T>
102-
constexpr auto sinh_series_expansion(T z) noexcept;
102+
constexpr auto sinh_series_expansion(T z2) noexcept;
103103

104104
template <>
105105
constexpr auto sinh_series_expansion<decimal32>(decimal32 z2) noexcept

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ constexpr typename tanh_table_imp<b>::d128_coeffs_t tanh_table_imp<b>::d128_coef
107107
using tanh_table = tanh_detail::tanh_table_imp<true>;
108108

109109
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE T>
110-
constexpr auto tanh_series_expansion(T z) noexcept;
110+
constexpr auto tanh_series_expansion(T z2) noexcept;
111111

112112
template <>
113113
constexpr auto tanh_series_expansion<decimal32>(decimal32 z2) noexcept

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

Lines changed: 6 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
#ifndef BOOST_DECIMAL_DETAIL_CMATH_LOG_HPP
77
#define BOOST_DECIMAL_DETAIL_CMATH_LOG_HPP
88

9-
#include <boost/decimal/fwd.hpp>
10-
#include <boost/decimal/detail/type_traits.hpp>
9+
#include <boost/decimal/fwd.hpp> // NOLINT(llvm-include-order)
10+
#include <boost/decimal/detail/cmath/impl/log_impl.hpp>
1111
#include <boost/decimal/detail/concepts.hpp>
12-
#include <boost/decimal/detail/emulated128.hpp>
1312
#include <boost/decimal/detail/config.hpp>
13+
#include <boost/decimal/detail/type_traits.hpp>
1414
#include <boost/decimal/numbers.hpp>
1515

1616
#ifndef BOOST_DECIMAL_BUILD_MODULE
@@ -21,61 +21,8 @@
2121
namespace boost {
2222
namespace decimal {
2323

24-
namespace detail {
25-
26-
#if (defined(__clang__) && (__clang__ < 6))
27-
# pragma clang diagnostic push
28-
# pragma clang diagnostic ignored "-Wmissing-braces"
29-
#endif
30-
31-
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE T>
32-
BOOST_DECIMAL_CONSTEXPR_VARIABLE std::array<T, 11> small_coefficient_table {
33-
// (1,) 12, 80, 448, 2304, 11264, 53248, 245760, 1114112, 4980736, 22020096, 96468992, ...
34-
// See also Sloane's A058962 at: https://oeis.org/A058962
35-
36-
// See also
37-
// Series[Log[(1 + (z/2))/(1 - (z/2))], {z, 0, 23}]
38-
// Or at Wolfram Alpha: https://www.wolframalpha.com/input?i=Series%5BLog%5B%281+%2B+%28z%2F2%29%29%2F%281+-+%28z%2F2%29%29%5D%2C+%7Bz%2C+0%2C+21%7D%5D
39-
40-
// TODO(ckormanyos)
41-
// Consider also Pade expansion of Log[1 + z/2].
42-
// PadeApproximant[Log[1 + z/2], {z, 6, 6}]
43-
// FullSimplify[%].
44-
45-
T { UINT64_C(833333333333333333), -18 - 1 }, // * z^3
46-
T { UINT64_C(125000000000000000), -18 - 1 }, // * z^5
47-
T { UINT64_C(223214285714285714), -18 - 2 }, // * z^7
48-
T { UINT64_C(434027777777777778), -18 - 3 }, // * z^9
49-
T { UINT64_C(887784090909090909), -18 - 4 }, // * z^11
50-
T { UINT64_C(187800480769230769), -18 - 4 }, // * z^13
51-
T { UINT64_C(406901041666666667), -18 - 5 }, // * z^15
52-
T { UINT64_C(897575827205882353), -18 - 6 }, // * z^17
53-
T { UINT64_C(200773540296052632), -18 - 6 }, // * z^19
54-
T { UINT64_C(454130626860119048), -18 - 7 }, // * z^21
55-
T { UINT64_C(103660251783288043), -18 - 7 } // * z^23
56-
};
57-
58-
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE T>
59-
BOOST_DECIMAL_CONSTEXPR_VARIABLE std::array<T, 11> large_coefficient_table {
60-
T{uint128{UINT64_C(451750905202293), UINT64_C(9484758194528277842)}, -35}, // z^3
61-
T{uint128{UINT64_C(67762635780344), UINT64_C(500376525493764096)}, -35}, // z^5
62-
T{uint128{UINT64_C(121004706750614), UINT64_C(6164027816584450626)}, -36}, // z^7
63-
T{uint128{UINT64_C(235286929792861), UINT64_C(3787056721709964394)}, -37}, // z^9
64-
T{uint128{UINT64_C(481268720030852), UINT64_C(8584740752302634068)}, -38}, // z^11
65-
T{uint128{UINT64_C(101806844621911), UINT64_C(1816002851448634124)}, -38}, // z^13
66-
T{uint128{UINT64_C(220581496680807), UINT64_C(7009130190423632548)}, -40}, // z^15
67-
T{uint128{UINT64_C(486576830913545), UINT64_C(12748560115094843630)}, -41}, // z^17
68-
T{uint128{UINT64_C(108839554283293), UINT64_C(2123490654414259032)}, -41}, // z^19
69-
T{uint128{UINT64_C(246184706116972), UINT64_C(9634423737622849438)}, -42}, // z^21
70-
T{uint128{UINT64_C(56194335091917), UINT64_C(11823550152479764302)}, -42} // z^23
71-
};
72-
73-
#if (defined(__clang__) && (__clang__ < 6))
74-
# pragma clang diagnostic pop
75-
#endif
76-
77-
template <typename T>
78-
constexpr auto log_impl(T x) noexcept
24+
BOOST_DECIMAL_EXPORT template <typename T>
25+
constexpr auto log(T x) noexcept
7926
BOOST_DECIMAL_REQUIRES(detail::is_decimal_floating_point_v, T)
8027
{
8128
constexpr T zero { 0, 0 };
@@ -138,17 +85,7 @@ constexpr auto log_impl(T x) noexcept
13885
const auto z = s + s;
13986
const auto zsq = z * z;
14087

141-
auto& coefficient_table = std::is_same<T, decimal128>::value ? detail::large_coefficient_table<T> : detail::small_coefficient_table<T>;
142-
143-
auto rit = coefficient_table.crbegin() + static_cast<std::size_t>((sizeof(T) == 4U) ? 5U : 0U);
144-
145-
result = *rit;
146-
147-
while(rit != coefficient_table.crend())
148-
{
149-
result = fma(result, zsq, *rit++);
150-
}
151-
88+
result = detail::log_series_expansion(zsq);
15289
result = z * fma(result, zsq, one);
15390

15491
if (exp2val > 0)
@@ -164,29 +101,6 @@ constexpr auto log_impl(T x) noexcept
164101
return result;
165102
}
166103

167-
} //namespace detail
168-
169-
BOOST_DECIMAL_EXPORT template <typename T>
170-
constexpr auto log(T x) noexcept
171-
BOOST_DECIMAL_REQUIRES(detail::is_decimal_floating_point_v, T)
172-
{
173-
#if BOOST_DECIMAL_DEC_EVAL_METHOD == 0
174-
175-
using evaluation_type = T;
176-
177-
#elif BOOST_DECIMAL_DEC_EVAL_METHOD == 1
178-
179-
using evaluation_type = detail::promote_args_t<T, decimal64>;
180-
181-
#else // BOOST_DECIMAL_DEC_EVAL_METHOD == 2
182-
183-
using evaluation_type = detail::promote_args_t<T, decimal128>;
184-
185-
#endif
186-
187-
return static_cast<T>(detail::log_impl(static_cast<evaluation_type>(x)));
188-
}
189-
190104
} // namespace decimal
191105
} // namespace boost
192106

test/test_log.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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

0 commit comments

Comments
 (0)