s_nexttowardf.c revision 143218
1143218Sdas/*
2143218Sdas * ====================================================
3143218Sdas * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4143218Sdas *
5143218Sdas * Developed at SunPro, a Sun Microsystems, Inc. business.
6143218Sdas * Permission to use, copy, modify, and distribute this
7143218Sdas * software is freely granted, provided that this notice
8143218Sdas * is preserved.
9143218Sdas * ====================================================
10143218Sdas */
11143218Sdas
12143218Sdas#ifndef lint
13143218Sdasstatic char rcsid[] = "$FreeBSD: head/lib/msun/src/s_nexttowardf.c 143218 2005-03-07 04:57:38Z das $";
14143218Sdas#endif
15143218Sdas
16143218Sdas#include <float.h>
17143218Sdas
18143218Sdas#include "fpmath.h"
19143218Sdas#include "math.h"
20143218Sdas#include "math_private.h"
21143218Sdas
22143218Sdas#define	LDBL_INFNAN_EXP	(LDBL_MAX_EXP * 2 - 1)
23143218Sdas
24143218Sdasfloat
25143218Sdasnexttowardf(float x, long double y)
26143218Sdas{
27143218Sdas	union IEEEl2bits uy;
28143218Sdas	volatile float t;
29143218Sdas	int32_t hx,ix;
30143218Sdas
31143218Sdas	GET_FLOAT_WORD(hx,x);
32143218Sdas	ix = hx&0x7fffffff;		/* |x| */
33143218Sdas	uy.e = y;
34143218Sdas
35143218Sdas	if((ix>0x7f800000) ||
36143218Sdas	   (uy.bits.exp == LDBL_INFNAN_EXP &&
37143218Sdas	    ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl) != 0))
38143218Sdas	   return x+y;	/* x or y is nan */
39143218Sdas	if(x==y) return (float)y;		/* x=y, return y */
40143218Sdas	if(ix==0) {				/* x == 0 */
41143218Sdas	    SET_FLOAT_WORD(x,(uy.bits.sign<<31)|1);/* return +-minsubnormal */
42143218Sdas	    t = x*x;
43143218Sdas	    if(t==x) return t; else return x;	/* raise underflow flag */
44143218Sdas	}
45143218Sdas	if(hx>=0 ^ x < y)			/* x -= ulp */
46143218Sdas	    hx -= 1;
47143218Sdas	else					/* x += ulp */
48143218Sdas	    hx += 1;
49143218Sdas	ix = hx&0x7f800000;
50143218Sdas	if(ix>=0x7f800000) return x+x;	/* overflow  */
51143218Sdas	if(ix<0x00800000) {		/* underflow */
52143218Sdas	    t = x*x;
53143218Sdas	    if(t!=x) {		/* raise underflow flag */
54143218Sdas	        SET_FLOAT_WORD(y,hx);
55143218Sdas		return y;
56143218Sdas	    }
57143218Sdas	}
58143218Sdas	SET_FLOAT_WORD(x,hx);
59143218Sdas	return x;
60143218Sdas}
61