fixed tgmath.h for functions with integral result
[musl] / include / tgmath.h
1 #ifndef _TGMATH_H
2 #define _TGMATH_H
3
4 /*
5 the return types are only correct 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_nocast(fun, x) ( \
63         __FLT(x) ? fun ## f (x) : \
64         __LDBL(x) ? fun ## l (x) : \
65         fun(x) )
66
67 #define __tg_real(fun, x) (__RETCAST(x)__tg_real_nocast(fun, x))
68
69 #define __tg_real_2_1(fun, x, y) (__RETCAST(x)( \
70         __FLT(x) ? fun ## f (x, y) : \
71         __LDBL(x) ? fun ## l (x, y) : \
72         fun(x, y) ))
73
74 #define __tg_real_2(fun, x, y) (__RETCAST_2(x, y)( \
75         __FLT(x) && __FLT(y) ? fun ## f (x, y) : \
76         __LDBL((x)+(y)) ? fun ## l (x, y) : \
77         fun(x, y) ))
78
79 #define __tg_complex(fun, x) (__RETCAST((x)+I)( \
80         __FLTCX((x)+I) && __IS_FP(x) ? fun ## f (x) : \
81         __LDBLCX((x)+I) ? fun ## l (x) : \
82         fun(x) ))
83
84 #define __tg_complex_retreal(fun, x) (__RETCAST(__TO_REAL(x))( \
85         __FLTCX((x)+I) && __IS_FP(x) ? fun ## f (x) : \
86         __LDBLCX((x)+I) ? fun ## l (x) : \
87         fun(x) ))
88
89 #define __tg_real_complex(fun, x) (__RETCAST(x)( \
90         __FLTCX(x) ? c ## fun ## f (x) : \
91         __DBLCX(x) ? c ## fun (x) : \
92         __LDBLCX(x) ? c ## fun ## l (x) : \
93         __FLT(x) ? fun ## f (x) : \
94         __LDBL(x) ? fun ## l (x) : \
95         fun(x) ))
96
97 /* special cases */
98
99 #define __tg_real_remquo(x, y, z) (__RETCAST_2(x, y)( \
100         __FLT(x) && __FLT(y) ? remquof(x, y, z) : \
101         __LDBL((x)+(y)) ? remquol(x, y, z) : \
102         remquo(x, y, z) ))
103
104 #define __tg_real_fma(x, y, z) (__RETCAST_3(x, y, z)( \
105         __FLT(x) && __FLT(y) && __FLT(z) ? fmaf(x, y, z) : \
106         __LDBL((x)+(y)+(z)) ? fmal(x, y, z) : \
107         fma(x, y, z) ))
108
109 #define __tg_real_complex_pow(x, y) (__RETCAST_2(x, y)( \
110         __FLTCX((x)+(y)) && __IS_FP(x) && __IS_FP(y) ? cpowf(x, y) : \
111         __FLTCX((x)+(y)) ? cpow(x, y) : \
112         __DBLCX((x)+(y)) ? cpow(x, y) : \
113         __LDBLCX((x)+(y)) ? cpowl(x, y) : \
114         __FLT(x) && __FLT(y) ? powf(x, y) : \
115         __LDBL((x)+(y)) ? powl(x, y) : \
116         pow(x, y) ))
117
118 #define __tg_real_complex_fabs(x) (__RETCAST(__TO_REAL(x))( \
119         __FLTCX(x) ? cabsf(x) : \
120         __DBLCX(x) ? cabs(x) : \
121         __LDBLCX(x) ? cabsl(x) : \
122         __FLT(x) ? fabsf(x) : \
123         __LDBL(x) ? fabsl(x) : \
124         fabs(x) ))
125
126 /* suppress any macros in math.h or complex.h */
127
128 #undef acos
129 #undef acosh
130 #undef asin
131 #undef asinh
132 #undef atan
133 #undef atan2
134 #undef atanh
135 #undef carg
136 #undef cbrt
137 #undef ceil
138 #undef cimag
139 #undef conj
140 #undef copysign
141 #undef cos
142 #undef cosh
143 #undef cproj
144 #undef creal
145 #undef erf
146 #undef erfc
147 #undef exp
148 #undef exp2
149 #undef expm1
150 #undef fabs
151 #undef fdim
152 #undef floor
153 #undef fma
154 #undef fmax
155 #undef fmin
156 #undef fmod
157 #undef frexp
158 #undef hypot
159 #undef ilogb
160 #undef ldexp
161 #undef lgamma
162 #undef llrint
163 #undef llround
164 #undef log
165 #undef log10
166 #undef log1p
167 #undef log2
168 #undef logb
169 #undef lrint
170 #undef lround
171 #undef nearbyint
172 #undef nextafter
173 #undef nexttoward
174 #undef pow
175 #undef remainder
176 #undef remquo
177 #undef rint
178 #undef round
179 #undef scalbln
180 #undef scalbn
181 #undef sin
182 #undef sinh
183 #undef sqrt
184 #undef tan
185 #undef tanh
186 #undef tgamma
187 #undef trunc
188
189 /* tg functions */
190
191 #define acos(x)         __tg_real_complex(acos, (x))
192 #define acosh(x)        __tg_real_complex(acosh, (x))
193 #define asin(x)         __tg_real_complex(asin, (x))
194 #define asinh(x)        __tg_real_complex(asinh, (x))
195 #define atan(x)         __tg_real_complex(atan, (x))
196 #define atan2(x,y)      __tg_real_2(atan2, (x), (y))
197 #define atanh(x)        __tg_real_complex(atanh, (x))
198 #define carg(x)         __tg_complex_retreal(carg, (x))
199 #define cbrt(x)         __tg_real(cbrt, (x))
200 #define ceil(x)         __tg_real(ceil, (x))
201 #define cimag(x)        __tg_complex_retreal(cimag, (x))
202 #define conj(x)         __tg_complex(conj, (x))
203 #define copysign(x,y)   __tg_real_2(copysign, (x), (y))
204 #define cos(x)          __tg_real_complex(cos, (x))
205 #define cosh(x)         __tg_real_complex(cosh, (x))
206 #define cproj(x)        __tg_complex(cproj, (x))
207 #define creal(x)        __tg_complex_retreal(creal, (x))
208 #define erf(x)          __tg_real(erf, (x))
209 #define erfc(x)         __tg_real(erfc, (x))
210 #define exp(x)          __tg_real_complex(exp, (x))
211 #define exp2(x)         __tg_real(exp2, (x))
212 #define expm1(x)        __tg_real(expm1, (x))
213 #define fabs(x)         __tg_real_complex_fabs(x)
214 #define fdim(x,y)       __tg_real_2(fdim, (x), (y))
215 #define floor(x)        __tg_real(floor, (x))
216 #define fma(x,y,z)      __tg_real_fma((x), (y), (z))
217 #define fmax(x,y)       __tg_real_2(fmax, (x), (y))
218 #define fmin(x,y)       __tg_real_2(fmin, (x), (y))
219 #define fmod(x,y)       __tg_real_2(fmod, (x), (y))
220 #define frexp(x,y)      __tg_real_2_1(frexp, (x), (y))
221 #define hypot(x,y)      __tg_real_2(hypot, (x), (y))
222 #define ilogb(x)        __tg_real_nocast(ilogb, (x))
223 #define ldexp(x,y)      __tg_real_2_1(ldexp, (x), (y))
224 #define lgamma(x)       __tg_real(lgamma, (x))
225 #define llrint(x)       __tg_real_nocast(llrint, (x))
226 #define llround(x)      __tg_real_nocast(llround, (x))
227 #define log(x)          __tg_real_complex(log, (x))
228 #define log10(x)        __tg_real(log10, (x))
229 #define log1p(x)        __tg_real(log1p, (x))
230 #define log2(x)         __tg_real(log2, (x))
231 #define logb(x)         __tg_real(logb, (x))
232 #define lrint(x)        __tg_real_nocast(lrint, (x))
233 #define lround(x)       __tg_real_nocast(lround, (x))
234 #define nearbyint(x)    __tg_real(nearbyint, (x))
235 #define nextafter(x,y)  __tg_real_2(nextafter, (x), (y))
236 #define nexttoward(x,y) __tg_real_2(nexttoward, (x), (y))
237 #define pow(x,y)        __tg_real_complex_pow((x), (y))
238 #define remainder(x,y)  __tg_real_2(remainder, (x), (y))
239 #define remquo(x,y,z)   __tg_real_remquo((x), (y), (z))
240 #define rint(x)         __tg_real(rint, (x))
241 #define round(x)        __tg_real(round, (x))
242 #define scalbln(x,y)    __tg_real_2_1(scalbln, (x), (y))
243 #define scalbn(x,y)     __tg_real_2_1(scalbn, (x), (y))
244 #define sin(x)          __tg_real_complex(sin, (x))
245 #define sinh(x)         __tg_real_complex(sinh, (x))
246 #define sqrt(x)         __tg_real_complex(sqrt, (x))
247 #define tan(x)          __tg_real_complex(tan, (x))
248 #define tanh(x)         __tg_real_complex(tanh, (x))
249 #define tgamma(x)       __tg_real(tgamma, (x))
250 #define trunc(x)        __tg_real(trunc, (x))
251
252 #endif