|
| 1 | +//===-- Half-precision acoshf16 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 "hdr/errno_macros.h" |
| 11 | +#include "hdr/fenv_macros.h" |
| 12 | +#include "src/__support/FPUtil/FEnvImpl.h" |
| 13 | +#include "src/__support/FPUtil/FPBits.h" |
| 14 | +#include "src/__support/FPUtil/sqrt.h" |
| 15 | +#include "src/__support/FPUtil/multiply_add.h" |
| 16 | +#include "src/__support/FPUtil/cast.h" |
| 17 | +#include "src/__support/macros/optimization.h" |
| 18 | +#include "src/math/generic/explogxf.h" |
| 19 | + |
| 20 | +namespace LIBC_NAMESPACE_DECL { |
| 21 | + |
| 22 | +LLVM_LIBC_FUNCTION(float16, acoshf16, (float16 x)) { |
| 23 | + using FPBits = fputil::FPBits<float16>; |
| 24 | + FPBits xbits(x); |
| 25 | + |
| 26 | + uint16_t x_u = xbits.uintval(); |
| 27 | + uint16_t x_abs = x_u & 0x7fff; |
| 28 | + |
| 29 | + // acoshf16(x) domain: x >= 1.0 |
| 30 | + if (LIBC_UNLIKELY(x_abs < 0x3c00)) { |
| 31 | + fputil::set_errno_if_required(EDOM); |
| 32 | + fputil::raise_except_if_required(FE_INVALID); |
| 33 | + return FPBits::quiet_nan().get_val(); |
| 34 | + } |
| 35 | + |
| 36 | + if (LIBC_UNLIKELY(x == float16(1.0f))) |
| 37 | + return float16(0.0f); |
| 38 | + |
| 39 | + // acosh(inf) = inf, acosh(NaN) = NaN |
| 40 | + if (LIBC_UNLIKELY(xbits.is_inf_or_nan())) |
| 41 | + return x; |
| 42 | + |
| 43 | + // Compute in float32 for accuracy |
| 44 | + float xf32 = static_cast<float>(x); |
| 45 | + // sqrt(x^2 - 1) |
| 46 | + float sqrt_term = fputil::sqrt<float>( |
| 47 | + fputil::multiply_add(xf32, xf32, -1.0f)); |
| 48 | + |
| 49 | + // log(x + sqrt(x^2 - 1)) |
| 50 | + float result = static_cast<float>(log_eval(xf32 + sqrt_term)); |
| 51 | + |
| 52 | + return fputil::cast<float16>(result); |
| 53 | +} |
| 54 | + |
| 55 | +} // namespace LIBC_NAMESPACE_DECL |
0 commit comments