e859252a514fa16eb727d3eff6b1aa75a8c3af66
[libm] / include / tgmath.h
1 #ifndef _TGMATH_H
2 #define _TGMATH_H
3
4 /*
5 wrong when sizeof(double) == sizeof(long double)
6 with __GNUC__ the return type is broken in case of mixed float and int arguments
7 without __GNUC__ the return type is always long double [complex]
8 (imaginary type is not supported)
9 */
10
11 #include <math.h>
12 #include <complex.h>
13
14 #define __IS_FP(x) !!((1?1:(x))/2)
15 #define __IS_CX(x) (__IS_FP(x) && sizeof(x) == sizeof((x)+I))
16 #define __IS_REAL(x) (__IS_FP(x) && 2*sizeof(x) == sizeof((x)+I))
17
18 #define __FLT(x) (__IS_REAL(x) && sizeof(x) == sizeof(float))
19 #define __LDBL(x) (__IS_REAL(x) && sizeof(x) == sizeof(long double))
20
21 #define __FLTCX(x) (__IS_CX(x) && sizeof(x) == sizeof(float complex))
22 #define __DBLCX(x) (__IS_CX(x) && sizeof(x) == sizeof(double complex))
23 #define __LDBLCX(x) (__IS_CX(x) && sizeof(x) == sizeof(long double complex))
24
25 #ifdef __GNUC__
26 /* typeof(x) if fp else double (using ?: with null pointers) */
27 #define __RETCAST(x) (__typeof__(*( \
28         0 ? (__typeof__(0 ? (double *)0 : (void *)__IS_FP(x)))0 : \
29             (__typeof__(0 ? (__typeof__(x) *)0 : (void *)!__IS_FP(x)))0 )))
30 /* drop complex from the type of x */
31 #define __TO_REAL(x) *( \
32         0 ? (__typeof__(0 ? (double *)0 : (void *)!__DBLCX(x)))0 : \
33         0 ? (__typeof__(0 ? (float *)0 : (void *)!__FLTCX(x)))0 : \
34         0 ? (__typeof__(0 ? (long double *)0 : (void *)!__LDBLCX(x)))0 : \
35             (__typeof__(0 ? (__typeof__(x) *)0 : (void *)__IS_CX(x)))0 )
36 #else
37 #define __RETCAST(x)
38 #endif
39
40 #define __tg_real(fun, x) (__RETCAST(x)( \
41         __FLT(x) ? fun ## f (x) : \
42         __LDBL(x) ? fun ## l (x) : \
43         fun(x) ))
44
45 #define __tg_real_2_1(fun, x, y) (__RETCAST(x)( \
46         __FLT(x) ? fun ## f (x, y) : \
47         __LDBL(x) ? fun ## l (x, y) : \
48         fun(x, y) ))
49
50 #define __tg_real_2(fun, x, y) (__RETCAST((x)+(y))( \
51         __FLT(x)  && __FLT(y) ? fun ## f (x, y) : \
52         __LDBL(x) || __LDBL(y) ? fun ## l (x, y) : \
53         fun(x, y) ))
54
55 #define __tg_complex(fun, x) (__RETCAST((x)+I)( \
56         __FLTCX(x) || __FLT(x) ? fun ## f (x) : \
57         __LDBLCX(x) || __LDBL(x) ? fun ## l (x) : \
58         fun(x) ))
59
60 #define __tg_complex_retreal(fun, x) (__RETCAST(__TO_REAL(x))( \
61         __FLTCX(x) || __FLT(x) ? fun ## f (x) : \
62         __LDBLCX(x) || __LDBL(x) ? fun ## l (x) : \
63         fun(x) ))
64
65 #define __tg_real_complex(fun, x) (__RETCAST(x)( \
66         __LDBLCX(x) ? c ## fun ## l (x) : \
67         __DBLCX(x) ? c ## fun (x) : \
68         __FLTCX(x) ? c ## fun ## f (x) : \
69         __FLT(x) ? fun ## f (x) : \
70         __LDBL(x) ? fun ## l (x) : \
71         fun(x) ))
72
73 /* special cases */
74
75 #define __tg_real_remquo(x, y, z) (__RETCAST((x)+(y))( \
76         __FLT(x)  && __FLT(y) ? remquof(x, y, z) : \
77         __LDBL(x) || __LDBL(y) ? remquol(x, y, z) : \
78         remquo(x, y, z) ))
79
80 #define __tg_real_fma(x, y, z) (__RETCAST((x)+(y)+(z))( \
81         __FLT(x) && __FLT(y) && __FLT(z) ? fmaf(x, y, z) : \
82         __LDBL(x) || __LDBL(y) || __LDBL(z) ? fmal(x, y, z) : \
83         fma(x, y, z) ))
84
85 #define __tg_real_complex_pow(x, y) (__RETCAST((x)+(y))( \
86         __LDBLCX(x) || __LDBLCX(y) ? cpowl(x, y) : \
87         __DBLCX(x) || __DBLCX(y) ? cpow(x, y) : \
88         __FLTCX(x) || __FLTCX(y) ? cpowf(x, y) : \
89         __tg_real_2(pow, x, y) ))
90
91 #define __tg_real_complex_fabs(x) (__RETCAST(__TO_REAL(x))( \
92         __LDBLCX(x) ? cabsl(x) : \
93         __DBLCX(x) ? cabs(x) : \
94         __FLTCX(x) ? cabsf(x) : \
95         __tg_real(fabs, x) ))
96
97 /* tg functions */
98
99 #define acos(x)         __tg_real_complex(acos, (x))
100 #define acosh(x)        __tg_real_complex(acosh, (x))
101 #define asin(x)         __tg_real_complex(asin, (x))
102 #define asinh(x)        __tg_real_complex(asinh, (x))
103 #define atan(x)         __tg_real_complex(atan, (x))
104 #define atan2(x,y)      __tg_real_2(atan2, (x), (y))
105 #define atanh(x)        __tg_real_complex(atanh, (x))
106 #define carg(x)         __tg_complex_retreal(carg, (x))
107 #define cbrt(x)         __tg_real(cbrt, (x))
108 #define ceil(x)         __tg_real(ceil, (x))
109 #define cimag(x)        __tg_complex_retreal(cimag, (x))
110 #define conj(x)         __tg_complex(conj, (x))
111 #define copysign(x,y)   __tg_real_2(copysign, (x), (y))
112 #define cos(x)          __tg_real_complex(cos, (x))
113 #define cosh(x)         __tg_real_complex(cosh, (x))
114 #define cproj(x)        __tg_complex(cproj, (x))
115 #define creal(x)        __tg_complex_retreal(creal, (x))
116 #define erf(x)          __tg_real(erf, (x))
117 #define erfc(x)         __tg_real(erfc, (x))
118 #define exp(x)          __tg_real_complex(exp, (x))
119 #define exp2(x)         __tg_real(exp2, (x))
120 #define expm1(x)        __tg_real(expm1, (x))
121 #define fabs(x)         __tg_real_complex_fabs(x)
122 #define fdim(x,y)       __tg_real_2(fdim, (x), (y))
123 #define floor(x)        __tg_real(floor, (x))
124 #define fma(x,y,z)      __tg_real_fma((x), (y), (z))
125 #define fmax(x,y)       __tg_real_2(fmax, (x), (y))
126 #define fmin(x,y)       __tg_real_2(fmin, (x), (y))
127 #define fmod(x,y)       __tg_real_2(fmod, (x), (y))
128 #define frexp(x,y)      __tg_real_2_1(frexp, (x), (y))
129 #define hypot(x,y)      __tg_real_2(hypot, (x), (y))
130 #define ilogb(x)        __tg_real(ilogb, (x))
131 #define ldexp(x,y)      __tg_real_2_1(ldexp, (x), (y))
132 #define lgamma(x)       __tg_real(lgamma, (x))
133 #define llrint(x)       __tg_real(llrint, (x))
134 #define llround(x)      __tg_real(llround, (x))
135 #define log(x)          __tg_real_complex(log, (x))
136 #define log10(x)        __tg_real(log10, (x))
137 #define log1p(x)        __tg_real(log1p, (x))
138 #define log2(x)         __tg_real(log2, (x))
139 #define logb(x)         __tg_real(logb, (x))
140 #define lrint(x)        __tg_real(lrint, (x))
141 #define lround(x)       __tg_real(lround, (x))
142 #define nearbyint(x)    __tg_real(nearbyint, (x))
143 #define nextafter(x,y)  __tg_real_2(nextafter, (x), (y)
144 #define nexttoward(x,y) __tg_real_2(nexttoward, (x), (y))
145 #define pow(x,y)        __tg_real_complex_pow((x), (y))
146 #define remainder(x,y)  __tg_real_2(remainder, (x), (y))
147 #define remquo(x,y,z)   __tg_real_remquo((x), (y), (z))
148 #define rint(x)         __tg_real(rint, (x))
149 #define round(x)        __tg_real(round, (x))
150 #define scalbln(x,y)    __tg_real_2_1(scalbln, (x), (y))
151 #define scalbn(x,y)     __tg_real_2_1(scalbn, (x), (y))
152 #define sin(x)          __tg_real_complex(sin, (x))
153 #define sinh(x)         __tg_real_complex(sinh, (x))
154 #define sqrt(x)         __tg_real_complex(sqrt, (x))
155 #define tan(x)          __tg_real_complex(tan, (x))
156 #define tanh(x)         __tg_real_complex(tanh, (x))
157 #define tgamma(x)       __tg_real(tgamma, (x))
158 #define trunc(x)        __tg_real(trunc, (x))
159
160 #endif