|
| 1 | +//===-- Implementation header for rsqrtf16 ----------------------*- C++ -*-===// |
| 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 | +#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_RSQRTF16_H |
| 10 | +#define LLVM_LIBC_SRC___SUPPORT_MATH_RSQRTF16_H |
| 11 | + |
| 12 | +#include "include/llvm-libc-macros/float16-macros.h" |
| 13 | + |
| 14 | +#ifdef LIBC_TYPES_HAS_FLOAT16 |
| 15 | + |
| 16 | +#include "src/__support/FPUtil/FEnvImpl.h" |
| 17 | +#include "src/__support/FPUtil/FPBits.h" |
| 18 | +#include "src/__support/FPUtil/ManipulationFunctions.h" |
| 19 | +#include "src/__support/FPUtil/cast.h" |
| 20 | +#include "src/__support/FPUtil/multiply_add.h" |
| 21 | +#include "src/__support/FPUtil/sqrt.h" |
| 22 | +#include "src/__support/macros/optimization.h" |
| 23 | + |
| 24 | +namespace LIBC_NAMESPACE_DECL { |
| 25 | +namespace math { |
| 26 | + |
| 27 | +LIBC_INLINE static constexpr float16 rsqrtf16(float16 x) { |
| 28 | + using FPBits = fputil::FPBits<float16>; |
| 29 | + FPBits xbits(x); |
| 30 | + |
| 31 | + const uint16_t x_u = xbits.uintval(); |
| 32 | + const uint16_t x_abs = x_u & 0x7fff; |
| 33 | + const uint16_t x_sign = x_u >> 15; |
| 34 | + |
| 35 | + // x is NaN |
| 36 | + if (LIBC_UNLIKELY(xbits.is_nan())) { |
| 37 | + if (xbits.is_signaling_nan()) { |
| 38 | + fputil::raise_except_if_required(FE_INVALID); |
| 39 | + return FPBits::quiet_nan().get_val(); |
| 40 | + } |
| 41 | + return x; |
| 42 | + } |
| 43 | + |
| 44 | + // |x| = 0 |
| 45 | + if (LIBC_UNLIKELY(x_abs == 0x0)) { |
| 46 | + fputil::raise_except_if_required(FE_DIVBYZERO); |
| 47 | + fputil::set_errno_if_required(ERANGE); |
| 48 | + return FPBits::inf(Sign::POS).get_val(); |
| 49 | + } |
| 50 | + |
| 51 | + // -inf <= x < 0 |
| 52 | + if (LIBC_UNLIKELY(x_sign == 1)) { |
| 53 | + fputil::raise_except_if_required(FE_INVALID); |
| 54 | + fputil::set_errno_if_required(EDOM); |
| 55 | + return FPBits::quiet_nan().get_val(); |
| 56 | + } |
| 57 | + |
| 58 | + // x = +inf => rsqrt(x) = 0 |
| 59 | + if (LIBC_UNLIKELY(xbits.is_inf())) |
| 60 | + return FPBits::zero().get_val(); |
| 61 | + |
| 62 | + // TODO: add integer based implementation when LIBC_TARGET_CPU_HAS_FPU_FLOAT |
| 63 | + // is not defined |
| 64 | + float result = 1.0f / fputil::sqrt<float>(fputil::cast<float>(x)); |
| 65 | + |
| 66 | + // Targeted post-corrections to ensure correct rounding in half for specific |
| 67 | + // mantissa patterns |
| 68 | + const uint16_t half_mantissa = x_abs & 0x3ff; |
| 69 | + if (LIBC_UNLIKELY(half_mantissa == 0x011F)) { |
| 70 | + result = fputil::multiply_add(result, 0x1.0p-21f, result); |
| 71 | + } else if (LIBC_UNLIKELY(half_mantissa == 0x0313)) { |
| 72 | + result = fputil::multiply_add(result, -0x1.0p-21f, result); |
| 73 | + } |
| 74 | + |
| 75 | + return fputil::cast<float16>(result); |
| 76 | +} |
| 77 | + |
| 78 | +} // namespace math |
| 79 | +} // namespace LIBC_NAMESPACE_DECL |
| 80 | + |
| 81 | +#endif // LIBC_TYPES_HAS_FLOAT16 |
| 82 | + |
| 83 | +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_RSQRTF16_H |
0 commit comments