1 //===-- nextupdown implementation for x86 long double 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_X86_64_NEXTUPDOWNLONGDOUBLE_H 10 #define LLVM_LIBC_SRC___SUPPORT_FPUTIL_X86_64_NEXTUPDOWNLONGDOUBLE_H 11 12 #include "src/__support/FPUtil/FPBits.h" 13 #include "src/__support/macros/attributes.h" 14 #include "src/__support/macros/properties/architectures.h" 15 16 #if !defined(LIBC_TARGET_ARCH_IS_X86) 17 #error "Invalid include" 18 #endif 19 20 namespace LIBC_NAMESPACE::fputil { 21 22 template <bool IsDown> nextupdown(long double x)23LIBC_INLINE constexpr long double nextupdown(long double x) { 24 constexpr Sign sign = IsDown ? Sign::NEG : Sign::POS; 25 26 using FPBits_t = FPBits<long double>; 27 FPBits_t xbits(x); 28 if (xbits.is_nan() || xbits == FPBits_t::max_normal(sign) || 29 xbits == FPBits_t::inf(sign)) 30 return x; 31 32 if (x == 0.0l) 33 return FPBits_t::min_subnormal(sign).get_val(); 34 35 using StorageType = typename FPBits_t::StorageType; 36 37 if (xbits.sign() == sign) { 38 if (xbits.get_mantissa() == FPBits_t::FRACTION_MASK) { 39 xbits.set_mantissa(0); 40 xbits.set_biased_exponent(xbits.get_biased_exponent() + 1); 41 } else { 42 xbits = FPBits_t(StorageType(xbits.uintval() + 1)); 43 } 44 45 return xbits.get_val(); 46 } 47 48 if (xbits.get_mantissa() == 0) { 49 xbits.set_mantissa(FPBits_t::FRACTION_MASK); 50 xbits.set_biased_exponent(xbits.get_biased_exponent() - 1); 51 } else { 52 xbits = FPBits_t(StorageType(xbits.uintval() - 1)); 53 } 54 55 return xbits.get_val(); 56 } 57 58 } // namespace LIBC_NAMESPACE::fputil 59 60 #endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_X86_64_NEXTUPDOWNLONGDOUBLE_H 61