xref: /relibc/openlibm/src/s_nexttowardf.c (revision 06dbb6e72b1d3e43ca813caa0b139094068e6a88)
1 /*
2  * ====================================================
3  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4  *
5  * Developed at SunPro, a Sun Microsystems, Inc. business.
6  * Permission to use, copy, modify, and distribute this
7  * software is freely granted, provided that this notice
8  * is preserved.
9  * ====================================================
10  */
11 
12 #include "cdefs-compat.h"
13 //__FBSDID("$FreeBSD: src/lib/msun/src/s_nexttowardf.c,v 1.3 2011/02/10 07:38:38 das Exp $");
14 
15 #include <float.h>
16 #include <openlibm.h>
17 
18 #include "fpmath.h"
19 #include "math_private.h"
20 
21 #define	LDBL_INFNAN_EXP	(LDBL_MAX_EXP * 2 - 1)
22 
23 #ifdef LONG_DOUBLE
24 DLLEXPORT float
25 nexttowardf(float x, long double y)
26 {
27 	union IEEEl2bits uy;
28 	volatile float t;
29 	int32_t hx,ix;
30 
31 	GET_FLOAT_WORD(hx,x);
32 	ix = hx&0x7fffffff;		/* |x| */
33 	uy.e = y;
34 
35 	if((ix>0x7f800000) ||
36 	   (uy.bits.exp == LDBL_INFNAN_EXP &&
37 	    ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl) != 0))
38 	   return x+y;	/* x or y is nan */
39 	if(x==y) return (float)y;		/* x=y, return y */
40 	if(ix==0) {				/* x == 0 */
41 	    SET_FLOAT_WORD(x,(uy.bits.sign<<31)|1);/* return +-minsubnormal */
42 	    t = x*x;
43 	    if(t==x) return t; else return x;	/* raise underflow flag */
44 	}
45 	if((hx>=0) ^ (x < y))			/* x -= ulp */
46 	    hx -= 1;
47 	else					/* x += ulp */
48 	    hx += 1;
49 	ix = hx&0x7f800000;
50 	if(ix>=0x7f800000) return x+x;	/* overflow  */
51 	if(ix<0x00800000) {		/* underflow */
52 	    t = x*x;
53 	    if(t!=x) {		/* raise underflow flag */
54 	        SET_FLOAT_WORD(x,hx);
55 		return x;
56 	    }
57 	}
58 	SET_FLOAT_WORD(x,hx);
59 	return x;
60 }
61 #endif
62