| 1 | //===-- nextafter 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_NEXTAFTERLONGDOUBLE_H |
| 10 | #define LLVM_LIBC_SRC___SUPPORT_FPUTIL_X86_64_NEXTAFTERLONGDOUBLE_H |
| 11 | |
| 12 | #include "src/__support/macros/config.h" |
| 13 | #include "src/__support/macros/properties/architectures.h" |
| 14 | |
| 15 | #if !defined(LIBC_TARGET_ARCH_IS_X86) |
| 16 | #error "Invalid include" |
| 17 | #endif |
| 18 | |
| 19 | #include "src/__support/CPP/bit.h" |
| 20 | #include "src/__support/FPUtil/FEnvImpl.h" |
| 21 | #include "src/__support/FPUtil/FPBits.h" |
| 22 | #include "src/__support/macros/attributes.h" |
| 23 | #include "src/__support/sign.h" |
| 24 | |
| 25 | namespace LIBC_NAMESPACE_DECL { |
| 26 | namespace fputil { |
| 27 | |
| 28 | LIBC_INLINE LIBC_BIT_CAST_CONSTEXPR long double nextafter(long double from, |
| 29 | long double to) { |
| 30 | using FPBits = FPBits<long double>; |
| 31 | FPBits from_bits(from); |
| 32 | if (from_bits.is_nan()) |
| 33 | return from; |
| 34 | |
| 35 | FPBits to_bits(to); |
| 36 | if (to_bits.is_nan()) |
| 37 | return to; |
| 38 | |
| 39 | if (from == to) |
| 40 | return to; |
| 41 | |
| 42 | // Convert pseudo subnormal number to normal number. |
| 43 | if (from_bits.get_implicit_bit() == 1 && from_bits.is_subnormal()) { |
| 44 | from_bits.set_biased_exponent(1); |
| 45 | } |
| 46 | |
| 47 | using StorageType = FPBits::StorageType; |
| 48 | |
| 49 | constexpr StorageType FRACTION_MASK = FPBits::FRACTION_MASK; |
| 50 | // StorageType int_val = from_bits.uintval(); |
| 51 | if (from == 0.0l) { // +0.0 / -0.0 |
| 52 | from_bits = FPBits::min_subnormal(sign: from > to ? Sign::NEG : Sign::POS); |
| 53 | } else if (from < 0.0l) { |
| 54 | if (to < from) { // toward -inf |
| 55 | if (from_bits == FPBits::max_subnormal(sign: Sign::NEG)) { |
| 56 | // We deal with normal/subnormal boundary separately to avoid |
| 57 | // dealing with the implicit bit. |
| 58 | from_bits = FPBits::min_normal(sign: Sign::NEG); |
| 59 | } else if (from_bits.get_mantissa() == FRACTION_MASK) { |
| 60 | from_bits.set_mantissa(0); |
| 61 | // Incrementing exponent might overflow the value to infinity, |
| 62 | // which is what is expected. Since NaNs are handling separately, |
| 63 | // it will never overflow "beyond" infinity. |
| 64 | from_bits.set_biased_exponent(from_bits.get_biased_exponent() + 1); |
| 65 | if (from_bits.is_inf()) |
| 66 | raise_overflow_except_if_required<long double>(); |
| 67 | return from_bits.get_val(); |
| 68 | } else { |
| 69 | from_bits = FPBits(StorageType(from_bits.uintval() + 1)); |
| 70 | } |
| 71 | } else { // toward +inf |
| 72 | if (from_bits == FPBits::min_normal(sign: Sign::NEG)) { |
| 73 | // We deal with normal/subnormal boundary separately to avoid |
| 74 | // dealing with the implicit bit. |
| 75 | from_bits = FPBits::max_subnormal(sign: Sign::NEG); |
| 76 | } else if (from_bits.get_mantissa() == 0) { |
| 77 | from_bits.set_mantissa(FRACTION_MASK); |
| 78 | // from == 0 is handled separately so decrementing the exponent will not |
| 79 | // lead to underflow. |
| 80 | from_bits.set_biased_exponent(from_bits.get_biased_exponent() - 1); |
| 81 | return from_bits.get_val(); |
| 82 | } else { |
| 83 | from_bits = FPBits(StorageType(from_bits.uintval() - 1)); |
| 84 | } |
| 85 | } |
| 86 | } else { |
| 87 | if (to < from) { // toward -inf |
| 88 | if (from_bits == FPBits::min_normal(sign: Sign::POS)) { |
| 89 | from_bits = FPBits::max_subnormal(sign: Sign::POS); |
| 90 | } else if (from_bits.get_mantissa() == 0) { |
| 91 | from_bits.set_mantissa(FRACTION_MASK); |
| 92 | // from == 0 is handled separately so decrementing the exponent will not |
| 93 | // lead to underflow. |
| 94 | from_bits.set_biased_exponent(from_bits.get_biased_exponent() - 1); |
| 95 | return from_bits.get_val(); |
| 96 | } else { |
| 97 | from_bits = FPBits(StorageType(from_bits.uintval() - 1)); |
| 98 | } |
| 99 | } else { // toward +inf |
| 100 | if (from_bits == FPBits::max_subnormal(sign: Sign::POS)) { |
| 101 | from_bits = FPBits::min_normal(sign: Sign::POS); |
| 102 | } else if (from_bits.get_mantissa() == FRACTION_MASK) { |
| 103 | from_bits.set_mantissa(0); |
| 104 | // Incrementing exponent might overflow the value to infinity, |
| 105 | // which is what is expected. Since NaNs are handling separately, |
| 106 | // it will never overflow "beyond" infinity. |
| 107 | from_bits.set_biased_exponent(from_bits.get_biased_exponent() + 1); |
| 108 | if (from_bits.is_inf()) |
| 109 | raise_overflow_except_if_required<long double>(); |
| 110 | return from_bits.get_val(); |
| 111 | } else { |
| 112 | from_bits = FPBits(StorageType(from_bits.uintval() + 1)); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if (!from_bits.get_implicit_bit()) |
| 118 | raise_underflow_except_if_required<long double>(); |
| 119 | |
| 120 | return from_bits.get_val(); |
| 121 | } |
| 122 | |
| 123 | } // namespace fputil |
| 124 | } // namespace LIBC_NAMESPACE_DECL |
| 125 | |
| 126 | #endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_X86_64_NEXTAFTERLONGDOUBLE_H |
| 127 |