1 /* lrintf adapted to be llrintf for Newlib, 2009 by Craig Howland.  */
2 /* @(#)sf_lrint.c 5.1 93/09/24 */
3 /*
4  * ====================================================
5  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6  *
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
10  * is preserved.
11  * ====================================================
12  */
13 
14 /*
15  * llrintf(x)
16  * Return x rounded to integral value according to the prevailing
17  * rounding mode.
18  * Method:
19  *	Using floating addition.
20  * Exception:
21  *	Inexact flag raised if x not equal to llrintf(x).
22  */
23 
24 #include "fdlibm.h"
25 #include <limits.h>
26 
27 static const float
28 /* Adding a float, x, to 2^23 will cause the result to be rounded based on
29    the fractional part of x, according to the implementation's current rounding
30    mode.  2^23 is the smallest float that can be represented using all 23 significant
31    digits. */
32 TWO23[2]={
33   8.3886080000e+06, /* 0x4b000000 */
34  -8.3886080000e+06, /* 0xcb000000 */
35 };
36 
llrintf(float x)37 long long int llrintf(float x)
38 {
39   __int32_t j0,sx;
40   __uint32_t i0;
41   float t;
42   volatile float w;
43   long long int result;
44 
45   GET_FLOAT_WORD(i0,x);
46 
47   /* Extract sign bit. */
48   sx = (i0 >> 31);
49 
50   /* Extract exponent field. */
51   j0 = ((i0 & 0x7f800000) >> 23) - 127;
52 
53   if (j0 < (int)(sizeof (long long int) * 8) - 1)
54     {
55       if (j0 >= 23)
56         result = (long long int) ((i0 & 0x7fffff) | 0x800000) << (j0 - 23);
57       else
58         {
59           w = TWO23[sx] + x;
60           t = w - TWO23[sx];
61           GET_FLOAT_WORD (i0, t);
62           /* Detect the all-zeros representation of plus and
63              minus zero, which fails the calculation below. */
64           if ((i0 & ~((__uint32_t)1 << 31)) == 0)
65               return 0;
66           j0 = ((i0 >> 23) & 0xff) - 0x7f;
67           i0 &= 0x7fffff;
68           i0 |= 0x800000;
69           result = (j0 < 0 ? 0 : i0 >> (23 - j0));
70         }
71     }
72   else
73     {
74       if (x != LLONG_MIN)
75       {
76         __math_set_invalidf();
77         return sx ? LLONG_MIN : LLONG_MAX;
78       }
79       return (long long) x;
80     }
81   return sx ? -result : result;
82 }
83 
84 _MATH_ALIAS_k_f(llrint)
85