1 /* sf_rint.c -- float version of s_rint.c.
2 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3 */
4
5 /*
6 * ====================================================
7 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8 *
9 * Developed at SunPro, a Sun Microsystems, Inc. business.
10 * Permission to use, copy, modify, and distribute this
11 * software is freely granted, provided that this notice
12 * is preserved.
13 * ====================================================
14 */
15
16 #include "fdlibm.h"
17
18 static const float
19 TWO23[2]={
20 8.3886080000e+06, /* 0x4b000000 */
21 -8.3886080000e+06, /* 0xcb000000 */
22 };
23
24 float
rintf(float x)25 rintf(float x)
26 {
27 __int32_t i0,j0,sx;
28 __uint32_t i,i1,ix;
29 float t;
30 volatile float w;
31 GET_FLOAT_WORD(i0,x);
32 sx = (i0>>31)&1;
33 ix = (i0&0x7fffffff);
34 j0 = (ix>>23)-0x7f;
35 if(j0<23) {
36 if(FLT_UWORD_IS_ZERO(ix))
37 return x;
38 if(j0<0) {
39 i1 = (i0&0x07fffff);
40 i0 &= 0xfff00000;
41 i0 |= ((i1|-i1)>>9)&0x400000;
42 SET_FLOAT_WORD(x,i0);
43 w = TWO23[sx]+x;
44 t = w-TWO23[sx];
45 GET_FLOAT_WORD(i0,t);
46 SET_FLOAT_WORD(t,(i0&0x7fffffff)|(sx<<31));
47 return t;
48 } else {
49 i = (0x007fffff)>>j0;
50 if((i0&i)==0) return x; /* x is integral */
51 i>>=1;
52 if((i0&i)!=0) i0 = (i0&(~i))|((0x200000)>>j0);
53 }
54 } else {
55 if(!FLT_UWORD_IS_FINITE(ix))
56 return opt_barrier_float(x+x); /* inf or NaN */
57 else
58 return x; /* x is integral */
59 }
60 SET_FLOAT_WORD(x,i0);
61 w = TWO23[sx]+x;
62 return w-TWO23[sx];
63 }
64
65 _MATH_ALIAS_f_f(rint)
66