initial check-in, version 0.5.0
[musl] / src / math / s_nextafter.c
1 /* @(#)s_nextafter.c 5.1 93/09/24 */
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunPro, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  */
12
13 /* IEEE functions
14  *      nextafter(x,y)
15  *      return the next machine floating-point number of x in the
16  *      direction toward y.
17  *   Special cases:
18  */
19
20 #include <math.h>
21 #include "math_private.h"
22
23 double
24 nextafter(double x, double y)
25 {
26         volatile double t;
27         int32_t hx,hy,ix,iy;
28         uint32_t lx,ly;
29
30         EXTRACT_WORDS(hx,lx,x);
31         EXTRACT_WORDS(hy,ly,y);
32         ix = hx&0x7fffffff;             /* |x| */
33         iy = hy&0x7fffffff;             /* |y| */
34
35         if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) ||   /* x is nan */
36            ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0))     /* y is nan */
37            return x+y;
38         if(x==y) return y;              /* x=y, return y */
39         if((ix|lx)==0) {                        /* x == 0 */
40             INSERT_WORDS(x,hy&0x80000000,1);    /* return +-minsubnormal */
41             t = x*x;
42             if(t==x) return t; else return x;   /* raise underflow flag */
43         }
44         if(hx>=0) {                             /* x > 0 */
45             if(hx>hy||((hx==hy)&&(lx>ly))) {    /* x > y, x -= ulp */
46                 if(lx==0) hx -= 1;
47                 lx -= 1;
48             } else {                            /* x < y, x += ulp */
49                 lx += 1;
50                 if(lx==0) hx += 1;
51             }
52         } else {                                /* x < 0 */
53             if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){/* x < y, x -= ulp */
54                 if(lx==0) hx -= 1;
55                 lx -= 1;
56             } else {                            /* x > y, x += ulp */
57                 lx += 1;
58                 if(lx==0) hx += 1;
59             }
60         }
61         hy = hx&0x7ff00000;
62         if(hy>=0x7ff00000) return x+x;  /* overflow  */
63         if(hy<0x00100000) {             /* underflow */
64             t = x*x;
65             if(t!=x) {          /* raise underflow flag */
66                 INSERT_WORDS(y,hx,lx);
67                 return y;
68             }
69         }
70         INSERT_WORDS(x,hx,lx);
71         return x;
72 }