#include <sys/cdefs.h>
__RCSID("$NetBSD: s_nextafterl.c,v 1.7 2026/04/02 22:02:10 nat Exp $");
#include <float.h>
#include <math.h>
#include <machine/ieee.h>
#ifdef __HAVE_LONG_DOUBLE
#ifdef EXT_EXP_INFNAN
#if LDBL_MAX_EXP != 0x4000
#error "Unsupported long double format"
#endif
#ifdef LDBL_IMPLICIT_NBIT
#define LDBL_NBIT 0
#endif
__strong_alias(nexttowardl, nextafterl)
long double
nextafterl(long double x, long double y)
{
volatile long double t;
union ieee_ext_u ux, uy;
ux.extu_ld = x;
uy.extu_ld = y;
if ((ux.extu_exp == EXT_EXP_INFNAN &&
((ux.extu_frach &~ LDBL_NBIT)|ux.extu_fracl) != 0) ||
(uy.extu_exp == EXT_EXP_INFNAN &&
((uy.extu_frach &~ LDBL_NBIT)|uy.extu_fracl) != 0))
return x+y;
if (x == y) return y;
if (x == 0.0) {
ux.extu_frach = 0;
ux.extu_fracl = 1;
ux.extu_sign = uy.extu_sign;
t = ux.extu_ld * ux.extu_ld;
if (t == ux.extu_ld)
return t;
else
return ux.extu_ld;
}
uint32_t nbit = (ux.extu_frach & LDBL_NBIT);
if ((x>0.0) ^ (x<y)) {
if (ux.extu_fracl == 0) {
if ((ux.extu_frach & ~LDBL_NBIT) == 0) {
#ifdef __m68k__
if (ux.extu_exp == 0)
nbit = 0;
else
#endif
ux.extu_exp -= 1;
}
ux.extu_frach = (ux.extu_frach - 1) | nbit;
}
ux.extu_fracl -= 1;
} else {
ux.extu_fracl += 1;
if (ux.extu_fracl == 0) {
ux.extu_frach = (ux.extu_frach + 1) | nbit;
if ((ux.extu_frach & ~LDBL_NBIT) == 0) {
#ifdef __m68k__
if (nbit == 0)
ux.extu_frach |= LDBL_NBIT;
else
#endif
ux.extu_exp += 1;
}
}
}
if (ux.extu_exp == EXT_EXP_INFNAN)
return x+x;
#ifdef __m68k__
if (ux.extu_exp == 0 && (ux.extu_frach & LDBL_NBIT) == 0) {
#else
if (ux.extu_exp == 0) {
#endif
#ifndef LDBL_IMPLICIT_NBIT
mask_nbit_l(ux);
#endif
t = ux.extu_ld * ux.extu_ld;
if (t != ux.extu_ld)
return ux.extu_ld;
}
return ux.extu_ld;
}
#endif
#endif