|
| 1 | +//===-- Implementation of hypotf16 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/hypotf16.h" |
| 10 | +#include "src/__support/FPUtil/FEnvImpl.h" |
| 11 | +#include "src/__support/FPUtil/FPBits.h" |
| 12 | +#include "src/__support/FPUtil/cast.h" |
| 13 | +#include "src/__support/FPUtil/multiply_add.h" |
| 14 | +#include "src/__support/FPUtil/sqrt.h" |
| 15 | +#include "src/__support/common.h" |
| 16 | +#include "src/__support/macros/optimization.h" |
| 17 | +#include "src/__support/macros/properties/types.h" |
| 18 | + |
| 19 | +namespace LIBC_NAMESPACE_DECL { |
| 20 | + |
| 21 | +// For targets where conversion from float to float16 has to be |
| 22 | +// emulated, fputil::hypot<float16> is faster |
| 23 | +LLVM_LIBC_FUNCTION(float16, hypotf16, (float16 x, float16 y)) { |
| 24 | + using FloatBits = fputil::FPBits<float>; |
| 25 | + using FPBits = fputil::FPBits<float16>; |
| 26 | + |
| 27 | + FPBits x_abs = FPBits(x).abs(); |
| 28 | + FPBits y_abs = FPBits(y).abs(); |
| 29 | + |
| 30 | + bool x_abs_larger = x_abs.uintval() >= y_abs.uintval(); |
| 31 | + |
| 32 | + FPBits a_bits = x_abs_larger ? x_abs : y_abs; |
| 33 | + FPBits b_bits = x_abs_larger ? y_abs : x_abs; |
| 34 | + |
| 35 | + uint16_t a_u = a_bits.uintval(); |
| 36 | + uint16_t b_u = b_bits.uintval(); |
| 37 | + |
| 38 | + // Note: replacing `a_u >= FPBits::EXP_MASK` with `a_bits.is_inf_or_nan()` |
| 39 | + // generates extra exponent bit masking instructions on x86-64. |
| 40 | + if (LIBC_UNLIKELY(a_u >= FPBits::EXP_MASK)) { |
| 41 | + // x or y is inf or nan |
| 42 | + if (a_bits.is_signaling_nan() || b_bits.is_signaling_nan()) { |
| 43 | + fputil::raise_except_if_required(FE_INVALID); |
| 44 | + return FPBits::quiet_nan().get_val(); |
| 45 | + } |
| 46 | + if (a_bits.is_inf() || b_bits.is_inf()) |
| 47 | + return FPBits::inf().get_val(); |
| 48 | + return a_bits.get_val(); |
| 49 | + } |
| 50 | + |
| 51 | + if (LIBC_UNLIKELY(a_u - b_u >= |
| 52 | + static_cast<uint16_t>((FPBits::FRACTION_LEN + 2) |
| 53 | + << FPBits::FRACTION_LEN))) |
| 54 | + return x_abs.get_val() + y_abs.get_val(); |
| 55 | + |
| 56 | + float af = fputil::cast<float>(a_bits.get_val()); |
| 57 | + float bf = fputil::cast<float>(b_bits.get_val()); |
| 58 | + |
| 59 | + // These squares are exact. |
| 60 | + float a_sq = af * af; |
| 61 | + float sum_sq = fputil::multiply_add(bf, bf, a_sq); |
| 62 | + |
| 63 | + FloatBits result(fputil::sqrt<float>(sum_sq)); |
| 64 | + uint32_t r_u = result.uintval(); |
| 65 | + |
| 66 | + // If any of the sticky bits of the result are non-zero, except the LSB, then |
| 67 | + // the rounded result is correct. |
| 68 | + if (LIBC_UNLIKELY(((r_u + 1) & 0x0000'0FFE) == 0)) { |
| 69 | + float r_d = result.get_val(); |
| 70 | + |
| 71 | + // Perform rounding correction. |
| 72 | + float sum_sq_lo = fputil::multiply_add(bf, bf, a_sq - sum_sq); |
| 73 | + float err = sum_sq_lo - fputil::multiply_add(r_d, r_d, -sum_sq); |
| 74 | + |
| 75 | + if (err > 0) { |
| 76 | + r_u |= 1; |
| 77 | + } else if ((err < 0) && (r_u & 1) == 0) { |
| 78 | + r_u -= 1; |
| 79 | + } else if ((r_u & 0x0000'1FFF) == 0) { |
| 80 | + // The rounded result is exact. |
| 81 | + fputil::clear_except_if_required(FE_INEXACT); |
| 82 | + } |
| 83 | + return fputil::cast<float16>(FloatBits(r_u).get_val()); |
| 84 | + } |
| 85 | + |
| 86 | + return fputil::cast<float16>(result.get_val()); |
| 87 | +} |
| 88 | + |
| 89 | +} // namespace LIBC_NAMESPACE_DECL |
0 commit comments