remove dependency of memmove on memcpy direction
[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(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 /* suppress any macros in math.h or complex.h */
125
126 #undef acos
127 #undef acosh
128 #undef asin
129 #undef asinh
130 #undef atan
131 #undef atan2
132 #undef atanh
133 #undef carg
134 #undef cbrt
135 #undef ceil
136 #undef cimag
137 #undef conj
138 #undef copysign
139 #undef cos
140 #undef cosh
141 #undef cproj
142 #undef creal
143 #undef erf
144 #undef erfc
145 #undef exp
146 #undef exp2
147 #undef expm1
148 #undef fabs
149 #undef fdim
150 #undef floor
151 #undef fma
152 #undef fmax
153 #undef fmin
154 #undef fmod
155 #undef frexp
156 #undef hypot
157 #undef ilogb
158 #undef ldexp
159 #undef lgamma
160 #undef llrint
161 #undef llround
162 #undef log
163 #undef log10
164 #undef log1p
165 #undef log2
166 #undef logb
167 #undef lrint
168 #undef lround
169 #undef nearbyint
170 #undef nextafter
171 #undef nexttoward
172 #undef pow
173 #undef remainder
174 #undef remquo
175 #undef rint
176 #undef round
177 #undef scalbln
178 #undef scalbn
179 #undef sin
180 #undef sinh
181 #undef sqrt
182 #undef tan
183 #undef tanh
184 #undef tgamma
185 #undef trunc
186
187 /* tg functions */
188
189 #define acos(x)         __tg_real_complex(acos, (x))
190 #define acosh(x)        __tg_real_complex(acosh, (x))
191 #define asin(x)         __tg_real_complex(asin, (x))
192 #define asinh(x)        __tg_real_complex(asinh, (x))
193 #define atan(x)         __tg_real_complex(atan, (x))
194 #define atan2(x,y)      __tg_real_2(atan2, (x), (y))
195 #define atanh(x)        __tg_real_complex(atanh, (x))
196 #define carg(x)         __tg_complex_retreal(carg, (x))
197 #define cbrt(x)         __tg_real(cbrt, (x))
198 #define ceil(x)         __tg_real(ceil, (x))
199 #define cimag(x)        __tg_complex_retreal(cimag, (x))
200 #define conj(x)         __tg_complex(conj, (x))
201 #define copysign(x,y)   __tg_real_2(copysign, (x), (y))
202 #define cos(x)          __tg_real_complex(cos, (x))
203 #define cosh(x)         __tg_real_complex(cosh, (x))
204 #define cproj(x)        __tg_complex(cproj, (x))
205 #define creal(x)        __tg_complex_retreal(creal, (x))
206 #define erf(x)          __tg_real(erf, (x))
207 #define erfc(x)         __tg_real(erfc, (x))
208 #define exp(x)          __tg_real_complex(exp, (x))
209 #define exp2(x)         __tg_real(exp2, (x))
210 #define expm1(x)        __tg_real(expm1, (x))
211 #define fabs(x)         __tg_real_complex_fabs(x)
212 #define fdim(x,y)       __tg_real_2(fdim, (x), (y))
213 #define floor(x)        __tg_real(floor, (x))
214 #define fma(x,y,z)      __tg_real_fma((x), (y), (z))
215 #define fmax(x,y)       __tg_real_2(fmax, (x), (y))
216 #define fmin(x,y)       __tg_real_2(fmin, (x), (y))
217 #define fmod(x,y)       __tg_real_2(fmod, (x), (y))
218 #define frexp(x,y)      __tg_real_2_1(frexp, (x), (y))
219 #define hypot(x,y)      __tg_real_2(hypot, (x), (y))
220 #define ilogb(x)        __tg_real(ilogb, (x))
221 #define ldexp(x,y)      __tg_real_2_1(ldexp, (x), (y))
222 #define lgamma(x)       __tg_real(lgamma, (x))
223 #define llrint(x)       __tg_real(llrint, (x))
224 #define llround(x)      __tg_real(llround, (x))
225 #define log(x)          __tg_real_complex(log, (x))
226 #define log10(x)        __tg_real(log10, (x))
227 #define log1p(x)        __tg_real(log1p, (x))
228 #define log2(x)         __tg_real(log2, (x))
229 #define logb(x)         __tg_real(logb, (x))
230 #define lrint(x)        __tg_real(lrint, (x))
231 #define lround(x)       __tg_real(lround, (x))
232 #define nearbyint(x)    __tg_real(nearbyint, (x))
233 #define nextafter(x,y)  __tg_real_2(nextafter, (x), (y))
234 #define nexttoward(x,y) __tg_real_2(nexttoward, (x), (y))
235 #define pow(x,y)        __tg_real_complex_pow((x), (y))
236 #define remainder(x,y)  __tg_real_2(remainder, (x), (y))
237 #define remquo(x,y,z)   __tg_real_remquo((x), (y), (z))
238 #define rint(x)         __tg_real(rint, (x))
239 #define round(x)        __tg_real(round, (x))
240 #define scalbln(x,y)    __tg_real_2_1(scalbln, (x), (y))
241 #define scalbn(x,y)     __tg_real_2_1(scalbn, (x), (y))
242 #define sin(x)          __tg_real_complex(sin, (x))
243 #define sinh(x)         __tg_real_complex(sinh, (x))
244 #define sqrt(x)         __tg_real_complex(sqrt, (x))
245 #define tan(x)          __tg_real_complex(tan, (x))
246 #define tanh(x)         __tg_real_complex(tanh, (x))
247 #define tgamma(x)       __tg_real(tgamma, (x))
248 #define trunc(x)        __tg_real(trunc, (x))
249
250 #endif