mirror of
https://sourceware.org/git/glibc.git
synced 2024-11-23 13:30:06 +00:00
41e8926aa4
This patch helps some math functions performance by adding the libc_fexxx variant of inline functions to handle both FPU round and exception set/restore and by using them on the libc_fexxx_ctx functions. It is based on already coded fexxx family functions for PPC with fpu. Here is the summary of performance improvements due this patch (measured on a POWER7 machine): Before: cos(): ITERS:9.5895e+07: TOTAL:5116.03Mcy, MAX:77.6cy, MIN:49.792cy, 18744 calls/Mcy exp(): ITERS:2.827e+07: TOTAL:5187.15Mcy, MAX:494.018cy, MIN:38.422cy, 5450.01 calls/Mcy pow(): ITERS:6.1705e+07: TOTAL:5144.26Mcy, MAX:171.95cy, MIN:29.935cy, 11994.9 calls/Mcy sin(): ITERS:8.6898e+07: TOTAL:5117.06Mcy, MAX:83.841cy, MIN:46.582cy, 16982 calls/Mcy tan(): ITERS:2.9473e+07: TOTAL:5115.39Mcy, MAX:191.017cy, MIN:172.352cy, 5761.63 calls/Mcy After: cos(): ITERS:2.05265e+08: TOTAL:5111.37Mcy, MAX:78.754cy, MIN:24.196cy, 40158.5 calls/Mcy exp(): ITERS:3.341e+07: TOTAL:5170.84Mcy, MAX:476.317cy, MIN:15.574cy, 6461.23 calls/Mcy pow(): ITERS:7.6153e+07: TOTAL:5129.1Mcy, MAX:147.5cy, MIN:30.916cy, 14847.2 calls/Mcy sin(): ITERS:1.58816e+08: TOTAL:5115.11Mcy, MAX:1490.39cy, MIN:22.341cy, 31048.4 calls/Mcy tan(): ITERS:3.4964e+07: TOTAL:5114.18Mcy, MAX:177.422cy, MIN:146.115cy, 6836.68 calls/Mcy
113 lines
3.1 KiB
C
113 lines
3.1 KiB
C
/* Round to int long double floating-point values.
|
|
IBM extended format long double version.
|
|
Copyright (C) 2006-2013 Free Software Foundation, Inc.
|
|
This file is part of the GNU C Library.
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Lesser General Public
|
|
License as published by the Free Software Foundation; either
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
License along with the GNU C Library; if not, see
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
/* This has been coded in assembler because GCC makes such a mess of it
|
|
when it's coded in C. */
|
|
|
|
#include <math.h>
|
|
#include <fenv_libc.h>
|
|
#include <math_ldbl_opt.h>
|
|
#include <float.h>
|
|
#include <ieee754.h>
|
|
|
|
|
|
long double
|
|
__rintl (long double x)
|
|
{
|
|
double xh, xl, hi, lo;
|
|
|
|
ldbl_unpack (x, &xh, &xl);
|
|
|
|
/* Return Inf, Nan, +/-0 unchanged. */
|
|
if (__builtin_expect (xh != 0.0
|
|
&& __builtin_isless (__builtin_fabs (xh),
|
|
__builtin_inf ()), 1))
|
|
{
|
|
double orig_xh;
|
|
int save_round = __fegetround ();
|
|
|
|
/* Long double arithmetic, including the canonicalisation below,
|
|
only works in round-to-nearest mode. */
|
|
fesetround (FE_TONEAREST);
|
|
|
|
/* Convert the high double to integer. */
|
|
orig_xh = xh;
|
|
hi = ldbl_nearbyint (xh);
|
|
|
|
/* Subtract integral high part from the value. If the low double
|
|
happens to be exactly 0.5 or -0.5, you might think that this
|
|
subtraction could result in an incorrect conversion. For
|
|
instance, subtracting an odd number would cause this function
|
|
to round in the wrong direction. However, if we have a
|
|
canonical long double with the low double 0.5 or -0.5, then the
|
|
high double must be even. */
|
|
xh -= hi;
|
|
ldbl_canonicalize (&xh, &xl);
|
|
|
|
/* Now convert the low double, adjusted for any remainder from the
|
|
high double. */
|
|
lo = ldbl_nearbyint (xh);
|
|
|
|
xh -= lo;
|
|
ldbl_canonicalize (&xh, &xl);
|
|
|
|
switch (save_round)
|
|
{
|
|
case FE_TONEAREST:
|
|
if (xl > 0.0 && xh == 0.5)
|
|
lo += 1.0;
|
|
else if (xl < 0.0 && -xh == 0.5)
|
|
lo -= 1.0;
|
|
break;
|
|
|
|
case FE_TOWARDZERO:
|
|
if (orig_xh < 0.0)
|
|
goto do_up;
|
|
/* Fall thru */
|
|
|
|
case FE_DOWNWARD:
|
|
if (xh < 0.0 || (xh == 0.0 && xl < 0.0))
|
|
lo -= 1.0;
|
|
break;
|
|
|
|
case FE_UPWARD:
|
|
do_up:
|
|
if (xh > 0.0 || (xh == 0.0 && xl > 0.0))
|
|
lo += 1.0;
|
|
break;
|
|
}
|
|
|
|
/* Ensure the final value is canonical. In certain cases,
|
|
rounding causes hi,lo calculated so far to be non-canonical. */
|
|
xh = hi;
|
|
xl = lo;
|
|
ldbl_canonicalize (&xh, &xl);
|
|
|
|
/* Ensure we return -0 rather than +0 when appropriate. */
|
|
if (orig_xh < 0.0)
|
|
xh = -__builtin_fabs (xh);
|
|
|
|
fesetround (save_round);
|
|
}
|
|
|
|
return ldbl_pack (xh, xl);
|
|
}
|
|
|
|
long_double_symbol (libm, __rintl, rintl);
|