|
Packit |
6c4009 |
/*
|
|
Packit |
6c4009 |
* ====================================================
|
|
Packit |
6c4009 |
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
|
Packit |
6c4009 |
*
|
|
Packit |
6c4009 |
* Developed at SunPro, a Sun Microsystems, Inc. business.
|
|
Packit |
6c4009 |
* Permission to use, copy, modify, and distribute this
|
|
Packit |
6c4009 |
* software is freely granted, provided that this notice
|
|
Packit |
6c4009 |
* is preserved.
|
|
Packit |
6c4009 |
* ====================================================
|
|
Packit |
6c4009 |
*/
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/*
|
|
Packit |
6c4009 |
* rint(x)
|
|
Packit |
6c4009 |
* Return x rounded to integral value according to the prevailing
|
|
Packit |
6c4009 |
* rounding mode.
|
|
Packit |
6c4009 |
* Method:
|
|
Packit |
6c4009 |
* Using floating addition.
|
|
Packit |
6c4009 |
* Exception:
|
|
Packit |
6c4009 |
* Inexact flag raised if x not equal to rint(x).
|
|
Packit |
6c4009 |
*/
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <math.h>
|
|
Packit |
6c4009 |
#include <math_private.h>
|
|
Packit |
6c4009 |
#include <libm-alias-double.h>
|
|
Packit Service |
a2e3a9 |
#include <math-use-builtins.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
double
|
|
Packit |
6c4009 |
__rint (double x)
|
|
Packit |
6c4009 |
{
|
|
Packit Service |
a2e3a9 |
#if USE_RINT_BUILTIN
|
|
Packit Service |
a2e3a9 |
return __builtin_rint (x);
|
|
Packit Service |
a2e3a9 |
#else
|
|
Packit Service |
a2e3a9 |
/* Use generic implementation. */
|
|
Packit Service |
a2e3a9 |
static const double
|
|
Packit Service |
a2e3a9 |
TWO52[2] = {
|
|
Packit Service |
a2e3a9 |
4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */
|
|
Packit Service |
a2e3a9 |
-4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */
|
|
Packit Service |
a2e3a9 |
};
|
|
Packit Service |
a498a2 |
int64_t i0, sx;
|
|
Packit Service |
a498a2 |
int32_t j0;
|
|
Packit Service |
a498a2 |
EXTRACT_WORDS64 (i0, x);
|
|
Packit Service |
a498a2 |
sx = (i0 >> 63) & 1;
|
|
Packit Service |
a498a2 |
j0 = ((i0 >> 52) & 0x7ff) - 0x3ff;
|
|
Packit |
6c4009 |
if (j0 < 52)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (j0 < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit Service |
a498a2 |
double w = TWO52[sx] + x;
|
|
Packit Service |
a498a2 |
double t = w - TWO52[sx];
|
|
Packit Service |
a498a2 |
EXTRACT_WORDS64 (i0, t);
|
|
Packit Service |
a498a2 |
INSERT_WORDS64 (t, (i0 & UINT64_C (0x7fffffffffffffff))
|
|
Packit Service |
a498a2 |
| (sx << 63));
|
|
Packit |
6c4009 |
return t;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (j0 == 0x400)
|
|
Packit Service |
a498a2 |
return x + x; /* inf or NaN */
|
|
Packit |
6c4009 |
else
|
|
Packit Service |
a498a2 |
return x; /* x is integral */
|
|
Packit |
6c4009 |
}
|
|
Packit Service |
a498a2 |
double w = TWO52[sx] + x;
|
|
Packit |
6c4009 |
return w - TWO52[sx];
|
|
Packit Service |
a2e3a9 |
#endif /* ! USE_RINT_BUILTIN */
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
#ifndef __rint
|
|
Packit |
6c4009 |
libm_alias_double (__rint, rint)
|
|
Packit |
6c4009 |
#endif
|