#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_X86_64_NEXTAFTERLONGDOUBLE_H
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_X86_64_NEXTAFTERLONGDOUBLE_H
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/architectures.h"
#if !defined(LIBC_TARGET_ARCH_IS_X86)
#error "Invalid include"
#endif
#include "src/__support/CPP/bit.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
#include <stdint.h>
namespace LIBC_NAMESPACE_DECL {
namespace fputil {
LIBC_INLINE long double nextafter(long double from, long double to) {
using FPBits = FPBits<long double>;
FPBits from_bits(from);
if (from_bits.is_nan())
return from;
FPBits to_bits(to);
if (to_bits.is_nan())
return to;
if (from == to)
return to;
if (from_bits.get_implicit_bit() == 1 && from_bits.is_subnormal()) {
from_bits.set_biased_exponent(1);
}
using StorageType = FPBits::StorageType;
constexpr StorageType FRACTION_MASK = FPBits::FRACTION_MASK;
if (from == 0.0l) {
from_bits = FPBits::min_subnormal(from > to ? Sign::NEG : Sign::POS);
} else if (from < 0.0l) {
if (to < from) {
if (from_bits == FPBits::max_subnormal(Sign::NEG)) {
from_bits = FPBits::min_normal(Sign::NEG);
} else if (from_bits.get_mantissa() == FRACTION_MASK) {
from_bits.set_mantissa(0);
from_bits.set_biased_exponent(from_bits.get_biased_exponent() + 1);
if (from_bits.is_inf())
raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
return from_bits.get_val();
} else {
from_bits = FPBits(StorageType(from_bits.uintval() + 1));
}
} else {
if (from_bits == FPBits::min_normal(Sign::NEG)) {
from_bits = FPBits::max_subnormal(Sign::NEG);
} else if (from_bits.get_mantissa() == 0) {
from_bits.set_mantissa(FRACTION_MASK);
from_bits.set_biased_exponent(from_bits.get_biased_exponent() - 1);
return from_bits.get_val();
} else {
from_bits = FPBits(StorageType(from_bits.uintval() - 1));
}
}
} else {
if (to < from) {
if (from_bits == FPBits::min_normal(Sign::POS)) {
from_bits = FPBits::max_subnormal(Sign::POS);
} else if (from_bits.get_mantissa() == 0) {
from_bits.set_mantissa(FRACTION_MASK);
from_bits.set_biased_exponent(from_bits.get_biased_exponent() - 1);
return from_bits.get_val();
} else {
from_bits = FPBits(StorageType(from_bits.uintval() - 1));
}
} else {
if (from_bits == FPBits::max_subnormal(Sign::POS)) {
from_bits = FPBits::min_normal(Sign::POS);
} else if (from_bits.get_mantissa() == FRACTION_MASK) {
from_bits.set_mantissa(0);
from_bits.set_biased_exponent(from_bits.get_biased_exponent() + 1);
if (from_bits.is_inf())
raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
return from_bits.get_val();
} else {
from_bits = FPBits(StorageType(from_bits.uintval() + 1));
}
}
}
if (!from_bits.get_implicit_bit())
raise_except_if_required(FE_UNDERFLOW | FE_INEXACT);
return from_bits.get_val();
}
}
}
#endif