/***********************************************************************
Search for the worst cases for rounding of the Pochhammer symbol
function, poch(x,n).  For this case, we need test every significand
for which x is sufficiently small so that fl(x + n - 1) != x
(otherwise, poch(x,n) = x**n, which can be computed with fewew
multiplications), and sufficiently small so that fl(x + n) != n
(otherwise, poch(x,n) = n!, which can be precomputed and returned from
a table of factorials).

For binary32 arithmetic, where P = 7, we need to examine only the
decades beginning with 1e-7, 1e-6, ..., 1e8.  Those 16 decades contain
16e7 values for each n, which is computational feasible: a few minutes
of computation should suffice for a single n value.

Exhaustive search is not practical for binary64, binary80, and
binary128 formats because of the large number of x values that would
have to be examined.

In the interests of improving test speeds, the poch*(x,n) functions in
this file omit the several initial tests for special arguments, and
they assume the IEEE 754 default rounding mode, round-to-nearest with
ties-to-even.

Usage:
	cc [-O3] poch-dec-worst.c -lm && ./a.out

[12-Aug-2026]
***********************************************************************/

#include <float.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#define CONS(a,b) a##b

typedef float fp_t;
typedef double hp_t;

#define FP(x)		CONS(x,F)
#define FP_FABS(x)	fabsf((fp_t)x)
#define FP_POCH(x,n)	pochf((fp_t)(x), n)
#define FP_POW(x,n)	powf((fp_t)(x), (fp_t)n)
#define FP_SCALBN(x,n)	scalbnf((fp_t)(x), n)

#define ISINF(x)	isinf((fp_t)(x))

#define HP(x)		(x)
#define HP_POCH(x,n)	poch((hp_t)(x), n)
#define HP_POW(x,n)	pow((hp_t)(x), (hp_t)n)

#define EPS		FLT_EPSILON
#define ONE		FP(1.)
#define TWO		FP(2.)
#define ZERO		FP(0.)

fp_t
pochf(fp_t x, int n)
{
    fp_t result;
    int k;

    result = x;
    for (k = 1; k < n; ++k)
	result *= (x + (fp_t)k);

    return (result);
}

hp_t
poch(hp_t x, int n)
{
    hp_t result;
    int k;

    result = x;
    for (k = 1; k < n; ++k)
	result *= (x + (hp_t)k);

    return (result);

}

int
main(void)
{
    fp_t a, a_max, aa_max, dx, u, u_max, uu_max, x, x0, x1, x_max, xx_max;
    hp_t e, e_max, ee_max, eps_inv;
    int k, n, n_max, nn_max;

    eps_inv = ONE / EPS;

    n_max = 0;

    aa_max = ZERO;
    ee_max = ZERO;
    nn_max = 0;
    uu_max = ZERO;
    xx_max = ZERO;

    for (n = 2; n <= 1000; ++n)
    {
	for (k = -23; k < 24; ++k)
	{
	    x0 = FP_POW(2, k);
	    x1 = x0 * TWO;
	    dx = x0 * EPS;

	    a_max = ZERO;
	    e_max = ZERO;
	    u_max = ZERO;
	    x_max = ZERO;

	    for (x = x0; x < x1; x += dx)
	    {
		a = FP_POCH(x, n);		/* approximate poch(x,n) */

		if (ISINF(a))			/* overflow: skip rest of loop */
		    break;

		e = HP_POCH(x, n);		/* ``exact'' poch(x,n) */
		u = (fp_t)((((hp_t)a - e) / e) * eps_inv); /* error in ulps */

		if (FP_FABS(u) > FP_FABS(u_max))
		{
		    n_max = n;

		    a_max = a;
		    e_max = e;
		    u_max = u;
		    x_max = x;
		}
	    }

	    if (ISINF(a))			/* overflow: skip rest of loop */
		break;

	    (void)printf("%4d  %.6a  %.6a  %a  %12.6f\n",
			 n, (double)x_max, (double)a_max,
			 (double)e_max, (double)u_max);

	    (void)fflush(stdout);

	    if (FP_FABS(u_max) > FP_FABS(uu_max))
	    {
		nn_max = n_max;

		aa_max = a_max;
		ee_max = e_max;
		uu_max = u_max;
		xx_max = x_max;
	    }
	}
    }

    (void)printf("\nWorst case report:\n");
    (void)printf("%4d  %.6a  %.6a  %a  %12.6f\n",
		 nn_max, (double)xx_max, (double)aa_max,
		 (double)ee_max, (double)uu_max);

    return (EXIT_SUCCESS);
}
