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-barriers.h> |
26 | #include <math_private.h> |
27 | #include <fenv_private.h> |
28 | #include <libm-alias-double.h> |
29 | |
30 | static const double |
31 | TWO52[2]={ |
32 | 4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */ |
33 | -4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */ |
34 | }; |
35 | |
36 | double |
37 | __nearbyint(double x) |
38 | { |
39 | fenv_t env; |
40 | int64_t i0,sx; |
41 | int32_t j0; |
42 | EXTRACT_WORDS64(i0,x); |
43 | sx = (i0>>63)&1; |
44 | j0 = ((i0>>52)&0x7ff)-0x3ff; |
45 | if(__builtin_expect(j0<52, 1)) { |
46 | if(j0<0) { |
47 | libc_feholdexcept (&env); |
48 | double w = TWO52[sx] + math_opt_barrier (x); |
49 | double t = w-TWO52[sx]; |
50 | math_force_eval (t); |
51 | libc_fesetenv (&env); |
52 | return copysign (t, x); |
53 | } |
54 | } else { |
55 | if(j0==0x400) return x+x; /* inf or NaN */ |
56 | else return x; /* x is integral */ |
57 | } |
58 | libc_feholdexcept (&env); |
59 | double w = TWO52[sx] + math_opt_barrier (x); |
60 | double t = w-TWO52[sx]; |
61 | math_force_eval (t); |
62 | libc_fesetenv (&env); |
63 | return t; |
64 | } |
65 | libm_alias_double (__nearbyint, nearbyint) |
66 | |