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