Rev 4872 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4349 | Serge | 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 | #ifdef __STDC__ |
||
19 | static const float |
||
20 | #else |
||
21 | static float |
||
22 | #endif |
||
23 | TWO23[2]={ |
||
24 | 8.3886080000e+06, /* 0x4b000000 */ |
||
25 | -8.3886080000e+06, /* 0xcb000000 */ |
||
26 | }; |
||
27 | |||
28 | #ifdef __STDC__ |
||
29 | float rintf(float x) |
||
30 | #else |
||
31 | float rintf(x) |
||
32 | float x; |
||
33 | #endif |
||
34 | { |
||
35 | __int32_t i0,j0,sx; |
||
36 | __uint32_t i,i1,ix; |
||
37 | float t; |
||
38 | volatile float w; |
||
39 | GET_FLOAT_WORD(i0,x); |
||
40 | sx = (i0>>31)&1; |
||
41 | ix = (i0&0x7fffffff); |
||
42 | j0 = (ix>>23)-0x7f; |
||
43 | if(j0<23) { |
||
44 | if(FLT_UWORD_IS_ZERO(ix)) |
||
45 | return x; |
||
46 | if(j0<0) { |
||
47 | i1 = (i0&0x07fffff); |
||
48 | i0 &= 0xfff00000; |
||
49 | i0 |= ((i1|-i1)>>9)&0x400000; |
||
50 | SET_FLOAT_WORD(x,i0); |
||
51 | w = TWO23[sx]+x; |
||
52 | t = w-TWO23[sx]; |
||
53 | GET_FLOAT_WORD(i0,t); |
||
54 | SET_FLOAT_WORD(t,(i0&0x7fffffff)|(sx<<31)); |
||
55 | return t; |
||
56 | } else { |
||
57 | i = (0x007fffff)>>j0; |
||
58 | if((i0&i)==0) return x; /* x is integral */ |
||
59 | i>>=1; |
||
60 | if((i0&i)!=0) i0 = (i0&(~i))|((0x200000)>>j0); |
||
61 | } |
||
62 | } else { |
||
63 | if(!FLT_UWORD_IS_FINITE(ix)) return x+x; /* inf or NaN */ |
||
64 | else |
||
65 | return x; /* x is integral */ |
||
66 | } |
||
67 | SET_FLOAT_WORD(x,i0); |
||
68 | w = TWO23[sx]+x; |
||
69 | return w-TWO23[sx]; |
||
70 | } |
||
71 | |||
72 | #ifdef _DOUBLE_IS_32BITS |
||
73 | |||
74 | #ifdef __STDC__ |
||
75 | double rint(double x) |
||
76 | #else |
||
77 | double rint(x) |
||
78 | double x; |
||
79 | #endif |
||
80 | { |
||
81 | return (double) rintf((float) x); |
||
82 | } |
||
83 | |||
84 | #endif /* defined(_DOUBLE_IS_32BITS) */31)); |