#include "arb.h"
#include "arb_hypgeom.h"
#include "arb_hypgeom/impl.h"
static void
arb_hypgeom_lgamma_stirling(arb_t y, const arb_t x, slong prec)
{
int reflect;
slong r, n, wp;
arb_t t, u;
double acc;
acc = arb_rel_accuracy_bits(x);
acc = FLINT_MAX(acc, 0);
wp = FLINT_MIN(prec, acc + 20);
wp = FLINT_MAX(wp, 2);
wp = wp + FLINT_BIT_COUNT(wp);
arb_hypgeom_gamma_stirling_choose_param(&reflect, &r, &n, x, 0, 0, wp);
arb_init(t);
arb_init(u);
arb_init(t);
arb_init(u);
arb_add_ui(t, x, r, wp);
arb_hypgeom_gamma_stirling_inner(u, t, n, wp);
arb_hypgeom_rising_ui_rec(t, x, r, wp);
arb_log(t, t, wp);
arb_sub(y, u, t, prec);
arb_clear(t);
arb_clear(u);
}
void
arb_hypgeom_lgamma(arb_t res, const arb_t x, slong prec)
{
if (!arb_is_positive(x) || !arb_is_finite(x))
{
arb_indeterminate(res);
return;
}
if (arb_hypgeom_gamma_exact(res, x, 0, prec))
{
arb_log(res, res, prec);
return;
}
if (arb_hypgeom_gamma_taylor(res, x, 0, prec))
{
arb_log(res, res, prec);
return;
}
arb_hypgeom_lgamma_stirling(res, x, prec);
}