1 /* Copyright (C) 1997-2021 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 <https://www.gnu.org/licenses/>. */
19 * ISO C99 Standard: 7.22 Type-generic math <tgmath.h>
25 #define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION
26 #include <bits/libc-header-start.h>
28 /* Include the needed headers. */
29 #include <bits/floatn.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 /* Certain cases of narrowing macros only need to call a single
47 function so cannot use __builtin_tgmath and do not need any
50 # error "Unsupported _Float128x type for <tgmath.h>."
52 # if ((__HAVE_FLOAT64X && !__HAVE_FLOAT128) \
53 || (__HAVE_FLOAT128 && !__HAVE_FLOAT64X))
54 # error "Unsupported combination of types for <tgmath.h>."
56 # define __TGMATH_2_NARROW_D(F, X, Y) \
58 # define __TGMATH_2_NARROW_F64X(F, X, Y) \
61 # define __TGMATH_2_NARROW_F32X(F, X, Y) \
65 # if __HAVE_BUILTIN_TGMATH
67 # if __HAVE_FLOAT16 && __GLIBC_USE (IEC_60559_TYPES_EXT)
68 # define __TG_F16_ARG(X) X ## f16,
70 # define __TG_F16_ARG(X)
72 # if __HAVE_FLOAT32 && __GLIBC_USE (IEC_60559_TYPES_EXT)
73 # define __TG_F32_ARG(X) X ## f32,
75 # define __TG_F32_ARG(X)
77 # if __HAVE_FLOAT64 && __GLIBC_USE (IEC_60559_TYPES_EXT)
78 # define __TG_F64_ARG(X) X ## f64,
80 # define __TG_F64_ARG(X)
82 # if __HAVE_FLOAT128 && __GLIBC_USE (IEC_60559_TYPES_EXT)
83 # define __TG_F128_ARG(X) X ## f128,
85 # define __TG_F128_ARG(X)
87 # if __HAVE_FLOAT32X && __GLIBC_USE (IEC_60559_TYPES_EXT)
88 # define __TG_F32X_ARG(X) X ## f32x,
90 # define __TG_F32X_ARG(X)
92 # if __HAVE_FLOAT64X && __GLIBC_USE (IEC_60559_TYPES_EXT)
93 # define __TG_F64X_ARG(X) X ## f64x,
95 # define __TG_F64X_ARG(X)
97 # if __HAVE_FLOAT128X && __GLIBC_USE (IEC_60559_TYPES_EXT)
98 # define __TG_F128X_ARG(X) X ## f128x,
100 # define __TG_F128X_ARG(X)
103 # define __TGMATH_FUNCS(X) X ## f, X, X ## l, \
104 __TG_F16_ARG (X) __TG_F32_ARG (X) __TG_F64_ARG (X) __TG_F128_ARG (X) \
105 __TG_F32X_ARG (X) __TG_F64X_ARG (X) __TG_F128X_ARG (X)
106 # define __TGMATH_RCFUNCS(F, C) __TGMATH_FUNCS (F) __TGMATH_FUNCS (C)
107 # define __TGMATH_1(F, X) __builtin_tgmath (__TGMATH_FUNCS (F) (X))
108 # define __TGMATH_2(F, X, Y) __builtin_tgmath (__TGMATH_FUNCS (F) (X), (Y))
109 # define __TGMATH_2STD(F, X, Y) __builtin_tgmath (F ## f, F, F ## l, (X), (Y))
110 # define __TGMATH_3(F, X, Y, Z) __builtin_tgmath (__TGMATH_FUNCS (F) \
112 # define __TGMATH_1C(F, C, X) __builtin_tgmath (__TGMATH_RCFUNCS (F, C) (X))
113 # define __TGMATH_2C(F, C, X, Y) __builtin_tgmath (__TGMATH_RCFUNCS (F, C) \
116 # define __TGMATH_NARROW_FUNCS_F(X) X, X ## l,
117 # define __TGMATH_NARROW_FUNCS_F16(X) \
118 __TG_F32_ARG (X) __TG_F64_ARG (X) __TG_F128_ARG (X) \
119 __TG_F32X_ARG (X) __TG_F64X_ARG (X) __TG_F128X_ARG (X)
120 # define __TGMATH_NARROW_FUNCS_F32(X) \
121 __TG_F64_ARG (X) __TG_F128_ARG (X) \
122 __TG_F32X_ARG (X) __TG_F64X_ARG (X) __TG_F128X_ARG (X)
123 # define __TGMATH_NARROW_FUNCS_F64(X) \
125 __TG_F64X_ARG (X) __TG_F128X_ARG (X)
126 # define __TGMATH_NARROW_FUNCS_F32X(X) \
127 __TG_F64X_ARG (X) __TG_F128X_ARG (X) \
128 __TG_F64_ARG (X) __TG_F128_ARG (X)
130 # define __TGMATH_2_NARROW_F(F, X, Y) \
131 __builtin_tgmath (__TGMATH_NARROW_FUNCS_F (F) (X), (Y))
132 # define __TGMATH_2_NARROW_F16(F, X, Y) \
133 __builtin_tgmath (__TGMATH_NARROW_FUNCS_F16 (F) (X), (Y))
134 # define __TGMATH_2_NARROW_F32(F, X, Y) \
135 __builtin_tgmath (__TGMATH_NARROW_FUNCS_F32 (F) (X), (Y))
136 # define __TGMATH_2_NARROW_F64(F, X, Y) \
137 __builtin_tgmath (__TGMATH_NARROW_FUNCS_F64 (F) (X), (Y))
139 # define __TGMATH_2_NARROW_F32X(F, X, Y) \
140 __builtin_tgmath (__TGMATH_NARROW_FUNCS_F32X (F) (X), (Y))
143 # else /* !__HAVE_BUILTIN_TGMATH. */
145 # ifdef __NO_LONG_DOUBLE_MATH
146 # define __tgml(fct) fct
148 # define __tgml(fct) fct ## l
151 /* __floating_type expands to 1 if TYPE is a floating type (including
152 complex floating types), 0 if TYPE is an integer type (including
153 complex integer types). __real_integer_type expands to 1 if TYPE
154 is a real integer type. __complex_integer_type expands to 1 if
155 TYPE is a complex integer type. All these macros expand to integer
156 constant expressions. All these macros can assume their argument
157 has an arithmetic type (not vector, decimal floating-point or
158 fixed-point), valid to pass to tgmath.h macros. */
159 # if __GNUC_PREREQ (3, 1)
160 /* __builtin_classify_type expands to an integer constant expression
161 in GCC 3.1 and later. Default conversions applied to the argument
162 of __builtin_classify_type mean it always returns 1 for real
163 integer types rather than ever returning different values for
164 character, boolean or enumerated types. */
165 # define __floating_type(type) \
166 (__builtin_classify_type (__real__ ((type) 0)) == 8)
167 # define __real_integer_type(type) \
168 (__builtin_classify_type ((type) 0) == 1)
169 # define __complex_integer_type(type) \
170 (__builtin_classify_type ((type) 0) == 9 \
171 && __builtin_classify_type (__real__ ((type) 0)) == 1)
173 /* GCC versions predating __builtin_classify_type are also looser on
174 what counts as an integer constant expression. */
175 # define __floating_type(type) (((type) 1.25) != 1)
176 # define __real_integer_type(type) (((type) (1.25 + _Complex_I)) == 1)
177 # define __complex_integer_type(type) \
178 (((type) (1.25 + _Complex_I)) == (1 + _Complex_I))
181 /* Whether an expression (of arithmetic type) has a real type. */
182 # define __expr_is_real(E) (__builtin_classify_type (E) != 9)
184 /* The tgmath real type for T, where E is 0 if T is an integer type
185 and 1 for a floating type. If T has a complex type, it is
186 unspecified whether the return type is real or complex (but it has
187 the correct corresponding real type). */
188 # define __tgmath_real_type_sub(T, E) \
189 __typeof__ (*(0 ? (__typeof__ (0 ? (double *) 0 : (void *) (E))) 0 \
190 : (__typeof__ (0 ? (T *) 0 : (void *) (!(E)))) 0))
192 /* The tgmath real type of EXPR. */
193 # define __tgmath_real_type(expr) \
194 __tgmath_real_type_sub (__typeof__ ((__typeof__ (+(expr))) 0), \
195 __floating_type (__typeof__ (+(expr))))
197 /* The tgmath complex type for T, where E1 is 1 if T has a floating
198 type and 0 otherwise, E2 is 1 if T has a real integer type and 0
199 otherwise, and E3 is 1 if T has a complex type and 0 otherwise. */
200 # define __tgmath_complex_type_sub(T, E1, E2, E3) \
202 ? (__typeof__ (0 ? (T *) 0 : (void *) (!(E1)))) 0 \
206 : (void *) (!(E2)))) 0 \
208 ? (_Complex double *) 0 \
209 : (void *) (!(E3)))) 0)) 0))
211 /* The tgmath complex type of EXPR. */
212 # define __tgmath_complex_type(expr) \
213 __tgmath_complex_type_sub (__typeof__ ((__typeof__ (+(expr))) 0), \
214 __floating_type (__typeof__ (+(expr))), \
215 __real_integer_type (__typeof__ (+(expr))), \
216 __complex_integer_type (__typeof__ (+(expr))))
218 # if (__HAVE_DISTINCT_FLOAT16 \
219 || __HAVE_DISTINCT_FLOAT32 \
220 || __HAVE_DISTINCT_FLOAT64 \
221 || __HAVE_DISTINCT_FLOAT32X \
222 || __HAVE_DISTINCT_FLOAT64X \
223 || __HAVE_DISTINCT_FLOAT128X)
224 # error "Unsupported _FloatN or _FloatNx types for <tgmath.h>."
227 /* Expand to text that checks if ARG_COMB has type _Float128, and if
228 so calls the appropriately suffixed FCT (which may include a cast),
229 or FCT and CFCT for complex functions, with arguments ARG_CALL. */
230 # if __HAVE_DISTINCT_FLOAT128 && __GLIBC_USE (IEC_60559_TYPES_EXT)
231 # if (!__HAVE_FLOAT64X \
232 || __HAVE_FLOAT64X_LONG_DOUBLE \
233 || !__HAVE_FLOATN_NOT_TYPEDEF)
234 # define __TGMATH_F128(arg_comb, fct, arg_call) \
235 __builtin_types_compatible_p (__typeof (+(arg_comb)), _Float128) \
236 ? fct ## f128 arg_call :
237 # define __TGMATH_CF128(arg_comb, fct, cfct, arg_call) \
238 __builtin_types_compatible_p (__typeof (+__real__ (arg_comb)), _Float128) \
239 ? (__expr_is_real (arg_comb) \
240 ? fct ## f128 arg_call \
241 : cfct ## f128 arg_call) :
243 /* _Float64x is a distinct type at the C language level, which must be
244 handled like _Float128. */
245 # define __TGMATH_F128(arg_comb, fct, arg_call) \
246 (__builtin_types_compatible_p (__typeof (+(arg_comb)), _Float128) \
247 || __builtin_types_compatible_p (__typeof (+(arg_comb)), _Float64x)) \
248 ? fct ## f128 arg_call :
249 # define __TGMATH_CF128(arg_comb, fct, cfct, arg_call) \
250 (__builtin_types_compatible_p (__typeof (+__real__ (arg_comb)), _Float128) \
251 || __builtin_types_compatible_p (__typeof (+__real__ (arg_comb)), \
253 ? (__expr_is_real (arg_comb) \
254 ? fct ## f128 arg_call \
255 : cfct ## f128 arg_call) :
258 # define __TGMATH_F128(arg_comb, fct, arg_call) /* Nothing. */
259 # define __TGMATH_CF128(arg_comb, fct, cfct, arg_call) /* Nothing. */
262 # endif /* !__HAVE_BUILTIN_TGMATH. */
264 /* We have two kinds of generic macros: to support functions which are
265 only defined on real valued parameters and those which are defined
266 for complex functions as well. */
267 # if __HAVE_BUILTIN_TGMATH
269 # define __TGMATH_UNARY_REAL_ONLY(Val, Fct) __TGMATH_1 (Fct, (Val))
270 # define __TGMATH_UNARY_REAL_RET_ONLY(Val, Fct) __TGMATH_1 (Fct, (Val))
271 # define __TGMATH_BINARY_FIRST_REAL_ONLY(Val1, Val2, Fct) \
272 __TGMATH_2 (Fct, (Val1), (Val2))
273 # define __TGMATH_BINARY_FIRST_REAL_STD_ONLY(Val1, Val2, Fct) \
274 __TGMATH_2STD (Fct, (Val1), (Val2))
275 # define __TGMATH_BINARY_REAL_ONLY(Val1, Val2, Fct) \
276 __TGMATH_2 (Fct, (Val1), (Val2))
277 # define __TGMATH_BINARY_REAL_STD_ONLY(Val1, Val2, Fct) \
278 __TGMATH_2STD (Fct, (Val1), (Val2))
279 # define __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY(Val1, Val2, Val3, Fct) \
280 __TGMATH_3 (Fct, (Val1), (Val2), (Val3))
281 # define __TGMATH_TERNARY_REAL_ONLY(Val1, Val2, Val3, Fct) \
282 __TGMATH_3 (Fct, (Val1), (Val2), (Val3))
283 # define __TGMATH_TERNARY_FIRST_REAL_RET_ONLY(Val1, Val2, Val3, Fct) \
284 __TGMATH_3 (Fct, (Val1), (Val2), (Val3))
285 # define __TGMATH_UNARY_REAL_IMAG(Val, Fct, Cfct) \
286 __TGMATH_1C (Fct, Cfct, (Val))
287 # define __TGMATH_UNARY_IMAG(Val, Cfct) __TGMATH_1 (Cfct, (Val))
288 # define __TGMATH_UNARY_REAL_IMAG_RET_REAL(Val, Fct, Cfct) \
289 __TGMATH_1C (Fct, Cfct, (Val))
290 # define __TGMATH_UNARY_REAL_IMAG_RET_REAL_SAME(Val, Cfct) \
291 __TGMATH_1 (Cfct, (Val))
292 # define __TGMATH_BINARY_REAL_IMAG(Val1, Val2, Fct, Cfct) \
293 __TGMATH_2C (Fct, Cfct, (Val1), (Val2))
295 # else /* !__HAVE_BUILTIN_TGMATH. */
297 # define __TGMATH_UNARY_REAL_ONLY(Val, Fct) \
298 (__extension__ ((sizeof (+(Val)) == sizeof (double) \
299 || __builtin_classify_type (Val) != 8) \
300 ? (__tgmath_real_type (Val)) Fct (Val) \
301 : (sizeof (+(Val)) == sizeof (float)) \
302 ? (__tgmath_real_type (Val)) Fct##f (Val) \
303 : __TGMATH_F128 ((Val), (__tgmath_real_type (Val)) Fct, \
305 (__tgmath_real_type (Val)) __tgml(Fct) (Val)))
307 # define __TGMATH_UNARY_REAL_RET_ONLY(Val, Fct) \
308 (__extension__ ((sizeof (+(Val)) == sizeof (double) \
309 || __builtin_classify_type (Val) != 8) \
311 : (sizeof (+(Val)) == sizeof (float)) \
313 : __TGMATH_F128 ((Val), Fct, (Val)) \
316 # define __TGMATH_BINARY_FIRST_REAL_ONLY(Val1, Val2, Fct) \
317 (__extension__ ((sizeof (+(Val1)) == sizeof (double) \
318 || __builtin_classify_type (Val1) != 8) \
319 ? (__tgmath_real_type (Val1)) Fct (Val1, Val2) \
320 : (sizeof (+(Val1)) == sizeof (float)) \
321 ? (__tgmath_real_type (Val1)) Fct##f (Val1, Val2) \
322 : __TGMATH_F128 ((Val1), (__tgmath_real_type (Val1)) Fct, \
324 (__tgmath_real_type (Val1)) __tgml(Fct) (Val1, Val2)))
326 # define __TGMATH_BINARY_FIRST_REAL_STD_ONLY(Val1, Val2, Fct) \
327 (__extension__ ((sizeof (+(Val1)) == sizeof (double) \
328 || __builtin_classify_type (Val1) != 8) \
329 ? (__tgmath_real_type (Val1)) Fct (Val1, Val2) \
330 : (sizeof (+(Val1)) == sizeof (float)) \
331 ? (__tgmath_real_type (Val1)) Fct##f (Val1, Val2) \
332 : (__tgmath_real_type (Val1)) __tgml(Fct) (Val1, Val2)))
334 # define __TGMATH_BINARY_REAL_ONLY(Val1, Val2, Fct) \
335 (__extension__ ((sizeof ((Val1) + (Val2)) > sizeof (double) \
336 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
337 ? __TGMATH_F128 ((Val1) + (Val2), \
339 ((__tgmath_real_type (Val1)) 0 \
340 + (__tgmath_real_type (Val2)) 0)) Fct, \
342 (__typeof ((__tgmath_real_type (Val1)) 0 \
343 + (__tgmath_real_type (Val2)) 0)) \
344 __tgml(Fct) (Val1, Val2) \
345 : (sizeof (+(Val1)) == sizeof (double) \
346 || sizeof (+(Val2)) == sizeof (double) \
347 || __builtin_classify_type (Val1) != 8 \
348 || __builtin_classify_type (Val2) != 8) \
349 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
350 + (__tgmath_real_type (Val2)) 0)) \
352 : (__typeof ((__tgmath_real_type (Val1)) 0 \
353 + (__tgmath_real_type (Val2)) 0)) \
354 Fct##f (Val1, Val2)))
356 # define __TGMATH_BINARY_REAL_STD_ONLY(Val1, Val2, Fct) \
357 (__extension__ ((sizeof ((Val1) + (Val2)) > sizeof (double) \
358 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
359 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
360 + (__tgmath_real_type (Val2)) 0)) \
361 __tgml(Fct) (Val1, Val2) \
362 : (sizeof (+(Val1)) == sizeof (double) \
363 || sizeof (+(Val2)) == sizeof (double) \
364 || __builtin_classify_type (Val1) != 8 \
365 || __builtin_classify_type (Val2) != 8) \
366 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
367 + (__tgmath_real_type (Val2)) 0)) \
369 : (__typeof ((__tgmath_real_type (Val1)) 0 \
370 + (__tgmath_real_type (Val2)) 0)) \
371 Fct##f (Val1, Val2)))
373 # define __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY(Val1, Val2, Val3, Fct) \
374 (__extension__ ((sizeof ((Val1) + (Val2)) > sizeof (double) \
375 && __builtin_classify_type ((Val1) + (Val2)) == 8) \
376 ? __TGMATH_F128 ((Val1) + (Val2), \
378 ((__tgmath_real_type (Val1)) 0 \
379 + (__tgmath_real_type (Val2)) 0)) Fct, \
380 (Val1, Val2, Val3)) \
381 (__typeof ((__tgmath_real_type (Val1)) 0 \
382 + (__tgmath_real_type (Val2)) 0)) \
383 __tgml(Fct) (Val1, Val2, Val3) \
384 : (sizeof (+(Val1)) == sizeof (double) \
385 || sizeof (+(Val2)) == sizeof (double) \
386 || __builtin_classify_type (Val1) != 8 \
387 || __builtin_classify_type (Val2) != 8) \
388 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
389 + (__tgmath_real_type (Val2)) 0)) \
390 Fct (Val1, Val2, Val3) \
391 : (__typeof ((__tgmath_real_type (Val1)) 0 \
392 + (__tgmath_real_type (Val2)) 0)) \
393 Fct##f (Val1, Val2, Val3)))
395 # define __TGMATH_TERNARY_REAL_ONLY(Val1, Val2, Val3, Fct) \
396 (__extension__ ((sizeof ((Val1) + (Val2) + (Val3)) > sizeof (double) \
397 && __builtin_classify_type ((Val1) + (Val2) + (Val3)) \
399 ? __TGMATH_F128 ((Val1) + (Val2) + (Val3), \
401 ((__tgmath_real_type (Val1)) 0 \
402 + (__tgmath_real_type (Val2)) 0 \
403 + (__tgmath_real_type (Val3)) 0)) Fct, \
404 (Val1, Val2, Val3)) \
405 (__typeof ((__tgmath_real_type (Val1)) 0 \
406 + (__tgmath_real_type (Val2)) 0 \
407 + (__tgmath_real_type (Val3)) 0)) \
408 __tgml(Fct) (Val1, Val2, Val3) \
409 : (sizeof (+(Val1)) == sizeof (double) \
410 || sizeof (+(Val2)) == sizeof (double) \
411 || sizeof (+(Val3)) == sizeof (double) \
412 || __builtin_classify_type (Val1) != 8 \
413 || __builtin_classify_type (Val2) != 8 \
414 || __builtin_classify_type (Val3) != 8) \
415 ? (__typeof ((__tgmath_real_type (Val1)) 0 \
416 + (__tgmath_real_type (Val2)) 0 \
417 + (__tgmath_real_type (Val3)) 0)) \
418 Fct (Val1, Val2, Val3) \
419 : (__typeof ((__tgmath_real_type (Val1)) 0 \
420 + (__tgmath_real_type (Val2)) 0 \
421 + (__tgmath_real_type (Val3)) 0)) \
422 Fct##f (Val1, Val2, Val3)))
424 # define __TGMATH_TERNARY_FIRST_REAL_RET_ONLY(Val1, Val2, Val3, Fct) \
425 (__extension__ ((sizeof (+(Val1)) == sizeof (double) \
426 || __builtin_classify_type (Val1) != 8) \
427 ? Fct (Val1, Val2, Val3) \
428 : (sizeof (+(Val1)) == sizeof (float)) \
429 ? Fct##f (Val1, Val2, Val3) \
430 : __TGMATH_F128 ((Val1), Fct, (Val1, Val2, Val3)) \
431 __tgml(Fct) (Val1, Val2, Val3)))
433 /* XXX This definition has to be changed as soon as the compiler understands
434 the imaginary keyword. */
435 # define __TGMATH_UNARY_REAL_IMAG(Val, Fct, Cfct) \
436 (__extension__ ((sizeof (+__real__ (Val)) == sizeof (double) \
437 || __builtin_classify_type (__real__ (Val)) != 8) \
438 ? (__expr_is_real (Val) \
439 ? (__tgmath_complex_type (Val)) Fct (Val) \
440 : (__tgmath_complex_type (Val)) Cfct (Val)) \
441 : (sizeof (+__real__ (Val)) == sizeof (float)) \
442 ? (__expr_is_real (Val) \
443 ? (__tgmath_complex_type (Val)) Fct##f (Val) \
444 : (__tgmath_complex_type (Val)) Cfct##f (Val)) \
445 : __TGMATH_CF128 ((Val), \
446 (__tgmath_complex_type (Val)) Fct, \
447 (__tgmath_complex_type (Val)) Cfct, \
449 (__expr_is_real (Val) \
450 ? (__tgmath_complex_type (Val)) __tgml(Fct) (Val) \
451 : (__tgmath_complex_type (Val)) __tgml(Cfct) (Val))))
453 # define __TGMATH_UNARY_IMAG(Val, Cfct) \
454 (__extension__ ((sizeof (+__real__ (Val)) == sizeof (double) \
455 || __builtin_classify_type (__real__ (Val)) != 8) \
456 ? (__typeof__ ((__tgmath_real_type (Val)) 0 \
457 + _Complex_I)) Cfct (Val) \
458 : (sizeof (+__real__ (Val)) == sizeof (float)) \
459 ? (__typeof__ ((__tgmath_real_type (Val)) 0 \
460 + _Complex_I)) Cfct##f (Val) \
461 : __TGMATH_F128 (__real__ (Val), \
463 ((__tgmath_real_type (Val)) 0 \
464 + _Complex_I)) Cfct, (Val)) \
465 (__typeof__ ((__tgmath_real_type (Val)) 0 \
466 + _Complex_I)) __tgml(Cfct) (Val)))
468 /* XXX This definition has to be changed as soon as the compiler understands
469 the imaginary keyword. */
470 # define __TGMATH_UNARY_REAL_IMAG_RET_REAL(Val, Fct, Cfct) \
471 (__extension__ ((sizeof (+__real__ (Val)) == sizeof (double) \
472 || __builtin_classify_type (__real__ (Val)) != 8) \
473 ? (__expr_is_real (Val) \
474 ? (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
476 : (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
478 : (sizeof (+__real__ (Val)) == sizeof (float)) \
479 ? (__expr_is_real (Val) \
480 ? (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
482 : (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
484 : __TGMATH_CF128 ((Val), \
487 (__tgmath_real_type (Val)) 0)) Fct, \
490 (__tgmath_real_type (Val)) 0)) Cfct, \
492 (__expr_is_real (Val) \
493 ? (__typeof__ (__real__ (__tgmath_real_type (Val)) 0)) \
495 : (__typeof__ (__real__ (__tgmath_real_type (Val)) 0)) \
496 __tgml(Cfct) (Val))))
497 # define __TGMATH_UNARY_REAL_IMAG_RET_REAL_SAME(Val, Cfct) \
498 __TGMATH_UNARY_REAL_IMAG_RET_REAL ((Val), Cfct, Cfct)
500 /* XXX This definition has to be changed as soon as the compiler understands
501 the imaginary keyword. */
502 # define __TGMATH_BINARY_REAL_IMAG(Val1, Val2, Fct, Cfct) \
503 (__extension__ ((sizeof (__real__ (Val1) \
504 + __real__ (Val2)) > sizeof (double) \
505 && __builtin_classify_type (__real__ (Val1) \
506 + __real__ (Val2)) == 8) \
507 ? __TGMATH_CF128 ((Val1) + (Val2), \
509 ((__tgmath_complex_type (Val1)) 0 \
510 + (__tgmath_complex_type (Val2)) 0)) \
513 ((__tgmath_complex_type (Val1)) 0 \
514 + (__tgmath_complex_type (Val2)) 0)) \
517 (__expr_is_real ((Val1) + (Val2)) \
518 ? (__typeof ((__tgmath_complex_type (Val1)) 0 \
519 + (__tgmath_complex_type (Val2)) 0)) \
520 __tgml(Fct) (Val1, Val2) \
521 : (__typeof ((__tgmath_complex_type (Val1)) 0 \
522 + (__tgmath_complex_type (Val2)) 0)) \
523 __tgml(Cfct) (Val1, Val2)) \
524 : (sizeof (+__real__ (Val1)) == sizeof (double) \
525 || sizeof (+__real__ (Val2)) == sizeof (double) \
526 || __builtin_classify_type (__real__ (Val1)) != 8 \
527 || __builtin_classify_type (__real__ (Val2)) != 8) \
528 ? (__expr_is_real ((Val1) + (Val2)) \
529 ? (__typeof ((__tgmath_complex_type (Val1)) 0 \
530 + (__tgmath_complex_type (Val2)) 0)) \
532 : (__typeof ((__tgmath_complex_type (Val1)) 0 \
533 + (__tgmath_complex_type (Val2)) 0)) \
535 : (__expr_is_real ((Val1) + (Val2)) \
536 ? (__typeof ((__tgmath_complex_type (Val1)) 0 \
537 + (__tgmath_complex_type (Val2)) 0)) \
538 Fct##f (Val1, Val2) \
539 : (__typeof ((__tgmath_complex_type (Val1)) 0 \
540 + (__tgmath_complex_type (Val2)) 0)) \
541 Cfct##f (Val1, Val2))))
543 # define __TGMATH_2_NARROW_F(F, X, Y) \
544 (__extension__ (sizeof ((__tgmath_real_type (X)) 0 \
545 + (__tgmath_real_type (Y)) 0) > sizeof (double) \
548 /* In most cases, these narrowing macro definitions based on sizeof
549 ensure that the function called has the right argument format, as
550 for other <tgmath.h> macros for compilers before GCC 8, but may not
551 have exactly the argument type (among the types with that format)
552 specified in the standard logic.
554 In the case of macros for _Float32x return type, when _Float64x
555 exists, _Float64 arguments should result in the *f64 function being
556 called while _Float32x arguments should result in the *f64x
557 function being called. These cases cannot be distinguished using
558 sizeof (or at all if the types are typedefs rather than different
559 types). However, for these functions it is OK (does not affect the
560 final result) to call a function with any argument format at least
561 as wide as all the floating-point arguments, unless that affects
562 rounding of integer arguments. Integer arguments are considered to
563 have type _Float64, so the *f64 functions are preferred for f32x*
564 macros when no argument has a wider floating-point type. */
565 # if __HAVE_FLOAT64X_LONG_DOUBLE && __HAVE_DISTINCT_FLOAT128
566 # define __TGMATH_2_NARROW_F32(F, X, Y) \
567 (__extension__ (sizeof ((__tgmath_real_type (X)) 0 \
568 + (__tgmath_real_type (Y)) 0) > sizeof (_Float64) \
569 ? __TGMATH_F128 ((X) + (Y), F, (X, Y)) \
572 # define __TGMATH_2_NARROW_F64(F, X, Y) \
573 (__extension__ (sizeof ((__tgmath_real_type (X)) 0 \
574 + (__tgmath_real_type (Y)) 0) > sizeof (_Float64) \
575 ? __TGMATH_F128 ((X) + (Y), F, (X, Y)) \
578 # define __TGMATH_2_NARROW_F32X(F, X, Y) \
579 (__extension__ (sizeof ((__tgmath_real_type (X)) 0 \
580 + (__tgmath_real_type (Y)) 0) > sizeof (_Float64) \
581 ? __TGMATH_F128 ((X) + (Y), F, (X, Y)) \
584 # elif __HAVE_FLOAT128
585 # define __TGMATH_2_NARROW_F32(F, X, Y) \
586 (__extension__ (sizeof ((__tgmath_real_type (X)) 0 \
587 + (__tgmath_real_type (Y)) 0) > sizeof (_Float64) \
590 # define __TGMATH_2_NARROW_F64(F, X, Y) \
592 # define __TGMATH_2_NARROW_F32X(F, X, Y) \
593 (__extension__ (sizeof ((__tgmath_real_type (X)) 0 \
594 + (__tgmath_real_type (Y)) 0) > sizeof (_Float32x) \
598 # define __TGMATH_2_NARROW_F32(F, X, Y) \
601 # endif /* !__HAVE_BUILTIN_TGMATH. */
603 # error "Unsupported compiler; you cannot use <tgmath.h>"
607 /* Unary functions defined for real and complex values. */
610 /* Trigonometric functions. */
612 /* Arc cosine of X. */
613 #define acos(Val) __TGMATH_UNARY_REAL_IMAG (Val, acos, cacos)
615 #define asin(Val) __TGMATH_UNARY_REAL_IMAG (Val, asin, casin)
616 /* Arc tangent of X. */
617 #define atan(Val) __TGMATH_UNARY_REAL_IMAG (Val, atan, catan)
618 /* Arc tangent of Y/X. */
619 #define atan2(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, atan2)
622 #define cos(Val) __TGMATH_UNARY_REAL_IMAG (Val, cos, ccos)
624 #define sin(Val) __TGMATH_UNARY_REAL_IMAG (Val, sin, csin)
626 #define tan(Val) __TGMATH_UNARY_REAL_IMAG (Val, tan, ctan)
629 /* Hyperbolic functions. */
631 /* Hyperbolic arc cosine of X. */
632 #define acosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, acosh, cacosh)
633 /* Hyperbolic arc sine of X. */
634 #define asinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, asinh, casinh)
635 /* Hyperbolic arc tangent of X. */
636 #define atanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, atanh, catanh)
638 /* Hyperbolic cosine of X. */
639 #define cosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, cosh, ccosh)
640 /* Hyperbolic sine of X. */
641 #define sinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, sinh, csinh)
642 /* Hyperbolic tangent of X. */
643 #define tanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, tanh, ctanh)
646 /* Exponential and logarithmic functions. */
648 /* Exponential function of X. */
649 #define exp(Val) __TGMATH_UNARY_REAL_IMAG (Val, exp, cexp)
651 /* Break VALUE into a normalized fraction and an integral power of 2. */
652 #define frexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, frexp)
654 /* X times (two to the EXP power). */
655 #define ldexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, ldexp)
657 /* Natural logarithm of X. */
658 #define log(Val) __TGMATH_UNARY_REAL_IMAG (Val, log, clog)
660 /* Base-ten logarithm of X. */
662 # define log10(Val) __TGMATH_UNARY_REAL_IMAG (Val, log10, clog10)
664 # define log10(Val) __TGMATH_UNARY_REAL_ONLY (Val, log10)
667 /* Return exp(X) - 1. */
668 #define expm1(Val) __TGMATH_UNARY_REAL_ONLY (Val, expm1)
670 /* Return log(1 + X). */
671 #define log1p(Val) __TGMATH_UNARY_REAL_ONLY (Val, log1p)
673 /* Return the base 2 signed integral exponent of X. */
674 #define logb(Val) __TGMATH_UNARY_REAL_ONLY (Val, logb)
676 /* Compute base-2 exponential of X. */
677 #define exp2(Val) __TGMATH_UNARY_REAL_ONLY (Val, exp2)
679 /* Compute base-2 logarithm of X. */
680 #define log2(Val) __TGMATH_UNARY_REAL_ONLY (Val, log2)
683 /* Power functions. */
685 /* Return X to the Y power. */
686 #define pow(Val1, Val2) __TGMATH_BINARY_REAL_IMAG (Val1, Val2, pow, cpow)
688 /* Return the square root of X. */
689 #define sqrt(Val) __TGMATH_UNARY_REAL_IMAG (Val, sqrt, csqrt)
691 /* Return `sqrt(X*X + Y*Y)'. */
692 #define hypot(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, hypot)
694 /* Return the cube root of X. */
695 #define cbrt(Val) __TGMATH_UNARY_REAL_ONLY (Val, cbrt)
698 /* Nearest integer, absolute value, and remainder functions. */
700 /* Smallest integral value not less than X. */
701 #define ceil(Val) __TGMATH_UNARY_REAL_ONLY (Val, ceil)
703 /* Absolute value of X. */
704 #define fabs(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL (Val, fabs, cabs)
706 /* Largest integer not greater than X. */
707 #define floor(Val) __TGMATH_UNARY_REAL_ONLY (Val, floor)
709 /* Floating-point modulo remainder of X/Y. */
710 #define fmod(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmod)
712 /* Round X to integral valuein floating-point format using current
713 rounding direction, but do not raise inexact exception. */
714 #define nearbyint(Val) __TGMATH_UNARY_REAL_ONLY (Val, nearbyint)
716 /* Round X to nearest integral value, rounding halfway cases away from
718 #define round(Val) __TGMATH_UNARY_REAL_ONLY (Val, round)
720 /* Round X to the integral value in floating-point format nearest but
721 not larger in magnitude. */
722 #define trunc(Val) __TGMATH_UNARY_REAL_ONLY (Val, trunc)
724 /* Compute remainder of X and Y and put in *QUO a value with sign of x/y
725 and magnitude congruent `mod 2^n' to the magnitude of the integral
726 quotient x/y, with n >= 3. */
727 #define remquo(Val1, Val2, Val3) \
728 __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY (Val1, Val2, Val3, remquo)
730 /* Round X to nearest integral value according to current rounding
732 #define lrint(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, lrint)
733 #define llrint(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, llrint)
735 /* Round X to nearest integral value, rounding halfway cases away from
737 #define lround(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, lround)
738 #define llround(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, llround)
741 /* Return X with its signed changed to Y's. */
742 #define copysign(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, copysign)
744 /* Error and gamma functions. */
745 #define erf(Val) __TGMATH_UNARY_REAL_ONLY (Val, erf)
746 #define erfc(Val) __TGMATH_UNARY_REAL_ONLY (Val, erfc)
747 #define tgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, tgamma)
748 #define lgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, lgamma)
751 /* Return the integer nearest X in the direction of the
752 prevailing rounding mode. */
753 #define rint(Val) __TGMATH_UNARY_REAL_ONLY (Val, rint)
755 #if __GLIBC_USE (IEC_60559_BFP_EXT_C2X)
756 /* Return X - epsilon. */
757 # define nextdown(Val) __TGMATH_UNARY_REAL_ONLY (Val, nextdown)
758 /* Return X + epsilon. */
759 # define nextup(Val) __TGMATH_UNARY_REAL_ONLY (Val, nextup)
762 /* Return X + epsilon if X < Y, X - epsilon if X > Y. */
763 #define nextafter(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, nextafter)
764 #define nexttoward(Val1, Val2) \
765 __TGMATH_BINARY_FIRST_REAL_STD_ONLY (Val1, Val2, nexttoward)
767 /* Return the remainder of integer divison X / Y with infinite precision. */
768 #define remainder(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, remainder)
770 /* Return X times (2 to the Nth power). */
772 # define scalb(Val1, Val2) __TGMATH_BINARY_REAL_STD_ONLY (Val1, Val2, scalb)
775 /* Return X times (2 to the Nth power). */
776 #define scalbn(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbn)
778 /* Return X times (2 to the Nth power). */
779 #define scalbln(Val1, Val2) \
780 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbln)
782 /* Return the binary exponent of X, which must be nonzero. */
783 #define ilogb(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, ilogb)
786 /* Return positive difference between X and Y. */
787 #define fdim(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fdim)
789 /* Return maximum numeric value from X and Y. */
790 #define fmax(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmax)
792 /* Return minimum numeric value from X and Y. */
793 #define fmin(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmin)
796 /* Multiply-add function computed as a ternary operation. */
797 #define fma(Val1, Val2, Val3) \
798 __TGMATH_TERNARY_REAL_ONLY (Val1, Val2, Val3, fma)
800 #if __GLIBC_USE (IEC_60559_BFP_EXT_C2X)
801 /* Round X to nearest integer value, rounding halfway cases to even. */
802 # define roundeven(Val) __TGMATH_UNARY_REAL_ONLY (Val, roundeven)
804 # define fromfp(Val1, Val2, Val3) \
805 __TGMATH_TERNARY_FIRST_REAL_RET_ONLY (Val1, Val2, Val3, fromfp)
807 # define ufromfp(Val1, Val2, Val3) \
808 __TGMATH_TERNARY_FIRST_REAL_RET_ONLY (Val1, Val2, Val3, ufromfp)
810 # define fromfpx(Val1, Val2, Val3) \
811 __TGMATH_TERNARY_FIRST_REAL_RET_ONLY (Val1, Val2, Val3, fromfpx)
813 # define ufromfpx(Val1, Val2, Val3) \
814 __TGMATH_TERNARY_FIRST_REAL_RET_ONLY (Val1, Val2, Val3, ufromfpx)
816 /* Like ilogb, but returning long int. */
817 # define llogb(Val) __TGMATH_UNARY_REAL_RET_ONLY (Val, llogb)
819 /* Return value with maximum magnitude. */
820 # define fmaxmag(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmaxmag)
822 /* Return value with minimum magnitude. */
823 # define fminmag(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fminmag)
827 /* Absolute value, conjugates, and projection. */
829 /* Argument value of Z. */
830 #define carg(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL_SAME (Val, carg)
832 /* Complex conjugate of Z. */
833 #define conj(Val) __TGMATH_UNARY_IMAG (Val, conj)
835 /* Projection of Z onto the Riemann sphere. */
836 #define cproj(Val) __TGMATH_UNARY_IMAG (Val, cproj)
839 /* Decomposing complex values. */
841 /* Imaginary part of Z. */
842 #define cimag(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL_SAME (Val, cimag)
844 /* Real part of Z. */
845 #define creal(Val) __TGMATH_UNARY_REAL_IMAG_RET_REAL_SAME (Val, creal)
848 /* Narrowing functions. */
850 #if __GLIBC_USE (IEC_60559_BFP_EXT_C2X)
853 # define fadd(Val1, Val2) __TGMATH_2_NARROW_F (fadd, Val1, Val2)
854 # define dadd(Val1, Val2) __TGMATH_2_NARROW_D (dadd, Val1, Val2)
857 # define fdiv(Val1, Val2) __TGMATH_2_NARROW_F (fdiv, Val1, Val2)
858 # define ddiv(Val1, Val2) __TGMATH_2_NARROW_D (ddiv, Val1, Val2)
861 # define fmul(Val1, Val2) __TGMATH_2_NARROW_F (fmul, Val1, Val2)
862 # define dmul(Val1, Val2) __TGMATH_2_NARROW_D (dmul, Val1, Val2)
865 # define fsub(Val1, Val2) __TGMATH_2_NARROW_F (fsub, Val1, Val2)
866 # define dsub(Val1, Val2) __TGMATH_2_NARROW_D (dsub, Val1, Val2)
870 #if __GLIBC_USE (IEC_60559_TYPES_EXT)
873 # define f16add(Val1, Val2) __TGMATH_2_NARROW_F16 (f16add, Val1, Val2)
874 # define f16div(Val1, Val2) __TGMATH_2_NARROW_F16 (f16div, Val1, Val2)
875 # define f16mul(Val1, Val2) __TGMATH_2_NARROW_F16 (f16mul, Val1, Val2)
876 # define f16sub(Val1, Val2) __TGMATH_2_NARROW_F16 (f16sub, Val1, Val2)
880 # define f32add(Val1, Val2) __TGMATH_2_NARROW_F32 (f32add, Val1, Val2)
881 # define f32div(Val1, Val2) __TGMATH_2_NARROW_F32 (f32div, Val1, Val2)
882 # define f32mul(Val1, Val2) __TGMATH_2_NARROW_F32 (f32mul, Val1, Val2)
883 # define f32sub(Val1, Val2) __TGMATH_2_NARROW_F32 (f32sub, Val1, Val2)
886 # if __HAVE_FLOAT64 && (__HAVE_FLOAT64X || __HAVE_FLOAT128)
887 # define f64add(Val1, Val2) __TGMATH_2_NARROW_F64 (f64add, Val1, Val2)
888 # define f64div(Val1, Val2) __TGMATH_2_NARROW_F64 (f64div, Val1, Val2)
889 # define f64mul(Val1, Val2) __TGMATH_2_NARROW_F64 (f64mul, Val1, Val2)
890 # define f64sub(Val1, Val2) __TGMATH_2_NARROW_F64 (f64sub, Val1, Val2)
894 # define f32xadd(Val1, Val2) __TGMATH_2_NARROW_F32X (f32xadd, Val1, Val2)
895 # define f32xdiv(Val1, Val2) __TGMATH_2_NARROW_F32X (f32xdiv, Val1, Val2)
896 # define f32xmul(Val1, Val2) __TGMATH_2_NARROW_F32X (f32xmul, Val1, Val2)
897 # define f32xsub(Val1, Val2) __TGMATH_2_NARROW_F32X (f32xsub, Val1, Val2)
900 # if __HAVE_FLOAT64X && (__HAVE_FLOAT128X || __HAVE_FLOAT128)
901 # define f64xadd(Val1, Val2) __TGMATH_2_NARROW_F64X (f64xadd, Val1, Val2)
902 # define f64xdiv(Val1, Val2) __TGMATH_2_NARROW_F64X (f64xdiv, Val1, Val2)
903 # define f64xmul(Val1, Val2) __TGMATH_2_NARROW_F64X (f64xmul, Val1, Val2)
904 # define f64xsub(Val1, Val2) __TGMATH_2_NARROW_F64X (f64xsub, Val1, Val2)
909 #endif /* tgmath.h */