|
1 | | -//===-- Half-precision acoshf16 function ----------------------------------===// |
| 1 | +//===-- Half-precision acosh(x) function ----------------------------------===// |
2 | 2 | // |
3 | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | 4 | // See https://llvm.org/LICENSE.txt for license information. |
|
7 | 7 | //===----------------------------------------------------------------------===// |
8 | 8 |
|
9 | 9 | #include "src/math/acoshf16.h" |
| 10 | +#include "explogxf.h" |
10 | 11 | #include "hdr/errno_macros.h" |
11 | 12 | #include "hdr/fenv_macros.h" |
12 | 13 | #include "src/__support/FPUtil/FEnvImpl.h" |
|
16 | 17 | #include "src/__support/FPUtil/multiply_add.h" |
17 | 18 | #include "src/__support/FPUtil/sqrt.h" |
18 | 19 | #include "src/__support/macros/optimization.h" |
19 | | -#include "src/math/generic/explogxf.h" |
20 | 20 |
|
21 | 21 | namespace LIBC_NAMESPACE_DECL { |
22 | 22 |
|
23 | 23 | static constexpr size_t N_EXCEPTS = 2; |
24 | 24 | static constexpr fputil::ExceptValues<float16, N_EXCEPTS> ACOSHF16_EXCEPTS{{ |
25 | 25 | // (input, RZ output, RU offset, RD offset, RN offset) |
26 | | - // x = 0x1.6ep+1, acoshf16(x) = 0x1.dbp+0 (RZ) |
| 26 | + // x = 0x1.6dcp+1, acoshf16(x) = 0x1.b6p+0 (RZ) |
27 | 27 | {0x41B7, 0x3ED8, 1, 0, 0}, |
28 | | - // x = 0x1.c8p+0, acoshf16(x) = 0x1.27cp-1 (RZ) |
| 28 | + // x = 0x1.39p+0, acoshf16(x) = 0x1.4f8p-1 (RZ) |
29 | 29 | {0x3CE4, 0x393E, 1, 0, 1}, |
30 | 30 | }}; |
31 | 31 |
|
@@ -70,42 +70,24 @@ LLVM_LIBC_FUNCTION(float16, acoshf16, (float16 x)) { |
70 | 70 | return r.value(); |
71 | 71 |
|
72 | 72 | float xf = x; |
73 | | - // High precision polynomial approximation for inputs very close to 1.0 |
74 | | - // Specifically, for inputs within the range [1, 1.25), we employ the |
75 | | - // following step-by-step Taylor expansion derivation to maintain numerical |
76 | | - // accuracy: |
| 73 | + // High-precision polynomial approximation for inputs close to 1.0 |
| 74 | + // ([1, 1.25)). |
77 | 75 | // |
78 | | - // Step-by-step derivation: |
79 | | - // 1. Define y = acosh(x), thus by definition x = cosh(y). |
80 | | - // |
81 | | - // 2. Expand cosh(y) using exponential identities: |
82 | | - // cosh(y) = (e^y + e^{-y}) / 2 |
83 | | - // For small y, let us set y ≈ sqrt(2 * delta), thus: |
84 | | - // x ≈ cosh(y) ≈ 1 + delta, for small delta |
85 | | - // hence delta = x - 1. |
86 | | - // |
87 | | - // 3. Express y explicitly in terms of delta (for small delta): |
88 | | - // y = acosh(1 + delta) ≈ sqrt(2 * delta) for very small delta. |
89 | | - // |
90 | | - // 4. Use Taylor expansion around delta = 0 to obtain a more accurate |
91 | | - // polynomial: |
92 | | - // acosh(1 + delta) ≈ sqrt(2 * delta) * [1 - delta/12 + 3*delta^2/160 - |
93 | | - // 5*delta^3/896 + 35*delta^4/18432 + ...] For practical computation and |
94 | | - // precision, truncate and fit the polynomial precisely in the range [0, |
95 | | - // 0.25]. |
96 | | - // |
97 | | - // 5. The implemented polynomial approximation (coefficients obtained from |
98 | | - // careful numerical fitting) is: |
99 | | - // P(delta) ≈ 1 - 0x1.55551ap-4 * delta + 0x1.33160cp-6 * delta^2 - |
100 | | - // 0x1.6890f4p-8 * delta^3 + 0x1.8f3a62p-10 * delta^4 |
101 | | - // |
102 | | - // Since delta = x - 1, and 0 <= delta < 0.25, this approximation achieves |
103 | | - // high precision and numerical stability. |
| 76 | + // Brief derivation: |
| 77 | + // 1. Start from the definition: acosh(x) = y means x = cosh(y). |
| 78 | + // 2. Set x = 1 + delta. For small delta, y ≈ sqrt(2 * delta). |
| 79 | + // 3. Expand acosh(1 + delta) using Taylor series around delta=0: |
| 80 | + // acosh(1 + delta) ≈ sqrt(2 * delta) * [1 - delta/12 + 3*delta^2/160 |
| 81 | + // - 5*delta^3/896 + 35*delta^4/18432 + ...] |
| 82 | + // 4. Truncate the series to fit accurately for delta in [0, 0.25]. |
| 83 | + // 5. Polynomial coefficients (from sollya) used here are: |
| 84 | + // P(delta) ≈ 1 - 0x1.555556p-4 * delta + 0x1.333334p-6 * delta^2 |
| 85 | + // - 0x1.6db6dcp-8 * delta^3 + 0x1.f1c71cp-10 * delta^4 |
104 | 86 | if (LIBC_UNLIKELY(xf < 1.25f)) { |
105 | 87 | float delta = xf - 1.0f; |
106 | 88 | float sqrt_2_delta = fputil::sqrt<float>(2.0 * delta); |
107 | | - float pe = fputil::polyeval(delta, 0x1p+0f, -0x1.55551ap-4f, 0x1.33160cp-6f, |
108 | | - -0x1.6890f4p-8f, 0x1.8f3a62p-10f); |
| 89 | + float pe = fputil::polyeval(delta, 0x1p+0f, -0x1.555556p-4f, 0x1.333334p-6f, |
| 90 | + -0x1.6db6dcp-8f, 0x1.f1c71cp-10f); |
109 | 91 | float approx = sqrt_2_delta * pe; |
110 | 92 | return fputil::cast<float16>(approx); |
111 | 93 | } |
|
0 commit comments