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