xref: /relibc/openlibm/ld128/s_nextafterl.c (revision 71f60ec6321f6a3ef50d9834255ef506135d4005)
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  *	nextafterl(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 
22 #include "math_private.h"
23 
24 long double
25 nextafterl(long double x, long double y)
26 {
27 	int64_t hx,hy,ix,iy;
28 	u_int64_t lx,ly;
29 
30 	GET_LDOUBLE_WORDS64(hx,lx,x);
31 	GET_LDOUBLE_WORDS64(hy,ly,y);
32 	ix = hx&0x7fffffffffffffffLL;		/* |x| */
33 	iy = hy&0x7fffffffffffffffLL;		/* |y| */
34 
35 	if(((ix>=0x7fff000000000000LL)&&((ix-0x7fff000000000000LL)|lx)!=0) ||   /* x is nan */
36 	   ((iy>=0x7fff000000000000LL)&&((iy-0x7fff000000000000LL)|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 	    volatile long double u;
41 	    SET_LDOUBLE_WORDS64(x,hy&0x8000000000000000ULL,1);/* return +-minsubnormal */
42 	    u = x;
43 	    u = u * u;				/* raise underflow flag */
44 	    return x;
45 	}
46 	if(hx>=0) {			/* x > 0 */
47 	    if(hx>hy||((hx==hy)&&(lx>ly))) {	/* x > y, x -= ulp */
48 		if(lx==0) hx--;
49 		lx--;
50 	    } else {				/* x < y, x += ulp */
51 		lx++;
52 		if(lx==0) hx++;
53 	    }
54 	} else {				/* x < 0 */
55 	    if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){/* x < y, x -= ulp */
56 		if(lx==0) hx--;
57 		lx--;
58 	    } else {				/* x > y, x += ulp */
59 		lx++;
60 		if(lx==0) hx++;
61 	    }
62 	}
63 	hy = hx&0x7fff000000000000LL;
64 	if(hy==0x7fff000000000000LL) return x+x;/* overflow  */
65 	if(hy==0) {
66 	    volatile long double u = x*x;	/* underflow */
67 	}
68 	SET_LDOUBLE_WORDS64(x,hx,lx);
69 	return x;
70 }
71 
72 __strong_alias(nexttowardl, nextafterl);
73