xref: /minix3/lib/libm/src/s_rintf.c (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras /* s_rintf.c -- float version of s_rint.c.
2*2fe8fb19SBen Gras  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3*2fe8fb19SBen Gras  */
4*2fe8fb19SBen Gras 
5*2fe8fb19SBen Gras /*
6*2fe8fb19SBen Gras  * ====================================================
7*2fe8fb19SBen Gras  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8*2fe8fb19SBen Gras  *
9*2fe8fb19SBen Gras  * Developed at SunPro, a Sun Microsystems, Inc. business.
10*2fe8fb19SBen Gras  * Permission to use, copy, modify, and distribute this
11*2fe8fb19SBen Gras  * software is freely granted, provided that this notice
12*2fe8fb19SBen Gras  * is preserved.
13*2fe8fb19SBen Gras  * ====================================================
14*2fe8fb19SBen Gras  */
15*2fe8fb19SBen Gras 
16*2fe8fb19SBen Gras #include <sys/cdefs.h>
17*2fe8fb19SBen Gras #if defined(LIBM_SCCS) && !defined(lint)
18*2fe8fb19SBen Gras __RCSID("$NetBSD: s_rintf.c,v 1.9 2008/04/25 22:21:53 christos Exp $");
19*2fe8fb19SBen Gras #endif
20*2fe8fb19SBen Gras 
21*2fe8fb19SBen Gras #include "math.h"
22*2fe8fb19SBen Gras #include "math_private.h"
23*2fe8fb19SBen Gras 
24*2fe8fb19SBen Gras static const float
25*2fe8fb19SBen Gras TWO23[2]={
26*2fe8fb19SBen Gras   8.3886080000e+06, /* 0x4b000000 */
27*2fe8fb19SBen Gras  -8.3886080000e+06, /* 0xcb000000 */
28*2fe8fb19SBen Gras };
29*2fe8fb19SBen Gras 
30*2fe8fb19SBen Gras float
rintf(float x)31*2fe8fb19SBen Gras rintf(float x)
32*2fe8fb19SBen Gras {
33*2fe8fb19SBen Gras 	int32_t i0,jj0,sx;
34*2fe8fb19SBen Gras 	u_int32_t i,i1;
35*2fe8fb19SBen Gras #ifdef __i386__ /* XXX gcc4 will omit the rounding otherwise */
36*2fe8fb19SBen Gras 	volatile
37*2fe8fb19SBen Gras #endif
38*2fe8fb19SBen Gras 		float w;
39*2fe8fb19SBen Gras 	float t;
40*2fe8fb19SBen Gras 	GET_FLOAT_WORD(i0,x);
41*2fe8fb19SBen Gras 	sx = (i0>>31)&1;
42*2fe8fb19SBen Gras 	jj0 = ((i0>>23)&0xff)-0x7f;
43*2fe8fb19SBen Gras 	if(jj0<23) {
44*2fe8fb19SBen Gras 	    if(jj0<0) {
45*2fe8fb19SBen Gras 		if((i0&0x7fffffff)==0) return x;
46*2fe8fb19SBen Gras 		i1 = (i0&0x07fffff);
47*2fe8fb19SBen Gras 		i0 &= 0xfff00000;
48*2fe8fb19SBen Gras 		i0 |= ((i1|-i1)>>9)&0x400000;
49*2fe8fb19SBen Gras 		SET_FLOAT_WORD(x,i0);
50*2fe8fb19SBen Gras 	        w = TWO23[sx]+x;
51*2fe8fb19SBen Gras 	        t =  w-TWO23[sx];
52*2fe8fb19SBen Gras 		GET_FLOAT_WORD(i0,t);
53*2fe8fb19SBen Gras 		SET_FLOAT_WORD(t,(i0&0x7fffffff)|(sx<<31));
54*2fe8fb19SBen Gras 	        return t;
55*2fe8fb19SBen Gras 	    } else {
56*2fe8fb19SBen Gras 		i = (0x007fffff)>>jj0;
57*2fe8fb19SBen Gras 		if((i0&i)==0) return x; /* x is integral */
58*2fe8fb19SBen Gras 		i>>=1;
59*2fe8fb19SBen Gras 		if((i0&i)!=0) i0 = (i0&(~i))|((0x100000)>>jj0);
60*2fe8fb19SBen Gras 	    }
61*2fe8fb19SBen Gras 	} else {
62*2fe8fb19SBen Gras 	    if(jj0==0x80) return x+x;	/* inf or NaN */
63*2fe8fb19SBen Gras 	    else return x;		/* x is integral */
64*2fe8fb19SBen Gras 	}
65*2fe8fb19SBen Gras 	SET_FLOAT_WORD(x,i0);
66*2fe8fb19SBen Gras 	w = TWO23[sx]+x;
67*2fe8fb19SBen Gras 	return w-TWO23[sx];
68*2fe8fb19SBen Gras }
69