1//===-- Floating-point manipulation functions -------------------*- 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_MANIPULATIONFUNCTIONS_H
10#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_MANIPULATIONFUNCTIONS_H
11
12#include "hdr/errno_macros.h"
13#include "hdr/fenv_macros.h"
14#include "hdr/math_macros.h"
15#include "src/__support/CPP/limits.h" // INT_MAX, INT_MIN
16#include "src/__support/CPP/type_traits.h"
17#include "src/__support/FPUtil/FEnvImpl.h"
18#include "src/__support/FPUtil/FPBits.h"
19#include "src/__support/FPUtil/NearestIntegerOperations.h"
20#include "src/__support/FPUtil/NormalFloat.h"
21#include "src/__support/FPUtil/cast.h"
22#include "src/__support/FPUtil/dyadic_float.h"
23#include "src/__support/FPUtil/except_value_utils.h"
24#include "src/__support/macros/attributes.h"
25#include "src/__support/macros/config.h"
26#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
27#include "src/__support/macros/properties/types.h"
28#include "src/__support/sign.h"
29
30namespace LIBC_NAMESPACE_DECL {
31namespace fputil {
32
33template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
34LIBC_INLINE constexpr T frexp(T x, int &exp) {
35 FPBits<T> bits(x);
36 if (bits.is_inf_or_nan()) {
37#ifdef LIBC_FREXP_INF_NAN_EXPONENT
38 // The value written back to the second parameter when calling
39 // frexp/frexpf/frexpl` with `+/-Inf`/`NaN` is unspecified in the standard.
40 // Set the exp value for Inf/NaN inputs explicitly to
41 // LIBC_FREXP_INF_NAN_EXPONENT if it is defined.
42 exp = LIBC_FREXP_INF_NAN_EXPONENT;
43#endif // LIBC_FREXP_INF_NAN_EXPONENT
44 return x;
45 }
46 if (bits.is_zero()) {
47 exp = 0;
48 return x;
49 }
50
51 NormalFloat<T> normal(bits);
52 exp = normal.exponent + 1;
53 normal.exponent = -1;
54 return normal;
55}
56
57template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
58LIBC_INLINE constexpr T modf(T x, T &iptr) {
59 FPBits<T> bits(x);
60 if (bits.is_zero() || bits.is_nan()) {
61 iptr = x;
62 return x;
63 } else if (bits.is_inf()) {
64 iptr = x;
65 return FPBits<T>::zero(bits.sign()).get_val();
66 } else {
67 iptr = trunc(x);
68 if (x == iptr) {
69 // If x is already an integer value, then return zero with the right
70 // sign.
71 return FPBits<T>::zero(bits.sign()).get_val();
72 } else {
73 return x - iptr;
74 }
75 }
76}
77
78template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
79LIBC_INLINE constexpr T copysign(T x, T y) {
80 FPBits<T> xbits(x);
81 xbits.set_sign(FPBits<T>(y).sign());
82 return xbits.get_val();
83}
84
85template <typename T> struct IntLogbConstants;
86
87template <> struct IntLogbConstants<int> {
88 LIBC_INLINE_VAR static constexpr int FP_LOGB0 = FP_ILOGB0;
89 LIBC_INLINE_VAR static constexpr int FP_LOGBNAN = FP_ILOGBNAN;
90 LIBC_INLINE_VAR static constexpr int T_MAX = INT_MAX;
91 LIBC_INLINE_VAR static constexpr int T_MIN = INT_MIN;
92};
93
94template <> struct IntLogbConstants<long> {
95 LIBC_INLINE_VAR static constexpr long FP_LOGB0 = FP_ILOGB0;
96 LIBC_INLINE_VAR static constexpr long FP_LOGBNAN = FP_ILOGBNAN;
97 LIBC_INLINE_VAR static constexpr long T_MAX = LONG_MAX;
98 LIBC_INLINE_VAR static constexpr long T_MIN = LONG_MIN;
99};
100
101template <typename T, typename U>
102LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<U>, T>
103intlogb(U x) {
104 FPBits<U> bits(x);
105 if (LIBC_UNLIKELY(bits.is_zero() || bits.is_inf_or_nan())) {
106 set_errno_if_required(EDOM);
107 raise_except_if_required(FE_INVALID);
108
109 if (bits.is_zero())
110 return IntLogbConstants<T>::FP_LOGB0;
111 if (bits.is_nan())
112 return IntLogbConstants<T>::FP_LOGBNAN;
113 // bits is inf.
114 return IntLogbConstants<T>::T_MAX;
115 }
116
117 DyadicFloat<FPBits<U>::STORAGE_LEN> normal(bits.get_val());
118 int exponent = normal.get_unbiased_exponent();
119 // The C standard does not specify the return value when an exponent is
120 // out of int range. However, XSI conformance required that INT_MAX or
121 // INT_MIN are returned.
122 // NOTE: It is highly unlikely that exponent will be out of int range as
123 // the exponent is only 15 bits wide even for the 128-bit floating point
124 // format.
125 if (LIBC_UNLIKELY(exponent > IntLogbConstants<T>::T_MAX ||
126 exponent < IntLogbConstants<T>::T_MIN)) {
127 set_errno_if_required(ERANGE);
128 raise_except_if_required(FE_INVALID);
129 return exponent > 0 ? IntLogbConstants<T>::T_MAX
130 : IntLogbConstants<T>::T_MIN;
131 }
132
133 return static_cast<T>(exponent);
134}
135
136template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
137LIBC_INLINE constexpr T logb(T x) {
138 FPBits<T> bits(x);
139 if (LIBC_UNLIKELY(bits.is_zero() || bits.is_inf_or_nan())) {
140 if (bits.is_nan()) {
141 if (bits.is_signaling_nan()) {
142 raise_except_if_required(FE_INVALID);
143 return FPBits<T>::quiet_nan().get_val();
144 }
145 return x;
146 }
147
148 if (bits.is_zero()) {
149 set_errno_if_required(ERANGE);
150 raise_except_if_required(FE_DIVBYZERO);
151 return FPBits<T>::inf(Sign::NEG).get_val();
152 }
153 // bits is inf.
154 return FPBits<T>::inf().get_val();
155 }
156
157 DyadicFloat<FPBits<T>::STORAGE_LEN> normal(bits.get_val());
158 return static_cast<T>(normal.get_unbiased_exponent());
159}
160
161template <typename T, typename U>
162LIBC_INLINE constexpr cpp::enable_if_t<
163 cpp::is_floating_point_v<T> && cpp::is_integral_v<U>, T>
164ldexp(T x, U exp) {
165 FPBits<T> bits(x);
166 if (LIBC_UNLIKELY((exp == 0) || bits.is_zero() || bits.is_inf_or_nan()))
167 return x;
168
169 // NormalFloat uses int32_t to store the true exponent value. We should ensure
170 // that adding |exp| to it does not lead to integer rollover. But, if |exp|
171 // value is larger the exponent range for type T, then we can return infinity
172 // early. Because the result of the ldexp operation can be a subnormal number,
173 // we need to accommodate the (mantissaWidth + 1) worth of shift in
174 // calculating the limit.
175 constexpr int EXP_LIMIT =
176 FPBits<T>::MAX_BIASED_EXPONENT + FPBits<T>::FRACTION_LEN + 1;
177 // Make sure that we can safely cast exp to int when not returning early.
178 static_assert(EXP_LIMIT <= INT_MAX && -EXP_LIMIT >= INT_MIN);
179
180 if (LIBC_UNLIKELY(exp > EXP_LIMIT))
181 return set_overflow<T>(bits.sign());
182
183 // Similarly on the negative side we return zero early if |exp| is too small.
184 if (LIBC_UNLIKELY(exp < -EXP_LIMIT))
185 return set_underflow<T>(bits.sign());
186
187 // For all other values, NormalFloat to T conversion handles it the right way.
188 DyadicFloat<FPBits<T>::STORAGE_LEN> normal(bits.get_val());
189 normal.exponent += static_cast<int>(exp);
190 // TODO: Add tests for exceptions.
191 return normal.template as<T, /*ShouldRaiseExceptions=*/true>();
192}
193
194template <typename T, typename U,
195 cpp::enable_if_t<cpp::is_floating_point_v<T> &&
196 cpp::is_floating_point_v<U> &&
197 (sizeof(T) <= sizeof(U)),
198 int> = 0>
199LIBC_INLINE constexpr T nextafter(T from, U to) {
200 FPBits<T> from_bits(from);
201 if (from_bits.is_nan())
202 return from;
203
204 FPBits<U> to_bits(to);
205 if (to_bits.is_nan())
206 return cast<T>(to);
207
208 // NOTE: This would work only if `U` has a greater or equal precision than
209 // `T`. Otherwise `from` could loose its precision and the following statement
210 // could incorrectly evaluate to `true`.
211 if (cast<U>(from) == to)
212 return cast<T>(to);
213
214 using StorageType = typename FPBits<T>::StorageType;
215 if (from != T(0)) {
216 if ((cast<U>(from) < to) == (from > T(0))) {
217 from_bits = FPBits<T>(StorageType(from_bits.uintval() + 1));
218 } else {
219 from_bits = FPBits<T>(StorageType(from_bits.uintval() - 1));
220 }
221 } else {
222 from_bits = FPBits<T>::min_subnormal(to_bits.sign());
223 }
224
225 if (from_bits.is_subnormal())
226 raise_underflow_except_if_required<T>();
227 else if (from_bits.is_inf())
228 raise_overflow_except_if_required<T>();
229
230 return from_bits.get_val();
231}
232
233template <bool IsDown, typename T,
234 cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
235LIBC_INLINE constexpr T nextupdown(T x) {
236 constexpr Sign sign = IsDown ? Sign::NEG : Sign::POS;
237
238 FPBits<T> xbits(x);
239 if (xbits.is_nan() || xbits == FPBits<T>::inf(sign))
240 return x;
241
242 using StorageType = typename FPBits<T>::StorageType;
243 if (!xbits.is_zero()) {
244 if (xbits.sign() == sign) {
245 xbits = FPBits<T>(StorageType(xbits.uintval() + 1));
246 } else {
247 xbits = FPBits<T>(StorageType(xbits.uintval() - 1));
248 }
249 } else {
250 xbits = FPBits<T>::min_subnormal(sign);
251 }
252
253 return xbits.get_val();
254}
255
256} // namespace fputil
257} // namespace LIBC_NAMESPACE_DECL
258
259#ifdef LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
260#include "src/__support/FPUtil/x86_64/NextAfterLongDouble.h"
261#include "src/__support/FPUtil/x86_64/NextUpDownLongDouble.h"
262#endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
263
264#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_MANIPULATIONFUNCTIONS_H
265