Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / ieee754 / ldbl-128ibm / s_rintl.c
blob20b75dd214b5e8beb7dabd70414e3b8e0bf49f42
1 /* Round to int long double floating-point values.
2 IBM extended format long double version.
3 Copyright (C) 2006-2014 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 /* This has been coded in assembler because GCC makes such a mess of it
21 when it's coded in C. */
23 #include <math.h>
24 #include <fenv_libc.h>
25 #include <math_ldbl_opt.h>
26 #include <float.h>
27 #include <ieee754.h>
30 long double
31 __rintl (long double x)
33 double xh, xl, hi, lo;
35 ldbl_unpack (x, &xh, &xl);
37 /* Return Inf, Nan, +/-0 unchanged. */
38 if (__builtin_expect (xh != 0.0
39 && __builtin_isless (__builtin_fabs (xh),
40 __builtin_inf ()), 1))
42 double orig_xh;
43 int save_round = __fegetround ();
45 /* Long double arithmetic, including the canonicalisation below,
46 only works in round-to-nearest mode. */
47 fesetround (FE_TONEAREST);
49 /* Convert the high double to integer. */
50 orig_xh = xh;
51 hi = ldbl_nearbyint (xh);
53 /* Subtract integral high part from the value. If the low double
54 happens to be exactly 0.5 or -0.5, you might think that this
55 subtraction could result in an incorrect conversion. For
56 instance, subtracting an odd number would cause this function
57 to round in the wrong direction. However, if we have a
58 canonical long double with the low double 0.5 or -0.5, then the
59 high double must be even. */
60 xh -= hi;
61 ldbl_canonicalize (&xh, &xl);
63 /* Now convert the low double, adjusted for any remainder from the
64 high double. */
65 lo = ldbl_nearbyint (xh);
67 xh -= lo;
68 ldbl_canonicalize (&xh, &xl);
70 switch (save_round)
72 case FE_TONEAREST:
73 if (xl > 0.0 && xh == 0.5)
74 lo += 1.0;
75 else if (xl < 0.0 && -xh == 0.5)
76 lo -= 1.0;
77 break;
79 case FE_TOWARDZERO:
80 if (orig_xh < 0.0)
81 goto do_up;
82 /* Fall thru */
84 case FE_DOWNWARD:
85 if (xh < 0.0 || (xh == 0.0 && xl < 0.0))
86 lo -= 1.0;
87 break;
89 case FE_UPWARD:
90 do_up:
91 if (xh > 0.0 || (xh == 0.0 && xl > 0.0))
92 lo += 1.0;
93 break;
96 /* Ensure the final value is canonical. In certain cases,
97 rounding causes hi,lo calculated so far to be non-canonical. */
98 xh = hi;
99 xl = lo;
100 ldbl_canonicalize (&xh, &xl);
102 /* Ensure we return -0 rather than +0 when appropriate. */
103 if (orig_xh < 0.0)
104 xh = -__builtin_fabs (xh);
106 fesetround (save_round);
109 return ldbl_pack (xh, xl);
112 long_double_symbol (libm, __rintl, rintl);