Files
semi-libc/src/math/nexttoward.c
T
Szabolcs Nagy 662ed20065 math: fix nextafter and nexttoward on maxdbl and maxflt input
old code (return x+x;) returns correct value and raises correct
flags only if the result is stored as double (or float)
2012-11-13 10:12:07 +01:00

46 lines
799 B
C

#include "libm.h"
#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
double nexttoward(double x, long double y)
{
return nextafter(x, y);
}
#else
#define SIGN ((uint64_t)1<<63)
double nexttoward(double x, long double y)
{
union dshape ux;
int e;
if (isnan(x) || isnan(y))
return x + y;
if (x == y)
return y;
ux.value = x;
if (x == 0) {
ux.bits = 1;
if (signbit(y))
ux.bits |= SIGN;
} else if (x < y) {
if (signbit(x))
ux.bits--;
else
ux.bits++;
} else {
if (signbit(x))
ux.bits++;
else
ux.bits--;
}
e = ux.bits>>52 & 0x7ff;
/* raise overflow if ux.value is infinite and x is finite */
if (e == 0x7ff)
FORCE_EVAL(x+x);
/* raise underflow if ux.value is subnormal or zero */
if (e == 0)
FORCE_EVAL(x*x + ux.value*ux.value);
return ux.value;
}
#endif