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