xref: /dflybsd-src/contrib/openbsd_libm/src/s_nextafterf.c (revision 4382f29d99a100bd77a81697c2f699c11f6a472a)
1*05a0b428SJohn Marino /* s_nextafterf.c -- float version of s_nextafter.c.
2*05a0b428SJohn Marino  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3*05a0b428SJohn Marino  */
4*05a0b428SJohn Marino 
5*05a0b428SJohn Marino /*
6*05a0b428SJohn Marino  * ====================================================
7*05a0b428SJohn Marino  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8*05a0b428SJohn Marino  *
9*05a0b428SJohn Marino  * Developed at SunPro, a Sun Microsystems, Inc. business.
10*05a0b428SJohn Marino  * Permission to use, copy, modify, and distribute this
11*05a0b428SJohn Marino  * software is freely granted, provided that this notice
12*05a0b428SJohn Marino  * is preserved.
13*05a0b428SJohn Marino  * ====================================================
14*05a0b428SJohn Marino  */
15*05a0b428SJohn Marino 
16*05a0b428SJohn Marino #include "math.h"
17*05a0b428SJohn Marino #include "math_private.h"
18*05a0b428SJohn Marino 
19*05a0b428SJohn Marino float
nextafterf(float x,float y)20*05a0b428SJohn Marino nextafterf(float x, float y)
21*05a0b428SJohn Marino {
22*05a0b428SJohn Marino 	int32_t hx,hy,ix,iy;
23*05a0b428SJohn Marino 
24*05a0b428SJohn Marino 	GET_FLOAT_WORD(hx,x);
25*05a0b428SJohn Marino 	GET_FLOAT_WORD(hy,y);
26*05a0b428SJohn Marino 	ix = hx&0x7fffffff;		/* |x| */
27*05a0b428SJohn Marino 	iy = hy&0x7fffffff;		/* |y| */
28*05a0b428SJohn Marino 
29*05a0b428SJohn Marino 	if((ix>0x7f800000) ||   /* x is nan */
30*05a0b428SJohn Marino 	   (iy>0x7f800000))     /* y is nan */
31*05a0b428SJohn Marino 	   return x+y;
32*05a0b428SJohn Marino 	if(x==y) return y;		/* x=y, return y */
33*05a0b428SJohn Marino 	if(ix==0) {				/* x == 0 */
34*05a0b428SJohn Marino 	    SET_FLOAT_WORD(x,(hy&0x80000000)|1);/* return +-minsubnormal */
35*05a0b428SJohn Marino 	    y = x*x;
36*05a0b428SJohn Marino 	    if(y==x) return y; else return x;	/* raise underflow flag */
37*05a0b428SJohn Marino 	}
38*05a0b428SJohn Marino 	if(hx>=0) {				/* x > 0 */
39*05a0b428SJohn Marino 	    if(hx>hy) {				/* x > y, x -= ulp */
40*05a0b428SJohn Marino 		hx -= 1;
41*05a0b428SJohn Marino 	    } else {				/* x < y, x += ulp */
42*05a0b428SJohn Marino 		hx += 1;
43*05a0b428SJohn Marino 	    }
44*05a0b428SJohn Marino 	} else {				/* x < 0 */
45*05a0b428SJohn Marino 	    if(hy>=0||hx>hy){			/* x < y, x -= ulp */
46*05a0b428SJohn Marino 		hx -= 1;
47*05a0b428SJohn Marino 	    } else {				/* x > y, x += ulp */
48*05a0b428SJohn Marino 		hx += 1;
49*05a0b428SJohn Marino 	    }
50*05a0b428SJohn Marino 	}
51*05a0b428SJohn Marino 	hy = hx&0x7f800000;
52*05a0b428SJohn Marino 	if(hy>=0x7f800000) return x+x;	/* overflow  */
53*05a0b428SJohn Marino 	if(hy<0x00800000) {		/* underflow */
54*05a0b428SJohn Marino 	    y = x*x;
55*05a0b428SJohn Marino 	    if(y!=x) {		/* raise underflow flag */
56*05a0b428SJohn Marino 	        SET_FLOAT_WORD(y,hx);
57*05a0b428SJohn Marino 		return y;
58*05a0b428SJohn Marino 	    }
59*05a0b428SJohn Marino 	}
60*05a0b428SJohn Marino 	SET_FLOAT_WORD(x,hx);
61*05a0b428SJohn Marino 	return x;
62*05a0b428SJohn Marino }
63