Path: blob/main/contrib/llvm-project/libc/src/__support/FPUtil/ManipulationFunctions.h
213799 views
//===-- Floating-point manipulation functions -------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_MANIPULATIONFUNCTIONS_H9#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_MANIPULATIONFUNCTIONS_H1011#include "FPBits.h"12#include "NearestIntegerOperations.h"13#include "NormalFloat.h"14#include "cast.h"15#include "dyadic_float.h"16#include "rounding_mode.h"1718#include "hdr/math_macros.h"19#include "src/__support/CPP/bit.h"20#include "src/__support/CPP/limits.h" // INT_MAX, INT_MIN21#include "src/__support/CPP/type_traits.h"22#include "src/__support/FPUtil/FEnvImpl.h"23#include "src/__support/macros/attributes.h"24#include "src/__support/macros/config.h"25#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY2627namespace LIBC_NAMESPACE_DECL {28namespace fputil {2930template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>31LIBC_INLINE constexpr T frexp(T x, int &exp) {32FPBits<T> bits(x);33if (bits.is_inf_or_nan()) {34#ifdef LIBC_FREXP_INF_NAN_EXPONENT35// The value written back to the second parameter when calling36// frexp/frexpf/frexpl` with `+/-Inf`/`NaN` is unspecified in the standard.37// Set the exp value for Inf/NaN inputs explicitly to38// LIBC_FREXP_INF_NAN_EXPONENT if it is defined.39exp = LIBC_FREXP_INF_NAN_EXPONENT;40#endif // LIBC_FREXP_INF_NAN_EXPONENT41return x;42}43if (bits.is_zero()) {44exp = 0;45return x;46}4748NormalFloat<T> normal(bits);49exp = normal.exponent + 1;50normal.exponent = -1;51return normal;52}5354template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>55LIBC_INLINE T modf(T x, T &iptr) {56FPBits<T> bits(x);57if (bits.is_zero() || bits.is_nan()) {58iptr = x;59return x;60} else if (bits.is_inf()) {61iptr = x;62return FPBits<T>::zero(bits.sign()).get_val();63} else {64iptr = trunc(x);65if (x == iptr) {66// If x is already an integer value, then return zero with the right67// sign.68return FPBits<T>::zero(bits.sign()).get_val();69} else {70return x - iptr;71}72}73}7475template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>76LIBC_INLINE T copysign(T x, T y) {77FPBits<T> xbits(x);78xbits.set_sign(FPBits<T>(y).sign());79return xbits.get_val();80}8182template <typename T> struct IntLogbConstants;8384template <> struct IntLogbConstants<int> {85LIBC_INLINE_VAR static constexpr int FP_LOGB0 = FP_ILOGB0;86LIBC_INLINE_VAR static constexpr int FP_LOGBNAN = FP_ILOGBNAN;87LIBC_INLINE_VAR static constexpr int T_MAX = INT_MAX;88LIBC_INLINE_VAR static constexpr int T_MIN = INT_MIN;89};9091template <> struct IntLogbConstants<long> {92LIBC_INLINE_VAR static constexpr long FP_LOGB0 = FP_ILOGB0;93LIBC_INLINE_VAR static constexpr long FP_LOGBNAN = FP_ILOGBNAN;94LIBC_INLINE_VAR static constexpr long T_MAX = LONG_MAX;95LIBC_INLINE_VAR static constexpr long T_MIN = LONG_MIN;96};9798template <typename T, typename U>99LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<U>, T>100intlogb(U x) {101FPBits<U> bits(x);102if (LIBC_UNLIKELY(bits.is_zero() || bits.is_inf_or_nan())) {103set_errno_if_required(EDOM);104raise_except_if_required(FE_INVALID);105106if (bits.is_zero())107return IntLogbConstants<T>::FP_LOGB0;108if (bits.is_nan())109return IntLogbConstants<T>::FP_LOGBNAN;110// bits is inf.111return IntLogbConstants<T>::T_MAX;112}113114DyadicFloat<FPBits<U>::STORAGE_LEN> normal(bits.get_val());115int exponent = normal.get_unbiased_exponent();116// The C standard does not specify the return value when an exponent is117// out of int range. However, XSI conformance required that INT_MAX or118// INT_MIN are returned.119// NOTE: It is highly unlikely that exponent will be out of int range as120// the exponent is only 15 bits wide even for the 128-bit floating point121// format.122if (LIBC_UNLIKELY(exponent > IntLogbConstants<T>::T_MAX ||123exponent < IntLogbConstants<T>::T_MIN)) {124set_errno_if_required(ERANGE);125raise_except_if_required(FE_INVALID);126return exponent > 0 ? IntLogbConstants<T>::T_MAX127: IntLogbConstants<T>::T_MIN;128}129130return static_cast<T>(exponent);131}132133template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>134LIBC_INLINE constexpr T logb(T x) {135FPBits<T> bits(x);136if (LIBC_UNLIKELY(bits.is_zero() || bits.is_inf_or_nan())) {137if (bits.is_nan())138return x;139140raise_except_if_required(FE_DIVBYZERO);141142if (bits.is_zero()) {143set_errno_if_required(ERANGE);144return FPBits<T>::inf(Sign::NEG).get_val();145}146// bits is inf.147return FPBits<T>::inf().get_val();148}149150DyadicFloat<FPBits<T>::STORAGE_LEN> normal(bits.get_val());151return static_cast<T>(normal.get_unbiased_exponent());152}153154template <typename T, typename U>155LIBC_INLINE constexpr cpp::enable_if_t<156cpp::is_floating_point_v<T> && cpp::is_integral_v<U>, T>157ldexp(T x, U exp) {158FPBits<T> bits(x);159if (LIBC_UNLIKELY((exp == 0) || bits.is_zero() || bits.is_inf_or_nan()))160return x;161162// NormalFloat uses int32_t to store the true exponent value. We should ensure163// that adding |exp| to it does not lead to integer rollover. But, if |exp|164// value is larger the exponent range for type T, then we can return infinity165// early. Because the result of the ldexp operation can be a subnormal number,166// we need to accommodate the (mantissaWidth + 1) worth of shift in167// calculating the limit.168constexpr int EXP_LIMIT =169FPBits<T>::MAX_BIASED_EXPONENT + FPBits<T>::FRACTION_LEN + 1;170// Make sure that we can safely cast exp to int when not returning early.171static_assert(EXP_LIMIT <= INT_MAX && -EXP_LIMIT >= INT_MIN);172if (LIBC_UNLIKELY(exp > EXP_LIMIT)) {173int rounding_mode = quick_get_round();174Sign sign = bits.sign();175176if ((sign == Sign::POS && rounding_mode == FE_DOWNWARD) ||177(sign == Sign::NEG && rounding_mode == FE_UPWARD) ||178(rounding_mode == FE_TOWARDZERO))179return FPBits<T>::max_normal(sign).get_val();180181set_errno_if_required(ERANGE);182raise_except_if_required(FE_OVERFLOW);183return FPBits<T>::inf(sign).get_val();184}185186// Similarly on the negative side we return zero early if |exp| is too small.187if (LIBC_UNLIKELY(exp < -EXP_LIMIT)) {188int rounding_mode = quick_get_round();189Sign sign = bits.sign();190191if ((sign == Sign::POS && rounding_mode == FE_UPWARD) ||192(sign == Sign::NEG && rounding_mode == FE_DOWNWARD))193return FPBits<T>::min_subnormal(sign).get_val();194195set_errno_if_required(ERANGE);196raise_except_if_required(FE_UNDERFLOW);197return FPBits<T>::zero(sign).get_val();198}199200// For all other values, NormalFloat to T conversion handles it the right way.201DyadicFloat<FPBits<T>::STORAGE_LEN> normal(bits.get_val());202normal.exponent += static_cast<int>(exp);203// TODO: Add tests for exceptions.204return normal.template as<T, /*ShouldRaiseExceptions=*/true>();205}206207template <typename T, typename U,208cpp::enable_if_t<cpp::is_floating_point_v<T> &&209cpp::is_floating_point_v<U> &&210(sizeof(T) <= sizeof(U)),211int> = 0>212LIBC_INLINE T nextafter(T from, U to) {213FPBits<T> from_bits(from);214if (from_bits.is_nan())215return from;216217FPBits<U> to_bits(to);218if (to_bits.is_nan())219return cast<T>(to);220221// NOTE: This would work only if `U` has a greater or equal precision than222// `T`. Otherwise `from` could loose its precision and the following statement223// could incorrectly evaluate to `true`.224if (cast<U>(from) == to)225return cast<T>(to);226227using StorageType = typename FPBits<T>::StorageType;228if (from != T(0)) {229if ((cast<U>(from) < to) == (from > T(0))) {230from_bits = FPBits<T>(StorageType(from_bits.uintval() + 1));231} else {232from_bits = FPBits<T>(StorageType(from_bits.uintval() - 1));233}234} else {235from_bits = FPBits<T>::min_subnormal(to_bits.sign());236}237238if (from_bits.is_subnormal())239raise_except_if_required(FE_UNDERFLOW | FE_INEXACT);240else if (from_bits.is_inf())241raise_except_if_required(FE_OVERFLOW | FE_INEXACT);242243return from_bits.get_val();244}245246template <bool IsDown, typename T,247cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>248LIBC_INLINE constexpr T nextupdown(T x) {249constexpr Sign sign = IsDown ? Sign::NEG : Sign::POS;250251FPBits<T> xbits(x);252if (xbits.is_nan() || xbits == FPBits<T>::max_normal(sign) ||253xbits == FPBits<T>::inf(sign))254return x;255256using StorageType = typename FPBits<T>::StorageType;257if (x != T(0)) {258if (xbits.sign() == sign) {259xbits = FPBits<T>(StorageType(xbits.uintval() + 1));260} else {261xbits = FPBits<T>(StorageType(xbits.uintval() - 1));262}263} else {264xbits = FPBits<T>::min_subnormal(sign);265}266267return xbits.get_val();268}269270} // namespace fputil271} // namespace LIBC_NAMESPACE_DECL272273#ifdef LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80274#include "x86_64/NextAfterLongDouble.h"275#include "x86_64/NextUpDownLongDouble.h"276#endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80277278#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_MANIPULATIONFUNCTIONS_H279280281