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