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