1/* Adapted for use as nearbyint by Ulrich Drepper <drepper@cygnus.com>. */
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/*
14 * rint(x)
15 * Return x rounded to integral value according to the prevailing
16 * rounding mode.
17 * Method:
18 * Using floating addition.
19 * Exception:
20 * Inexact flag raised if x not equal to rint(x).
21 */
22
23#include <fenv.h>
24#include <math.h>
25#include <math_private.h>
26#include <libm-alias-double.h>
27
28static const double
29TWO52[2]={
30 4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */
31 -4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */
32};
33
34double
35__nearbyint(double x)
36{
37 fenv_t env;
38 int64_t i0,sx;
39 int32_t j0;
40 EXTRACT_WORDS64(i0,x);
41 sx = (i0>>63)&1;
42 j0 = ((i0>>52)&0x7ff)-0x3ff;
43 if(__builtin_expect(j0<52, 1)) {
44 if(j0<0) {
45 libc_feholdexcept (&env);
46 double w = TWO52[sx] + math_opt_barrier (x);
47 double t = w-TWO52[sx];
48 math_force_eval (t);
49 libc_fesetenv (&env);
50 return __copysign (t, x);
51 }
52 } else {
53 if(j0==0x400) return x+x; /* inf or NaN */
54 else return x; /* x is integral */
55 }
56 libc_feholdexcept (&env);
57 double w = TWO52[sx] + math_opt_barrier (x);
58 double t = w-TWO52[sx];
59 math_force_eval (t);
60 libc_fesetenv (&env);
61 return t;
62}
63libm_alias_double (__nearbyint, nearbyint)
64