1 /* lrintf adapted to be llrintf for Newlib, 2009 by Craig Howland. */
2 /* @(#)sf_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 * ====================================================
16 * Return x rounded to integral value according to the prevailing
19 * Using floating addition.
21 * Inexact flag raised if x not equal to llrintf(x).
31 /* Adding a float, x, to 2^23 will cause the result to be rounded based on
32 the fractional part of x, according to the implementation's current rounding
33 mode. 2^23 is the smallest float that can be represented using all 23 significant
36 8.3886080000e+06, /* 0x4b000000 */
37 -8.3886080000e+06, /* 0xcb000000 */
41 long long int llrintf(float x
)
43 long long int llrintf(x
)
55 /* Extract sign bit. */
58 /* Extract exponent field. */
59 j0
= ((i0
& 0x7f800000) >> 23) - 127;
61 if (j0
< (int)(sizeof (long long int) * 8) - 1)
64 result
= (long long int) ((i0
& 0x7fffff) | 0x800000) << (j0
- 23);
69 GET_FLOAT_WORD (i0
, t
);
70 /* Detect the all-zeros representation of plus and
71 minus zero, which fails the calculation below. */
72 if ((i0
& ~((__uint32_t
)1 << 31)) == 0)
74 j0
= ((i0
>> 23) & 0xff) - 0x7f;
77 result
= (j0
< 0 ? 0 : i0
>> (23 - j0
));
82 return (long long int) x
;
84 return sx
? -result
: result
;
87 #ifdef _DOUBLE_IS_32BITS
90 long long int llrint(double x
)
92 long long int llrint(x
)
96 return llrintf((float) x
);
99 #endif /* defined(_DOUBLE_IS_32BITS) */