Add sysdeps/ieee754/soft-fp.
[glibc.git] / math / tgmath.h
blob12161771b1d4af88474be17c98130da41ee14053
1 /* Copyright (C) 1997-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
19 * ISO C99 Standard: 7.22 Type-generic math <tgmath.h>
22 #ifndef _TGMATH_H
23 #define _TGMATH_H 1
25 #define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION
26 #include <bits/libc-header-start.h>
28 /* Include the needed headers. */
29 #include <bits/floatn.h>
30 #include <math.h>
31 #include <complex.h>
34 /* There are two variant implementations of type-generic macros in
35 this file: one for GCC 8 and later, using __builtin_tgmath and
36 where each macro expands each of its arguments only once, and one
37 for older GCC, using other compiler extensions but with macros
38 expanding their arguments many times (so resulting in exponential
39 blowup of the size of expansions when calls to such macros are
40 nested inside arguments to such macros). */
42 #define __HAVE_BUILTIN_TGMATH __GNUC_PREREQ (8, 0)
44 #if __GNUC_PREREQ (2, 7)
46 # if __HAVE_BUILTIN_TGMATH
48 # if __HAVE_FLOAT16 && __GLIBC_USE (IEC_60559_TYPES_EXT)
49 # define __TG_F16_ARG(X) X ## f16,
50 # else
51 # define __TG_F16_ARG(X)
52 # endif
53 # if __HAVE_FLOAT32 && __GLIBC_USE (IEC_60559_TYPES_EXT)
54 # define __TG_F32_ARG(X) X ## f32,
55 # else
56 # define __TG_F32_ARG(X)
57 # endif
58 # if __HAVE_FLOAT64 && __GLIBC_USE (IEC_60559_TYPES_EXT)
59 # define __TG_F64_ARG(X) X ## f64,
60 # else
61 # define __TG_F64_ARG(X)
62 # endif
63 # if __HAVE_FLOAT128 && __GLIBC_USE (IEC_60559_TYPES_EXT)
64 # define __TG_F128_ARG(X) X ## f128,
65 # else
66 # define __TG_F128_ARG(X)
67 # endif
68 # if __HAVE_FLOAT32X && __GLIBC_USE (IEC_60559_TYPES_EXT)
69 # define __TG_F32X_ARG(X) X ## f32x,
70 # else
71 # define __TG_F32X_ARG(X)
72 # endif
73 # if __HAVE_FLOAT64X && __GLIBC_USE (IEC_60559_TYPES_EXT)
74 # define __TG_F64X_ARG(X) X ## f64x,
75 # else
76 # define __TG_F64X_ARG(X)
77 # endif
78 # if __HAVE_FLOAT128X && __GLIBC_USE (IEC_60559_TYPES_EXT)
79 # define __TG_F128X_ARG(X) X ## f128x,
80 # else
81 # define __TG_F128X_ARG(X)
82 # endif
84 # define __TGMATH_FUNCS(X) X ## f, X, X ## l, \
85 __TG_F16_ARG (X) __TG_F32_ARG (X) __TG_F64_ARG (X) __TG_F128_ARG (X) \
86 __TG_F32X_ARG (X) __TG_F64X_ARG (X) __TG_F128X_ARG (X)
87 # define __TGMATH_RCFUNCS(F, C) __TGMATH_FUNCS (F) __TGMATH_FUNCS (C)
88 # define __TGMATH_1(F, X) __builtin_tgmath (__TGMATH_FUNCS (F) (X))
89 # define __TGMATH_2(F, X, Y) __builtin_tgmath (__TGMATH_FUNCS (F) (X), (Y))
90 # define __TGMATH_2STD(F, X, Y) __builtin_tgmath (F ## f, F, F ## l, (X), (Y))
91 # define __TGMATH_3(F, X, Y, Z) __builtin_tgmath (__TGMATH_FUNCS (F) \
92 (X), (Y), (Z))
93 # define __TGMATH_1C(F, C, X) __builtin_tgmath (__TGMATH_RCFUNCS (F, C) (X))
94 # define __TGMATH_2C(F, C, X, Y) __builtin_tgmath (__TGMATH_RCFUNCS (F, C) \
95 (X), (Y))
97 # else /* !__HAVE_BUILTIN_TGMATH. */
99 # ifdef __NO_LONG_DOUBLE_MATH
100 # define __tgml(fct) fct
101 # else
102 # define __tgml(fct) fct ## l
103 # endif
105 /* __floating_type expands to 1 if TYPE is a floating type (including
106 complex floating types), 0 if TYPE is an integer type (including
107 complex integer types). __real_integer_type expands to 1 if TYPE
108 is a real integer type. __complex_integer_type expands to 1 if
109 TYPE is a complex integer type. All these macros expand to integer
110 constant expressions. All these macros can assume their argument
111 has an arithmetic type (not vector, decimal floating-point or
112 fixed-point), valid to pass to tgmath.h macros. */
113 # if __GNUC_PREREQ (3, 1)
114 /* __builtin_classify_type expands to an integer constant expression
115 in GCC 3.1 and later. Default conversions applied to the argument
116 of __builtin_classify_type mean it always returns 1 for real
117 integer types rather than ever returning different values for
118 character, boolean or enumerated types. */
119 # define __floating_type(type) \
120 (__builtin_classify_type (__real__ ((type) 0)) == 8)
121 # define __real_integer_type(type) \
122 (__builtin_classify_type ((type) 0) == 1)
123 # define __complex_integer_type(type) \
124 (__builtin_classify_type ((type) 0) == 9 \
125 && __builtin_classify_type (__real__ ((type) 0)) == 1)
126 # else
127 /* GCC versions predating __builtin_classify_type are also looser on
128 what counts as an integer constant expression. */
129 # define __floating_type(type) (((type) 1.25) != 1)
130 # define __real_integer_type(type) (((type) (1.25 + _Complex_I)) == 1)
131 # define __complex_integer_type(type) \
132 (((type) (1.25 + _Complex_I)) == (1 + _Complex_I))
133 # endif
135 /* Whether an expression (of arithmetic type) has a real type. */
136 # define __expr_is_real(E) (__builtin_classify_type (E) != 9)
138 /* The tgmath real type for T, where E is 0 if T is an integer type
139 and 1 for a floating type. If T has a complex type, it is
140 unspecified whether the return type is real or complex (but it has
141 the correct corresponding real type). */
142 # define __tgmath_real_type_sub(T, E) \
143 __typeof__ (*(0 ? (__typeof__ (0 ? (double *) 0 : (void *) (E))) 0 \
144 : (__typeof__ (0 ? (T *) 0 : (void *) (!(E)))) 0))
146 /* The tgmath real type of EXPR. */
147 # define __tgmath_real_type(expr) \
148 __tgmath_real_type_sub (__typeof__ ((__typeof__ (+(expr))) 0), \
149 __floating_type (__typeof__ (+(expr))))
151 /* The tgmath complex type for T, where E1 is 1 if T has a floating
152 type and 0 otherwise, E2 is 1 if T has a real integer type and 0
153 otherwise, and E3 is 1 if T has a complex type and 0 otherwise. */
154 # define __tgmath_complex_type_sub(T, E1, E2, E3) \
155 __typeof__ (*(0 \
156 ? (__typeof__ (0 ? (T *) 0 : (void *) (!(E1)))) 0 \
157 : (__typeof__ (0 \
158 ? (__typeof__ (0 \
159 ? (double *) 0 \
160 : (void *) (!(E2)))) 0 \
161 : (__typeof__ (0 \
162 ? (_Complex double *) 0 \
163 : (void *) (!(E3)))) 0)) 0))
165 /* The tgmath complex type of EXPR. */
166 # define __tgmath_complex_type(expr) \
167 __tgmath_complex_type_sub (__typeof__ ((__typeof__ (+(expr))) 0), \
168 __floating_type (__typeof__ (+(expr))), \
169 __real_integer_type (__typeof__ (+(expr))), \
170 __complex_integer_type (__typeof__ (+(expr))))
172 # if (__HAVE_DISTINCT_FLOAT16 \
173 || __HAVE_DISTINCT_FLOAT32 \
174 || __HAVE_DISTINCT_FLOAT64 \
175 || __HAVE_DISTINCT_FLOAT32X \
176 || __HAVE_DISTINCT_FLOAT64X \
177 || __HAVE_DISTINCT_FLOAT128X)
178 # error "Unsupported _FloatN or _FloatNx types for <tgmath.h>."
179 # endif
181 /* Expand to text that checks if ARG_COMB has type _Float128, and if
182 so calls the appropriately suffixed FCT (which may include a cast),
183 or FCT and CFCT for complex functions, with arguments ARG_CALL. */
184 # if __HAVE_DISTINCT_FLOAT128 && __GLIBC_USE (IEC_60559_TYPES_EXT)
185 # if (!__HAVE_FLOAT64X \
186 || __HAVE_FLOAT64X_LONG_DOUBLE \
187 || !__HAVE_FLOATN_NOT_TYPEDEF)
188 # define __TGMATH_F128(arg_comb, fct, arg_call) \
189 __builtin_types_compatible_p (__typeof (+(arg_comb)), _Float128) \
190 ? fct ## f128 arg_call :
191 # define __TGMATH_CF128(arg_comb, fct, cfct, arg_call) \
192 __builtin_types_compatible_p (__typeof (+__real__ (arg_comb)), _Float128) \
193 ? (__expr_is_real (arg_comb) \
194 ? fct ## f128 arg_call \
195 : cfct ## f128 arg_call) :
196 # else
197 /* _Float64x is a distinct type at the C language level, which must be
198 handled like _Float128. */
199 # define __TGMATH_F128(arg_comb, fct, arg_call) \
200 (__builtin_types_compatible_p (__typeof (+(arg_comb)), _Float128) \
201 || __builtin_types_compatible_p (__typeof (+(arg_comb)), _Float64x)) \
202 ? fct ## f128 arg_call :
203 # define __TGMATH_CF128(arg_comb, fct, cfct, arg_call) \
204 (__builtin_types_compatible_p (__typeof (+__real__ (arg_comb)), _Float128) \
205 || __builtin_types_compatible_p (__typeof (+__real__ (arg_comb)), \
206 _Float64x)) \
207 ? (__expr_is_real (arg_comb) \
208 ? fct ## f128 arg_call \
209 : cfct ## f128 arg_call) :
210 # endif
211 # else
212 # define __TGMATH_F128(arg_comb, fct, arg_call) /* Nothing. */
213 # define __TGMATH_CF128(arg_comb, fct, cfct, arg_call) /* Nothing. */
214 # endif
216 # endif /* !__HAVE_BUILTIN_TGMATH. */
218 /* We have two kinds of generic macros: to support functions which are
219 only defined on real valued parameters and those which are defined
220 for complex functions as well. */
221 # if __HAVE_BUILTIN_TGMATH
223 # define __TGMATH_UNARY_REAL_ONLY(Val, Fct) __TGMATH_1 (Fct, (Val))
224 # define __TGMATH_UNARY_REAL_RET_ONLY(Val, Fct) __TGMATH_1 (Fct, (Val))
225 # define __TGMATH_BINARY_FIRST_REAL_ONLY(Val1, Val2, Fct) \
226 __TGMATH_2 (Fct, (Val1), (Val2))
227 # define __TGMATH_BINARY_FIRST_REAL_STD_ONLY(Val1, Val2, Fct) \
228 __TGMATH_2STD (Fct, (Val1), (Val2))
229 # define __TGMATH_BINARY_REAL_ONLY(Val1, Val2, Fct) \
230 __TGMATH_2 (Fct, (Val1), (Val2))
231 # define __TGMATH_BINARY_REAL_STD_ONLY(Val1, Val2, Fct) \
232 __TGMATH_2STD (Fct, (Val1), (Val2))
233 # define __TGMATH_BINARY_REAL_RET_ONLY(Val1, Val2, Fct) \
234 __TGMATH_2 (Fct, (Val1), (Val2))
235 # define __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY(Val1, Val2, Val3, Fct) \
236 __TGMATH_3 (Fct, (Val1), (Val2), (Val3))
237 # define __TGMATH_TERNARY_REAL_ONLY(Val1, Val2, Val3, Fct) \
238 __TGMATH_3 (Fct, (Val1), (Val2), (Val3))
239 # define __TGMATH_TERNARY_FIRST_REAL_RET_ONLY(Val1, Val2, Val3, Fct) \
240 __TGMATH_3 (Fct, (Val1), (Val2), (Val3))
241 # define __TGMATH_UNARY_REAL_IMAG(Val, Fct, Cfct) \
242 __TGMATH_1C (Fct, Cfct, (Val))
243 # define __TGMATH_UNARY_IMAG(Val, Cfct) __TGMATH_1 (Cfct, (Val))
244 # define __TGMATH_UNARY_REAL_IMAG_RET_REAL(Val, Fct, Cfct) \
245 __TGMATH_1C (Fct, Cfct, (Val))
246 # define __TGMATH_UNARY_REAL_IMAG_RET_REAL_SAME(Val, Cfct) \
247 __TGMATH_1 (Cfct, (Val))
248 # define __TGMATH_BINARY_REAL_IMAG(Val1, Val2, Fct, Cfct) \
249 __TGMATH_2C (Fct, Cfct, (Val1), (Val2))
251 # else /* !__HAVE_BUILTIN_TGMATH. */
253 # define __TGMATH_UNARY_REAL_ONLY(Val, Fct) \
254 (__extension__ ((sizeof (+(Val)) == sizeof (double) \
255 || __builtin_classify_type (Val) != 8) \
256 ? (__tgmath_real_type (Val)) Fct (Val) \
257 : (sizeof (+(Val)) == sizeof (float)) \
258 ? (__tgmath_real_type (Val)) Fct##f (Val) \
259 : __TGMATH_F128 ((Val), (__tgmath_real_type (Val)) Fct, \
260 (Val)) \
261 (__tgmath_real_type (Val)) __tgml(Fct) (Val)))
263 # define __TGMATH_UNARY_REAL_RET_ONLY(Val, Fct) \
264 (__extension__ ((sizeof (+(Val)) == sizeof (double) \
265 || __builtin_classify_type (Val) != 8) \
266 ? Fct (Val) \
267 : (sizeof (+(Val)) == sizeof (float)) \
268 ? Fct##f (Val) \
269 : __TGMATH_F128 ((Val), Fct, (Val)) \
270 __tgml(Fct) (Val)))
272 # define __TGMATH_BINARY_FIRST_REAL_ONLY(Val1, Val2, Fct) \
273 (__extension__ ((sizeof (+(Val1)) == sizeof (double) \
274 || __builtin_classify_type (Val1) != 8) \
275 ? (__tgmath_real_type (Val1)) Fct (Val1, Val2) \
276 : (sizeof (+(Val1)) == sizeof (float)) \
277 ? (__tgmath_real_type (Val1)) Fct##f (Val1, Val2) \
278 : __TGMATH_F128 ((Val1), (__tgmath_real_type (Val1)) Fct, \
279 (Val1, Val2)) \
280 (__tgmath_real_type (Val1)) __tgml(Fct) (Val1, Val2)))
282 # define __TGMATH_BINARY_FIRST_REAL_STD_ONLY(Val1, Val2, Fct) \
283 (__extension__ ((sizeof (+(Val1)) == sizeof (double) \
284 || __builtin_classify_type (Val1) != 8) \
285 ? (__tgmath_real_type (Val1)) Fct (Val1, Val2) \
286 : (sizeof (+(Val1)) == sizeof (float)) \
287 ? (__tgmath_real_type (Val1)) Fct##f (Val1, Val2) \
288 : (__tgmath_real_type (Val1)) __tgml(Fct) (Val1, Val2)))
290 # define __TGMATH_BINARY_REAL_ONLY(Val1, Val2, Fct) \
291 (__extension__ ((sizeof ((Val1) + (Val2)) > sizeof (double) \
292 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
293 ? __TGMATH_F128 ((Val1) + (Val2), \
294 (__typeof \
295 ((__tgmath_real_type (Val1)) 0 \
296 + (__tgmath_real_type (Val2)) 0)) Fct, \
297 (Val1, Val2)) \
298 (__typeof ((__tgmath_real_type (Val1)) 0 \
299 + (__tgmath_real_type (Val2)) 0)) \
300 __tgml(Fct) (Val1, Val2) \
301 : (sizeof (+(Val1)) == sizeof (double) \
302 || sizeof (+(Val2)) == sizeof (double) \
303 || __builtin_classify_type (Val1) != 8 \
304 || __builtin_classify_type (Val2) != 8) \
305 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
306 + (__tgmath_real_type (Val2)) 0)) \
307 Fct (Val1, Val2) \
308 : (__typeof ((__tgmath_real_type (Val1)) 0 \
309 + (__tgmath_real_type (Val2)) 0)) \
310 Fct##f (Val1, Val2)))
312 # define __TGMATH_BINARY_REAL_STD_ONLY(Val1, Val2, Fct) \
313 (__extension__ ((sizeof ((Val1) + (Val2)) > sizeof (double) \
314 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
315 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
316 + (__tgmath_real_type (Val2)) 0)) \
317 __tgml(Fct) (Val1, Val2) \
318 : (sizeof (+(Val1)) == sizeof (double) \
319 || sizeof (+(Val2)) == sizeof (double) \
320 || __builtin_classify_type (Val1) != 8 \
321 || __builtin_classify_type (Val2) != 8) \
322 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
323 + (__tgmath_real_type (Val2)) 0)) \
324 Fct (Val1, Val2) \
325 : (__typeof ((__tgmath_real_type (Val1)) 0 \
326 + (__tgmath_real_type (Val2)) 0)) \
327 Fct##f (Val1, Val2)))
329 # define __TGMATH_BINARY_REAL_RET_ONLY(Val1, Val2, Fct) \
330 (__extension__ ((sizeof ((Val1) + (Val2)) > sizeof (double) \
331 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
332 ? __TGMATH_F128 ((Val1) + (Val2), Fct, (Val1, Val2)) \
333 __tgml(Fct) (Val1, Val2) \
334 : (sizeof (+(Val1)) == sizeof (double) \
335 || sizeof (+(Val2)) == sizeof (double) \
336 || __builtin_classify_type (Val1) != 8 \
337 || __builtin_classify_type (Val2) != 8) \
338 ? Fct (Val1, Val2) \
339 : Fct##f (Val1, Val2)))
341 # define __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY(Val1, Val2, Val3, Fct) \
342 (__extension__ ((sizeof ((Val1) + (Val2)) > sizeof (double) \
343 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
344 ? __TGMATH_F128 ((Val1) + (Val2), \
345 (__typeof \
346 ((__tgmath_real_type (Val1)) 0 \
347 + (__tgmath_real_type (Val2)) 0)) Fct, \
348 (Val1, Val2, Val3)) \
349 (__typeof ((__tgmath_real_type (Val1)) 0 \
350 + (__tgmath_real_type (Val2)) 0)) \
351 __tgml(Fct) (Val1, Val2, Val3) \
352 : (sizeof (+(Val1)) == sizeof (double) \
353 || sizeof (+(Val2)) == sizeof (double) \
354 || __builtin_classify_type (Val1) != 8 \
355 || __builtin_classify_type (Val2) != 8) \
356 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
357 + (__tgmath_real_type (Val2)) 0)) \
358 Fct (Val1, Val2, Val3) \
359 : (__typeof ((__tgmath_real_type (Val1)) 0 \
360 + (__tgmath_real_type (Val2)) 0)) \
361 Fct##f (Val1, Val2, Val3)))
363 # define __TGMATH_TERNARY_REAL_ONLY(Val1, Val2, Val3, Fct) \
364 (__extension__ ((sizeof ((Val1) + (Val2) + (Val3)) > sizeof (double) \
365 && __builtin_classify_type ((Val1) + (Val2) + (Val3)) \
366 == 8) \
367 ? __TGMATH_F128 ((Val1) + (Val2) + (Val3), \
368 (__typeof \
369 ((__tgmath_real_type (Val1)) 0 \
370 + (__tgmath_real_type (Val2)) 0 \
371 + (__tgmath_real_type (Val3)) 0)) Fct, \
372 (Val1, Val2, Val3)) \
373 (__typeof ((__tgmath_real_type (Val1)) 0 \
374 + (__tgmath_real_type (Val2)) 0 \
375 + (__tgmath_real_type (Val3)) 0)) \
376 __tgml(Fct) (Val1, Val2, Val3) \
377 : (sizeof (+(Val1)) == sizeof (double) \
378 || sizeof (+(Val2)) == sizeof (double) \
379 || sizeof (+(Val3)) == sizeof (double) \
380 || __builtin_classify_type (Val1) != 8 \
381 || __builtin_classify_type (Val2) != 8 \
382 || __builtin_classify_type (Val3) != 8) \
383 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
384 + (__tgmath_real_type (Val2)) 0 \
385 + (__tgmath_real_type (Val3)) 0)) \
386 Fct (Val1, Val2, Val3) \
387 : (__typeof ((__tgmath_real_type (Val1)) 0 \
388 + (__tgmath_real_type (Val2)) 0 \
389 + (__tgmath_real_type (Val3)) 0)) \
390 Fct##f (Val1, Val2, Val3)))
392 # define __TGMATH_TERNARY_FIRST_REAL_RET_ONLY(Val1, Val2, Val3, Fct) \
393 (__extension__ ((sizeof (+(Val1)) == sizeof (double) \
394 || __builtin_classify_type (Val1) != 8) \
395 ? Fct (Val1, Val2, Val3) \
396 : (sizeof (+(Val1)) == sizeof (float)) \
397 ? Fct##f (Val1, Val2, Val3) \
398 : __TGMATH_F128 ((Val1), Fct, (Val1, Val2, Val3)) \
399 __tgml(Fct) (Val1, Val2, Val3)))
401 /* XXX This definition has to be changed as soon as the compiler understands
402 the imaginary keyword. */
403 # define __TGMATH_UNARY_REAL_IMAG(Val, Fct, Cfct) \
404 (__extension__ ((sizeof (+__real__ (Val)) == sizeof (double) \
405 || __builtin_classify_type (__real__ (Val)) != 8) \
406 ? (__expr_is_real (Val) \
407 ? (__tgmath_complex_type (Val)) Fct (Val) \
408 : (__tgmath_complex_type (Val)) Cfct (Val)) \
409 : (sizeof (+__real__ (Val)) == sizeof (float)) \
410 ? (__expr_is_real (Val) \
411 ? (__tgmath_complex_type (Val)) Fct##f (Val) \
412 : (__tgmath_complex_type (Val)) Cfct##f (Val)) \
413 : __TGMATH_CF128 ((Val), \
414 (__tgmath_complex_type (Val)) Fct, \
415 (__tgmath_complex_type (Val)) Cfct, \
416 (Val)) \
417 (__expr_is_real (Val) \
418 ? (__tgmath_complex_type (Val)) __tgml(Fct) (Val) \
419 : (__tgmath_complex_type (Val)) __tgml(Cfct) (Val))))
421 # define __TGMATH_UNARY_IMAG(Val, Cfct) \
422 (__extension__ ((sizeof (+__real__ (Val)) == sizeof (double) \
423 || __builtin_classify_type (__real__ (Val)) != 8) \
424 ? (__typeof__ ((__tgmath_real_type (Val)) 0 \
425 + _Complex_I)) Cfct (Val) \
426 : (sizeof (+__real__ (Val)) == sizeof (float)) \
427 ? (__typeof__ ((__tgmath_real_type (Val)) 0 \
428 + _Complex_I)) Cfct##f (Val) \
429 : __TGMATH_F128 (__real__ (Val), \
430 (__typeof__ \
431 ((__tgmath_real_type (Val)) 0 \
432 + _Complex_I)) Cfct, (Val)) \
433 (__typeof__ ((__tgmath_real_type (Val)) 0 \
434 + _Complex_I)) __tgml(Cfct) (Val)))
436 /* XXX This definition has to be changed as soon as the compiler understands
437 the imaginary keyword. */
438 # define __TGMATH_UNARY_REAL_IMAG_RET_REAL(Val, Fct, Cfct) \
439 (__extension__ ((sizeof (+__real__ (Val)) == sizeof (double) \
440 || __builtin_classify_type (__real__ (Val)) != 8) \
441 ? (__expr_is_real (Val) \
442 ? (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
443 Fct (Val) \
444 : (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
445 Cfct (Val)) \
446 : (sizeof (+__real__ (Val)) == sizeof (float)) \
447 ? (__expr_is_real (Val) \
448 ? (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
449 Fct##f (Val) \
450 : (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
451 Cfct##f (Val)) \
452 : __TGMATH_CF128 ((Val), \
453 (__typeof__ \
454 (__real__ \
455 (__tgmath_real_type (Val)) 0)) Fct, \
456 (__typeof__ \
457 (__real__ \
458 (__tgmath_real_type (Val)) 0)) Cfct, \
459 (Val)) \
460 (__expr_is_real (Val) \
461 ? (__typeof__ (__real__ (__tgmath_real_type (Val)) 0)) \
462 __tgml(Fct) (Val) \
463 : (__typeof__ (__real__ (__tgmath_real_type (Val)) 0)) \
464 __tgml(Cfct) (Val))))
465 # define __TGMATH_UNARY_REAL_IMAG_RET_REAL_SAME(Val, Cfct) \
466 __TGMATH_UNARY_REAL_IMAG_RET_REAL ((Val), Cfct, Cfct)
468 /* XXX This definition has to be changed as soon as the compiler understands
469 the imaginary keyword. */
470 # define __TGMATH_BINARY_REAL_IMAG(Val1, Val2, Fct, Cfct) \
471 (__extension__ ((sizeof (__real__ (Val1) \
472 + __real__ (Val2)) > sizeof (double) \
473 && __builtin_classify_type (__real__ (Val1) \
474 + __real__ (Val2)) == 8) \
475 ? __TGMATH_CF128 ((Val1) + (Val2), \
476 (__typeof \
477 ((__tgmath_complex_type (Val1)) 0 \
478 + (__tgmath_complex_type (Val2)) 0)) \
479 Fct, \
480 (__typeof \
481 ((__tgmath_complex_type (Val1)) 0 \
482 + (__tgmath_complex_type (Val2)) 0)) \
483 Cfct, \
484 (Val1, Val2)) \
485 (__expr_is_real ((Val1) + (Val2)) \
486 ? (__typeof ((__tgmath_complex_type (Val1)) 0 \
487 + (__tgmath_complex_type (Val2)) 0)) \
488 __tgml(Fct) (Val1, Val2) \
489 : (__typeof ((__tgmath_complex_type (Val1)) 0 \
490 + (__tgmath_complex_type (Val2)) 0)) \
491 __tgml(Cfct) (Val1, Val2)) \
492 : (sizeof (+__real__ (Val1)) == sizeof (double) \
493 || sizeof (+__real__ (Val2)) == sizeof (double) \
494 || __builtin_classify_type (__real__ (Val1)) != 8 \
495 || __builtin_classify_type (__real__ (Val2)) != 8) \
496 ? (__expr_is_real ((Val1) + (Val2)) \
497 ? (__typeof ((__tgmath_complex_type (Val1)) 0 \
498 + (__tgmath_complex_type (Val2)) 0)) \
499 Fct (Val1, Val2) \
500 : (__typeof ((__tgmath_complex_type (Val1)) 0 \
501 + (__tgmath_complex_type (Val2)) 0)) \
502 Cfct (Val1, Val2)) \
503 : (__expr_is_real ((Val1) + (Val2)) \
504 ? (__typeof ((__tgmath_complex_type (Val1)) 0 \
505 + (__tgmath_complex_type (Val2)) 0)) \
506 Fct##f (Val1, Val2) \
507 : (__typeof ((__tgmath_complex_type (Val1)) 0 \
508 + (__tgmath_complex_type (Val2)) 0)) \
509 Cfct##f (Val1, Val2))))
510 # endif /* !__HAVE_BUILTIN_TGMATH. */
511 #else
512 # error "Unsupported compiler; you cannot use <tgmath.h>"
513 #endif
516 /* Unary functions defined for real and complex values. */
519 /* Trigonometric functions. */
521 /* Arc cosine of X. */
522 #define acos(Val) __TGMATH_UNARY_REAL_IMAG (Val, acos, cacos)
523 /* Arc sine of X. */
524 #define asin(Val) __TGMATH_UNARY_REAL_IMAG (Val, asin, casin)
525 /* Arc tangent of X. */
526 #define atan(Val) __TGMATH_UNARY_REAL_IMAG (Val, atan, catan)
527 /* Arc tangent of Y/X. */
528 #define atan2(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, atan2)
530 /* Cosine of X. */
531 #define cos(Val) __TGMATH_UNARY_REAL_IMAG (Val, cos, ccos)
532 /* Sine of X. */
533 #define sin(Val) __TGMATH_UNARY_REAL_IMAG (Val, sin, csin)
534 /* Tangent of X. */
535 #define tan(Val) __TGMATH_UNARY_REAL_IMAG (Val, tan, ctan)
538 /* Hyperbolic functions. */
540 /* Hyperbolic arc cosine of X. */
541 #define acosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, acosh, cacosh)
542 /* Hyperbolic arc sine of X. */
543 #define asinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, asinh, casinh)
544 /* Hyperbolic arc tangent of X. */
545 #define atanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, atanh, catanh)
547 /* Hyperbolic cosine of X. */
548 #define cosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, cosh, ccosh)
549 /* Hyperbolic sine of X. */
550 #define sinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, sinh, csinh)
551 /* Hyperbolic tangent of X. */
552 #define tanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, tanh, ctanh)
555 /* Exponential and logarithmic functions. */
557 /* Exponential function of X. */
558 #define exp(Val) __TGMATH_UNARY_REAL_IMAG (Val, exp, cexp)
560 /* Break VALUE into a normalized fraction and an integral power of 2. */
561 #define frexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, frexp)
563 /* X times (two to the EXP power). */
564 #define ldexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, ldexp)
566 /* Natural logarithm of X. */
567 #define log(Val) __TGMATH_UNARY_REAL_IMAG (Val, log, clog)
569 /* Base-ten logarithm of X. */
570 #ifdef __USE_GNU
571 # define log10(Val) __TGMATH_UNARY_REAL_IMAG (Val, log10, clog10)
572 #else
573 # define log10(Val) __TGMATH_UNARY_REAL_ONLY (Val, log10)
574 #endif
576 /* Return exp(X) - 1. */
577 #define expm1(Val) __TGMATH_UNARY_REAL_ONLY (Val, expm1)
579 /* Return log(1 + X). */
580 #define log1p(Val) __TGMATH_UNARY_REAL_ONLY (Val, log1p)
582 /* Return the base 2 signed integral exponent of X. */
583 #define logb(Val) __TGMATH_UNARY_REAL_ONLY (Val, logb)
585 /* Compute base-2 exponential of X. */
586 #define exp2(Val) __TGMATH_UNARY_REAL_ONLY (Val, exp2)
588 /* Compute base-2 logarithm of X. */
589 #define log2(Val) __TGMATH_UNARY_REAL_ONLY (Val, log2)
592 /* Power functions. */
594 /* Return X to the Y power. */
595 #define pow(Val1, Val2) __TGMATH_BINARY_REAL_IMAG (Val1, Val2, pow, cpow)
597 /* Return the square root of X. */
598 #define sqrt(Val) __TGMATH_UNARY_REAL_IMAG (Val, sqrt, csqrt)
600 /* Return `sqrt(X*X + Y*Y)'. */
601 #define hypot(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, hypot)
603 /* Return the cube root of X. */
604 #define cbrt(Val) __TGMATH_UNARY_REAL_ONLY (Val, cbrt)
607 /* Nearest integer, absolute value, and remainder functions. */
609 /* Smallest integral value not less than X. */
610 #define ceil(Val) __TGMATH_UNARY_REAL_ONLY (Val, ceil)
612 /* Absolute value of X. */
613 #define fabs(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, fabs, cabs)
615 /* Largest integer not greater than X. */
616 #define floor(Val) __TGMATH_UNARY_REAL_ONLY (Val, floor)
618 /* Floating-point modulo remainder of X/Y. */
619 #define fmod(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmod)
621 /* Round X to integral valuein floating-point format using current
622 rounding direction, but do not raise inexact exception. */
623 #define nearbyint(Val) __TGMATH_UNARY_REAL_ONLY (Val, nearbyint)
625 /* Round X to nearest integral value, rounding halfway cases away from
626 zero. */
627 #define round(Val) __TGMATH_UNARY_REAL_ONLY (Val, round)
629 /* Round X to the integral value in floating-point format nearest but
630 not larger in magnitude. */
631 #define trunc(Val) __TGMATH_UNARY_REAL_ONLY (Val, trunc)
633 /* Compute remainder of X and Y and put in *QUO a value with sign of x/y
634 and magnitude congruent `mod 2^n' to the magnitude of the integral
635 quotient x/y, with n >= 3. */
636 #define remquo(Val1, Val2, Val3) \
637 __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY (Val1, Val2, Val3, remquo)
639 /* Round X to nearest integral value according to current rounding
640 direction. */
641 #define lrint(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, lrint)
642 #define llrint(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, llrint)
644 /* Round X to nearest integral value, rounding halfway cases away from
645 zero. */
646 #define lround(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, lround)
647 #define llround(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, llround)
650 /* Return X with its signed changed to Y's. */
651 #define copysign(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, copysign)
653 /* Error and gamma functions. */
654 #define erf(Val) __TGMATH_UNARY_REAL_ONLY (Val, erf)
655 #define erfc(Val) __TGMATH_UNARY_REAL_ONLY (Val, erfc)
656 #define tgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, tgamma)
657 #define lgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, lgamma)
660 /* Return the integer nearest X in the direction of the
661 prevailing rounding mode. */
662 #define rint(Val) __TGMATH_UNARY_REAL_ONLY (Val, rint)
664 #if __GLIBC_USE (IEC_60559_BFP_EXT)
665 /* Return X - epsilon. */
666 # define nextdown(Val) __TGMATH_UNARY_REAL_ONLY (Val, nextdown)
667 /* Return X + epsilon. */
668 # define nextup(Val) __TGMATH_UNARY_REAL_ONLY (Val, nextup)
669 #endif
671 /* Return X + epsilon if X < Y, X - epsilon if X > Y. */
672 #define nextafter(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, nextafter)
673 #define nexttoward(Val1, Val2) \
674 __TGMATH_BINARY_FIRST_REAL_STD_ONLY (Val1, Val2, nexttoward)
676 /* Return the remainder of integer divison X / Y with infinite precision. */
677 #define remainder(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, remainder)
679 /* Return X times (2 to the Nth power). */
680 #ifdef __USE_MISC
681 # define scalb(Val1, Val2) __TGMATH_BINARY_REAL_STD_ONLY (Val1, Val2, scalb)
682 #endif
684 /* Return X times (2 to the Nth power). */
685 #define scalbn(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbn)
687 /* Return X times (2 to the Nth power). */
688 #define scalbln(Val1, Val2) \
689 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbln)
691 /* Return the binary exponent of X, which must be nonzero. */
692 #define ilogb(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, ilogb)
695 /* Return positive difference between X and Y. */
696 #define fdim(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fdim)
698 /* Return maximum numeric value from X and Y. */
699 #define fmax(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmax)
701 /* Return minimum numeric value from X and Y. */
702 #define fmin(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmin)
705 /* Multiply-add function computed as a ternary operation. */
706 #define fma(Val1, Val2, Val3) \
707 __TGMATH_TERNARY_REAL_ONLY (Val1, Val2, Val3, fma)
709 #if __GLIBC_USE (IEC_60559_BFP_EXT)
710 /* Round X to nearest integer value, rounding halfway cases to even. */
711 # define roundeven(Val) __TGMATH_UNARY_REAL_ONLY (Val, roundeven)
713 # define fromfp(Val1, Val2, Val3) \
714 __TGMATH_TERNARY_FIRST_REAL_RET_ONLY (Val1, Val2, Val3, fromfp)
716 # define ufromfp(Val1, Val2, Val3) \
717 __TGMATH_TERNARY_FIRST_REAL_RET_ONLY (Val1, Val2, Val3, ufromfp)
719 # define fromfpx(Val1, Val2, Val3) \
720 __TGMATH_TERNARY_FIRST_REAL_RET_ONLY (Val1, Val2, Val3, fromfpx)
722 # define ufromfpx(Val1, Val2, Val3) \
723 __TGMATH_TERNARY_FIRST_REAL_RET_ONLY (Val1, Val2, Val3, ufromfpx)
725 /* Like ilogb, but returning long int. */
726 # define llogb(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, llogb)
728 /* Return value with maximum magnitude. */
729 # define fmaxmag(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmaxmag)
731 /* Return value with minimum magnitude. */
732 # define fminmag(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fminmag)
734 /* Total order operation. */
735 # define totalorder(Val1, Val2) \
736 __TGMATH_BINARY_REAL_RET_ONLY (Val1, Val2, totalorder)
738 /* Total order operation on absolute values. */
739 # define totalordermag(Val1, Val2) \
740 __TGMATH_BINARY_REAL_RET_ONLY (Val1, Val2, totalordermag)
741 #endif
744 /* Absolute value, conjugates, and projection. */
746 /* Argument value of Z. */
747 #define carg(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL_SAME (Val, carg)
749 /* Complex conjugate of Z. */
750 #define conj(Val) __TGMATH_UNARY_IMAG (Val, conj)
752 /* Projection of Z onto the Riemann sphere. */
753 #define cproj(Val) __TGMATH_UNARY_IMAG (Val, cproj)
756 /* Decomposing complex values. */
758 /* Imaginary part of Z. */
759 #define cimag(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL_SAME (Val, cimag)
761 /* Real part of Z. */
762 #define creal(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL_SAME (Val, creal)
764 #endif /* tgmath.h */