2012-03-13 01:17:53 -04:00
|
|
|
#include "libm.h"
|
|
|
|
|
2012-05-06 13:08:59 +02:00
|
|
|
#define SIGN 0x80000000
|
|
|
|
|
2012-03-13 01:17:53 -04:00
|
|
|
float nextafterf(float x, float y)
|
|
|
|
{
|
2012-05-06 13:08:59 +02:00
|
|
|
union fshape ux, uy;
|
|
|
|
uint32_t ax, ay, e;
|
2012-03-13 01:17:53 -04:00
|
|
|
|
2012-05-06 13:08:59 +02:00
|
|
|
if (isnan(x) || isnan(y))
|
|
|
|
return x + y;
|
|
|
|
ux.value = x;
|
|
|
|
uy.value = y;
|
|
|
|
if (ux.bits == uy.bits)
|
2012-03-13 01:17:53 -04:00
|
|
|
return y;
|
2012-05-06 13:08:59 +02:00
|
|
|
ax = ux.bits & ~SIGN;
|
|
|
|
ay = uy.bits & ~SIGN;
|
|
|
|
if (ax == 0) {
|
|
|
|
if (ay == 0)
|
2012-03-13 01:17:53 -04:00
|
|
|
return y;
|
2012-05-06 13:08:59 +02:00
|
|
|
ux.bits = (uy.bits & SIGN) | 1;
|
|
|
|
} else if (ax > ay || ((ux.bits ^ uy.bits) & SIGN))
|
|
|
|
ux.bits--;
|
|
|
|
else
|
|
|
|
ux.bits++;
|
|
|
|
e = ux.bits & 0x7f800000;
|
|
|
|
/* raise overflow if ux.value is infinite and x is finite */
|
|
|
|
if (e == 0x7f800000)
|
|
|
|
return x + x;
|
|
|
|
/* raise underflow if ux.value is subnormal or zero */
|
|
|
|
if (e == 0) {
|
|
|
|
volatile float z = x*x + ux.value*ux.value;
|
2012-03-13 01:17:53 -04:00
|
|
|
}
|
2012-05-06 13:08:59 +02:00
|
|
|
return ux.value;
|
2012-03-13 01:17:53 -04:00
|
|
|
}
|