xref: /minix3/lib/libm/src/s_nextafterf.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
12fe8fb19SBen Gras /* s_nextafterf.c -- float version of s_nextafter.c.
22fe8fb19SBen Gras  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
32fe8fb19SBen Gras  */
42fe8fb19SBen Gras 
52fe8fb19SBen Gras /*
62fe8fb19SBen Gras  * ====================================================
72fe8fb19SBen Gras  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
82fe8fb19SBen Gras  *
92fe8fb19SBen Gras  * Developed at SunPro, a Sun Microsystems, Inc. business.
102fe8fb19SBen Gras  * Permission to use, copy, modify, and distribute this
112fe8fb19SBen Gras  * software is freely granted, provided that this notice
122fe8fb19SBen Gras  * is preserved.
132fe8fb19SBen Gras  * ====================================================
142fe8fb19SBen Gras  */
152fe8fb19SBen Gras 
162fe8fb19SBen Gras #include <sys/cdefs.h>
172fe8fb19SBen Gras #if defined(LIBM_SCCS) && !defined(lint)
18*84d9c625SLionel Sambuc __RCSID("$NetBSD: s_nextafterf.c,v 1.8 2011/04/18 15:59:09 drochner Exp $");
192fe8fb19SBen Gras #endif
202fe8fb19SBen Gras 
212fe8fb19SBen Gras #include "math.h"
222fe8fb19SBen Gras #include "math_private.h"
232fe8fb19SBen Gras 
242fe8fb19SBen Gras float
nextafterf(float x,float y)252fe8fb19SBen Gras nextafterf(float x, float y)
262fe8fb19SBen Gras {
272fe8fb19SBen Gras 	int32_t hx,hy,ix,iy;
282fe8fb19SBen Gras 
292fe8fb19SBen Gras 	GET_FLOAT_WORD(hx,x);
302fe8fb19SBen Gras 	GET_FLOAT_WORD(hy,y);
312fe8fb19SBen Gras 	ix = hx&0x7fffffff;		/* |x| */
322fe8fb19SBen Gras 	iy = hy&0x7fffffff;		/* |y| */
332fe8fb19SBen Gras 
342fe8fb19SBen Gras 	if((ix>0x7f800000) ||   /* x is nan */
352fe8fb19SBen Gras 	   (iy>0x7f800000))     /* y is nan */
362fe8fb19SBen Gras 	   return x+y;
37*84d9c625SLionel Sambuc 	if(x==y) return y;		/* x=y, return y */
382fe8fb19SBen Gras 	if(ix==0) {				/* x == 0 */
392fe8fb19SBen Gras 	    SET_FLOAT_WORD(x,(hy&0x80000000)|1);/* return +-minsubnormal */
402fe8fb19SBen Gras 	    y = x*x;
412fe8fb19SBen Gras 	    if(y==x) return y; else return x;	/* raise underflow flag */
422fe8fb19SBen Gras 	}
432fe8fb19SBen Gras 	if(hx>=0) {				/* x > 0 */
442fe8fb19SBen Gras 	    if(hx>hy) {				/* x > y, x -= ulp */
452fe8fb19SBen Gras 		hx -= 1;
462fe8fb19SBen Gras 	    } else {				/* x < y, x += ulp */
472fe8fb19SBen Gras 		hx += 1;
482fe8fb19SBen Gras 	    }
492fe8fb19SBen Gras 	} else {				/* x < 0 */
502fe8fb19SBen Gras 	    if(hy>=0||hx>hy){			/* x < y, x -= ulp */
512fe8fb19SBen Gras 		hx -= 1;
522fe8fb19SBen Gras 	    } else {				/* x > y, x += ulp */
532fe8fb19SBen Gras 		hx += 1;
542fe8fb19SBen Gras 	    }
552fe8fb19SBen Gras 	}
562fe8fb19SBen Gras 	hy = hx&0x7f800000;
572fe8fb19SBen Gras 	if(hy>=0x7f800000) return x+x;	/* overflow  */
582fe8fb19SBen Gras 	if(hy<0x00800000) {		/* underflow */
592fe8fb19SBen Gras 	    y = x*x;
602fe8fb19SBen Gras 	    if(y!=x) {		/* raise underflow flag */
612fe8fb19SBen Gras 	        SET_FLOAT_WORD(y,hx);
622fe8fb19SBen Gras 		return y;
632fe8fb19SBen Gras 	    }
642fe8fb19SBen Gras 	}
652fe8fb19SBen Gras 	SET_FLOAT_WORD(x,hx);
662fe8fb19SBen Gras 	return x;
672fe8fb19SBen Gras }
68