POK
rintf.c
1 /*
2  * POK header
3  *
4  * The following file is a part of the POK project. Any modification should
5  * made according to the POK licence. You CANNOT use this file or a part of
6  * this file is this part of a file for your own project
7  *
8  * For more information on the POK licence, please see our LICENCE FILE
9  *
10  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
11  *
12  * Copyright (c) 2007-2009 POK team
13  *
14  * Created by julien on Fri Jan 30 14:41:34 2009
15  */
16 
17 /* s_rintf.c -- float version of s_rint.c.
18  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
19  */
20 
21 /*
22  * ====================================================
23  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
24  *
25  * Developed at SunPro, a Sun Microsystems, Inc. business.
26  * Permission to use, copy, modify, and distribute this
27  * software is freely granted, provided that this notice
28  * is preserved.
29  * ====================================================
30  */
31 
32 #ifdef POK_NEEDS_LIBMATH
33 
34 #include <libm.h>
35 #include "math_private.h"
36 
37 static const float
38 TWO23[2]={
39  8.3886080000e+06, /* 0x4b000000 */
40  -8.3886080000e+06, /* 0xcb000000 */
41 };
42 
43 float
44 rintf(float x)
45 {
46  int32_t i0,jj0,sx;
47  uint32_t i,i1;
48 #ifdef __i386__ /* XXX gcc4 will omit the rounding otherwise */
49  volatile
50 #endif
51  float w;
52  float t;
53  GET_FLOAT_WORD(i0,x);
54  sx = (i0>>31)&1;
55  jj0 = ((i0>>23)&0xff)-0x7f;
56  if(jj0<23) {
57  if(jj0<0) {
58  if((i0&0x7fffffff)==0) return x;
59  i1 = (i0&0x07fffff);
60  i0 &= 0xfff00000;
61  i0 |= ((i1|-i1)>>9)&0x400000;
62  SET_FLOAT_WORD(x,i0);
63  w = TWO23[sx]+x;
64  t = w-TWO23[sx];
65  GET_FLOAT_WORD(i0,t);
66  SET_FLOAT_WORD(t,(i0&0x7fffffff)|(sx<<31));
67  return t;
68  } else {
69  i = (0x007fffff)>>jj0;
70  if((i0&i)==0) return x; /* x is integral */
71  i>>=1;
72  if((i0&i)!=0) i0 = (i0&(~i))|((0x100000)>>jj0);
73  }
74  } else {
75  if(jj0==0x80) return x+x; /* inf or NaN */
76  else return x; /* x is integral */
77  }
78  SET_FLOAT_WORD(x,i0);
79  w = TWO23[sx]+x;
80  return w-TWO23[sx];
81 }
82 
83 #endif