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