2 * ====================================================
3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 * Developed at SunPro, a Sun Microsystems, Inc. business.
6 * Permission to use, copy, modify, and distribute this
7 * software is freely granted, provided that this notice
9 * ====================================================
14 * Return x rounded to integral value according to the prevailing
17 * Using floating addition.
19 * Inexact flag raised if x not equal to rint(x).
23 #include "math_private.h"
27 4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */
28 -4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */
36 /* We use w = x + 2^52; t = w - 2^52; trick to round x to integer.
37 * This trick requires that compiler does not optimize it
38 * by keeping intermediate result w in a register wider than double.
39 * Declaring w volatile assures that value gets truncated to double
40 * (unfortunately, it also forces store+load):
44 EXTRACT_WORDS(i0
,i1
,x
);
45 /* Unbiased exponent */
46 _j0
= ((((u_int32_t
)i0
) >> 20)&0x7ff)-0x3ff;
49 //Why bother? Just returning x works too
50 //if (_j0 == 0x400) /* inf or NaN */
52 return x
; /* x is integral */
56 sx
= ((u_int32_t
)i0
) >> 31;
59 if (_j0
<0) { /* |x| < 1 */
60 if (((i0
&0x7fffffff)|i1
)==0) return x
;
63 i0
|= ((i1
|-i1
)>>12)&0x80000;
68 SET_HIGH_WORD(t
,(i0
&0x7fffffff)|(sx
<<31));
71 i
= (0x000fffff)>>_j0
;
72 if (((i0
&i
)|i1
)==0) return x
; /* x is integral */
75 if (_j0
==19) i1
= 0x40000000;
76 else i0
= (i0
&(~i
))|((0x20000)>>_j0
);
80 i
= ((u_int32_t
)(0xffffffff))>>(_j0
-20);
81 if ((i1
&i
)==0) return x
; /* x is integral */
83 if ((i1
&i
)!=0) i1
= (i1
&(~i
))|((0x40000000)>>(_j0
-20));
85 INSERT_WORDS(x
,i0
,i1
);
91 strong_alias(rint
, nearbyint
)
92 libm_hidden_def(nearbyint
)