Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(217)

Side by Side Diff: fusl/include/tgmath.h

Issue 1714623002: [fusl] clang-format fusl (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: headers too Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #ifndef _TGMATH_H 1 #ifndef _TGMATH_H
2 #define _TGMATH_H 2 #define _TGMATH_H
3 3
4 /* 4 /*
5 the return types are only correct with gcc (__GNUC__) 5 the return types are only correct with gcc (__GNUC__)
6 otherwise they are long double or long double complex 6 otherwise they are long double or long double complex
7 7
8 the long double version of a function is never chosen when 8 the long double version of a function is never chosen when
9 sizeof(double) == sizeof(long double) 9 sizeof(double) == sizeof(long double)
10 (but the return type is set correctly with gcc) 10 (but the return type is set correctly with gcc)
11 */ 11 */
12 12
13 #include <math.h> 13 #include <math.h>
14 #include <complex.h> 14 #include <complex.h>
15 15
16 #define __IS_FP(x) (sizeof((x)+1ULL) == sizeof((x)+1.0f)) 16 #define __IS_FP(x) (sizeof((x) + 1ULL) == sizeof((x) + 1.0f))
17 #define __IS_CX(x) (__IS_FP(x) && sizeof(x) == sizeof((x)+I)) 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)) 18 #define __IS_REAL(x) (__IS_FP(x) && 2 * sizeof(x) == sizeof((x) + I))
19 19
20 #define __FLT(x) (__IS_REAL(x) && sizeof(x) == sizeof(float)) 20 #define __FLT(x) (__IS_REAL(x) && sizeof(x) == sizeof(float))
21 #define __LDBL(x) (__IS_REAL(x) && sizeof(x) == sizeof(long double) && sizeof(lo ng double) != sizeof(double)) 21 #define __LDBL(x) \
22 (__IS_REAL(x) && sizeof(x) == sizeof(long double) && \
23 sizeof(long double) != sizeof(double))
22 24
23 #define __FLTCX(x) (__IS_CX(x) && sizeof(x) == sizeof(float complex)) 25 #define __FLTCX(x) (__IS_CX(x) && sizeof(x) == sizeof(float complex))
24 #define __DBLCX(x) (__IS_CX(x) && sizeof(x) == sizeof(double complex)) 26 #define __DBLCX(x) (__IS_CX(x) && sizeof(x) == sizeof(double complex))
25 #define __LDBLCX(x) (__IS_CX(x) && sizeof(x) == sizeof(long double complex) && s izeof(long double) != sizeof(double)) 27 #define __LDBLCX(x) \
28 (__IS_CX(x) && sizeof(x) == sizeof(long double complex) && \
29 sizeof(long double) != sizeof(double))
26 30
27 /* return type */ 31 /* return type */
28 32
29 #ifdef __GNUC__ 33 #ifdef __GNUC__
30 /* 34 /*
31 the result must be casted to the right type 35 the result must be casted to the right type
32 (otherwise the result type is determined by the conversion 36 (otherwise the result type is determined by the conversion
33 rules applied to all the function return types so it is long 37 rules applied to all the function return types so it is long
34 double or long double complex except for integral functions) 38 double or long double complex except for integral functions)
35 39
36 this cannot be done in c99, so the typeof gcc extension is 40 this cannot be done in c99, so the typeof gcc extension is
37 used and that the type of ?: depends on wether an operand is 41 used and that the type of ?: depends on wether an operand is
38 a null pointer constant or not 42 a null pointer constant or not
39 (in c11 _Generic can be used) 43 (in c11 _Generic can be used)
40 44
41 the c arguments below must be integer constant expressions 45 the c arguments below must be integer constant expressions
42 so they can be in null pointer constants 46 so they can be in null pointer constants
43 (__IS_FP above was carefully chosen this way) 47 (__IS_FP above was carefully chosen this way)
44 */ 48 */
45 /* if c then t else void */ 49 /* if c then t else void */
46 #define __type1(c,t) __typeof__(*(0?(t*)0:(void*)!(c))) 50 #define __type1(c, t) __typeof__(*(0 ? (t*)0 : (void*)!(c)))
47 /* if c then t1 else t2 */ 51 /* if c then t1 else t2 */
48 #define __type2(c,t1,t2) __typeof__(*(0?(__type1(c,t1)*)0:(__type1(!(c),t2)*)0)) 52 #define __type2(c, t1, t2) \
53 __typeof__(*(0 ? (__type1(c, t1)*)0 : (__type1(!(c), t2)*)0))
49 /* cast to double when x is integral, otherwise use typeof(x) */ 54 /* cast to double when x is integral, otherwise use typeof(x) */
50 #define __RETCAST(x) ( \ 55 #define __RETCAST(x) (__type2(__IS_FP(x), __typeof__(x), double))
51 » __type2(__IS_FP(x), __typeof__(x), double))
52 /* 2 args case, should work for complex types (cpow) */ 56 /* 2 args case, should work for complex types (cpow) */
53 #define __RETCAST_2(x, y) ( \ 57 #define __RETCAST_2(x, y) \
54 » __type2(__IS_FP(x) && __IS_FP(y), \ 58 (__type2(__IS_FP(x) && __IS_FP(y), __typeof__((x) + (y)), \
55 » » __typeof__((x)+(y)), \ 59 __typeof__((x) + (y) + 1.0)))
56 » » __typeof__((x)+(y)+1.0)))
57 /* 3 args case (fma only) */ 60 /* 3 args case (fma only) */
58 #define __RETCAST_3(x, y, z) ( \ 61 #define __RETCAST_3(x, y, z) \
59 » __type2(__IS_FP(x) && __IS_FP(y) && __IS_FP(z), \ 62 (__type2(__IS_FP(x) && __IS_FP(y) && __IS_FP(z), \
60 » » __typeof__((x)+(y)+(z)), \ 63 __typeof__((x) + (y) + (z)), __typeof__((x) + (y) + (z) + 1.0)))
61 » » __typeof__((x)+(y)+(z)+1.0)))
62 /* drop complex from the type of x */ 64 /* drop complex from the type of x */
63 /* TODO: wrong when sizeof(long double)==sizeof(double) */ 65 /* TODO: wrong when sizeof(long double)==sizeof(double) */
64 #define __RETCAST_REAL(x) ( \ 66 #define __RETCAST_REAL(x) \
65 » __type2(__IS_FP(x) && sizeof((x)+I) == sizeof(float complex), float, \ 67 (__type2(__IS_FP(x) && sizeof((x) + I) == sizeof(float complex), float, \
66 » __type2(sizeof((x)+1.0+I) == sizeof(double complex), double, \ 68 __type2(sizeof((x) + 1.0 + I) == sizeof(double complex), double, \
67 » » long double))) 69 long double)))
68 /* add complex to the type of x */ 70 /* add complex to the type of x */
69 #define __RETCAST_CX(x) (__typeof__(__RETCAST(x)0+I)) 71 #define __RETCAST_CX(x) (__typeof__(__RETCAST(x) 0 + I))
70 #else 72 #else
71 #define __RETCAST(x) 73 #define __RETCAST(x)
72 #define __RETCAST_2(x, y) 74 #define __RETCAST_2(x, y)
73 #define __RETCAST_3(x, y, z) 75 #define __RETCAST_3(x, y, z)
74 #define __RETCAST_REAL(x) 76 #define __RETCAST_REAL(x)
75 #define __RETCAST_CX(x) 77 #define __RETCAST_CX(x)
76 #endif 78 #endif
77 79
78 /* function selection */ 80 /* function selection */
79 81
80 #define __tg_real_nocast(fun, x) ( \ 82 #define __tg_real_nocast(fun, x) \
81 » __FLT(x) ? fun ## f (x) : \ 83 (__FLT(x) ? fun##f(x) : __LDBL(x) ? fun##l(x) : fun(x))
82 » __LDBL(x) ? fun ## l (x) : \
83 » fun(x) )
84 84
85 #define __tg_real(fun, x) (__RETCAST(x)__tg_real_nocast(fun, x)) 85 #define __tg_real(fun, x) (__RETCAST(x) __tg_real_nocast(fun, x))
86 86
87 #define __tg_real_2_1(fun, x, y) (__RETCAST(x)( \ 87 #define __tg_real_2_1(fun, x, y) \
88 » __FLT(x) ? fun ## f (x, y) : \ 88 (__RETCAST(x)(__FLT(x) ? fun##f(x, y) : __LDBL(x) ? fun##l(x, y) : fun(x, y)))
89 » __LDBL(x) ? fun ## l (x, y) : \
90 » fun(x, y) ))
91 89
92 #define __tg_real_2(fun, x, y) (__RETCAST_2(x, y)( \ 90 #define __tg_real_2(fun, x, y) \
93 » __FLT(x) && __FLT(y) ? fun ## f (x, y) : \ 91 (__RETCAST_2(x, y)(__FLT(x) && __FLT(y) ? fun##f(x, y) : __LDBL((x) + (y)) \
94 » __LDBL((x)+(y)) ? fun ## l (x, y) : \ 92 ? fun##l(x, y) \
95 » fun(x, y) )) 93 : fun(x, y)))
96 94
97 #define __tg_complex(fun, x) (__RETCAST_CX(x)( \ 95 #define __tg_complex(fun, x) \
98 » __FLTCX((x)+I) && __IS_FP(x) ? fun ## f (x) : \ 96 (__RETCAST_CX(x)(__FLTCX((x) + I) && __IS_FP(x) \
99 » __LDBLCX((x)+I) ? fun ## l (x) : \ 97 ? fun##f(x) \
100 » fun(x) )) 98 : __LDBLCX((x) + I) ? fun##l(x) : fun(x)))
101 99
102 #define __tg_complex_retreal(fun, x) (__RETCAST_REAL(x)( \ 100 #define __tg_complex_retreal(fun, x) \
103 » __FLTCX((x)+I) && __IS_FP(x) ? fun ## f (x) : \ 101 (__RETCAST_REAL(x)(__FLTCX((x) + I) && __IS_FP(x) \
104 » __LDBLCX((x)+I) ? fun ## l (x) : \ 102 ? fun##f(x) \
105 » fun(x) )) 103 : __LDBLCX((x) + I) ? fun##l(x) : fun(x)))
106 104
107 #define __tg_real_complex(fun, x) (__RETCAST(x)( \ 105 #define __tg_real_complex(fun, x) \
108 » __FLTCX(x) ? c ## fun ## f (x) : \ 106 (__RETCAST(x)( \
109 » __DBLCX(x) ? c ## fun (x) : \ 107 __FLTCX(x) ? c##fun##f(x) \
110 » __LDBLCX(x) ? c ## fun ## l (x) : \ 108 : __DBLCX(x) ? c##fun(x) \
111 » __FLT(x) ? fun ## f (x) : \ 109 : __LDBLCX(x) ? c##fun##l(x) \
112 » __LDBL(x) ? fun ## l (x) : \ 110 : __FLT(x) ? fun##f(x) \
113 » fun(x) )) 111 : __LDBL(x) ? fun##l(x) \
112 : fun(x)))
114 113
115 /* special cases */ 114 /* special cases */
116 115
117 #define __tg_real_remquo(x, y, z) (__RETCAST_2(x, y)( \ 116 #define __tg_real_remquo(x, y, z) \
118 » __FLT(x) && __FLT(y) ? remquof(x, y, z) : \ 117 (__RETCAST_2( \
119 » __LDBL((x)+(y)) ? remquol(x, y, z) : \ 118 x, y)(__FLT(x) && __FLT(y) ? remquof(x, y, z) : __LDBL((x) + (y)) \
120 » remquo(x, y, z) )) 119 ? remquol(x, y, z) \
120 : remquo(x, y, z)))
121 121
122 #define __tg_real_fma(x, y, z) (__RETCAST_3(x, y, z)( \ 122 #define __tg_real_fma(x, y, z) \
123 » __FLT(x) && __FLT(y) && __FLT(z) ? fmaf(x, y, z) : \ 123 (__RETCAST_3( \
124 » __LDBL((x)+(y)+(z)) ? fmal(x, y, z) : \ 124 x, y, z)(__FLT(x) && __FLT(y) && __FLT(z) \
125 » fma(x, y, z) )) 125 ? fmaf(x, y, z) \
126 : __LDBL((x) + (y) + (z)) ? fmal(x, y, z) : fma(x, y, z)))
126 127
127 #define __tg_real_complex_pow(x, y) (__RETCAST_2(x, y)( \ 128 #define __tg_real_complex_pow(x, y) \
128 » __FLTCX((x)+(y)) && __IS_FP(x) && __IS_FP(y) ? cpowf(x, y) : \ 129 (__RETCAST_2(x, y)(__FLTCX((x) + (y)) && __IS_FP(x) && __IS_FP(y) \
129 » __FLTCX((x)+(y)) ? cpow(x, y) : \ 130 ? cpowf(x, y) \
130 » __DBLCX((x)+(y)) ? cpow(x, y) : \ 131 : __FLTCX((x) + (y)) \
131 » __LDBLCX((x)+(y)) ? cpowl(x, y) : \ 132 ? cpow(x, y) \
132 » __FLT(x) && __FLT(y) ? powf(x, y) : \ 133 : __DBLCX((x) + (y)) \
133 » __LDBL((x)+(y)) ? powl(x, y) : \ 134 ? cpow(x, y) \
134 » pow(x, y) )) 135 : __LDBLCX((x) + (y)) \
136 ? cpowl(x, y) \
137 : __FLT(x) && __FLT(y) \
138 ? powf(x, y) \
139 : __LDBL((x) + (y)) \
140 ? powl(x, y) \
141 : pow(x, y)))
135 142
136 #define __tg_real_complex_fabs(x) (__RETCAST_REAL(x)( \ 143 #define __tg_real_complex_fabs(x) \
137 » __FLTCX(x) ? cabsf(x) : \ 144 (__RETCAST_REAL(x)( \
138 » __DBLCX(x) ? cabs(x) : \ 145 __FLTCX(x) ? cabsf(x) \
139 » __LDBLCX(x) ? cabsl(x) : \ 146 : __DBLCX(x) ? cabs(x) \
140 » __FLT(x) ? fabsf(x) : \ 147 : __LDBLCX(x) ? cabsl(x) \
141 » __LDBL(x) ? fabsl(x) : \ 148 : __FLT(x) ? fabsf(x) \
142 » fabs(x) )) 149 : __LDBL(x) ? fabsl(x) \
150 : fabs(x)))
143 151
144 /* suppress any macros in math.h or complex.h */ 152 /* suppress any macros in math.h or complex.h */
145 153
146 #undef acos 154 #undef acos
147 #undef acosh 155 #undef acosh
148 #undef asin 156 #undef asin
149 #undef asinh 157 #undef asinh
150 #undef atan 158 #undef atan
151 #undef atan2 159 #undef atan2
152 #undef atanh 160 #undef atanh
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 #undef sin 207 #undef sin
200 #undef sinh 208 #undef sinh
201 #undef sqrt 209 #undef sqrt
202 #undef tan 210 #undef tan
203 #undef tanh 211 #undef tanh
204 #undef tgamma 212 #undef tgamma
205 #undef trunc 213 #undef trunc
206 214
207 /* tg functions */ 215 /* tg functions */
208 216
209 #define acos(x) __tg_real_complex(acos, (x)) 217 #define acos(x) __tg_real_complex(acos, (x))
210 #define acosh(x) __tg_real_complex(acosh, (x)) 218 #define acosh(x) __tg_real_complex(acosh, (x))
211 #define asin(x) __tg_real_complex(asin, (x)) 219 #define asin(x) __tg_real_complex(asin, (x))
212 #define asinh(x) __tg_real_complex(asinh, (x)) 220 #define asinh(x) __tg_real_complex(asinh, (x))
213 #define atan(x) __tg_real_complex(atan, (x)) 221 #define atan(x) __tg_real_complex(atan, (x))
214 #define atan2(x,y) __tg_real_2(atan2, (x), (y)) 222 #define atan2(x, y) __tg_real_2(atan2, (x), (y))
215 #define atanh(x) __tg_real_complex(atanh, (x)) 223 #define atanh(x) __tg_real_complex(atanh, (x))
216 #define carg(x) __tg_complex_retreal(carg, (x)) 224 #define carg(x) __tg_complex_retreal(carg, (x))
217 #define cbrt(x) __tg_real(cbrt, (x)) 225 #define cbrt(x) __tg_real(cbrt, (x))
218 #define ceil(x) __tg_real(ceil, (x)) 226 #define ceil(x) __tg_real(ceil, (x))
219 #define cimag(x) __tg_complex_retreal(cimag, (x)) 227 #define cimag(x) __tg_complex_retreal(cimag, (x))
220 #define conj(x) __tg_complex(conj, (x)) 228 #define conj(x) __tg_complex(conj, (x))
221 #define copysign(x,y) __tg_real_2(copysign, (x), (y)) 229 #define copysign(x, y) __tg_real_2(copysign, (x), (y))
222 #define cos(x) __tg_real_complex(cos, (x)) 230 #define cos(x) __tg_real_complex(cos, (x))
223 #define cosh(x) __tg_real_complex(cosh, (x)) 231 #define cosh(x) __tg_real_complex(cosh, (x))
224 #define cproj(x) __tg_complex(cproj, (x)) 232 #define cproj(x) __tg_complex(cproj, (x))
225 #define creal(x) __tg_complex_retreal(creal, (x)) 233 #define creal(x) __tg_complex_retreal(creal, (x))
226 #define erf(x) __tg_real(erf, (x)) 234 #define erf(x) __tg_real(erf, (x))
227 #define erfc(x) __tg_real(erfc, (x)) 235 #define erfc(x) __tg_real(erfc, (x))
228 #define exp(x) __tg_real_complex(exp, (x)) 236 #define exp(x) __tg_real_complex(exp, (x))
229 #define exp2(x) __tg_real(exp2, (x)) 237 #define exp2(x) __tg_real(exp2, (x))
230 #define expm1(x) __tg_real(expm1, (x)) 238 #define expm1(x) __tg_real(expm1, (x))
231 #define fabs(x) __tg_real_complex_fabs(x) 239 #define fabs(x) __tg_real_complex_fabs(x)
232 #define fdim(x,y) __tg_real_2(fdim, (x), (y)) 240 #define fdim(x, y) __tg_real_2(fdim, (x), (y))
233 #define floor(x) __tg_real(floor, (x)) 241 #define floor(x) __tg_real(floor, (x))
234 #define fma(x,y,z) __tg_real_fma((x), (y), (z)) 242 #define fma(x, y, z) __tg_real_fma((x), (y), (z))
235 #define fmax(x,y) __tg_real_2(fmax, (x), (y)) 243 #define fmax(x, y) __tg_real_2(fmax, (x), (y))
236 #define fmin(x,y) __tg_real_2(fmin, (x), (y)) 244 #define fmin(x, y) __tg_real_2(fmin, (x), (y))
237 #define fmod(x,y) __tg_real_2(fmod, (x), (y)) 245 #define fmod(x, y) __tg_real_2(fmod, (x), (y))
238 #define frexp(x,y) __tg_real_2_1(frexp, (x), (y)) 246 #define frexp(x, y) __tg_real_2_1(frexp, (x), (y))
239 #define hypot(x,y) __tg_real_2(hypot, (x), (y)) 247 #define hypot(x, y) __tg_real_2(hypot, (x), (y))
240 #define ilogb(x) __tg_real_nocast(ilogb, (x)) 248 #define ilogb(x) __tg_real_nocast(ilogb, (x))
241 #define ldexp(x,y) __tg_real_2_1(ldexp, (x), (y)) 249 #define ldexp(x, y) __tg_real_2_1(ldexp, (x), (y))
242 #define lgamma(x) __tg_real(lgamma, (x)) 250 #define lgamma(x) __tg_real(lgamma, (x))
243 #define llrint(x) __tg_real_nocast(llrint, (x)) 251 #define llrint(x) __tg_real_nocast(llrint, (x))
244 #define llround(x) __tg_real_nocast(llround, (x)) 252 #define llround(x) __tg_real_nocast(llround, (x))
245 #define log(x) __tg_real_complex(log, (x)) 253 #define log(x) __tg_real_complex(log, (x))
246 #define log10(x) __tg_real(log10, (x)) 254 #define log10(x) __tg_real(log10, (x))
247 #define log1p(x) __tg_real(log1p, (x)) 255 #define log1p(x) __tg_real(log1p, (x))
248 #define log2(x) __tg_real(log2, (x)) 256 #define log2(x) __tg_real(log2, (x))
249 #define logb(x) __tg_real(logb, (x)) 257 #define logb(x) __tg_real(logb, (x))
250 #define lrint(x) __tg_real_nocast(lrint, (x)) 258 #define lrint(x) __tg_real_nocast(lrint, (x))
251 #define lround(x) __tg_real_nocast(lround, (x)) 259 #define lround(x) __tg_real_nocast(lround, (x))
252 #define nearbyint(x) __tg_real(nearbyint, (x)) 260 #define nearbyint(x) __tg_real(nearbyint, (x))
253 #define nextafter(x,y) __tg_real_2(nextafter, (x), (y)) 261 #define nextafter(x, y) __tg_real_2(nextafter, (x), (y))
254 #define nexttoward(x,y) __tg_real_2(nexttoward, (x), (y)) 262 #define nexttoward(x, y) __tg_real_2(nexttoward, (x), (y))
255 #define pow(x,y) __tg_real_complex_pow((x), (y)) 263 #define pow(x, y) __tg_real_complex_pow((x), (y))
256 #define remainder(x,y) __tg_real_2(remainder, (x), (y)) 264 #define remainder(x, y) __tg_real_2(remainder, (x), (y))
257 #define remquo(x,y,z) __tg_real_remquo((x), (y), (z)) 265 #define remquo(x, y, z) __tg_real_remquo((x), (y), (z))
258 #define rint(x) __tg_real(rint, (x)) 266 #define rint(x) __tg_real(rint, (x))
259 #define round(x) __tg_real(round, (x)) 267 #define round(x) __tg_real(round, (x))
260 #define scalbln(x,y) __tg_real_2_1(scalbln, (x), (y)) 268 #define scalbln(x, y) __tg_real_2_1(scalbln, (x), (y))
261 #define scalbn(x,y) __tg_real_2_1(scalbn, (x), (y)) 269 #define scalbn(x, y) __tg_real_2_1(scalbn, (x), (y))
262 #define sin(x) __tg_real_complex(sin, (x)) 270 #define sin(x) __tg_real_complex(sin, (x))
263 #define sinh(x) __tg_real_complex(sinh, (x)) 271 #define sinh(x) __tg_real_complex(sinh, (x))
264 #define sqrt(x) __tg_real_complex(sqrt, (x)) 272 #define sqrt(x) __tg_real_complex(sqrt, (x))
265 #define tan(x) __tg_real_complex(tan, (x)) 273 #define tan(x) __tg_real_complex(tan, (x))
266 #define tanh(x) __tg_real_complex(tanh, (x)) 274 #define tanh(x) __tg_real_complex(tanh, (x))
267 #define tgamma(x) __tg_real(tgamma, (x)) 275 #define tgamma(x) __tg_real(tgamma, (x))
268 #define trunc(x) __tg_real(trunc, (x)) 276 #define trunc(x) __tg_real(trunc, (x))
269 277
270 #endif 278 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698