#ifndef _TGMATH_H_
#define _TGMATH_H_
#include <complex.h>
#include <math.h>
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
__has_extension(c_generic_selections)
#define __tg_generic(x, cfnl, cfn, cfnf, fnl, fn, fnf) \
_Generic(x, \
long double _Complex: cfnl, \
double _Complex: cfn, \
float _Complex: cfnf, \
long double: fnl, \
default: fn, \
float: fnf \
)
#define __tg_type(x) \
__tg_generic(x, (long double _Complex)0, (double _Complex)0, \
(float _Complex)0, (long double)0, (double)0, (float)0)
#define __tg_impl_simple(x, y, z, fnl, fn, fnf, ...) \
__tg_generic( \
__tg_type(x) + __tg_type(y) + __tg_type(z), \
fnl, fn, fnf, fnl, fn, fnf)(__VA_ARGS__)
#define __tg_impl_full(x, y, cfnl, cfn, cfnf, fnl, fn, fnf, ...) \
__tg_generic( \
__tg_type(x) + __tg_type(y), \
cfnl, cfn, cfnf, fnl, fn, fnf)(__VA_ARGS__)
#elif defined(__generic)
#define __tg_generic_simple(x, fnl, fn, fnf) \
__generic(x, long double _Complex, fnl, \
__generic(x, double _Complex, fn, \
__generic(x, float _Complex, fnf, \
__generic(x, long double, fnl, \
__generic(x, float, fnf, fn)))))
#define __tg_impl_simple(x, y, z, fnl, fn, fnf, ...) \
__tg_generic_simple(x, \
__tg_generic_simple(y, \
__tg_generic_simple(z, fnl, fnl, fnl), \
__tg_generic_simple(z, fnl, fnl, fnl), \
__tg_generic_simple(z, fnl, fnl, fnl)), \
__tg_generic_simple(y, \
__tg_generic_simple(z, fnl, fnl, fnl), \
__tg_generic_simple(z, fnl, fn , fn ), \
__tg_generic_simple(z, fnl, fn , fn )), \
__tg_generic_simple(y, \
__tg_generic_simple(z, fnl, fnl, fnl), \
__tg_generic_simple(z, fnl, fn , fn ), \
__tg_generic_simple(z, fnl, fn , fnf)))(__VA_ARGS__)
#define __tg_generic_full(x, cfnl, cfn, cfnf, fnl, fn, fnf) \
__generic(x, long double _Complex, cfnl, \
__generic(x, double _Complex, cfn, \
__generic(x, float _Complex, cfnf, \
__generic(x, long double, fnl, \
__generic(x, float, fnf, fn)))))
#define __tg_impl_full(x, y, cfnl, cfn, cfnf, fnl, fn, fnf, ...) \
__tg_generic_full(x, \
__tg_generic_full(y, cfnl, cfnl, cfnl, cfnl, cfnl, cfnl), \
__tg_generic_full(y, cfnl, cfn , cfn , cfnl, cfn , cfn ), \
__tg_generic_full(y, cfnl, cfn , cfnf, cfnl, cfn , cfnf), \
__tg_generic_full(y, cfnl, cfnl, cfnl, fnl , fnl , fnl ), \
__tg_generic_full(y, cfnl, cfn , cfn , fnl , fn , fn ), \
__tg_generic_full(y, cfnl, cfn , cfnf, fnl , fn , fnf )) \
(__VA_ARGS__)
#else
#error "<tgmath.h> not implemented for this compiler"
#endif
#define __tg_simple(x, fn) \
__tg_impl_simple(x, x, x, fn##l, fn, fn##f, x)
#define __tg_simple2(x, y, fn) \
__tg_impl_simple(x, x, y, fn##l, fn, fn##f, x, y)
#define __tg_simple3(x, y, z, fn) \
__tg_impl_simple(x, y, z, fn##l, fn, fn##f, x, y, z)
#define __tg_simplev(x, fn, ...) \
__tg_impl_simple(x, x, x, fn##l, fn, fn##f, __VA_ARGS__)
#define __tg_full(x, fn) \
__tg_impl_full(x, x, c##fn##l, c##fn, c##fn##f, fn##l, fn, fn##f, x)
#define __tg_full2(x, y, fn) \
__tg_impl_full(x, y, c##fn##l, c##fn, c##fn##f, fn##l, fn, fn##f, x, y)
#define acos(x) __tg_full(x, acos)
#define asin(x) __tg_full(x, asin)
#define atan(x) __tg_full(x, atan)
#define acosh(x) __tg_full(x, acosh)
#define asinh(x) __tg_full(x, asinh)
#define atanh(x) __tg_full(x, atanh)
#define cos(x) __tg_full(x, cos)
#define sin(x) __tg_full(x, sin)
#define tan(x) __tg_full(x, tan)
#define cosh(x) __tg_full(x, cosh)
#define sinh(x) __tg_full(x, sinh)
#define tanh(x) __tg_full(x, tanh)
#define exp(x) __tg_full(x, exp)
#define log(x) __tg_full(x, log)
#define pow(x, y) __tg_full2(x, y, pow)
#define sqrt(x) __tg_full(x, sqrt)
#define fabs(x) __tg_impl_full(x, x, cabsl, cabs, cabsf, \
fabsl, fabs, fabsf, x)
#define atan2(x, y) __tg_simple2(x, y, atan2)
#define cbrt(x) __tg_simple(x, cbrt)
#define ceil(x) __tg_simple(x, ceil)
#define copysign(x, y) __tg_simple2(x, y, copysign)
#define erf(x) __tg_simple(x, erf)
#define erfc(x) __tg_simple(x, erfc)
#define exp2(x) __tg_simple(x, exp2)
#define expm1(x) __tg_simple(x, expm1)
#define fdim(x, y) __tg_simple2(x, y, fdim)
#define floor(x) __tg_simple(x, floor)
#define fma(x, y, z) __tg_simple3(x, y, z, fma)
#define fmax(x, y) __tg_simple2(x, y, fmax)
#define fmin(x, y) __tg_simple2(x, y, fmin)
#define fmod(x, y) __tg_simple2(x, y, fmod)
#define frexp(x, y) __tg_simplev(x, frexp, x, y)
#define hypot(x, y) __tg_simple2(x, y, hypot)
#define ilogb(x) __tg_simple(x, ilogb)
#define ldexp(x, y) __tg_simplev(x, ldexp, x, y)
#define lgamma(x) __tg_simple(x, lgamma)
#define llrint(x) __tg_simple(x, llrint)
#define llround(x) __tg_simple(x, llround)
#define log10(x) __tg_simple(x, log10)
#define log1p(x) __tg_simple(x, log1p)
#define log2(x) __tg_simple(x, log2)
#define logb(x) __tg_simple(x, logb)
#define lrint(x) __tg_simple(x, lrint)
#define lround(x) __tg_simple(x, lround)
#define nearbyint(x) __tg_simple(x, nearbyint)
#define nextafter(x, y) __tg_simple2(x, y, nextafter)
#define nexttoward(x, y) __tg_simplev(x, nexttoward, x, y)
#define remainder(x, y) __tg_simple2(x, y, remainder)
#define remquo(x, y, z) __tg_impl_simple(x, x, y, remquol, remquo, \
remquof, x, y, z)
#define rint(x) __tg_simple(x, rint)
#define round(x) __tg_simple(x, round)
#define scalbn(x, y) __tg_simplev(x, scalbn, x, y)
#define scalbln(x, y) __tg_simplev(x, scalbln, x, y)
#define tgamma(x) __tg_simple(x, tgamma)
#define trunc(x) __tg_simple(x, trunc)
#define carg(x) __tg_simple(x, carg)
#define cimag(x) __tg_simple(x, cimag)
#define conj(x) __tg_simple(x, conj)
#define cproj(x) __tg_simple(x, cproj)
#define creal(x) __tg_simple(x, creal)
#endif