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