|
| 1 | +//===-- Half-precision acosh(x) function ----------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "src/math/acoshf16.h" |
| 10 | +#include "explogxf.h" |
| 11 | +#include "hdr/errno_macros.h" |
| 12 | +#include "hdr/fenv_macros.h" |
| 13 | +#include "src/__support/FPUtil/FEnvImpl.h" |
| 14 | +#include "src/__support/FPUtil/FPBits.h" |
| 15 | +#include "src/__support/FPUtil/PolyEval.h" |
| 16 | +#include "src/__support/FPUtil/cast.h" |
| 17 | +#include "src/__support/FPUtil/except_value_utils.h" |
| 18 | +#include "src/__support/FPUtil/multiply_add.h" |
| 19 | +#include "src/__support/FPUtil/sqrt.h" |
| 20 | +#include "src/__support/common.h" |
| 21 | +#include "src/__support/macros/config.h" |
| 22 | +#include "src/__support/macros/optimization.h" |
| 23 | + |
| 24 | +namespace LIBC_NAMESPACE_DECL { |
| 25 | + |
| 26 | +static constexpr size_t N_EXCEPTS = 2; |
| 27 | +static constexpr fputil::ExceptValues<float16, N_EXCEPTS> ACOSHF16_EXCEPTS{{ |
| 28 | + // (input, RZ output, RU offset, RD offset, RN offset) |
| 29 | + // x = 0x1.6dcp+1, acoshf16(x) = 0x1.b6p+0 (RZ) |
| 30 | + {0x41B7, 0x3ED8, 1, 0, 0}, |
| 31 | + // x = 0x1.39p+0, acoshf16(x) = 0x1.4f8p-1 (RZ) |
| 32 | + {0x3CE4, 0x393E, 1, 0, 1}, |
| 33 | +}}; |
| 34 | + |
| 35 | +LLVM_LIBC_FUNCTION(float16, acoshf16, (float16 x)) { |
| 36 | + using FPBits = fputil::FPBits<float16>; |
| 37 | + FPBits xbits(x); |
| 38 | + uint16_t x_u = xbits.uintval(); |
| 39 | + |
| 40 | + // Check for NaN input first. |
| 41 | + if (LIBC_UNLIKELY(xbits.is_inf_or_nan())) { |
| 42 | + if (xbits.is_signaling_nan()) { |
| 43 | + fputil::raise_except_if_required(FE_INVALID); |
| 44 | + return FPBits::quiet_nan().get_val(); |
| 45 | + } |
| 46 | + if (xbits.is_neg()) { |
| 47 | + fputil::set_errno_if_required(EDOM); |
| 48 | + fputil::raise_except_if_required(FE_INVALID); |
| 49 | + return FPBits::quiet_nan().get_val(); |
| 50 | + } |
| 51 | + return x; |
| 52 | + } |
| 53 | + |
| 54 | + // Domain error for inputs less than 1.0. |
| 55 | + if (LIBC_UNLIKELY(x <= 1.0f)) { |
| 56 | + if (x == 1.0f) |
| 57 | + return FPBits::zero().get_val(); |
| 58 | + fputil::set_errno_if_required(EDOM); |
| 59 | + fputil::raise_except_if_required(FE_INVALID); |
| 60 | + return FPBits::quiet_nan().get_val(); |
| 61 | + } |
| 62 | + |
| 63 | + if (auto r = ACOSHF16_EXCEPTS.lookup(xbits.uintval()); |
| 64 | + LIBC_UNLIKELY(r.has_value())) |
| 65 | + return r.value(); |
| 66 | + |
| 67 | + float xf = x; |
| 68 | + // High-precision polynomial approximation for inputs close to 1.0 |
| 69 | + // ([1, 1.25)). |
| 70 | + // |
| 71 | + // Brief derivation: |
| 72 | + // 1. Expand acosh(1 + delta) using Taylor series around delta=0: |
| 73 | + // acosh(1 + delta) ≈ sqrt(2 * delta) * [1 - delta/12 + 3*delta^2/160 |
| 74 | + // - 5*delta^3/896 + 35*delta^4/18432 + ...] |
| 75 | + // 2. Truncate the series to fit accurately for delta in [0, 0.25]. |
| 76 | + // 3. Polynomial coefficients (from sollya) used here are: |
| 77 | + // P(delta) ≈ 1 - 0x1.555556p-4 * delta + 0x1.333334p-6 * delta^2 |
| 78 | + // - 0x1.6db6dcp-8 * delta^3 + 0x1.f1c71cp-10 * delta^4 |
| 79 | + // 4. The Sollya commands used to generate these coefficients were: |
| 80 | + // > display = hexadecimal; |
| 81 | + // > round(1/12, SG, RN); |
| 82 | + // > round(3/160, SG, RN); |
| 83 | + // > round(5/896, SG, RN); |
| 84 | + // > round(35/18432, SG, RN); |
| 85 | + // With hexadecimal display mode enabled, the outputs were: |
| 86 | + // 0x1.555556p-4 |
| 87 | + // 0x1.333334p-6 |
| 88 | + // 0x1.6db6dcp-8 |
| 89 | + // 0x1.f1c71cp-10 |
| 90 | + // 5. The maximum absolute error, estimated using: |
| 91 | + // dirtyinfnorm(acosh(1 + x) - sqrt(2*x) * P(x), [0, 0.25]) |
| 92 | + // is: |
| 93 | + // 0x1.d84281p-22 |
| 94 | + if (LIBC_UNLIKELY(x_u < 0x3D00U)) { |
| 95 | + float delta = xf - 1.0f; |
| 96 | + float sqrt_2_delta = fputil::sqrt<float>(2.0 * delta); |
| 97 | + float pe = fputil::polyeval(delta, 0x1p+0f, -0x1.555556p-4f, 0x1.333334p-6f, |
| 98 | + -0x1.6db6dcp-8f, 0x1.f1c71cp-10f); |
| 99 | + float approx = sqrt_2_delta * pe; |
| 100 | + return fputil::cast<float16>(approx); |
| 101 | + } |
| 102 | + |
| 103 | + // acosh(x) = log(x + sqrt(x^2 - 1)) |
| 104 | + float sqrt_term = fputil::sqrt<float>(fputil::multiply_add(xf, xf, -1.0f)); |
| 105 | + float result = static_cast<float>(log_eval(xf + sqrt_term)); |
| 106 | + |
| 107 | + return fputil::cast<float16>(result); |
| 108 | +} |
| 109 | + |
| 110 | +} // namespace LIBC_NAMESPACE_DECL |
0 commit comments