xref: /openbsd-src/lib/libm/src/ld80/s_nextafterl.c (revision 93832e9776a5e931b4ce8f96e5c7b0a6071117af)
149393c00Smartynas /* @(#)s_nextafter.c 5.1 93/09/24 */
249393c00Smartynas /*
349393c00Smartynas  * ====================================================
449393c00Smartynas  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
549393c00Smartynas  *
649393c00Smartynas  * Developed at SunPro, a Sun Microsystems, Inc. business.
749393c00Smartynas  * Permission to use, copy, modify, and distribute this
849393c00Smartynas  * software is freely granted, provided that this notice
949393c00Smartynas  * is preserved.
1049393c00Smartynas  * ====================================================
1149393c00Smartynas  */
1249393c00Smartynas 
1349393c00Smartynas /* IEEE functions
1449393c00Smartynas  *	nextafterl(x,y)
1549393c00Smartynas  *	return the next machine floating-point number of x in the
1649393c00Smartynas  *	direction toward y.
1749393c00Smartynas  *   Special cases:
1849393c00Smartynas  */
1949393c00Smartynas 
2049393c00Smartynas #include <math.h>
2149393c00Smartynas 
2249393c00Smartynas #include "math_private.h"
2349393c00Smartynas 
2449393c00Smartynas long double
nextafterl(long double x,long double y)2549393c00Smartynas nextafterl(long double x, long double y)
2649393c00Smartynas {
2749393c00Smartynas 	int32_t hx,hy,ix,iy;
2833282fddSmartynas 	u_int32_t lx,ly;
2933282fddSmartynas 	int32_t esx,esy;
3049393c00Smartynas 
3149393c00Smartynas 	GET_LDOUBLE_WORDS(esx,hx,lx,x);
3249393c00Smartynas 	GET_LDOUBLE_WORDS(esy,hy,ly,y);
3349393c00Smartynas 	ix = esx&0x7fff;		/* |x| */
3449393c00Smartynas 	iy = esy&0x7fff;		/* |y| */
3549393c00Smartynas 
36*93832e97Skrw 	if (((ix==0x7fff)&&(((hx&0x7fffffff)|lx)!=0)) ||   /* x is nan */
37*93832e97Skrw 	    ((iy==0x7fff)&&(((hy&0x7fffffff)|ly)!=0)))     /* y is nan */
3849393c00Smartynas 	   return x+y;
3949393c00Smartynas 	if(x==y) return y;		/* x=y, return y */
4049393c00Smartynas 	if((ix|hx|lx)==0) {			/* x == 0 */
4149393c00Smartynas 	    volatile long double u;
4249393c00Smartynas 	    SET_LDOUBLE_WORDS(x,esy&0x8000,0,1);/* return +-minsubnormal */
4349393c00Smartynas 	    u = x;
4449393c00Smartynas 	    u = u * u;				/* raise underflow flag */
4549393c00Smartynas 	    return x;
4649393c00Smartynas 	}
4733282fddSmartynas 	if(esx>=0) {			/* x > 0 */
4833282fddSmartynas 	    if(esx>esy||((esx==esy) && (hx>hy||((hx==hy)&&(lx>ly))))) {
4949393c00Smartynas 	      /* x > y, x -= ulp */
5049393c00Smartynas 		if(lx==0) {
510b84a240Skettenis 		    if ((hx&0x7fffffff)==0) esx -= 1;
520b84a240Skettenis 		    hx = (hx - 1) | (hx & 0x80000000);
5349393c00Smartynas 		}
5449393c00Smartynas 		lx -= 1;
5549393c00Smartynas 	    } else {				/* x < y, x += ulp */
5649393c00Smartynas 		lx += 1;
5749393c00Smartynas 		if(lx==0) {
580b84a240Skettenis 		    hx = (hx + 1) | (hx & 0x80000000);
590b84a240Skettenis 		    if ((hx&0x7fffffff)==0) esx += 1;
6049393c00Smartynas 		}
6149393c00Smartynas 	    }
6249393c00Smartynas 	} else {				/* x < 0 */
6333282fddSmartynas 	    if(esy>=0||(esx>esy||((esx==esy)&&(hx>hy||((hx==hy)&&(lx>ly)))))){
6449393c00Smartynas 	      /* x < y, x -= ulp */
6549393c00Smartynas 		if(lx==0) {
660b84a240Skettenis 		    if ((hx&0x7fffffff)==0) esx -= 1;
670b84a240Skettenis 		    hx = (hx - 1) | (hx & 0x80000000);
6849393c00Smartynas 		}
6949393c00Smartynas 		lx -= 1;
7049393c00Smartynas 	    } else {				/* x > y, x += ulp */
7149393c00Smartynas 		lx += 1;
7249393c00Smartynas 		if(lx==0) {
730b84a240Skettenis 		    hx = (hx + 1) | (hx & 0x80000000);
740b84a240Skettenis 		    if ((hx&0x7fffffff)==0) esx += 1;
7549393c00Smartynas 		}
7649393c00Smartynas 	    }
7749393c00Smartynas 	}
7849393c00Smartynas 	esy = esx&0x7fff;
7949393c00Smartynas 	if(esy==0x7fff) return x+x;		/* overflow  */
8049393c00Smartynas 	if(esy==0) {
8149393c00Smartynas 	    volatile long double u = x*x;	/* underflow */
827eebeddeSmartynas 	    if(u==x) {
837eebeddeSmartynas 		SET_LDOUBLE_WORDS(x,esx,hx,lx);
847eebeddeSmartynas 		return x;
857eebeddeSmartynas 	    }
8649393c00Smartynas 	}
8749393c00Smartynas 	SET_LDOUBLE_WORDS(x,esx,hx,lx);
8849393c00Smartynas 	return x;
8949393c00Smartynas }
902f2c0062Sguenther DEF_STD(nextafterl);
912f2c0062Sguenther MAKE_UNUSED_CLONE(nexttowardl, nextafterl);
92