/* Test whether a basic cpowf invocation works. */
/* This test is generated by misc/genbasic.c. */

#include <complex.h>
#include <errno.h>
#include <fenv.h>
#include <math.h>

#include "../basic.h"

#pragma STDC FENV_ACCESS ON

#define MF_UNSPEC1 (1 << 0)
#define MF_UNSPEC2 (1 << 1)
#define MF_MAYERR (1 << 2)
#define MF_ANYSIGN1 (1 << 2)
#define MF_ANYSIGN2 (1 << 3)

// Soft fail on rounding errors and report only one.
int imprecise;

void test(int variant, float complex input1, float complex input2, float complex lower, float complex expected, float complex upper, int flags)
{
	float complex output = cpowf(input1, input2);
	if ( !(flags & MF_UNSPEC1) )
	{
		float real = crealf(output);
		real = (flags & MF_ANYSIGN1) && real < 0.0 ? -real : real;
		float lower_real = crealf(lower);
		float expected_real = crealf(expected);
		float upper_real = crealf(upper);
		if ( !(isnan(expected_real) ? isnan(real) :
		       isfinite(expected_real) && expected_real != 0.0 ?
		       isfinite(real) && (real == expected_real || (lower_real < real && real < upper_real)) :
		       real == expected_real) )
		{
			if ( imprecise && isfinite(real) && isfinite(expected_real) )
				return;
			warnx("(%d.) cpowf(%.4f + i*%.4f, %.4f + i*%.4f).real = %.6a, not %.6a, diff %.6a, ratio %.8g",
			     variant, crealf(input1), cimagf(input1), crealf(input2), cimagf(input2), real, expected_real,
			     real - expected_real, real / expected_real);
			if ( !isfinite(real) || !isfinite(expected_real) )
				exit(1);
			imprecise = 1;
		}
		float imag = cimagf(output);
		imag = (flags & MF_ANYSIGN2) && imag < 0.0 ? -imag : imag;
		float lower_imag = cimagf(lower);
		float expected_imag = cimagf(expected);
		float upper_imag = cimagf(upper);
		if ( !(isnan(expected_imag) ? isnan(imag) :
		       isfinite(expected_imag) && expected_imag != 0.0 ?
		       isfinite(imag) && (imag == expected_imag || (lower_imag < imag && imag < upper_imag)) :
		       imag == expected_imag) )
		{
			if ( imprecise && isfinite(imag) && isfinite(expected_imag) )
				return;
			warnx("(%d.) cpowf(%.4f + i*%.4f, %.4f + i*%.4f).imag = %.6a, not %.6a, diff %.6a, ratio %.8g",
			     variant, crealf(input1), cimagf(input1), crealf(input2), cimagf(input2), imag, expected_imag,
			     imag - expected_imag, imag / expected_imag);
			if ( !isfinite(imag) || !isfinite(expected_imag) )
				exit(1);
			imprecise = 1;
		}
	}
}

int main(void)
{
	test(1, CMPLXF(90.01, 13.37), CMPLXF(10.1, 4.2), CMPLXF(-0x1.a7b1dap+58, 0x1.c89084p+64), CMPLXF(-0x1.a7b1d8p+58, 0x1.c89086p+64), CMPLXF(-0x1.a7b1d6p+58, 0x1.c89088p+64), 0);
	test(2, CMPLXF(0.0, 13.37), CMPLXF(10.1, 4.2), CMPLXF(-0x1.00b118p+24, 0x1.3349dcp+28), CMPLXF(-0x1.00b116p+24, 0x1.3349dep+28), CMPLXF(-0x1.00b114p+24, 0x1.3349ep+28), 0);
	test(3, CMPLXF(90.01, 0.0), CMPLXF(10.1, 4.2), CMPLXF(0x1.7b6308p+65, 0x1.306f8p+61), CMPLXF(0x1.7b630ap+65, 0x1.306f82p+61), CMPLXF(0x1.7b630cp+65, 0x1.306f84p+61), 0);
	test(4, CMPLXF(0.0, 0.0), CMPLXF(10.1, 4.2), CMPLXF(-0x1.0p-149, -0x1.0p-149), CMPLXF(0x0.0p+0, -0x0.0p+0), CMPLXF(0x1.0p-149, 0x1.0p-149), 0);
	test(5, CMPLXF(90.01, 13.37), CMPLXF(0.0, 4.2), CMPLXF(0x1.1257d8p-1, 0x1.a66db4p-5), CMPLXF(0x1.1257dap-1, 0x1.a66db6p-5), CMPLXF(0x1.1257dcp-1, 0x1.a66db8p-5), 0);
	test(6, CMPLXF(0.0, 13.37), CMPLXF(0.0, 4.2), CMPLXF(-0x1.2b9158p-13, -0x1.63983p-10), CMPLXF(-0x1.2b9156p-13, -0x1.63982ep-10), CMPLXF(-0x1.2b9154p-13, -0x1.63982cp-10), 0);
	test(7, CMPLXF(90.01, 0.0), CMPLXF(0.0, 4.2), CMPLXF(0x1.ff5b76p-1, 0x1.9a5582p-5), CMPLXF(0x1.ff5b78p-1, 0x1.9a5584p-5), CMPLXF(0x1.ff5b7ap-1, 0x1.9a5586p-5), 0);
	test(8, CMPLXF(90.01, 13.37), CMPLXF(10.1, 0.0), CMPLXF(0x1.14068p+62, 0x1.a6b79ap+65), CMPLXF(0x1.140682p+62, 0x1.a6b79cp+65), CMPLXF(0x1.140684p+62, 0x1.a6b79ep+65), 0);
	test(9, CMPLXF(0.0, 13.37), CMPLXF(10.1, 0.0), CMPLXF(-0x1.b33052p+37, -0x1.13b5a6p+35), CMPLXF(-0x1.b3305p+37, -0x1.13b5a4p+35), CMPLXF(-0x1.b3304ep+37, -0x1.13b5a2p+35), 0);
	test(10, CMPLXF(90.01, 0.0), CMPLXF(10.1, 0.0), CMPLXF(0x1.7bdd1ap+65, -0x1.0p-149), CMPLXF(0x1.7bdd1cp+65, 0x0.0p+0), CMPLXF(0x1.7bdd1ep+65, 0x1.0p-149), 0);
	test(11, CMPLXF(0.0, 0.0), CMPLXF(10.1, 0.0), CMPLXF(-0x1.0p-149, -0x1.0p-149), CMPLXF(0x0.0p+0, -0x0.0p+0), CMPLXF(0x1.0p-149, 0x1.0p-149), 0);
	test(12, CMPLXF(90.01, 13.37), CMPLXF(0.0, 0.0), CMPLXF(0x1.fffffep-1, -0x1.0p-149), CMPLXF(0x1.0p+0, 0x0.0p+0), CMPLXF(0x1.000002p+0, 0x1.0p-149), 0);
	test(13, CMPLXF(0.0, 13.37), CMPLXF(0.0, 0.0), CMPLXF(0x1.fffffep-1, -0x1.0p-149), CMPLXF(0x1.0p+0, 0x0.0p+0), CMPLXF(0x1.000002p+0, 0x1.0p-149), 0);
	test(14, CMPLXF(90.01, 0.0), CMPLXF(0.0, 0.0), CMPLXF(0x1.fffffep-1, -0x1.0p-149), CMPLXF(0x1.0p+0, 0x0.0p+0), CMPLXF(0x1.000002p+0, 0x1.0p-149), 0);
	return imprecise;
}
