glibc/sysdeps/ieee754/flt-32/lgamma_negf.c
Joseph Myers e44acb2063 Use floor functions not __floor functions in glibc libm.
Similar to the changes that were made to call sqrt functions directly
in glibc, instead of __ieee754_sqrt variants, so that the compiler
could inline them automatically without needing special inline
definitions in lots of math_private.h headers, this patch makes libm
code call floor functions directly instead of __floor variants,
removing the inlines / macros for x86_64 (SSE4.1) and powerpc
(POWER5).

The redirection used to ensure that __ieee754_sqrt does still get
called when the compiler doesn't inline a built-in function expansion
is refactored so it can be applied to other functions; the refactoring
is arranged so it's not limited to unary functions either (it would be
reasonable to use this mechanism for copysign - removing the inline in
math_private_calls.h but also eliminating unnecessary local PLT entry
use in the cases (powerpc soft-float and e500v1, for IBM long double)
where copysign calls don't get inlined).

The point of this change is that more architectures can get floor
calls inlined where they weren't previously (AArch64, for example),
without needing special inline definitions in their math_private.h,
and existing such definitions in math_private.h headers can be
removed.

Note that it's possible that in some cases an inline may be used where
an IFUNC call was previously used - this is the case on x86_64, for
example.  I think the direct calls to floor are still appropriate; if
there's any significant performance cost from inline SSE2 floor
instead of an IFUNC call ending up with SSE4.1 floor, that indicates
that either the function should be doing something else that's faster
than using floor at all, or it should itself have IFUNC variants, or
that the compiler choice of inlining for generic tuning should change
to allow for the possibility that, by not inlining, an SSE4.1 IFUNC
might be called at runtime - but not that glibc should avoid calling
floor internally.  (After all, all the same considerations would apply
to any user program calling floor, where it might either be inlined or
left as an out-of-line call allowing for a possible IFUNC.)

Tested for x86_64, and with build-many-glibcs.py.

	* include/math.h [!_ISOMAC && !(__FINITE_MATH_ONLY__ &&
	__FINITE_MATH_ONLY__ > 0) && !NO_MATH_REDIRECT] (MATH_REDIRECT):
	New macro.
	[!_ISOMAC && !(__FINITE_MATH_ONLY__ && __FINITE_MATH_ONLY__ > 0)
	&& !NO_MATH_REDIRECT] (MATH_REDIRECT_LDBL): Likewise.
	[!_ISOMAC && !(__FINITE_MATH_ONLY__ && __FINITE_MATH_ONLY__ > 0)
	&& !NO_MATH_REDIRECT] (MATH_REDIRECT_F128): Likewise.
	[!_ISOMAC && !(__FINITE_MATH_ONLY__ && __FINITE_MATH_ONLY__ > 0)
	&& !NO_MATH_REDIRECT] (MATH_REDIRECT_UNARY_ARGS): Likewise.
	[!_ISOMAC && !(__FINITE_MATH_ONLY__ && __FINITE_MATH_ONLY__ > 0)
	&& !NO_MATH_REDIRECT] (sqrt): Redirect using MATH_REDIRECT.
	[!_ISOMAC && !(__FINITE_MATH_ONLY__ && __FINITE_MATH_ONLY__ > 0)
	&& !NO_MATH_REDIRECT] (floor): Likewise.
	* sysdeps/aarch64/fpu/s_floor.c: Define NO_MATH_REDIRECT before
	header inclusion.
	* sysdeps/aarch64/fpu/s_floorf.c: Likewise.
	* sysdeps/ieee754/dbl-64/s_floor.c: Likewise.
	* sysdeps/ieee754/dbl-64/wordsize-64/s_floor.c: Likewise.
	* sysdeps/ieee754/float128/s_floorf128.c: Likewise.
	* sysdeps/ieee754/flt-32/s_floorf.c: Likewise.
	* sysdeps/ieee754/ldbl-128/s_floorl.c: Likewise.
	* sysdeps/ieee754/ldbl-128ibm/s_floorl.c: Likewise.
	* sysdeps/m68k/m680x0/fpu/s_floor_template.c: Likewise.
	* sysdeps/powerpc/powerpc32/power4/fpu/multiarch/s_floor.c: Likewise.
	* sysdeps/powerpc/powerpc32/power4/fpu/multiarch/s_floorf.c: Likewise.
	* sysdeps/powerpc/powerpc64/fpu/multiarch/s_floor.c: Likewise.
	* sysdeps/powerpc/powerpc64/fpu/multiarch/s_floorf.c: Likewise.
	* sysdeps/riscv/rv64/rvd/s_floor.c: Likewise.
	* sysdeps/riscv/rvf/s_floorf.c: Likewise.
	* sysdeps/sparc/sparc64/fpu/multiarch/s_floor.c: Likewise.
	* sysdeps/sparc/sparc64/fpu/multiarch/s_floorf.c: Likewise.
	* sysdeps/x86_64/fpu/multiarch/s_floor.c: Likewise.
	* sysdeps/x86_64/fpu/multiarch/s_floorf.c: Likewise.
	* sysdeps/powerpc/fpu/math_private.h [_ARCH_PWR5X] (__floor):
	Remove macro.
	[_ARCH_PWR5X] (__floorf): Likewise.
	* sysdeps/x86_64/fpu/math_private.h [__SSE4_1__] (__floor): Remove
	inline function.
	[__SSE4_1__] (__floorf): Likewise.
	* math/w_lgamma_main.c (LGFUNC (__lgamma)): Use floor functions
	instead of __floor variants.
	* math/w_lgamma_r_compat.c (__lgamma_r): Likewise.
	* math/w_lgammaf_main.c (LGFUNC (__lgammaf)): Likewise.
	* math/w_lgammaf_r_compat.c (__lgammaf_r): Likewise.
	* math/w_lgammal_main.c (LGFUNC (__lgammal)): Likewise.
	* math/w_lgammal_r_compat.c (__lgammal_r): Likewise.
	* math/w_tgamma_compat.c (__tgamma): Likewise.
	* math/w_tgamma_template.c (M_DECL_FUNC (__tgamma)): Likewise.
	* math/w_tgammaf_compat.c (__tgammaf): Likewise.
	* math/w_tgammal_compat.c (__tgammal): Likewise.
	* sysdeps/ieee754/dbl-64/e_lgamma_r.c (sin_pi): Likewise.
	* sysdeps/ieee754/dbl-64/k_rem_pio2.c (__kernel_rem_pio2):
	Likewise.
	* sysdeps/ieee754/dbl-64/lgamma_neg.c (__lgamma_neg): Likewise.
	* sysdeps/ieee754/flt-32/e_lgammaf_r.c (sin_pif): Likewise.
	* sysdeps/ieee754/flt-32/lgamma_negf.c (__lgamma_negf): Likewise.
	* sysdeps/ieee754/ldbl-128/e_lgammal_r.c (__ieee754_lgammal_r):
	Likewise.
	* sysdeps/ieee754/ldbl-128/e_powl.c (__ieee754_powl): Likewise.
	* sysdeps/ieee754/ldbl-128/lgamma_negl.c (__lgamma_negl):
	Likewise.
	* sysdeps/ieee754/ldbl-128/s_expm1l.c (__expm1l): Likewise.
	* sysdeps/ieee754/ldbl-128ibm/e_lgammal_r.c (__ieee754_lgammal_r):
	Likewise.
	* sysdeps/ieee754/ldbl-128ibm/e_powl.c (__ieee754_powl): Likewise.
	* sysdeps/ieee754/ldbl-128ibm/lgamma_negl.c (__lgamma_negl):
	Likewise.
	* sysdeps/ieee754/ldbl-128ibm/s_expm1l.c (__expm1l): Likewise.
	* sysdeps/ieee754/ldbl-128ibm/s_truncl.c (__truncl): Likewise.
	* sysdeps/ieee754/ldbl-96/e_lgammal_r.c (sin_pi): Likewise.
	* sysdeps/ieee754/ldbl-96/lgamma_negl.c (__lgamma_negl): Likewise.
	* sysdeps/powerpc/power5+/fpu/s_modf.c (__modf): Likewise.
	* sysdeps/powerpc/power5+/fpu/s_modff.c (__modff): Likewise.
2018-09-14 13:09:01 +00:00

283 lines
8.0 KiB
C

/* lgammaf expanding around zeros.
Copyright (C) 2015-2018 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/>. */
#include <float.h>
#include <math.h>
#include <math-narrow-eval.h>
#include <math_private.h>
#include <fenv_private.h>
static const float lgamma_zeros[][2] =
{
{ -0x2.74ff94p+0f, 0x1.3fe0f2p-24f },
{ -0x2.bf682p+0f, -0x1.437b2p-24f },
{ -0x3.24c1b8p+0f, 0x6.c34cap-28f },
{ -0x3.f48e2cp+0f, 0x1.707a04p-24f },
{ -0x4.0a13ap+0f, 0x1.e99aap-24f },
{ -0x4.fdd5ep+0f, 0x1.64454p-24f },
{ -0x5.021a98p+0f, 0x2.03d248p-24f },
{ -0x5.ffa4cp+0f, 0x2.9b82fcp-24f },
{ -0x6.005ac8p+0f, -0x1.625f24p-24f },
{ -0x6.fff3p+0f, 0x2.251e44p-24f },
{ -0x7.000dp+0f, 0x8.48078p-28f },
{ -0x7.fffe6p+0f, 0x1.fa98c4p-28f },
{ -0x8.0001ap+0f, -0x1.459fcap-28f },
{ -0x8.ffffdp+0f, -0x1.c425e8p-24f },
{ -0x9.00003p+0f, 0x1.c44b82p-24f },
{ -0xap+0f, 0x4.9f942p-24f },
{ -0xap+0f, -0x4.9f93b8p-24f },
{ -0xbp+0f, 0x6.b9916p-28f },
{ -0xbp+0f, -0x6.b9915p-28f },
{ -0xcp+0f, 0x8.f76c8p-32f },
{ -0xcp+0f, -0x8.f76c7p-32f },
{ -0xdp+0f, 0xb.09231p-36f },
{ -0xdp+0f, -0xb.09231p-36f },
{ -0xep+0f, 0xc.9cba5p-40f },
{ -0xep+0f, -0xc.9cba5p-40f },
{ -0xfp+0f, 0xd.73f9fp-44f },
};
static const float e_hi = 0x2.b7e15p+0f, e_lo = 0x1.628aeep-24f;
/* Coefficients B_2k / 2k(2k-1) of x^-(2k-1) in Stirling's
approximation to lgamma function. */
static const float lgamma_coeff[] =
{
0x1.555556p-4f,
-0xb.60b61p-12f,
0x3.403404p-12f,
};
#define NCOEFF (sizeof (lgamma_coeff) / sizeof (lgamma_coeff[0]))
/* Polynomial approximations to (|gamma(x)|-1)(x-n)/(x-x0), where n is
the integer end-point of the half-integer interval containing x and
x0 is the zero of lgamma in that half-integer interval. Each
polynomial is expressed in terms of x-xm, where xm is the midpoint
of the interval for which the polynomial applies. */
static const float poly_coeff[] =
{
/* Interval [-2.125, -2] (polynomial degree 5). */
-0x1.0b71c6p+0f,
-0xc.73a1ep-4f,
-0x1.ec8462p-4f,
-0xe.37b93p-4f,
-0x1.02ed36p-4f,
-0xe.cbe26p-4f,
/* Interval [-2.25, -2.125] (polynomial degree 5). */
-0xf.29309p-4f,
-0xc.a5cfep-4f,
0x3.9c93fcp-4f,
-0x1.02a2fp+0f,
0x9.896bep-4f,
-0x1.519704p+0f,
/* Interval [-2.375, -2.25] (polynomial degree 5). */
-0xd.7d28dp-4f,
-0xe.6964cp-4f,
0xb.0d4f1p-4f,
-0x1.9240aep+0f,
0x1.dadabap+0f,
-0x3.1778c4p+0f,
/* Interval [-2.5, -2.375] (polynomial degree 6). */
-0xb.74ea2p-4f,
-0x1.2a82cp+0f,
0x1.880234p+0f,
-0x3.320c4p+0f,
0x5.572a38p+0f,
-0x9.f92bap+0f,
0x1.1c347ep+4f,
/* Interval [-2.625, -2.5] (polynomial degree 6). */
-0x3.d10108p-4f,
0x1.cd5584p+0f,
0x3.819c24p+0f,
0x6.84cbb8p+0f,
0xb.bf269p+0f,
0x1.57fb12p+4f,
0x2.7b9854p+4f,
/* Interval [-2.75, -2.625] (polynomial degree 6). */
-0x6.b5d25p-4f,
0x1.28d604p+0f,
0x1.db6526p+0f,
0x2.e20b38p+0f,
0x4.44c378p+0f,
0x6.62a08p+0f,
0x9.6db3ap+0f,
/* Interval [-2.875, -2.75] (polynomial degree 5). */
-0x8.a41b2p-4f,
0xc.da87fp-4f,
0x1.147312p+0f,
0x1.7617dap+0f,
0x1.d6c13p+0f,
0x2.57a358p+0f,
/* Interval [-3, -2.875] (polynomial degree 5). */
-0xa.046d6p-4f,
0x9.70b89p-4f,
0xa.a89a6p-4f,
0xd.2f2d8p-4f,
0xd.e32b4p-4f,
0xf.fb741p-4f,
};
static const size_t poly_deg[] =
{
5,
5,
5,
6,
6,
6,
5,
5,
};
static const size_t poly_end[] =
{
5,
11,
17,
24,
31,
38,
44,
50,
};
/* Compute sin (pi * X) for -0.25 <= X <= 0.5. */
static float
lg_sinpi (float x)
{
if (x <= 0.25f)
return __sinf ((float) M_PI * x);
else
return __cosf ((float) M_PI * (0.5f - x));
}
/* Compute cos (pi * X) for -0.25 <= X <= 0.5. */
static float
lg_cospi (float x)
{
if (x <= 0.25f)
return __cosf ((float) M_PI * x);
else
return __sinf ((float) M_PI * (0.5f - x));
}
/* Compute cot (pi * X) for -0.25 <= X <= 0.5. */
static float
lg_cotpi (float x)
{
return lg_cospi (x) / lg_sinpi (x);
}
/* Compute lgamma of a negative argument -15 < X < -2, setting
*SIGNGAMP accordingly. */
float
__lgamma_negf (float x, int *signgamp)
{
/* Determine the half-integer region X lies in, handle exact
integers and determine the sign of the result. */
int i = floorf (-2 * x);
if ((i & 1) == 0 && i == -2 * x)
return 1.0f / 0.0f;
float xn = ((i & 1) == 0 ? -i / 2 : (-i - 1) / 2);
i -= 4;
*signgamp = ((i & 2) == 0 ? -1 : 1);
SET_RESTORE_ROUNDF (FE_TONEAREST);
/* Expand around the zero X0 = X0_HI + X0_LO. */
float x0_hi = lgamma_zeros[i][0], x0_lo = lgamma_zeros[i][1];
float xdiff = x - x0_hi - x0_lo;
/* For arguments in the range -3 to -2, use polynomial
approximations to an adjusted version of the gamma function. */
if (i < 2)
{
int j = floorf (-8 * x) - 16;
float xm = (-33 - 2 * j) * 0.0625f;
float x_adj = x - xm;
size_t deg = poly_deg[j];
size_t end = poly_end[j];
float g = poly_coeff[end];
for (size_t j = 1; j <= deg; j++)
g = g * x_adj + poly_coeff[end - j];
return __log1pf (g * xdiff / (x - xn));
}
/* The result we want is log (sinpi (X0) / sinpi (X))
+ log (gamma (1 - X0) / gamma (1 - X)). */
float x_idiff = fabsf (xn - x), x0_idiff = fabsf (xn - x0_hi - x0_lo);
float log_sinpi_ratio;
if (x0_idiff < x_idiff * 0.5f)
/* Use log not log1p to avoid inaccuracy from log1p of arguments
close to -1. */
log_sinpi_ratio = __ieee754_logf (lg_sinpi (x0_idiff)
/ lg_sinpi (x_idiff));
else
{
/* Use log1p not log to avoid inaccuracy from log of arguments
close to 1. X0DIFF2 has positive sign if X0 is further from
XN than X is from XN, negative sign otherwise. */
float x0diff2 = ((i & 1) == 0 ? xdiff : -xdiff) * 0.5f;
float sx0d2 = lg_sinpi (x0diff2);
float cx0d2 = lg_cospi (x0diff2);
log_sinpi_ratio = __log1pf (2 * sx0d2
* (-sx0d2 + cx0d2 * lg_cotpi (x_idiff)));
}
float log_gamma_ratio;
float y0 = math_narrow_eval (1 - x0_hi);
float y0_eps = -x0_hi + (1 - y0) - x0_lo;
float y = math_narrow_eval (1 - x);
float y_eps = -x + (1 - y);
/* We now wish to compute LOG_GAMMA_RATIO
= log (gamma (Y0 + Y0_EPS) / gamma (Y + Y_EPS)). XDIFF
accurately approximates the difference Y0 + Y0_EPS - Y -
Y_EPS. Use Stirling's approximation. */
float log_gamma_high
= (xdiff * __log1pf ((y0 - e_hi - e_lo + y0_eps) / e_hi)
+ (y - 0.5f + y_eps) * __log1pf (xdiff / y));
/* Compute the sum of (B_2k / 2k(2k-1))(Y0^-(2k-1) - Y^-(2k-1)). */
float y0r = 1 / y0, yr = 1 / y;
float y0r2 = y0r * y0r, yr2 = yr * yr;
float rdiff = -xdiff / (y * y0);
float bterm[NCOEFF];
float dlast = rdiff, elast = rdiff * yr * (yr + y0r);
bterm[0] = dlast * lgamma_coeff[0];
for (size_t j = 1; j < NCOEFF; j++)
{
float dnext = dlast * y0r2 + elast;
float enext = elast * yr2;
bterm[j] = dnext * lgamma_coeff[j];
dlast = dnext;
elast = enext;
}
float log_gamma_low = 0;
for (size_t j = 0; j < NCOEFF; j++)
log_gamma_low += bterm[NCOEFF - 1 - j];
log_gamma_ratio = log_gamma_high + log_gamma_low;
return log_sinpi_ratio + log_gamma_ratio;
}