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