/***********************************************************************
Test a higher-precision Pochhammer function against an fp_t version, or
against a recursive fp_t version with claimed lower average error, as
given by Johansson:2023:APC, page 6, in elefunt.bib.

NB: This version uses error compensation in the product in pochcp_std(),
as in file fact.c in hoc.

Usage:
	cc [-DMAXTEST=nnn] [-DMAXN=nnn] [-DMAXX=d.dd] [-DTEST1 | -DTEST2] \
	   [-DUSE_FLOAT] pochcp.c && ./a.out > foo.dat

TEST1 selects the standard version, while TEST2 selects the
divide-and-conquer version.

Testing is over the range n in [2, MAXN], and x uniformly sampled on
[0, MAXX].

A sample run looks like this:

	% cc -DTEST1 -DMAXTEST=25000 pochcp2.c -lm && time ./a.out > pochcp2.dat.1
	% cc -DTEST2 -DMAXTEST=25000 pochcp2.c -lm && time ./a.out > pochcp2.dat.2

The output file contains triples n x ulps, and is suitable for input
to gnuplot, with coomands like this:

	% gnuplot
	set yrange [-1:1]
	set ytics 0.5
	set term x11  1; plot pochcp.dat.1" using ($1):($3)
	set term x11  2; plot pochcp.dat.2" using ($1):($3)

[06-Jul-2026]
***********************************************************************/

#include <errno.h>
#include <fenv.h>	/* for fegetround() and fesetround */
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#if !defined(TEST1) && !defined(TEST2)
#define TEST1
#endif

#if !defined(MAXTEST)
#define MAXTEST 250
#endif /* !defined(MAXTEST) */

#if !defined(MAXN)
#define MAXN 251
#endif /* !defined(MAXN) */

#if !defined(MAXX)
#define MAXX 1024.0
#endif /* !defined(MAXX) */

/* CONS(a,b) should return ab, the concatenation of its arguments */
#if defined(STDC)
#define CONS(a,b) a##b
#else
#define CONS(a,b) a/**/b
#endif

#if defined(USE_FLOAT)
typedef float fp_t;
typedef long double hp_t;
const fp_t EPS = FLT_EPSILON;
#define FMA	fmal
#define FP(x) CONS(x,F)
#else
typedef double fp_t;
typedef long double hp_t;
const fp_t EPS = DBL_EPSILON;
#define FMA	fma
#define FP(x) (x)
#endif

static fp_t ONE = FP(1.0);
static fp_t ZERO = FP(0.0);

#define iseven(n) ((n & 1) == 0)

#define qnan nan

static fp_t
pochcp_std(fp_t x, int n)
{   /*
    ** Pochhammer symbol, with standard mathematical notation (x)_n,
    ** defined by:
    **
    **     (x)_n = (x + 0) * (x + 1) * (x + 2) * ... * (x + n - 1)
    **
    ** Special cases: (+/-0)_n = +/-0, (x)_0 = 1, and (x)_1 = x
    **
    ** Identities: (1)_n = n! and (2)_n = (n + 1)!
    */

    fp_t result;

    if (isnan(x))    /* generate run-time NaN, preserve payload */
    {
	errno = ERANGE;
	(void)nan("");
	result = x;
    }
    else if (n < 0)
    {
	errno = ERANGE;
	result = qnan("0xfac50000");
    }
    else if (x == ZERO)
	result = x;        /* NB: preserve sign of zero! */
    else if (n == 0)
	result = ONE;
    else if (n == 1)
	result = x;
    else if (isinf(x))
	result = (x > (fp_t)0.0) ? x : (iseven(n) ? -x : x);
    else
    {
	fp_t err, prod;
	int k;

	err = ZERO;
        prod = x;

	for (k = 1; k < n; ++k)
	{
	    fp_t e, f, p;

	    f = x + (fp_t)k;		/* fl(new factor) */
	    p = prod * f;		/* fl(new product) */
	    e = FMA(prod, f, -p);	/* error in new product */
	    prod = p;			/* current product */
	    err = FMA(err, f, e);	/* current error */
	}

	result = prod + err;
    }

    return (result);
}

static hp_t
poch_hp(fp_t x, int n)
{   /*
    ** Pochhammer symbol, with standard mathematical notation (x)_n,
    ** defined by:
    **
    **     (x)_n = (x + 0) * (x + 1) * (x + 2) * ... * (x + n - 1)
    **
    ** Special cases: (+/-0)_n = +/-0, (x)_0 = 1, and (x)_1 = x
    **
    ** Identities: (1)_n = n! and (2)_n = (n + 1)!
    */

    hp_t result;

    if (isnan(x))    /* generate run-time NaN, preserve payload */
    {
	errno = ERANGE;
	(void)nan("");
	result = x;
    }
    else if (n < 0)
    {
	errno = ERANGE;
	result = qnan("0xfac50000");
    }
    else if (x == ZERO)
	result = x;        /* NB: preserve sign of zero! */
    else if (n == 0)
	result = ONE;
    else if (n == 1)
	result = x;
    else if (isinf(x))
	result = (x > (fp_t)0.0) ? x : (iseven(n) ? -x : x);
    else
    {
	int k;
	hp_t prod, xx;

	prod = (hp_t)ONE;
	xx = (hp_t)x;

	for (k = 0; k < n; ++k)
	    prod *= (xx + (hp_t)k);

	result = prod;
    }

    return (result);
}

#if defined(TEST2)

static const int __NPOCH_DAQ__ = 10;

static fp_t
poch_daq(fp_t x, int n)
{
    fp_t result;

    if (n < __NPOCH_DAQ__)
	result = pochcp_std(x, n);
    else
    {
	int m;

	m = n / 2;
	result = poch_daq(x, m) * poch_daq(x + m, n - m);
    }

    return (result);
}

#endif /* defined(TEST2) */

int
main(void)
{
    fp_t a, ulp, ulpmax, ulpsum, x;
    hp_t e;
    int k, n, nsum;

    srand48(0xcafefeed);
    nsum = 0;
    ulpsum = ZERO;
    ulpmax = ZERO;

    (void)fprintf(stderr, "hp_t is binary%d\n",
		  (LDBL_MANT_DIG == 64) ? 80 : (int)sizeof(hp_t) * 8);

#if defined(TEST1)

    (void)fprintf(stderr, "Test of pochcp_std() in binary%d\n", (int)sizeof(fp_t) * 8);

    for (k = 0; k < MAXTEST; ++k)
    {
	x = drand48() * MAXX;

	for (n = 2; n < MAXN; ++n)
	{
	    e = poch_hp(x, n);
	    a = pochcp_std(x, n);

	    if (isinf((fp_t)e))
		break;

	    ulp = (fp_t)(((hp_t)a - e) / e) / EPS;

	    if (isinf(ulp))
		continue;

	    ++nsum;
	    ulpsum += fabs(ulp);

	    if (fabs(ulpmax) < fabs(ulp))
		ulpmax = ulp;

	    (void)printf("%7d\t%15.7f\t%15.7f\n", n, x, ulp);
	}
    }

    (void)fprintf(stderr, "Average ulps for pochcp_std() = %.7f\n", (double)ulpsum / (double)nsum);
    (void)fprintf(stderr, "Maximum ulps for pochcp_std() = %.7f\n", (double)ulpmax);
    (void)fprintf(stderr, "Sample count for pochcp_std() = %7d\n", nsum);

#elif defined(TEST2)

    (void)fprintf(stderr, "Test of poch_daq() in binary%d\n", (int)sizeof(fp_t) * 8);

    for (k = 0; k < MAXTEST; ++k)
    {
	x = drand48() * MAXX;

	for (n = 2; n < MAXN; ++n)
	{
	    e = poch_hp(x, n);
	    a = poch_daq(x, n);

	    if (isinf((fp_t)e))
		break;

	    ulp = (fp_t)(((hp_t)a - e) / e) / EPS;

	    if (isinf(ulp))
		continue;

	    ++nsum;
	    ulpsum += fabs(ulp);

	    if (fabs(ulpmax) < fabs(ulp))
		ulpmax = ulp;

	    (void)printf("%7d\t%15.7f\t%15.7f\n", n, x, ulp);
	}
    }

    (void)fprintf(stderr, "Average ulps for poch_daq() = %.7f\n", (double)ulpsum / (double)nsum);
    (void)fprintf(stderr, "Maximum ulps for poch_daq() = %.7f\n", (double)ulpmax);
    (void)fprintf(stderr, "Sample count for poch_daq() = %7d\n", nsum);

#endif /* defined(TEST1) */

    return (EXIT_SUCCESS);
}
