1 //===-- Floating point divsion and remainder operations ---------*- 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_DIVISION_AND_REMAINDER_OPERATIONS_H
10 #define LLVM_LIBC_SRC_SUPPORT_FPUTIL_DIVISION_AND_REMAINDER_OPERATIONS_H
11
12 #include "FPBits.h"
13 #include "ManipulationFunctions.h"
14 #include "NormalFloat.h"
15
16 #include "utils/CPP/TypeTraits.h"
17
18 namespace __llvm_libc {
19 namespace fputil {
20
21 static constexpr int quotientLSBBits = 3;
22
23 // The implementation is a bit-by-bit algorithm which uses integer division
24 // to evaluate the quotient and remainder.
25 template <typename T,
26 cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
remquo(T x,T y,int & q)27 static inline T remquo(T x, T y, int &q) {
28 FPBits<T> xbits(x), ybits(y);
29 if (xbits.isNaN())
30 return x;
31 if (ybits.isNaN())
32 return y;
33 if (xbits.isInf() || ybits.isZero())
34 return FPBits<T>::buildNaN(1);
35
36 if (xbits.isZero()) {
37 q = 0;
38 return __llvm_libc::fputil::copysign(T(0.0), x);
39 }
40
41 if (ybits.isInf()) {
42 q = 0;
43 return x;
44 }
45
46 bool resultSign = (xbits.getSign() == ybits.getSign() ? false : true);
47
48 // Once we know the sign of the result, we can just operate on the absolute
49 // values. The correct sign can be applied to the result after the result
50 // is evaluated.
51 xbits.setSign(0);
52 ybits.setSign(0);
53
54 NormalFloat<T> normalx(xbits), normaly(ybits);
55 int exp = normalx.exponent - normaly.exponent;
56 typename NormalFloat<T>::UIntType mx = normalx.mantissa,
57 my = normaly.mantissa;
58
59 q = 0;
60 while (exp >= 0) {
61 unsigned shiftCount = 0;
62 typename NormalFloat<T>::UIntType n = mx;
63 for (shiftCount = 0; n < my; n <<= 1, ++shiftCount)
64 ;
65
66 if (static_cast<int>(shiftCount) > exp)
67 break;
68
69 exp -= shiftCount;
70 if (0 <= exp && exp < quotientLSBBits)
71 q |= (1 << exp);
72
73 mx = n - my;
74 if (mx == 0) {
75 q = resultSign ? -q : q;
76 return __llvm_libc::fputil::copysign(T(0.0), x);
77 }
78 }
79
80 NormalFloat<T> remainder(exp + normaly.exponent, mx, 0);
81
82 // Since NormalFloat to native type conversion is a truncation operation
83 // currently, the remainder value in the native type is correct as is.
84 // However, if NormalFloat to native type conversion is updated in future,
85 // then the conversion to native remainder value should be updated
86 // appropriately and some directed tests added.
87 T nativeRemainder(remainder);
88 T absy = T(ybits);
89 int cmp = remainder.mul2(1).cmp(normaly);
90 if (cmp > 0) {
91 q = q + 1;
92 if (x >= T(0.0))
93 nativeRemainder = nativeRemainder - absy;
94 else
95 nativeRemainder = absy - nativeRemainder;
96 } else if (cmp == 0) {
97 if (q & 1) {
98 q += 1;
99 if (x >= T(0.0))
100 nativeRemainder = -nativeRemainder;
101 } else {
102 if (x < T(0.0))
103 nativeRemainder = -nativeRemainder;
104 }
105 } else {
106 if (x < T(0.0))
107 nativeRemainder = -nativeRemainder;
108 }
109
110 q = resultSign ? -q : q;
111 if (nativeRemainder == T(0.0))
112 return __llvm_libc::fputil::copysign(T(0.0), x);
113 return nativeRemainder;
114 }
115
116 } // namespace fputil
117 } // namespace __llvm_libc
118
119 #endif // LLVM_LIBC_SRC_SUPPORT_FPUTIL_DIVISION_AND_REMAINDER_OPERATIONS_H
120