1c120edc7SMichael Jones //===-- Floating-point manipulation functions -------------------*- C++ -*-===// 2c120edc7SMichael Jones // 3c120edc7SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4c120edc7SMichael Jones // See https://llvm.org/LICENSE.txt for license information. 5c120edc7SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6c120edc7SMichael Jones // 7c120edc7SMichael Jones //===----------------------------------------------------------------------===// 8c120edc7SMichael Jones 9270547f3SGuillaume Chatelet #ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_MANIPULATIONFUNCTIONS_H 10270547f3SGuillaume Chatelet #define LLVM_LIBC_SRC___SUPPORT_FPUTIL_MANIPULATIONFUNCTIONS_H 11c120edc7SMichael Jones 12c120edc7SMichael Jones #include "FPBits.h" 13c120edc7SMichael Jones #include "NearestIntegerOperations.h" 14c120edc7SMichael Jones #include "NormalFloat.h" 15127349fcSOverMighty #include "cast.h" 16ff409d39Slntue #include "dyadic_float.h" 17ff409d39Slntue #include "rounding_mode.h" 18c120edc7SMichael Jones 195748ad84Slntue #include "hdr/math_macros.h" 20d769cd8cSGuillaume Chatelet #include "src/__support/CPP/bit.h" 2172ce6294Slntue #include "src/__support/CPP/limits.h" // INT_MAX, INT_MIN 22f7226150SGuillaume Chatelet #include "src/__support/CPP/type_traits.h" 230c49fc4cSNishant Mittal #include "src/__support/FPUtil/FEnvImpl.h" 24eb8e1bf9SSiva Chandra Reddy #include "src/__support/macros/attributes.h" 255ff3ff33SPetr Hosek #include "src/__support/macros/config.h" 26737e1cd1SGuillaume Chatelet #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY 27c120edc7SMichael Jones 285ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL { 29c120edc7SMichael Jones namespace fputil { 30c120edc7SMichael Jones 31f7226150SGuillaume Chatelet template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0> 3259c809cdSSiva Chandra Reddy LIBC_INLINE T frexp(T x, int &exp) { 33c120edc7SMichael Jones FPBits<T> bits(x); 34*b0dbd2caSlntue if (bits.is_inf_or_nan()) { 35*b0dbd2caSlntue #ifdef LIBC_FREXP_INF_NAN_EXPONENT 36*b0dbd2caSlntue // The value written back to the second parameter when calling 37*b0dbd2caSlntue // frexp/frexpf/frexpl` with `+/-Inf`/`NaN` is unspecified in the standard. 38*b0dbd2caSlntue // Set the exp value for Inf/NaN inputs explicitly to 39*b0dbd2caSlntue // LIBC_FREXP_INF_NAN_EXPONENT if it is defined. 40*b0dbd2caSlntue exp = LIBC_FREXP_INF_NAN_EXPONENT; 41*b0dbd2caSlntue #endif // LIBC_FREXP_INF_NAN_EXPONENT 42c120edc7SMichael Jones return x; 43*b0dbd2caSlntue } 441c92911eSMichael Jones if (bits.is_zero()) { 45c120edc7SMichael Jones exp = 0; 46c120edc7SMichael Jones return x; 47c120edc7SMichael Jones } 48c120edc7SMichael Jones 49c120edc7SMichael Jones NormalFloat<T> normal(bits); 50c120edc7SMichael Jones exp = normal.exponent + 1; 51c120edc7SMichael Jones normal.exponent = -1; 52c120edc7SMichael Jones return normal; 53c120edc7SMichael Jones } 54c120edc7SMichael Jones 55f7226150SGuillaume Chatelet template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0> 5659c809cdSSiva Chandra Reddy LIBC_INLINE T modf(T x, T &iptr) { 57c120edc7SMichael Jones FPBits<T> bits(x); 581c92911eSMichael Jones if (bits.is_zero() || bits.is_nan()) { 59c120edc7SMichael Jones iptr = x; 60c120edc7SMichael Jones return x; 611c92911eSMichael Jones } else if (bits.is_inf()) { 62c120edc7SMichael Jones iptr = x; 632856db0dSGuillaume Chatelet return FPBits<T>::zero(bits.sign()).get_val(); 64c120edc7SMichael Jones } else { 65c120edc7SMichael Jones iptr = trunc(x); 66c120edc7SMichael Jones if (x == iptr) { 67c120edc7SMichael Jones // If x is already an integer value, then return zero with the right 68c120edc7SMichael Jones // sign. 692856db0dSGuillaume Chatelet return FPBits<T>::zero(bits.sign()).get_val(); 70c120edc7SMichael Jones } else { 71c120edc7SMichael Jones return x - iptr; 72c120edc7SMichael Jones } 73c120edc7SMichael Jones } 74c120edc7SMichael Jones } 75c120edc7SMichael Jones 76f7226150SGuillaume Chatelet template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0> 7759c809cdSSiva Chandra Reddy LIBC_INLINE T copysign(T x, T y) { 78c120edc7SMichael Jones FPBits<T> xbits(x); 7911ec512fSGuillaume Chatelet xbits.set_sign(FPBits<T>(y).sign()); 802856db0dSGuillaume Chatelet return xbits.get_val(); 81c120edc7SMichael Jones } 82c120edc7SMichael Jones 83aa95aa69Slntue template <typename T> struct IntLogbConstants; 84aa95aa69Slntue 85aa95aa69Slntue template <> struct IntLogbConstants<int> { 86aa95aa69Slntue LIBC_INLINE_VAR static constexpr int FP_LOGB0 = FP_ILOGB0; 87aa95aa69Slntue LIBC_INLINE_VAR static constexpr int FP_LOGBNAN = FP_ILOGBNAN; 88aa95aa69Slntue LIBC_INLINE_VAR static constexpr int T_MAX = INT_MAX; 89aa95aa69Slntue LIBC_INLINE_VAR static constexpr int T_MIN = INT_MIN; 90aa95aa69Slntue }; 91aa95aa69Slntue 92aa95aa69Slntue template <> struct IntLogbConstants<long> { 93aa95aa69Slntue LIBC_INLINE_VAR static constexpr long FP_LOGB0 = FP_ILOGB0; 94aa95aa69Slntue LIBC_INLINE_VAR static constexpr long FP_LOGBNAN = FP_ILOGBNAN; 95aa95aa69Slntue LIBC_INLINE_VAR static constexpr long T_MAX = LONG_MAX; 96aa95aa69Slntue LIBC_INLINE_VAR static constexpr long T_MIN = LONG_MIN; 97aa95aa69Slntue }; 98aa95aa69Slntue 99aa95aa69Slntue template <typename T, typename U> 100aa95aa69Slntue LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<U>, T> 101aa95aa69Slntue intlogb(U x) { 102aa95aa69Slntue FPBits<U> bits(x); 103aa95aa69Slntue if (LIBC_UNLIKELY(bits.is_zero() || bits.is_inf_or_nan())) { 104aa95aa69Slntue set_errno_if_required(EDOM); 105aa95aa69Slntue raise_except_if_required(FE_INVALID); 106aa95aa69Slntue 107aa95aa69Slntue if (bits.is_zero()) 108aa95aa69Slntue return IntLogbConstants<T>::FP_LOGB0; 109aa95aa69Slntue if (bits.is_nan()) 110aa95aa69Slntue return IntLogbConstants<T>::FP_LOGBNAN; 111aa95aa69Slntue // bits is inf. 112aa95aa69Slntue return IntLogbConstants<T>::T_MAX; 113c120edc7SMichael Jones } 114c120edc7SMichael Jones 1156b21e170SOverMighty DyadicFloat<FPBits<U>::STORAGE_LEN> normal(bits.get_val()); 116aa95aa69Slntue int exponent = normal.get_unbiased_exponent(); 117c120edc7SMichael Jones // The C standard does not specify the return value when an exponent is 118c120edc7SMichael Jones // out of int range. However, XSI conformance required that INT_MAX or 119c120edc7SMichael Jones // INT_MIN are returned. 120c120edc7SMichael Jones // NOTE: It is highly unlikely that exponent will be out of int range as 121c120edc7SMichael Jones // the exponent is only 15 bits wide even for the 128-bit floating point 122c120edc7SMichael Jones // format. 123aa95aa69Slntue if (LIBC_UNLIKELY(exponent > IntLogbConstants<T>::T_MAX || 124aa95aa69Slntue exponent < IntLogbConstants<T>::T_MIN)) { 125aa95aa69Slntue set_errno_if_required(ERANGE); 126aa95aa69Slntue raise_except_if_required(FE_INVALID); 127aa95aa69Slntue return exponent > 0 ? IntLogbConstants<T>::T_MAX 128aa95aa69Slntue : IntLogbConstants<T>::T_MIN; 129aa95aa69Slntue } 130aa95aa69Slntue 131aa95aa69Slntue return static_cast<T>(exponent); 132c120edc7SMichael Jones } 133c120edc7SMichael Jones 134f7226150SGuillaume Chatelet template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0> 135aa95aa69Slntue LIBC_INLINE constexpr T logb(T x) { 136c120edc7SMichael Jones FPBits<T> bits(x); 137aa95aa69Slntue if (LIBC_UNLIKELY(bits.is_zero() || bits.is_inf_or_nan())) { 138aa95aa69Slntue if (bits.is_nan()) 139c120edc7SMichael Jones return x; 140aa95aa69Slntue 141aa95aa69Slntue raise_except_if_required(FE_DIVBYZERO); 142aa95aa69Slntue 143aa95aa69Slntue if (bits.is_zero()) { 144aa95aa69Slntue set_errno_if_required(ERANGE); 145aa95aa69Slntue return FPBits<T>::inf(Sign::NEG).get_val(); 146aa95aa69Slntue } 147aa95aa69Slntue // bits is inf. 1482856db0dSGuillaume Chatelet return FPBits<T>::inf().get_val(); 149c120edc7SMichael Jones } 150c120edc7SMichael Jones 1516b21e170SOverMighty DyadicFloat<FPBits<T>::STORAGE_LEN> normal(bits.get_val()); 152aa95aa69Slntue return static_cast<T>(normal.get_unbiased_exponent()); 153c120edc7SMichael Jones } 154c120edc7SMichael Jones 155b5efd214SOverMighty template <typename T, typename U> 156b5efd214SOverMighty LIBC_INLINE constexpr cpp::enable_if_t< 157b5efd214SOverMighty cpp::is_floating_point_v<T> && cpp::is_integral_v<U>, T> 158b5efd214SOverMighty ldexp(T x, U exp) { 159c120edc7SMichael Jones FPBits<T> bits(x); 160ff409d39Slntue if (LIBC_UNLIKELY((exp == 0) || bits.is_zero() || bits.is_inf_or_nan())) 161c120edc7SMichael Jones return x; 162c120edc7SMichael Jones 163c120edc7SMichael Jones // NormalFloat uses int32_t to store the true exponent value. We should ensure 164c120edc7SMichael Jones // that adding |exp| to it does not lead to integer rollover. But, if |exp| 165c120edc7SMichael Jones // value is larger the exponent range for type T, then we can return infinity 166c120edc7SMichael Jones // early. Because the result of the ldexp operation can be a subnormal number, 1676b02d2f8SGuillaume Chatelet // we need to accommodate the (mantissaWidth + 1) worth of shift in 168c120edc7SMichael Jones // calculating the limit. 169ff409d39Slntue constexpr int EXP_LIMIT = 170ff409d39Slntue FPBits<T>::MAX_BIASED_EXPONENT + FPBits<T>::FRACTION_LEN + 1; 171b5efd214SOverMighty // Make sure that we can safely cast exp to int when not returning early. 172b5efd214SOverMighty static_assert(EXP_LIMIT <= INT_MAX && -EXP_LIMIT >= INT_MIN); 173ff409d39Slntue if (LIBC_UNLIKELY(exp > EXP_LIMIT)) { 174ff409d39Slntue int rounding_mode = quick_get_round(); 175ff409d39Slntue Sign sign = bits.sign(); 176ff409d39Slntue 177ff409d39Slntue if ((sign == Sign::POS && rounding_mode == FE_DOWNWARD) || 178ff409d39Slntue (sign == Sign::NEG && rounding_mode == FE_UPWARD) || 179ff409d39Slntue (rounding_mode == FE_TOWARDZERO)) 180ff409d39Slntue return FPBits<T>::max_normal(sign).get_val(); 181ff409d39Slntue 182ff409d39Slntue set_errno_if_required(ERANGE); 183ff409d39Slntue raise_except_if_required(FE_OVERFLOW); 184ff409d39Slntue return FPBits<T>::inf(sign).get_val(); 185ff409d39Slntue } 186c120edc7SMichael Jones 187c120edc7SMichael Jones // Similarly on the negative side we return zero early if |exp| is too small. 188ff409d39Slntue if (LIBC_UNLIKELY(exp < -EXP_LIMIT)) { 189ff409d39Slntue int rounding_mode = quick_get_round(); 190ff409d39Slntue Sign sign = bits.sign(); 191ff409d39Slntue 192ff409d39Slntue if ((sign == Sign::POS && rounding_mode == FE_UPWARD) || 193ff409d39Slntue (sign == Sign::NEG && rounding_mode == FE_DOWNWARD)) 194ff409d39Slntue return FPBits<T>::min_subnormal(sign).get_val(); 195ff409d39Slntue 196ff409d39Slntue set_errno_if_required(ERANGE); 197ff409d39Slntue raise_except_if_required(FE_UNDERFLOW); 198ff409d39Slntue return FPBits<T>::zero(sign).get_val(); 199ff409d39Slntue } 200c120edc7SMichael Jones 201c120edc7SMichael Jones // For all other values, NormalFloat to T conversion handles it the right way. 202ff409d39Slntue DyadicFloat<FPBits<T>::STORAGE_LEN> normal(bits.get_val()); 203b5efd214SOverMighty normal.exponent += static_cast<int>(exp); 204127349fcSOverMighty // TODO: Add tests for exceptions. 205127349fcSOverMighty return normal.template as<T, /*ShouldRaiseExceptions=*/true>(); 206c120edc7SMichael Jones } 207c120edc7SMichael Jones 208ac640c62SNishant Mittal template <typename T, typename U, 209ac640c62SNishant Mittal cpp::enable_if_t<cpp::is_floating_point_v<T> && 210ac640c62SNishant Mittal cpp::is_floating_point_v<U> && 211ac640c62SNishant Mittal (sizeof(T) <= sizeof(U)), 21286b0ccaeSmichaelrj-google int> = 0> 21386b0ccaeSmichaelrj-google LIBC_INLINE T nextafter(T from, U to) { 2141c92911eSMichael Jones FPBits<T> from_bits(from); 2151c92911eSMichael Jones if (from_bits.is_nan()) 216c120edc7SMichael Jones return from; 217c120edc7SMichael Jones 21886b0ccaeSmichaelrj-google FPBits<U> to_bits(to); 2191c92911eSMichael Jones if (to_bits.is_nan()) 220127349fcSOverMighty return cast<T>(to); 221c120edc7SMichael Jones 222ac640c62SNishant Mittal // NOTE: This would work only if `U` has a greater or equal precision than 223ac640c62SNishant Mittal // `T`. Otherwise `from` could loose its precision and the following statement 224ac640c62SNishant Mittal // could incorrectly evaluate to `true`. 225127349fcSOverMighty if (cast<U>(from) == to) 226127349fcSOverMighty return cast<T>(to); 227c120edc7SMichael Jones 2283546f4daSGuillaume Chatelet using StorageType = typename FPBits<T>::StorageType; 2296b02d2f8SGuillaume Chatelet if (from != T(0)) { 230127349fcSOverMighty if ((cast<U>(from) < to) == (from > T(0))) { 2316b02d2f8SGuillaume Chatelet from_bits = FPBits<T>(StorageType(from_bits.uintval() + 1)); 2320c49fc4cSNishant Mittal } else { 2336b02d2f8SGuillaume Chatelet from_bits = FPBits<T>(StorageType(from_bits.uintval() - 1)); 2340c49fc4cSNishant Mittal } 2350c49fc4cSNishant Mittal } else { 2366b02d2f8SGuillaume Chatelet from_bits = FPBits<T>::min_subnormal(to_bits.sign()); 2370c49fc4cSNishant Mittal } 2380c49fc4cSNishant Mittal 2396b02d2f8SGuillaume Chatelet if (from_bits.is_subnormal()) 2400c49fc4cSNishant Mittal raise_except_if_required(FE_UNDERFLOW | FE_INEXACT); 2416b02d2f8SGuillaume Chatelet else if (from_bits.is_inf()) 2420c49fc4cSNishant Mittal raise_except_if_required(FE_OVERFLOW | FE_INEXACT); 2430c49fc4cSNishant Mittal 2446b02d2f8SGuillaume Chatelet return from_bits.get_val(); 245c120edc7SMichael Jones } 246c120edc7SMichael Jones 2478ff96eb1SOverMighty template <bool IsDown, typename T, 2488ff96eb1SOverMighty cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0> 2498ff96eb1SOverMighty LIBC_INLINE constexpr T nextupdown(T x) { 2508ff96eb1SOverMighty constexpr Sign sign = IsDown ? Sign::NEG : Sign::POS; 2518ff96eb1SOverMighty 2528ff96eb1SOverMighty FPBits<T> xbits(x); 2538ff96eb1SOverMighty if (xbits.is_nan() || xbits == FPBits<T>::max_normal(sign) || 2548ff96eb1SOverMighty xbits == FPBits<T>::inf(sign)) 2558ff96eb1SOverMighty return x; 2568ff96eb1SOverMighty 2578ff96eb1SOverMighty using StorageType = typename FPBits<T>::StorageType; 2588ff96eb1SOverMighty if (x != T(0)) { 2598ff96eb1SOverMighty if (xbits.sign() == sign) { 2608ff96eb1SOverMighty xbits = FPBits<T>(StorageType(xbits.uintval() + 1)); 2618ff96eb1SOverMighty } else { 2628ff96eb1SOverMighty xbits = FPBits<T>(StorageType(xbits.uintval() - 1)); 2638ff96eb1SOverMighty } 2648ff96eb1SOverMighty } else { 2658ff96eb1SOverMighty xbits = FPBits<T>::min_subnormal(sign); 2668ff96eb1SOverMighty } 2678ff96eb1SOverMighty 2688ff96eb1SOverMighty return xbits.get_val(); 2698ff96eb1SOverMighty } 2708ff96eb1SOverMighty 271c120edc7SMichael Jones } // namespace fputil 2725ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL 273c120edc7SMichael Jones 274f7d4236aSGuillaume Chatelet #ifdef LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80 27571405d90SGuillaume Chatelet #include "x86_64/NextAfterLongDouble.h" 276a2bad758SOverMighty #include "x86_64/NextUpDownLongDouble.h" 277f7d4236aSGuillaume Chatelet #endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80 278c120edc7SMichael Jones 279270547f3SGuillaume Chatelet #endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_MANIPULATIONFUNCTIONS_H 280