#include <assert.h>
#include <complex.h>
#include <fenv.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
#define ALL_STD_EXCEPT (FE_DIVBYZERO | FE_INEXACT | FE_INVALID | \
FE_OVERFLOW | FE_UNDERFLOW)
#define OPT_INVALID (ALL_STD_EXCEPT & ~FE_INVALID)
#define OPT_INEXACT (ALL_STD_EXCEPT & ~FE_INEXACT)
#define FLT_ULP() ldexpl(1.0, 1 - FLT_MANT_DIG)
#define DBL_ULP() ldexpl(1.0, 1 - DBL_MANT_DIG)
#define LDBL_ULP() ldexpl(1.0, 1 - LDBL_MANT_DIG)
#pragma STDC FENV_ACCESS ON
#pragma STDC CX_LIMITED_RANGE OFF
#define CS_REAL 1
#define CS_IMAG 2
#define CS_BOTH (CS_REAL | CS_IMAG)
#ifdef DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) (void)0
#endif
#define test_p(func, z, result, exceptmask, excepts, checksign) do { \
volatile long double complex _d = z; \
debug(" testing %s(%Lg + %Lg I) == %Lg + %Lg I\n", #func, \
creall(_d), cimagl(_d), creall(result), cimagl(result)); \
assert(feclearexcept(FE_ALL_EXCEPT) == 0); \
assert(cfpequal((func)(_d), (result), (checksign))); \
assert(((func), fetestexcept(exceptmask) == (excepts))); \
} while (0)
#define test_p_tol(func, z, result, tol) do { \
volatile long double complex _d = z; \
debug(" testing %s(%Lg + %Lg I) ~= %Lg + %Lg I\n", #func, \
creall(_d), cimagl(_d), creall(result), cimagl(result)); \
assert(cfpequal_tol((func)(_d), (result), (tol))); \
} while (0)
#define test(func, z, result, exceptmask, excepts, checksign) do { \
test_p(func, z, result, exceptmask, excepts, checksign); \
test_p(func, conjl(z), conjl(result), exceptmask, excepts, checksign); \
} while (0)
#define test_tol(func, z, result, tol) do { \
test_p_tol(func, z, result, tol); \
test_p_tol(func, conjl(z), conjl(result), tol); \
} while (0)
#define testall(func, x, result, exceptmask, excepts, checksign) do { \
test(func, x, result, exceptmask, excepts, checksign); \
test(func##f, x, result, exceptmask, excepts, checksign); \
} while (0)
#define testall_odd(func, x, result, exceptmask, excepts, checksign) do { \
testall(func, x, result, exceptmask, excepts, checksign); \
testall(func, -(x), -result, exceptmask, excepts, checksign); \
} while (0)
#define testall_even(func, x, result, exceptmask, excepts, checksign) do { \
testall(func, x, result, exceptmask, excepts, checksign); \
testall(func, -(x), result, exceptmask, excepts, checksign); \
} while (0)
#define testall_tol(func, x, result, tol) do { \
test_tol(func, x, result, (tol) * DBL_ULP()); \
test_tol(func##f, x, result, (tol) * FLT_ULP()); \
} while (0)
#define testall_odd_tol(func, x, result, tol) do { \
testall_tol(func, x, result, tol); \
testall_tol(func, -(x), -result, tol); \
} while (0)
#define testall_even_tol(func, x, result, tol) do { \
testall_tol(func, x, result, tol); \
testall_tol(func, -(x), result, tol); \
} while (0)
#define pi 3.14159265358979323846264338327950280L
#define c3pi 9.42477796076937971538793014983850839L
static int
fpequal(long double x, long double y, int checksign)
{
if (isnan(x) && isnan(y))
return (1);
if (checksign)
return (x == y && !signbit(x) == !signbit(y));
else
return (fabsl(x) == fabsl(y));
}
static int
fpequal_tol(long double x, long double y, long double tol)
{
fenv_t env;
int ret;
if (isnan(x) && isnan(y))
return (1);
if (!signbit(x) != !signbit(y))
return (0);
if (x == y)
return (1);
if (tol == 0 || y == 0.0)
return (0);
feholdexcept(&env);
ret = fabsl(x - y) <= fabsl(y * tol);
fesetenv(&env);
return (ret);
}
static int
cfpequal(long double complex x, long double complex y, int checksign)
{
return (fpequal(creal(x), creal(y), checksign & CS_REAL)
&& fpequal(cimag(x), cimag(y), checksign & CS_IMAG));
}
static int
cfpequal_tol(long double complex x, long double complex y, long double tol)
{
return (fpequal_tol(creal(x), creal(y), tol)
&& fpequal_tol(cimag(x), cimag(y), tol));
}
void
test_zero(void)
{
long double complex zero = CMPLXL(0.0, 0.0);
testall_tol(cacosh, zero, CMPLXL(0.0, pi / 2), 1);
testall_tol(cacosh, -zero, CMPLXL(0.0, -pi / 2), 1);
testall_tol(cacos, zero, CMPLXL(pi / 2, -0.0), 1);
testall_tol(cacos, -zero, CMPLXL(pi / 2, 0.0), 1);
testall_odd(casinh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
testall_odd(casin, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
testall_odd(catanh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
testall_odd(catan, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
}
void
test_nan()
{
long double complex nan_nan = CMPLXL(NAN, NAN);
long double complex z;
z = nan_nan;
testall(cacosh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
testall(cacos, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
testall(casinh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
testall(casin, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
testall(catanh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
testall(catan, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
z = CMPLXL(0.5, NAN);
testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
testall(casinh, z, nan_nan, OPT_INVALID, 0, 0);
testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
testall(catanh, z, nan_nan, OPT_INVALID, 0, 0);
testall(catan, z, nan_nan, OPT_INVALID, 0, 0);
z = CMPLXL(NAN, 0.5);
testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
testall(casinh, z, nan_nan, OPT_INVALID, 0, 0);
testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
testall(catanh, z, nan_nan, OPT_INVALID, 0, 0);
testall(catan, z, nan_nan, OPT_INVALID, 0, 0);
z = CMPLXL(NAN, INFINITY);
testall(cacosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
testall(cacosh, -z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
testall(cacos, z, CMPLXL(NAN, -INFINITY), ALL_STD_EXCEPT, 0, CS_IMAG);
testall(casinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, 0);
testall(casin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, CS_IMAG);
testall_tol(catanh, z, CMPLXL(0.0, pi / 2), 1);
testall(catan, z, CMPLXL(NAN, 0.0), ALL_STD_EXCEPT, 0, CS_IMAG);
z = CMPLXL(INFINITY, NAN);
testall_even(cacosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
CS_REAL);
testall_even(cacos, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
testall_odd(casinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0,
CS_REAL);
testall_odd(casin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
testall_odd(catanh, z, CMPLXL(0.0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
testall_odd_tol(catan, z, CMPLXL(pi / 2, 0.0), 1);
z = CMPLXL(0.0, NAN);
testall_even(cacosh, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0);
testall_even_tol(cacos, z, CMPLXL(pi / 2, NAN), 1);
testall_odd(casinh, z, nan_nan, OPT_INVALID, 0, 0);
testall_odd(casin, z, CMPLXL(0.0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
testall_odd(catanh, z, CMPLXL(0.0, NAN), OPT_INVALID, 0, CS_REAL);
testall_odd(catan, z, nan_nan, OPT_INVALID, 0, 0);
z = CMPLXL(NAN, 0.0);
testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0);
testall(cacos, z, nan_nan, OPT_INVALID, 0, 0);
testall(casinh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
testall(casin, z, nan_nan, OPT_INVALID, 0, 0);
testall(catanh, z, nan_nan, OPT_INVALID, 0, CS_IMAG);
testall(catan, z, CMPLXL(NAN, 0.0), ALL_STD_EXCEPT, 0, 0);
}
void
test_inf(void)
{
long double complex z;
z = CMPLXL(INFINITY, INFINITY);
testall_tol(cacosh, z, CMPLXL(INFINITY, pi / 4), 1);
testall_tol(cacosh, -z, CMPLXL(INFINITY, -c3pi / 4), 1);
testall_tol(cacos, z, CMPLXL(pi / 4, -INFINITY), 1);
testall_tol(cacos, -z, CMPLXL(c3pi / 4, INFINITY), 1);
testall_odd_tol(casinh, z, CMPLXL(INFINITY, pi / 4), 1);
testall_odd_tol(casin, z, CMPLXL(pi / 4, INFINITY), 1);
testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
z = CMPLXL(INFINITY, 0.5);
testall(cacosh, z, CMPLXL(INFINITY, 0), OPT_INEXACT, 0, CS_BOTH);
testall_tol(cacosh, -z, CMPLXL(INFINITY, -pi), 1);
testall(cacos, z, CMPLXL(0, -INFINITY), OPT_INEXACT, 0, CS_BOTH);
testall_tol(cacos, -z, CMPLXL(pi, INFINITY), 1);
testall_odd(casinh, z, CMPLXL(INFINITY, 0), OPT_INEXACT, 0, CS_BOTH);
testall_odd_tol(casin, z, CMPLXL(pi / 2, INFINITY), 1);
testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
z = CMPLXL(0.5, INFINITY);
testall_tol(cacosh, z, CMPLXL(INFINITY, pi / 2), 1);
testall_tol(cacosh, -z, CMPLXL(INFINITY, -pi / 2), 1);
testall_tol(cacos, z, CMPLXL(pi / 2, -INFINITY), 1);
testall_tol(cacos, -z, CMPLXL(pi / 2, INFINITY), 1);
testall_odd_tol(casinh, z, CMPLXL(INFINITY, pi / 2), 1);
testall_odd(casin, z, CMPLXL(0.0, INFINITY), OPT_INEXACT, 0, CS_BOTH);
testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1);
testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1);
}
void
test_axes(void)
{
static const long double nums[] = {
-2, -1, -0.5, 0.5, 1, 2
};
long double complex z;
int i;
for (i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
z = CMPLXL(nums[i], 0.0);
if (fabs(nums[i]) <= 1) {
testall_tol(cacosh, z, CMPLXL(0.0, acos(nums[i])), 1);
testall_tol(cacos, z, CMPLXL(acosl(nums[i]), -0.0), 1);
testall_tol(casin, z, CMPLXL(asinl(nums[i]), 0.0), 1);
testall_tol(catanh, z, CMPLXL(atanh(nums[i]), 0.0), 1);
} else {
testall_tol(cacosh, z,
CMPLXL(acosh(fabs(nums[i])),
(nums[i] < 0) ? pi : 0), 1);
testall_tol(cacos, z,
CMPLXL((nums[i] < 0) ? pi : 0,
-acosh(fabs(nums[i]))), 1);
testall_tol(casin, z,
CMPLXL(copysign(pi / 2, nums[i]),
acosh(fabs(nums[i]))), 1);
testall_tol(catanh, z,
CMPLXL(atanh(1 / nums[i]), pi / 2), 1);
}
testall_tol(casinh, z, CMPLXL(asinh(nums[i]), 0.0), 1);
testall_tol(catan, z, CMPLXL(atan(nums[i]), 0), 1);
}
}
void
test_small(void)
{
static const struct {
complex long double z;
complex long double acos_z;
complex long double asin_z;
complex long double atan_z;
} tests[] = {
{ CMPLXL(0.75L, 0.25L),
CMPLXL(pi / 4, -0.34657359027997265470861606072908828L),
CMPLXL(pi / 4, 0.34657359027997265470861606072908828L),
CMPLXL(0.66290883183401623252961960521423782L,
0.15899719167999917436476103600701878L) },
};
int i;
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
testall_tol(cacos, tests[i].z, tests[i].acos_z, 2);
testall_odd_tol(casin, tests[i].z, tests[i].asin_z, 2);
testall_odd_tol(catan, tests[i].z, tests[i].atan_z, 2);
}
}
void
test_large(void)
{
}
int
main(int argc, char *argv[])
{
printf("1..6\n");
test_zero();
printf("ok 1 - invctrig zero\n");
test_nan();
printf("ok 2 - invctrig nan\n");
test_inf();
printf("ok 3 - invctrig inf\n");
test_axes();
printf("ok 4 - invctrig axes\n");
test_small();
printf("ok 5 - invctrig small\n");
test_large();
printf("ok 6 - invctrig large\n");
return (0);
}