2 /* @(#)s_lrint.c 5.1 93/09/24 */
4 * ====================================================
5 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7 * Developed at SunPro, a Sun Microsystems, Inc. business.
8 * Permission to use, copy, modify, and distribute this
9 * software is freely granted, provided that this notice
11 * ====================================================
15 <<lrint>>, <<lrintf>>, <<llrint>>, <<llrintf>>---round to integer
27 long int lrint(double <[x]>);
28 long int lrintf(float <[x]>);
29 long long int llrint(double <[x]>);
30 long long int llrintf(float <[x]>);
33 The <<lrint>> and <<llrint>> functions round their argument to the nearest
34 integer value, using the current rounding direction. If the rounded value is
35 outside the range of the return type, the numeric result is unspecified. A
36 range error may occur if the magnitude of <[x]> is too large.
37 The "inexact" floating-point exception is raised in implementations that
38 support it when the result differs in value from the argument (i.e., when
39 a fraction actually has been truncated).
42 <[x]> rounded to an integral value, using the current rounding direction.
54 * Return x rounded to integral value according to the prevailing
57 * Using floating addition.
59 * Inexact flag raised if x not equal to lrint(x).
64 #ifndef _DOUBLE_IS_32BITS
72 /* Adding a double, x, to 2^52 will cause the result to be rounded based on
73 the fractional part of x, according to the implementation's current rounding
74 mode. 2^52 is the smallest double that can be represented using all 52 significant
77 4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */
78 -4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */
82 long int lrint(double x
)
94 EXTRACT_WORDS(i0
,i1
,x
);
96 /* Extract sign bit. */
99 /* Extract exponent field. */
100 j0
= ((i0
& 0x7ff00000) >> 20) - 1023;
101 /* j0 in [-1023,1024] */
105 /* j0 in [-1023,19] */
108 GET_HIGH_WORD(i0
, t
);
109 /* Detect the all-zeros representation of plus and
110 minus zero, which fails the calculation below. */
111 if ((i0
& ~(1L << 31)) == 0)
113 j0
= ((i0
& 0x7ff00000) >> 20) - 1023;
116 result
= (j0
< 0 ? 0 : i0
>> (20 - j0
));
118 else if (j0
< (int)(8 * sizeof (long int)) - 1)
120 /* 32bit return: j0 in [20,30] */
121 /* 64bit return: j0 in [20,62] */
123 /* 64bit return: j0 in [52,62] */
124 /* 64bit return: left shift amt in [32,42] */
125 result
= ((long int) ((i0
& 0x000fffff) | 0x00100000) << (j0
- 20)) |
126 /* 64bit return: right shift amt in [0,10] */
127 ((long int) i1
<< (j0
- 52));
130 /* 32bit return: j0 in [20,30] */
131 /* 64bit return: j0 in [20,51] */
134 EXTRACT_WORDS (i0
, i1
, t
);
135 j0
= ((i0
& 0x7ff00000) >> 20) - 1023;
139 * 32bit return: j0 in [20,31];
140 * 64bit return: j0 in [20,52] */
141 /* 32bit return: left shift amt in [0,11] */
142 /* 64bit return: left shift amt in [0,32] */
143 /* ***32bit return: right shift amt in [32,21] */
144 /* ***64bit return: right shift amt in [32,0] */
145 result
= ((long int) i0
<< (j0
- 20))
146 | SAFE_RIGHT_SHIFT (i1
, (52 - j0
));
154 return sx
? -result
: result
;
157 #endif /* _DOUBLE_IS_32BITS */