Warning: This file is not a C or C++ file. It does not have highlighting.
| 1 | //===-- Multiplication 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_MUL_H |
| 10 | #define LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_MUL_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/config.h" |
| 22 | #include "src/__support/macros/optimization.h" |
| 23 | |
| 24 | namespace LIBC_NAMESPACE_DECL { |
| 25 | namespace fputil::generic { |
| 26 | |
| 27 | template <typename OutType, typename InType> |
| 28 | LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<OutType> && |
| 29 | cpp::is_floating_point_v<InType> && |
| 30 | sizeof(OutType) <= sizeof(InType), |
| 31 | OutType> |
| 32 | mul(InType x, InType y) { |
| 33 | using OutFPBits = FPBits<OutType>; |
| 34 | using OutStorageType = typename OutFPBits::StorageType; |
| 35 | using InFPBits = FPBits<InType>; |
| 36 | using InStorageType = typename InFPBits::StorageType; |
| 37 | // The product of two p-digit numbers is a 2p-digit number. |
| 38 | using DyadicFloat = |
| 39 | DyadicFloat<cpp::bit_ceil(2 * static_cast<size_t>(InFPBits::SIG_LEN))>; |
| 40 | |
| 41 | InFPBits x_bits(x); |
| 42 | InFPBits y_bits(y); |
| 43 | |
| 44 | Sign result_sign = x_bits.sign() == y_bits.sign() ? Sign::POS : Sign::NEG; |
| 45 | |
| 46 | if (LIBC_UNLIKELY(x_bits.is_inf_or_nan() || y_bits.is_inf_or_nan() || |
| 47 | x_bits.is_zero() || y_bits.is_zero())) { |
| 48 | if (x_bits.is_nan() || y_bits.is_nan()) { |
| 49 | if (x_bits.is_signaling_nan() || y_bits.is_signaling_nan()) |
| 50 | raise_except_if_required(FE_INVALID); |
| 51 | |
| 52 | if (x_bits.is_quiet_nan()) { |
| 53 | InStorageType x_payload = x_bits.get_mantissa(); |
| 54 | x_payload >>= InFPBits::FRACTION_LEN - OutFPBits::FRACTION_LEN; |
| 55 | return OutFPBits::quiet_nan(x_bits.sign(), |
| 56 | static_cast<OutStorageType>(x_payload)) |
| 57 | .get_val(); |
| 58 | } |
| 59 | |
| 60 | if (y_bits.is_quiet_nan()) { |
| 61 | InStorageType y_payload = y_bits.get_mantissa(); |
| 62 | y_payload >>= InFPBits::FRACTION_LEN - OutFPBits::FRACTION_LEN; |
| 63 | return OutFPBits::quiet_nan(y_bits.sign(), |
| 64 | static_cast<OutStorageType>(y_payload)) |
| 65 | .get_val(); |
| 66 | } |
| 67 | |
| 68 | return OutFPBits::quiet_nan().get_val(); |
| 69 | } |
| 70 | |
| 71 | if (x_bits.is_inf()) { |
| 72 | if (y_bits.is_zero()) { |
| 73 | set_errno_if_required(EDOM); |
| 74 | raise_except_if_required(FE_INVALID); |
| 75 | return OutFPBits::quiet_nan().get_val(); |
| 76 | } |
| 77 | |
| 78 | return OutFPBits::inf(result_sign).get_val(); |
| 79 | } |
| 80 | |
| 81 | if (y_bits.is_inf()) { |
| 82 | if (x_bits.is_zero()) { |
| 83 | set_errno_if_required(EDOM); |
| 84 | raise_except_if_required(FE_INVALID); |
| 85 | return OutFPBits::quiet_nan().get_val(); |
| 86 | } |
| 87 | |
| 88 | return OutFPBits::inf(result_sign).get_val(); |
| 89 | } |
| 90 | |
| 91 | // Now either x or y is zero, and the other one is finite. |
| 92 | return OutFPBits::zero(result_sign).get_val(); |
| 93 | } |
| 94 | |
| 95 | DyadicFloat xd(x); |
| 96 | DyadicFloat yd(y); |
| 97 | |
| 98 | DyadicFloat result = quick_mul(xd, yd); |
| 99 | return result.template as<OutType, /*ShouldSignalExceptions=*/true>(); |
| 100 | } |
| 101 | |
| 102 | } // namespace fputil::generic |
| 103 | } // namespace LIBC_NAMESPACE_DECL |
| 104 | |
| 105 | #endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_MUL_H |
| 106 |
Warning: This file is not a C or C++ file. It does not have highlighting.
