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 * ====================================================
13 <<round>>, <<roundf>>---round to integer, to nearest
21 double round(double <[x]>);
22 float roundf(float <[x]>);
25 The <<round>> functions round their argument to the nearest integer
26 value in floating-point format, rounding halfway cases away from zero,
27 regardless of the current rounding direction. (While the "inexact"
28 floating-point exception behavior is unspecified by the C standard, the
29 <<round>> functions are written so that "inexact" is not raised if the
30 result does not equal the argument, which behavior is as recommended by
31 IEEE 754 for its related functions.)
34 <[x]> rounded to an integral value.
40 <<nearbyint>>, <<rint>>
46 #ifndef _DOUBLE_IS_32BITS
49 double round(double x
)
55 /* Most significant word, least significant word. */
56 __int32_t msw
, exponent_less_1023
;
59 EXTRACT_WORDS(msw
, lsw
, x
);
61 /* Extract exponent field. */
62 exponent_less_1023
= ((msw
& 0x7ff00000) >> 20) - 1023;
64 if (exponent_less_1023
< 20)
66 if (exponent_less_1023
< 0)
69 if (exponent_less_1023
== -1)
70 /* Result is +1.0 or -1.0. */
71 msw
|= ((__int32_t
)1023 << 20);
76 __uint32_t exponent_mask
= 0x000fffff >> exponent_less_1023
;
77 if ((msw
& exponent_mask
) == 0 && lsw
== 0)
78 /* x in an integral value. */
81 msw
+= 0x00080000 >> exponent_less_1023
;
82 msw
&= ~exponent_mask
;
86 else if (exponent_less_1023
> 51)
88 if (exponent_less_1023
== 1024)
89 /* x is NaN or infinite. */
96 __uint32_t exponent_mask
= 0xffffffff >> (exponent_less_1023
- 20);
99 if ((lsw
& exponent_mask
) == 0)
100 /* x is an integral value. */
103 tmp
= lsw
+ (1 << (51 - exponent_less_1023
));
108 lsw
&= ~exponent_mask
;
110 INSERT_WORDS(x
, msw
, lsw
);
115 #endif /* _DOUBLE_IS_32BITS */