Use libm_alias_double for remaining powerpc functions.
[glibc.git] / sysdeps / powerpc / powerpc32 / fpu / s_llround.c
blobffd56c80c3c6b98f18b2d2b4f4213bfe601fd599
1 /* Round double value to long long int.
2 Copyright (C) 1997-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <limits.h>
20 #include <math.h>
21 #include <math_ldbl_opt.h>
22 #include <math_private.h>
23 #include <stdint.h>
24 #include <libm-alias-double.h>
26 /* Round to the nearest integer, with values exactly on a 0.5 boundary
27 rounded away from zero, regardless of the current rounding mode.
28 If (long long)x, when x is out of range of a long long, clips at
29 LLONG_MAX or LLONG_MIN, then this implementation also clips. */
31 long long int
32 __llround (double x)
34 long long xr;
35 if (HAVE_PPC_FCTIDZ)
36 xr = (long long) x;
37 else
39 /* Avoid incorrect exceptions from libgcc conversions (as of GCC
40 5): <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59412>. */
41 if (fabs (x) < 0x1p31)
42 xr = (long long int) (long int) x;
43 else
45 uint64_t i0;
46 EXTRACT_WORDS64 (i0, x);
47 int exponent = ((i0 >> 52) & 0x7ff) - 0x3ff;
48 if (exponent < 63)
50 unsigned long long int mant
51 = (i0 & ((1ULL << 52) - 1)) | (1ULL << 52);
52 if (exponent < 52)
53 /* llround is not required to raise "inexact". */
54 mant >>= 52 - exponent;
55 else
56 mant <<= exponent - 52;
57 xr = (long long int) ((i0 & (1ULL << 63)) != 0 ? -mant : mant);
59 else if (x == (double) LLONG_MIN)
60 xr = LLONG_MIN;
61 else
62 xr = (long long int) (long int) x << 32;
65 /* Avoid spurious "inexact" converting LLONG_MAX to double, and from
66 subtraction when the result is out of range, by returning early
67 for arguments large enough that no rounding is needed. */
68 if (!(fabs (x) < 0x1p52))
69 return xr;
70 double xrf = (double) xr;
72 if (x >= 0.0)
74 if (x - xrf >= 0.5)
75 xr += (long long) ((unsigned long long) xr + 1) > 0;
77 else
79 if (xrf - x >= 0.5)
80 xr -= (long long) ((unsigned long long) xr - 1) < 0;
82 return xr;
84 libm_alias_double (__llround, llround)