17777dab0Sopenharmony_ci#ifndef _TGMATH_H
27777dab0Sopenharmony_ci#define _TGMATH_H
37777dab0Sopenharmony_ci
47777dab0Sopenharmony_ci/*
57777dab0Sopenharmony_cithe return types are only correct with gcc (__GNUC__)
67777dab0Sopenharmony_ciotherwise they are long double or long double complex
77777dab0Sopenharmony_ci
87777dab0Sopenharmony_cithe long double version of a function is never chosen when
97777dab0Sopenharmony_cisizeof(double) == sizeof(long double)
107777dab0Sopenharmony_ci(but the return type is set correctly with gcc)
117777dab0Sopenharmony_ci*/
127777dab0Sopenharmony_ci
137777dab0Sopenharmony_ci#include <math.h>
147777dab0Sopenharmony_ci#include <complex.h>
157777dab0Sopenharmony_ci
167777dab0Sopenharmony_ci#define __IS_FP(x) (sizeof((x)+1ULL) == sizeof((x)+1.0f))
177777dab0Sopenharmony_ci#define __IS_CX(x) (__IS_FP(x) && sizeof(x) == sizeof((x)+I))
187777dab0Sopenharmony_ci#define __IS_REAL(x) (__IS_FP(x) && 2*sizeof(x) == sizeof((x)+I))
197777dab0Sopenharmony_ci
207777dab0Sopenharmony_ci#define __FLT(x) (__IS_REAL(x) && sizeof(x) == sizeof(float))
217777dab0Sopenharmony_ci#define __LDBL(x) (__IS_REAL(x) && sizeof(x) == sizeof(long double) && sizeof(long double) != sizeof(double))
227777dab0Sopenharmony_ci
237777dab0Sopenharmony_ci#define __FLTCX(x) (__IS_CX(x) && sizeof(x) == sizeof(float complex))
247777dab0Sopenharmony_ci#define __DBLCX(x) (__IS_CX(x) && sizeof(x) == sizeof(double complex))
257777dab0Sopenharmony_ci#define __LDBLCX(x) (__IS_CX(x) && sizeof(x) == sizeof(long double complex) && sizeof(long double) != sizeof(double))
267777dab0Sopenharmony_ci
277777dab0Sopenharmony_ci/* return type */
287777dab0Sopenharmony_ci
297777dab0Sopenharmony_ci#ifdef __GNUC__
307777dab0Sopenharmony_ci/*
317777dab0Sopenharmony_cithe result must be casted to the right type
327777dab0Sopenharmony_ci(otherwise the result type is determined by the conversion
337777dab0Sopenharmony_cirules applied to all the function return types so it is long
347777dab0Sopenharmony_cidouble or long double complex except for integral functions)
357777dab0Sopenharmony_ci
367777dab0Sopenharmony_cithis cannot be done in c99, so the typeof gcc extension is
377777dab0Sopenharmony_ciused and that the type of ?: depends on wether an operand is
387777dab0Sopenharmony_cia null pointer constant or not
397777dab0Sopenharmony_ci(in c11 _Generic can be used)
407777dab0Sopenharmony_ci
417777dab0Sopenharmony_cithe c arguments below must be integer constant expressions
427777dab0Sopenharmony_ciso they can be in null pointer constants
437777dab0Sopenharmony_ci(__IS_FP above was carefully chosen this way)
447777dab0Sopenharmony_ci*/
457777dab0Sopenharmony_ci/* if c then t else void */
467777dab0Sopenharmony_ci#define __type1(c,t) __typeof__(*(0?(t*)0:(void*)!(c)))
477777dab0Sopenharmony_ci/* if c then t1 else t2 */
487777dab0Sopenharmony_ci#define __type2(c,t1,t2) __typeof__(*(0?(__type1(c,t1)*)0:(__type1(!(c),t2)*)0))
497777dab0Sopenharmony_ci/* cast to double when x is integral, otherwise use typeof(x) */
507777dab0Sopenharmony_ci#define __RETCAST(x) ( \
517777dab0Sopenharmony_ci	__type2(__IS_FP(x), __typeof__(x), double))
527777dab0Sopenharmony_ci/* 2 args case, should work for complex types (cpow) */
537777dab0Sopenharmony_ci#define __RETCAST_2(x, y) ( \
547777dab0Sopenharmony_ci	__type2(__IS_FP(x) && __IS_FP(y), \
557777dab0Sopenharmony_ci		__typeof__((x)+(y)), \
567777dab0Sopenharmony_ci		__typeof__((x)+(y)+1.0)))
577777dab0Sopenharmony_ci/* 3 args case (fma only) */
587777dab0Sopenharmony_ci#define __RETCAST_3(x, y, z) ( \
597777dab0Sopenharmony_ci	__type2(__IS_FP(x) && __IS_FP(y) && __IS_FP(z), \
607777dab0Sopenharmony_ci		__typeof__((x)+(y)+(z)), \
617777dab0Sopenharmony_ci		__typeof__((x)+(y)+(z)+1.0)))
627777dab0Sopenharmony_ci/* drop complex from the type of x */
637777dab0Sopenharmony_ci/* TODO: wrong when sizeof(long double)==sizeof(double) */
647777dab0Sopenharmony_ci#define __RETCAST_REAL(x) (  \
657777dab0Sopenharmony_ci	__type2(__IS_FP(x) && sizeof((x)+I) == sizeof(float complex), float, \
667777dab0Sopenharmony_ci	__type2(sizeof((x)+1.0+I) == sizeof(double complex), double, \
677777dab0Sopenharmony_ci		long double)))
687777dab0Sopenharmony_ci/* add complex to the type of x */
697777dab0Sopenharmony_ci#define __RETCAST_CX(x) (__typeof__(__RETCAST(x)0+I))
707777dab0Sopenharmony_ci#else
717777dab0Sopenharmony_ci#define __RETCAST(x)
727777dab0Sopenharmony_ci#define __RETCAST_2(x, y)
737777dab0Sopenharmony_ci#define __RETCAST_3(x, y, z)
747777dab0Sopenharmony_ci#define __RETCAST_REAL(x)
757777dab0Sopenharmony_ci#define __RETCAST_CX(x)
767777dab0Sopenharmony_ci#endif
777777dab0Sopenharmony_ci
787777dab0Sopenharmony_ci/* function selection */
797777dab0Sopenharmony_ci
807777dab0Sopenharmony_ci#define __tg_real_nocast(fun, x) ( \
817777dab0Sopenharmony_ci	__FLT(x) ? fun ## f (x) : \
827777dab0Sopenharmony_ci	__LDBL(x) ? fun ## l (x) : \
837777dab0Sopenharmony_ci	fun(x) )
847777dab0Sopenharmony_ci
857777dab0Sopenharmony_ci#define __tg_real(fun, x) (__RETCAST(x)__tg_real_nocast(fun, x))
867777dab0Sopenharmony_ci
877777dab0Sopenharmony_ci#define __tg_real_2_1(fun, x, y) (__RETCAST(x)( \
887777dab0Sopenharmony_ci	__FLT(x) ? fun ## f (x, y) : \
897777dab0Sopenharmony_ci	__LDBL(x) ? fun ## l (x, y) : \
907777dab0Sopenharmony_ci	fun(x, y) ))
917777dab0Sopenharmony_ci
927777dab0Sopenharmony_ci#define __tg_real_2(fun, x, y) (__RETCAST_2(x, y)( \
937777dab0Sopenharmony_ci	__FLT(x) && __FLT(y) ? fun ## f (x, y) : \
947777dab0Sopenharmony_ci	__LDBL((x)+(y)) ? fun ## l (x, y) : \
957777dab0Sopenharmony_ci	fun(x, y) ))
967777dab0Sopenharmony_ci
977777dab0Sopenharmony_ci#define __tg_complex(fun, x) (__RETCAST_CX(x)( \
987777dab0Sopenharmony_ci	__FLTCX((x)+I) && __IS_FP(x) ? fun ## f (x) : \
997777dab0Sopenharmony_ci	__LDBLCX((x)+I) ? fun ## l (x) : \
1007777dab0Sopenharmony_ci	fun(x) ))
1017777dab0Sopenharmony_ci
1027777dab0Sopenharmony_ci#define __tg_complex_retreal(fun, x) (__RETCAST_REAL(x)( \
1037777dab0Sopenharmony_ci	__FLTCX((x)+I) && __IS_FP(x) ? fun ## f (x) : \
1047777dab0Sopenharmony_ci	__LDBLCX((x)+I) ? fun ## l (x) : \
1057777dab0Sopenharmony_ci	fun(x) ))
1067777dab0Sopenharmony_ci
1077777dab0Sopenharmony_ci#define __tg_real_complex(fun, x) (__RETCAST(x)( \
1087777dab0Sopenharmony_ci	__FLTCX(x) ? c ## fun ## f (x) : \
1097777dab0Sopenharmony_ci	__DBLCX(x) ? c ## fun (x) : \
1107777dab0Sopenharmony_ci	__LDBLCX(x) ? c ## fun ## l (x) : \
1117777dab0Sopenharmony_ci	__FLT(x) ? fun ## f (x) : \
1127777dab0Sopenharmony_ci	__LDBL(x) ? fun ## l (x) : \
1137777dab0Sopenharmony_ci	fun(x) ))
1147777dab0Sopenharmony_ci
1157777dab0Sopenharmony_ci/* special cases */
1167777dab0Sopenharmony_ci
1177777dab0Sopenharmony_ci#define __tg_real_remquo(x, y, z) (__RETCAST_2(x, y)( \
1187777dab0Sopenharmony_ci	__FLT(x) && __FLT(y) ? remquof(x, y, z) : \
1197777dab0Sopenharmony_ci	__LDBL((x)+(y)) ? remquol(x, y, z) : \
1207777dab0Sopenharmony_ci	remquo(x, y, z) ))
1217777dab0Sopenharmony_ci
1227777dab0Sopenharmony_ci#define __tg_real_fma(x, y, z) (__RETCAST_3(x, y, z)( \
1237777dab0Sopenharmony_ci	__FLT(x) && __FLT(y) && __FLT(z) ? fmaf(x, y, z) : \
1247777dab0Sopenharmony_ci	__LDBL((x)+(y)+(z)) ? fmal(x, y, z) : \
1257777dab0Sopenharmony_ci	fma(x, y, z) ))
1267777dab0Sopenharmony_ci
1277777dab0Sopenharmony_ci#define __tg_real_complex_pow(x, y) (__RETCAST_2(x, y)( \
1287777dab0Sopenharmony_ci	__FLTCX((x)+(y)) && __IS_FP(x) && __IS_FP(y) ? cpowf(x, y) : \
1297777dab0Sopenharmony_ci	__FLTCX((x)+(y)) ? cpow(x, y) : \
1307777dab0Sopenharmony_ci	__DBLCX((x)+(y)) ? cpow(x, y) : \
1317777dab0Sopenharmony_ci	__LDBLCX((x)+(y)) ? cpowl(x, y) : \
1327777dab0Sopenharmony_ci	__FLT(x) && __FLT(y) ? powf(x, y) : \
1337777dab0Sopenharmony_ci	__LDBL((x)+(y)) ? powl(x, y) : \
1347777dab0Sopenharmony_ci	pow(x, y) ))
1357777dab0Sopenharmony_ci
1367777dab0Sopenharmony_ci#define __tg_real_complex_fabs(x) (__RETCAST_REAL(x)( \
1377777dab0Sopenharmony_ci	__FLTCX(x) ? cabsf(x) : \
1387777dab0Sopenharmony_ci	__DBLCX(x) ? cabs(x) : \
1397777dab0Sopenharmony_ci	__LDBLCX(x) ? cabsl(x) : \
1407777dab0Sopenharmony_ci	__FLT(x) ? fabsf(x) : \
1417777dab0Sopenharmony_ci	__LDBL(x) ? fabsl(x) : \
1427777dab0Sopenharmony_ci	fabs(x) ))
1437777dab0Sopenharmony_ci
1447777dab0Sopenharmony_ci/* suppress any macros in math.h or complex.h */
1457777dab0Sopenharmony_ci
1467777dab0Sopenharmony_ci#undef acos
1477777dab0Sopenharmony_ci#undef acosh
1487777dab0Sopenharmony_ci#undef asin
1497777dab0Sopenharmony_ci#undef asinh
1507777dab0Sopenharmony_ci#undef atan
1517777dab0Sopenharmony_ci#undef atan2
1527777dab0Sopenharmony_ci#undef atanh
1537777dab0Sopenharmony_ci#undef carg
1547777dab0Sopenharmony_ci#undef cbrt
1557777dab0Sopenharmony_ci#undef ceil
1567777dab0Sopenharmony_ci#undef cimag
1577777dab0Sopenharmony_ci#undef conj
1587777dab0Sopenharmony_ci#undef copysign
1597777dab0Sopenharmony_ci#undef cos
1607777dab0Sopenharmony_ci#undef cosh
1617777dab0Sopenharmony_ci#undef cproj
1627777dab0Sopenharmony_ci#undef creal
1637777dab0Sopenharmony_ci#undef erf
1647777dab0Sopenharmony_ci#undef erfc
1657777dab0Sopenharmony_ci#undef exp
1667777dab0Sopenharmony_ci#undef exp2
1677777dab0Sopenharmony_ci#undef expm1
1687777dab0Sopenharmony_ci#undef fabs
1697777dab0Sopenharmony_ci#undef fdim
1707777dab0Sopenharmony_ci#undef floor
1717777dab0Sopenharmony_ci#undef fma
1727777dab0Sopenharmony_ci#undef fmax
1737777dab0Sopenharmony_ci#undef fmin
1747777dab0Sopenharmony_ci#undef fmod
1757777dab0Sopenharmony_ci#undef frexp
1767777dab0Sopenharmony_ci#undef hypot
1777777dab0Sopenharmony_ci#undef ilogb
1787777dab0Sopenharmony_ci#undef ldexp
1797777dab0Sopenharmony_ci#undef lgamma
1807777dab0Sopenharmony_ci#undef llrint
1817777dab0Sopenharmony_ci#undef llround
1827777dab0Sopenharmony_ci#undef log
1837777dab0Sopenharmony_ci#undef log10
1847777dab0Sopenharmony_ci#undef log1p
1857777dab0Sopenharmony_ci#undef log2
1867777dab0Sopenharmony_ci#undef logb
1877777dab0Sopenharmony_ci#undef lrint
1887777dab0Sopenharmony_ci#undef lround
1897777dab0Sopenharmony_ci#undef nearbyint
1907777dab0Sopenharmony_ci#undef nextafter
1917777dab0Sopenharmony_ci#undef nexttoward
1927777dab0Sopenharmony_ci#undef pow
1937777dab0Sopenharmony_ci#undef remainder
1947777dab0Sopenharmony_ci#undef remquo
1957777dab0Sopenharmony_ci#undef rint
1967777dab0Sopenharmony_ci#undef round
1977777dab0Sopenharmony_ci#undef scalbln
1987777dab0Sopenharmony_ci#undef scalbn
1997777dab0Sopenharmony_ci#undef sin
2007777dab0Sopenharmony_ci#undef sinh
2017777dab0Sopenharmony_ci#undef sqrt
2027777dab0Sopenharmony_ci#undef tan
2037777dab0Sopenharmony_ci#undef tanh
2047777dab0Sopenharmony_ci#undef tgamma
2057777dab0Sopenharmony_ci#undef trunc
2067777dab0Sopenharmony_ci
2077777dab0Sopenharmony_ci/* tg functions */
2087777dab0Sopenharmony_ci
2097777dab0Sopenharmony_ci#define acos(x)         __tg_real_complex(acos, (x))
2107777dab0Sopenharmony_ci#define acosh(x)        __tg_real_complex(acosh, (x))
2117777dab0Sopenharmony_ci#define asin(x)         __tg_real_complex(asin, (x))
2127777dab0Sopenharmony_ci#define asinh(x)        __tg_real_complex(asinh, (x))
2137777dab0Sopenharmony_ci#define atan(x)         __tg_real_complex(atan, (x))
2147777dab0Sopenharmony_ci#define atan2(x,y)      __tg_real_2(atan2, (x), (y))
2157777dab0Sopenharmony_ci#define atanh(x)        __tg_real_complex(atanh, (x))
2167777dab0Sopenharmony_ci#define carg(x)         __tg_complex_retreal(carg, (x))
2177777dab0Sopenharmony_ci#define cbrt(x)         __tg_real(cbrt, (x))
2187777dab0Sopenharmony_ci#define ceil(x)         __tg_real(ceil, (x))
2197777dab0Sopenharmony_ci#define cimag(x)        __tg_complex_retreal(cimag, (x))
2207777dab0Sopenharmony_ci#define conj(x)         __tg_complex(conj, (x))
2217777dab0Sopenharmony_ci#define copysign(x,y)   __tg_real_2(copysign, (x), (y))
2227777dab0Sopenharmony_ci#define cos(x)          __tg_real_complex(cos, (x))
2237777dab0Sopenharmony_ci#define cosh(x)         __tg_real_complex(cosh, (x))
2247777dab0Sopenharmony_ci#define cproj(x)        __tg_complex(cproj, (x))
2257777dab0Sopenharmony_ci#define creal(x)        __tg_complex_retreal(creal, (x))
2267777dab0Sopenharmony_ci#define erf(x)          __tg_real(erf, (x))
2277777dab0Sopenharmony_ci#define erfc(x)         __tg_real(erfc, (x))
2287777dab0Sopenharmony_ci#define exp(x)          __tg_real_complex(exp, (x))
2297777dab0Sopenharmony_ci#define exp2(x)         __tg_real(exp2, (x))
2307777dab0Sopenharmony_ci#define expm1(x)        __tg_real(expm1, (x))
2317777dab0Sopenharmony_ci#define fabs(x)         __tg_real_complex_fabs(x)
2327777dab0Sopenharmony_ci#define fdim(x,y)       __tg_real_2(fdim, (x), (y))
2337777dab0Sopenharmony_ci#define floor(x)        __tg_real(floor, (x))
2347777dab0Sopenharmony_ci#define fma(x,y,z)      __tg_real_fma((x), (y), (z))
2357777dab0Sopenharmony_ci#define fmax(x,y)       __tg_real_2(fmax, (x), (y))
2367777dab0Sopenharmony_ci#define fmin(x,y)       __tg_real_2(fmin, (x), (y))
2377777dab0Sopenharmony_ci#define fmod(x,y)       __tg_real_2(fmod, (x), (y))
2387777dab0Sopenharmony_ci#define frexp(x,y)      __tg_real_2_1(frexp, (x), (y))
2397777dab0Sopenharmony_ci#define hypot(x,y)      __tg_real_2(hypot, (x), (y))
2407777dab0Sopenharmony_ci#define ilogb(x)        __tg_real_nocast(ilogb, (x))
2417777dab0Sopenharmony_ci#define ldexp(x,y)      __tg_real_2_1(ldexp, (x), (y))
2427777dab0Sopenharmony_ci#define lgamma(x)       __tg_real(lgamma, (x))
2437777dab0Sopenharmony_ci#define llrint(x)       __tg_real_nocast(llrint, (x))
2447777dab0Sopenharmony_ci#define llround(x)      __tg_real_nocast(llround, (x))
2457777dab0Sopenharmony_ci#define log(x)          __tg_real_complex(log, (x))
2467777dab0Sopenharmony_ci#define log10(x)        __tg_real(log10, (x))
2477777dab0Sopenharmony_ci#define log1p(x)        __tg_real(log1p, (x))
2487777dab0Sopenharmony_ci#define log2(x)         __tg_real(log2, (x))
2497777dab0Sopenharmony_ci#define logb(x)         __tg_real(logb, (x))
2507777dab0Sopenharmony_ci#define lrint(x)        __tg_real_nocast(lrint, (x))
2517777dab0Sopenharmony_ci#define lround(x)       __tg_real_nocast(lround, (x))
2527777dab0Sopenharmony_ci#define nearbyint(x)    __tg_real(nearbyint, (x))
2537777dab0Sopenharmony_ci#define nextafter(x,y)  __tg_real_2(nextafter, (x), (y))
2547777dab0Sopenharmony_ci#define nexttoward(x,y) __tg_real_2(nexttoward, (x), (y))
2557777dab0Sopenharmony_ci#define pow(x,y)        __tg_real_complex_pow((x), (y))
2567777dab0Sopenharmony_ci#define remainder(x,y)  __tg_real_2(remainder, (x), (y))
2577777dab0Sopenharmony_ci#define remquo(x,y,z)   __tg_real_remquo((x), (y), (z))
2587777dab0Sopenharmony_ci#define rint(x)         __tg_real(rint, (x))
2597777dab0Sopenharmony_ci#define round(x)        __tg_real(round, (x))
2607777dab0Sopenharmony_ci#define scalbln(x,y)    __tg_real_2_1(scalbln, (x), (y))
2617777dab0Sopenharmony_ci#define scalbn(x,y)     __tg_real_2_1(scalbn, (x), (y))
2627777dab0Sopenharmony_ci#define sin(x)          __tg_real_complex(sin, (x))
2637777dab0Sopenharmony_ci#define sinh(x)         __tg_real_complex(sinh, (x))
2647777dab0Sopenharmony_ci#define sqrt(x)         __tg_real_complex(sqrt, (x))
2657777dab0Sopenharmony_ci#define tan(x)          __tg_real_complex(tan, (x))
2667777dab0Sopenharmony_ci#define tanh(x)         __tg_real_complex(tanh, (x))
2677777dab0Sopenharmony_ci#define tgamma(x)       __tg_real(tgamma, (x))
2687777dab0Sopenharmony_ci#define trunc(x)        __tg_real(trunc, (x))
2697777dab0Sopenharmony_ci
2707777dab0Sopenharmony_ci#endif
271