|
| 1 | +//===-- Division of IEEE 754 floating-point numbers -------------*- 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_FPUTIL_GENERIC_DIV_H |
| 10 | +#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_DIV_H |
| 11 | + |
| 12 | +#include "hdr/errno_macros.h" |
| 13 | +#include "hdr/fenv_macros.h" |
| 14 | +#include "src/__support/CPP/bit.h" |
| 15 | +#include "src/__support/CPP/type_traits.h" |
| 16 | +#include "src/__support/FPUtil/BasicOperations.h" |
| 17 | +#include "src/__support/FPUtil/FEnvImpl.h" |
| 18 | +#include "src/__support/FPUtil/FPBits.h" |
| 19 | +#include "src/__support/FPUtil/dyadic_float.h" |
| 20 | +#include "src/__support/macros/attributes.h" |
| 21 | +#include "src/__support/macros/optimization.h" |
| 22 | + |
| 23 | +namespace LIBC_NAMESPACE::fputil::generic { |
| 24 | + |
| 25 | +template <typename OutType, typename InType> |
| 26 | +LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<OutType> && |
| 27 | + cpp::is_floating_point_v<InType> && |
| 28 | + sizeof(OutType) <= sizeof(InType), |
| 29 | + OutType> |
| 30 | +div(InType x, InType y) { |
| 31 | + using OutFPBits = FPBits<OutType>; |
| 32 | + using OutStorageType = typename OutFPBits::StorageType; |
| 33 | + using InFPBits = FPBits<InType>; |
| 34 | + using InStorageType = typename InFPBits::StorageType; |
| 35 | + using DyadicFloat = |
| 36 | + DyadicFloat<cpp::bit_ceil(static_cast<size_t>(InFPBits::FRACTION_LEN))>; |
| 37 | + |
| 38 | + InFPBits x_bits(x); |
| 39 | + InFPBits y_bits(y); |
| 40 | + |
| 41 | + Sign result_sign = x_bits.sign() == y_bits.sign() ? Sign::POS : Sign::NEG; |
| 42 | + |
| 43 | + if (LIBC_UNLIKELY(x_bits.is_inf_or_nan() || y_bits.is_inf_or_nan() || |
| 44 | + x_bits.is_zero() || y_bits.is_zero())) { |
| 45 | + if (x_bits.is_nan() || y_bits.is_nan()) { |
| 46 | + if (x_bits.is_signaling_nan() || y_bits.is_signaling_nan()) |
| 47 | + raise_except_if_required(FE_INVALID); |
| 48 | + |
| 49 | + if (x_bits.is_quiet_nan()) { |
| 50 | + InStorageType x_payload = static_cast<InStorageType>(getpayload(x)); |
| 51 | + if ((x_payload & ~(OutFPBits::FRACTION_MASK >> 1)) == 0) |
| 52 | + return OutFPBits::quiet_nan(x_bits.sign(), |
| 53 | + static_cast<OutStorageType>(x_payload)) |
| 54 | + .get_val(); |
| 55 | + } |
| 56 | + |
| 57 | + if (y_bits.is_quiet_nan()) { |
| 58 | + InStorageType y_payload = static_cast<InStorageType>(getpayload(y)); |
| 59 | + if ((y_payload & ~(OutFPBits::FRACTION_MASK >> 1)) == 0) |
| 60 | + return OutFPBits::quiet_nan(y_bits.sign(), |
| 61 | + static_cast<OutStorageType>(y_payload)) |
| 62 | + .get_val(); |
| 63 | + } |
| 64 | + |
| 65 | + return OutFPBits::quiet_nan().get_val(); |
| 66 | + } |
| 67 | + |
| 68 | + if (x_bits.is_inf()) { |
| 69 | + if (y_bits.is_inf()) { |
| 70 | + set_errno_if_required(EDOM); |
| 71 | + raise_except_if_required(FE_INVALID); |
| 72 | + return OutFPBits::quiet_nan().get_val(); |
| 73 | + } |
| 74 | + |
| 75 | + return OutFPBits::inf(result_sign).get_val(); |
| 76 | + } |
| 77 | + |
| 78 | + if (y_bits.is_inf()) |
| 79 | + return OutFPBits::inf(result_sign).get_val(); |
| 80 | + |
| 81 | + if (y_bits.is_zero()) { |
| 82 | + if (x_bits.is_zero()) { |
| 83 | + raise_except_if_required(FE_INVALID); |
| 84 | + return OutFPBits::quiet_nan().get_val(); |
| 85 | + } |
| 86 | + |
| 87 | + raise_except_if_required(FE_DIVBYZERO); |
| 88 | + return OutFPBits::inf(result_sign).get_val(); |
| 89 | + } |
| 90 | + |
| 91 | + if (x_bits.is_zero()) |
| 92 | + return OutFPBits::zero(result_sign).get_val(); |
| 93 | + } |
| 94 | + |
| 95 | + DyadicFloat xd(x); |
| 96 | + DyadicFloat yd(y); |
| 97 | + |
| 98 | + // Number of iterations = full output precision + 1 rounding bit + 1 potential |
| 99 | + // leading 0. |
| 100 | + constexpr size_t NUM_ITERS = OutFPBits::FRACTION_LEN + 3; |
| 101 | + int result_exp = xd.exponent - yd.exponent - (NUM_ITERS - 1); |
| 102 | + |
| 103 | + InStorageType q = 0; |
| 104 | + InStorageType r = static_cast<InStorageType>(xd.mantissa >> 2); |
| 105 | + InStorageType yd_mant_in = static_cast<InStorageType>(yd.mantissa >> 1); |
| 106 | + |
| 107 | + for (size_t i = 0; i < NUM_ITERS; ++i) { |
| 108 | + q <<= 1; |
| 109 | + r <<= 1; |
| 110 | + if (r >= yd_mant_in) { |
| 111 | + q += 1; |
| 112 | + r -= yd_mant_in; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + DyadicFloat result(result_sign, result_exp, q); |
| 117 | + result.mantissa += r != 0; |
| 118 | + |
| 119 | + return result.template as<OutType, /*ShouldSignalExceptions=*/true>(); |
| 120 | +} |
| 121 | + |
| 122 | +} // namespace LIBC_NAMESPACE::fputil::generic |
| 123 | + |
| 124 | +#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_DIV_H |
0 commit comments