math: nextafter and nexttoward cleanup
[musl] / src / math / nextafterf.c
index bdc88ca..4727f7b 100644 (file)
@@ -1,67 +1,35 @@
-/* origin: FreeBSD /usr/src/lib/msun/src/s_nextafterf.c */
-/*
- * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
- */
-/*
- * ====================================================
- * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
- *
- * Developed at SunPro, a Sun Microsystems, Inc. business.
- * Permission to use, copy, modify, and distribute this
- * software is freely granted, provided that this notice
- * is preserved.
- * ====================================================
- */
-
 #include "libm.h"
 
+#define SIGN 0x80000000
+
 float nextafterf(float x, float y)
 {
-       volatile float t;
-       int32_t hx,hy,ix,iy;
-
-       GET_FLOAT_WORD(hx, x);
-       GET_FLOAT_WORD(hy, y);
-       ix = hx & 0x7fffffff;  /* |x| */
-       iy = hy & 0x7fffffff;  /* |y| */
+       union fshape ux, uy;
+       uint32_t ax, ay, e;
 
-       if (ix > 0x7f800000 || /* x is nan */
-           iy > 0x7f800000)   /* y is nan */
-               return x+y;
-       if (x == y)            /* x == y */
+       if (isnan(x) || isnan(y))
+               return x + y;
+       ux.value = x;
+       uy.value = y;
+       if (ux.bits == uy.bits)
                return y;
-       if (ix == 0) {         /* x == 0 */
-               SET_FLOAT_WORD(x, (hy&0x80000000)|1); /* return +-minsubnormal */
-               /* raise underflow flag */
-               t = x*x;
-               if (t == x)
-                       return t;
-               return x;
-       }
-       if (hx >= 0) {         /* x > 0 */
-               if (hx > hy) {             /* x > y, x -= ulp */
-                       hx--;
-               } else {                   /* x < y, x += ulp */
-                       hx++;
-               }
-       } else {               /* x < 0 */
-               if (hy >= 0 || hx > hy) {  /* x < y, x -= ulp */
-                       hx--;
-               } else {                   /* x > y, x += ulp */
-                       hx++;
-               }
-       }
-       hy = hx & 0x7f800000;
-       if (hy >= 0x7f800000)  /* overflow */
-               return x+x;
-       if (hy < 0x00800000) { /* underflow */
-               /* raise underflow flag */
-               t = x*x;
-               if (t != x) {
-                       SET_FLOAT_WORD(y, hx);
+       ax = ux.bits & ~SIGN;
+       ay = uy.bits & ~SIGN;
+       if (ax == 0) {
+               if (ay == 0)
                        return y;
-               }
+               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;
        }
-       SET_FLOAT_WORD(x, hx);
-       return x;
+       return ux.value;
 }