1 /* s_nextafterl.c -- long double version of s_nextafter.c.
2  * Conversion to IEEE quad long double by Jakub Jelinek, jj@ultra.linux.cz.
3  */
4 
5 /*
6  * ====================================================
7  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8  *
9  * Developed at SunPro, a Sun Microsystems, Inc. business.
10  * Permission to use, copy, modify, and distribute this
11  * software is freely granted, provided that this notice
12  * is preserved.
13  * ====================================================
14  */
15 
16 #if defined(LIBM_SCCS) && !defined(lint)
17 static char rcsid[] = "$NetBSD: $";
18 #endif
19 
20 /* IEEE functions
21  *	nextafterq(x,y)
22  *	return the next machine floating-point number of x in the
23  *	direction toward y.
24  *   Special cases:
25  */
26 
27 #include "quadmath-imp.h"
28 
nextafterq(__float128 x,__float128 y)29 __float128 nextafterq(__float128 x, __float128 y)
30 {
31 	int64_t hx,hy,ix,iy;
32 	uint64_t lx,ly;
33 
34 	GET_FLT128_WORDS64(hx,lx,x);
35 	GET_FLT128_WORDS64(hy,ly,y);
36 	ix = hx&0x7fffffffffffffffLL;		/* |x| */
37 	iy = hy&0x7fffffffffffffffLL;		/* |y| */
38 
39 	if(((ix>=0x7fff000000000000LL)&&((ix-0x7fff000000000000LL)|lx)!=0) ||   /* x is nan */
40 	   ((iy>=0x7fff000000000000LL)&&((iy-0x7fff000000000000LL)|ly)!=0))     /* y is nan */
41 	   return x+y;
42 	if(x==y) return y;		/* x=y, return y */
43 	if((ix|lx)==0) {			/* x == 0 */
44 	    __float128 u;
45 	    SET_FLT128_WORDS64(x,hy&0x8000000000000000ULL,1);/* return +-minsubnormal */
46 	    u = math_opt_barrier (x);
47 	    u = u * u;
48 	    math_force_eval (u);		/* raise underflow flag */
49 	    return x;
50 	}
51 	if(hx>=0) {			/* x > 0 */
52 	    if(hx>hy||((hx==hy)&&(lx>ly))) {	/* x > y, x -= ulp */
53 		if(lx==0) hx--;
54 		lx--;
55 	    } else {				/* x < y, x += ulp */
56 		lx++;
57 		if(lx==0) hx++;
58 	    }
59 	} else {				/* x < 0 */
60 	    if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){/* x < y, x -= ulp */
61 		if(lx==0) hx--;
62 		lx--;
63 	    } else {				/* x > y, x += ulp */
64 		lx++;
65 		if(lx==0) hx++;
66 	    }
67 	}
68 	hy = hx&0x7fff000000000000LL;
69 	if(hy==0x7fff000000000000LL) {
70 	    __float128 u = x + x;		/* overflow  */
71 	    math_force_eval (u);
72 	    errno = ERANGE;
73 	}
74 	if(hy==0) {
75 	    __float128 u = x*x;		/* underflow */
76 	    math_force_eval (u);		/* raise underflow flag */
77 	    errno = ERANGE;
78 	}
79 	SET_FLT128_WORDS64(x,hx,lx);
80 	return x;
81 }
82