rework langinfo code for ABI compat and for use by time code
[musl] / src / math / rint.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/s_rint.c */
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunPro, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  */
12 /*
13  * rint(x)
14  * Return x rounded to integral value according to the prevailing
15  * rounding mode.
16  * Method:
17  *      Using floating addition.
18  * Exception:
19  *      Inexact flag raised if x not equal to rint(x).
20  */
21
22 #include "libm.h"
23
24 static const double
25 TWO52[2] = {
26   4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */
27  -4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */
28 };
29
30 double rint(double x)
31 {
32         int32_t i0,j0,sx;
33         uint32_t i,i1;
34         double w,t;
35
36         EXTRACT_WORDS(i0, i1, x);
37         // FIXME: signed shift
38         sx = (i0>>31) & 1;
39         j0 = ((i0>>20)&0x7ff) - 0x3ff;
40         if (j0 < 20) {
41                 if (j0 < 0) {
42                         if (((i0&0x7fffffff)|i1) == 0)
43                                 return x;
44                         i1 |= i0 & 0x0fffff;
45                         i0 &= 0xfffe0000;
46                         i0 |= ((i1|-i1)>>12) & 0x80000;
47                         SET_HIGH_WORD(x, i0);
48                         STRICT_ASSIGN(double, w, TWO52[sx] + x);
49                         t = w - TWO52[sx];
50                         GET_HIGH_WORD(i0, t);
51                         SET_HIGH_WORD(t, (i0&0x7fffffff)|(sx<<31));
52                         return t;
53                 } else {
54                         i = 0x000fffff>>j0;
55                         if (((i0&i)|i1) == 0)
56                                 return x; /* x is integral */
57                         i >>= 1;
58                         if (((i0&i)|i1) != 0) {
59                                 /*
60                                  * Some bit is set after the 0.5 bit.  To avoid the
61                                  * possibility of errors from double rounding in
62                                  * w = TWO52[sx]+x, adjust the 0.25 bit to a lower
63                                  * guard bit.  We do this for all j0<=51.  The
64                                  * adjustment is trickiest for j0==18 and j0==19
65                                  * since then it spans the word boundary.
66                                  */
67                                 if (j0 == 19)
68                                         i1 = 0x40000000;
69                                 else if (j0 == 18)
70                                         i1 = 0x80000000;
71                                 else
72                                         i0 = (i0 & ~i)|(0x20000>>j0);
73                         }
74                 }
75         } else if (j0 > 51) {
76                 if (j0 == 0x400)
77                         return x+x;  /* inf or NaN */
78                 return x;            /* x is integral */
79         } else {
80                 i = (uint32_t)0xffffffff>>(j0-20);
81                 if ((i1&i) == 0)
82                         return x;    /* x is integral */
83                 i >>= 1;
84                 if ((i1&i) != 0)
85                         i1 = (i1 & ~i)|(0x40000000>>(j0-20));
86         }
87         INSERT_WORDS(x, i0, i1);
88         STRICT_ASSIGN(double, w, TWO52[sx] + x);
89         return w - TWO52[sx];
90 }