1/*
2 * ====================================================
3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4 *
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
8 * is preserved.
9 * ====================================================
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 <math.h>
23#include <math_private.h>
24
25static const double
26TWO52[2]={
27 4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */
28 -4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */
29};
30
31double
32__rint(double x)
33{
34 int64_t i0,sx;
35 int32_t j0;
36 EXTRACT_WORDS64(i0,x);
37 sx = (i0>>63)&1;
38 j0 = ((i0>>52)&0x7ff)-0x3ff;
39 if(j0<52) {
40 if(j0<0) {
41 double w = TWO52[sx]+x;
42 double t = w-TWO52[sx];
43 EXTRACT_WORDS64(i0,t);
44 INSERT_WORDS64(t,(i0&UINT64_C(0x7fffffffffffffff))|(sx<<63));
45 return t;
46 }
47 } else {
48 if(j0==0x400) return x+x; /* inf or NaN */
49 else return x; /* x is integral */
50 }
51 double w = TWO52[sx]+x;
52 return w-TWO52[sx];
53}
54#ifndef __rint
55weak_alias (__rint, rint)
56# ifdef NO_LONG_DOUBLE
57strong_alias (__rint, __rintl)
58weak_alias (__rint, rintl)
59# endif
60#endif
61