2 # Generate tests for <tgmath.h> macros.
3 # Copyright (C) 2017 Free Software Foundation, Inc.
4 # This file is part of the GNU C Library.
6 # The GNU C Library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
11 # The GNU C Library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with the GNU C Library; if not, see
18 # <http://www.gnu.org/licenses/>.
20 # As glibc does not support decimal floating point, the types to
21 # consider for generic parameters are standard and binary
22 # floating-point types, and integer types which are treated as double.
23 # The corresponding complex types may also be used (including complex
24 # integer types, which are a GNU extension, but are currently disabled
25 # here because they do not work properly with tgmath.h).
27 # The proposed resolution to TS 18661-1 DR#9
28 # <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2149.htm#dr_9>
29 # makes the <tgmath.h> rules for selecting a function to call
30 # correspond to the usual arithmetic conversions (applied successively
31 # to the arguments for generic parameters in order), which choose the
32 # type whose set of values contains that of the other type (undefined
33 # behavior if neither type's set of values is a superset of the
34 # other), with interchange types being preferred to standard types
35 # (long double, double, float), being preferred to extended types
36 # (_Float128x, _Float64x, _Float32x).
38 # For the standard and binary floating-point types supported by GCC 7
39 # on any platform, this means the resulting type is the last of the
40 # given types in one of the following orders, or undefined behavior if
41 # types with both ibm128 and binary128 representation are specified.
43 # If double = long double: _Float16, float, _Float32, _Float32x,
44 # double, long double, _Float64, _Float64x, _Float128.
46 # Otherwise: _Float16, float, _Float32, _Float32x, double, _Float64,
47 # _Float64x, long double, _Float128.
49 # We generate tests to verify the return type is exactly as expected.
50 # We also verify that the function called is real or complex as
51 # expected, and that it is called for the right floating-point format
52 # (but it is OK to call a double function instead of a long double one
53 # if they have the same format, for example). For all the formats
54 # supported on any given configuration of glibc, the MANT_DIG value
55 # uniquely determines the format.
60 """A type that may be used as an argument for generic parameters."""
62 # All possible argument or result types.
65 argument_types_list
= []
66 # All real argument types.
67 real_argument_types_list
= []
68 # Real argument types that correspond to a standard floating type
69 # (float, double or long double; not _FloatN or _FloatNx).
70 standard_real_argument_types_list
= []
71 # The real floating types by their order properties (which are
72 # tuples giving the positions in both the possible orders above).
76 # The type _Complex double.
77 complex_double_type
= None
83 def __init__(self
, name
, suffix
=None, mant_dig
=None, condition
='1',
84 order
=None, integer
=False, complex=False, real_type
=None):
85 """Initialize a Type object, creating any corresponding complex type
89 self
.mant_dig
= mant_dig
90 self
.condition
= condition
92 self
.integer
= integer
93 self
.complex = complex
95 self
.complex_type
= self
96 self
.real_type
= real_type
98 # complex_type filled in by the caller once created.
99 self
.complex_type
= None
100 self
.real_type
= self
102 def register_type(self
, internal
):
103 """Record a type in the lists of all types."""
104 Type
.all_types_list
.append(self
)
106 Type
.argument_types_list
.append(self
)
108 Type
.real_argument_types_list
.append(self
)
109 if not self
.name
.startswith('_Float'):
110 Type
.standard_real_argument_types_list
.append(self
)
111 if self
.order
is not None:
112 Type
.real_types_order
[self
.order
] = self
113 if self
.name
== 'double':
114 Type
.double_type
= self
115 if self
.name
== '_Complex double':
116 Type
.complex_double_type
= self
117 if self
.name
== '_Float64':
118 Type
.float64_type
= self
119 if self
.name
== '_Float64x':
120 Type
.float64x_type
= self
123 def create_type(name
, suffix
=None, mant_dig
=None, condition
='1', order
=None,
124 integer
=False, complex_name
=None, complex_ok
=True,
126 """Create and register a Type object for a real type, creating any
127 corresponding complex type in the process."""
128 real_type
= Type(name
, suffix
=suffix
, mant_dig
=mant_dig
,
129 condition
=condition
, order
=order
, integer
=integer
,
132 if complex_name
is None:
133 complex_name
= '_Complex %s' % name
134 complex_type
= Type(complex_name
, condition
=condition
,
135 integer
=integer
, complex=True,
139 real_type
.complex_type
= complex_type
140 real_type
.register_type(internal
)
141 if complex_type
is not None:
142 complex_type
.register_type(internal
)
144 def floating_type(self
):
145 """Return the corresponding floating type."""
147 return (Type
.complex_double_type
149 else Type
.double_type
)
153 def real_floating_type(self
):
154 """Return the corresponding real floating type."""
155 return self
.real_type
.floating_type()
158 """Return string representation of a type."""
163 """Initialize all the known types."""
164 Type
.create_type('_Float16', 'f16', 'FLT16_MANT_DIG',
165 complex_name
='__CFLOAT16',
166 condition
='defined HUGE_VAL_F16', order
=(0, 0))
167 Type
.create_type('float', 'f', 'FLT_MANT_DIG', order
=(1, 1))
168 Type
.create_type('_Float32', 'f32', 'FLT32_MANT_DIG',
169 complex_name
='__CFLOAT32',
170 condition
='defined HUGE_VAL_F32', order
=(2, 2))
171 Type
.create_type('_Float32x', 'f32x', 'FLT32X_MANT_DIG',
172 complex_name
='__CFLOAT32X',
173 condition
='defined HUGE_VAL_F32X', order
=(3, 3))
174 Type
.create_type('double', '', 'DBL_MANT_DIG', order
=(4, 4))
175 Type
.create_type('long double', 'l', 'LDBL_MANT_DIG', order
=(5, 7))
176 Type
.create_type('_Float64', 'f64', 'FLT64_MANT_DIG',
177 complex_name
='__CFLOAT64',
178 condition
='defined HUGE_VAL_F64', order
=(6, 5))
179 Type
.create_type('_Float64x', 'f64x', 'FLT64X_MANT_DIG',
180 complex_name
='__CFLOAT64X',
181 condition
='defined HUGE_VAL_F64X', order
=(7, 6))
182 Type
.create_type('_Float128', 'f128', 'FLT128_MANT_DIG',
183 complex_name
='__CFLOAT128',
184 condition
='defined HUGE_VAL_F128', order
=(8, 8))
185 Type
.create_type('char', integer
=True)
186 Type
.create_type('signed char', integer
=True)
187 Type
.create_type('unsigned char', integer
=True)
188 Type
.create_type('short int', integer
=True)
189 Type
.create_type('unsigned short int', integer
=True)
190 Type
.create_type('int', integer
=True)
191 Type
.create_type('unsigned int', integer
=True)
192 Type
.create_type('long int', integer
=True)
193 Type
.create_type('unsigned long int', integer
=True)
194 Type
.create_type('long long int', integer
=True)
195 Type
.create_type('unsigned long long int', integer
=True)
196 Type
.create_type('__int128', integer
=True,
197 condition
='defined __SIZEOF_INT128__')
198 Type
.create_type('unsigned __int128', integer
=True,
199 condition
='defined __SIZEOF_INT128__')
200 Type
.create_type('enum e', integer
=True, complex_ok
=False)
201 Type
.create_type('_Bool', integer
=True, complex_ok
=False)
202 Type
.create_type('bit_field', integer
=True, complex_ok
=False)
203 # Internal types represent the combination of long double with
204 # _Float64 or _Float64x, for which the ordering depends on
205 # whether long double has the same format as double.
206 Type
.create_type('long_double_Float64', None, 'LDBL_MANT_DIG',
207 complex_name
='complex_long_double_Float64',
208 condition
='defined HUGE_VAL_F64', order
=(6, 7),
210 Type
.create_type('long_double_Float64x', None, 'FLT64X_MANT_DIG',
211 complex_name
='complex_long_double_Float64x',
212 condition
='defined HUGE_VAL_F64X', order
=(7, 7),
216 def can_combine_types(types
):
217 """Return a C preprocessor conditional for whether the given list of
218 types can be used together as type-generic macro arguments."""
219 have_long_double
= False
220 have_float128
= False
222 t
= t
.real_floating_type()
223 if t
.name
== 'long double':
224 have_long_double
= True
225 if t
.name
== '_Float128' or t
.name
== '_Float64x':
227 if have_long_double
and have_float128
:
228 # If ibm128 format is in use for long double, both
229 # _Float64x and _Float128 are binary128 and the types
230 # cannot be combined.
231 return '(LDBL_MANT_DIG != 106)'
235 def combine_types(types
):
236 """Return the result of combining a set of types."""
242 t
= t
.real_floating_type()
246 order
= (max(combined
.order
[0], t
.order
[0]),
247 max(combined
.order
[1], t
.order
[1]))
248 combined
= Type
.real_types_order
[order
]
249 return combined
.complex_type
if have_complex
else combined
251 def list_product_initial(initial
, lists
):
252 """Return a list of lists, with an initial sequence from the first
253 argument (a list of lists) followed by each sequence of one
254 element from each successive element of the second argument."""
257 return list_product_initial([a
+ [b
] for a
in initial
for b
in lists
[0]],
260 def list_product(lists
):
261 """Return a list of lists, with each sequence of one element from each
262 successive element of the argument."""
263 return list_product_initial([[]], lists
)
266 trans_id
= str.maketrans(' *', '_p')
267 except AttributeError:
268 trans_id
= string
.maketrans(' *', '_p')
269 def var_for_type(name
):
270 """Return the name of a variable with a given type (name)."""
271 return 'var_%s' % name
.translate(trans_id
)
273 def vol_var_for_type(name
):
274 """Return the name of a variable with a given volatile type (name)."""
275 return 'vol_var_%s' % name
.translate(trans_id
)
277 def define_vars_for_type(name
):
278 """Return the definitions of variables with a given type (name)."""
279 if name
== 'bit_field':
280 struct_vars
= define_vars_for_type('struct s');
281 return '%s#define %s %s.bf\n' % (struct_vars
,
282 vol_var_for_type(name
),
283 vol_var_for_type('struct s'))
284 return ('%s %s __attribute__ ((unused));\n'
285 '%s volatile %s __attribute__ ((unused));\n'
286 % (name
, var_for_type(name
), name
, vol_var_for_type(name
)))
288 def if_cond_text(conds
, text
):
289 """Return the result of making some text conditional under #if. The
290 text ends with a newline, as does the return value if not empty."""
293 conds
= [c
for c
in conds
if c
!= '1']
294 conds
= sorted(set(conds
))
297 return '#if %s\n%s#endif\n' % (' && '.join(conds
), text
)
300 """The state associated with testcase generation."""
303 """Initialize a Tests object."""
304 self
.header_list
= ['#define __STDC_WANT_IEC_60559_TYPES_EXT__\n'
305 '#include <float.h>\n'
306 '#include <stdbool.h>\n'
307 '#include <stdint.h>\n'
308 '#include <stdio.h>\n'
309 '#include <string.h>\n'
310 '#include <tgmath.h>\n'
314 ' void (*func) (void);\n'
315 ' const char *func_name;\n'
316 ' const char *test_name;\n'
319 'int num_pass, num_fail;\n'
320 'volatile int called_mant_dig;\n'
321 'const char *volatile called_func_name;\n'
327 float64_text
= ('# if LDBL_MANT_DIG == DBL_MANT_DIG\n'
328 'typedef _Float64 long_double_Float64;\n'
329 'typedef __CFLOAT64 complex_long_double_Float64;\n'
331 'typedef long double long_double_Float64;\n'
332 'typedef _Complex long double '
333 'complex_long_double_Float64;\n'
335 float64_text
= if_cond_text([Type
.float64_type
.condition
],
337 float64x_text
= ('# if LDBL_MANT_DIG == DBL_MANT_DIG\n'
338 'typedef _Float64x long_double_Float64x;\n'
339 'typedef __CFLOAT64X complex_long_double_Float64x;\n'
341 'typedef long double long_double_Float64x;\n'
342 'typedef _Complex long double '
343 'complex_long_double_Float64x;\n'
345 float64x_text
= if_cond_text([Type
.float64x_type
.condition
],
347 self
.header_list
.append(float64_text
)
348 self
.header_list
.append(float64x_text
)
349 self
.types_seen
= set()
350 for t
in Type
.all_types_list
:
351 self
.add_type_var(t
.name
, t
.condition
)
352 self
.test_text_list
= []
353 self
.test_array_list
= []
355 def add_type_var(self
, name
, cond
):
356 """Add declarations of variables for a type."""
357 if name
in self
.types_seen
:
359 t_vars
= define_vars_for_type(name
)
360 self
.header_list
.append(if_cond_text([cond
], t_vars
))
361 self
.types_seen
.add(name
)
363 def add_tests(self
, macro
, ret
, args
, complex_func
=None):
364 """Add tests for a given tgmath.h macro."""
365 # 'c' means the function argument or return type is
366 # type-generic and complex only (a complex function argument
367 # may still have a real macro argument). 'g' means it is
368 # type-generic and may be real or complex; 'r' means it is
369 # type-generic and may only be real; 's' means the same as
370 # 'r', but restricted to float, double and long double.
373 if ret
== 'c' or 'c' in args
:
378 elif ret
== 'g' or 'g' in args
:
381 if complex_func
== None:
382 complex_func
= 'c%s' % func
385 if t
!= 'c' and t
!= 'g' and t
!= 'r' and t
!= 's':
386 self
.add_type_var(t
, '1')
387 for t
in Type
.argument_types_list
:
390 if t
.complex and not have_complex
:
392 if func
== None and not t
.complex:
394 if ret
== 's' and t
.name
.startswith('_Float'):
397 ret_name
= t
.complex_type
.name
400 elif ret
== 'r' or ret
== 's':
401 ret_name
= t
.real_type
.name
404 dummy_func_name
= complex_func
if t
.complex else func
409 arg_name
= t
.complex_type
.name
412 elif a
== 'r' or a
== 's':
413 arg_name
= t
.real_type
.name
416 arg_list
.append('%s arg%d __attribute__ ((unused))'
417 % (arg_name
, arg_num
))
422 ' called_mant_dig = %s;\n'
423 ' called_func_name = "%s";\n'
425 '}\n' % (ret_name
, dummy_func_name
,
426 t
.real_type
.suffix
, ', '.join(arg_list
),
427 t
.real_type
.mant_dig
, dummy_func_name
))
428 dummy_func
= if_cond_text([t
.condition
], dummy_func
)
429 self
.test_text_list
.append(dummy_func
)
432 if t
== 'g' or t
== 'c':
433 arg_types
.append(Type
.argument_types_list
)
435 arg_types
.append(Type
.real_argument_types_list
)
437 arg_types
.append(Type
.standard_real_argument_types_list
)
438 arg_types_product
= list_product(arg_types
)
440 for this_args
in arg_types_product
:
441 comb_type
= Type
.combine_types(this_args
)
442 can_comb
= Type
.can_combine_types(this_args
)
443 all_conds
= [t
.condition
for t
in this_args
]
444 all_conds
.append(can_comb
)
445 any_complex
= func
== None
449 func_name
= complex_func
if any_complex
else func
450 test_name
= '%s (%s)' % (macro
,
451 ', '.join([t
.name
for t
in this_args
]))
452 test_func_name
= 'test_%s_%d' % (macro
, test_num
)
454 mant_dig
= comb_type
.real_type
.mant_dig
455 test_text
= '%s, "%s", "%s", %s' % (test_func_name
, func_name
,
457 test_text
= ' { %s },\n' % test_text
458 test_text
= if_cond_text(all_conds
, test_text
)
459 self
.test_array_list
.append(test_text
)
463 if t
== 'g' or t
== 'c' or t
== 'r' or t
== 's':
464 type = this_args
[call_arg_pos
].name
468 call_args
.append(vol_var_for_type(type))
469 call_args_text
= ', '.join(call_args
)
471 ret_type
= comb_type
.name
472 elif ret
== 'r' or ret
== 's':
473 ret_type
= comb_type
.real_type
.name
475 ret_type
= comb_type
.complex_type
.name
478 call_text
= '%s (%s)' % (macro
, call_args_text
)
479 test_func_text
= ('static void\n'
482 ' extern typeof (%s) %s '
483 '__attribute__ ((unused));\n'
485 '}\n' % (test_func_name
, call_text
,
486 var_for_type(ret_type
),
487 vol_var_for_type(ret_type
), call_text
))
488 test_func_text
= if_cond_text(all_conds
, test_func_text
)
489 self
.test_text_list
.append(test_func_text
)
491 def add_all_tests(self
):
492 """Add tests for all tgmath.h macros."""
493 # C99/C11 real-only functions.
494 self
.add_tests('atan2', 'r', ['r', 'r'])
495 self
.add_tests('cbrt', 'r', ['r'])
496 self
.add_tests('ceil', 'r', ['r'])
497 self
.add_tests('copysign', 'r', ['r', 'r'])
498 self
.add_tests('erf', 'r', ['r'])
499 self
.add_tests('erfc', 'r', ['r'])
500 self
.add_tests('exp2', 'r', ['r'])
501 self
.add_tests('expm1', 'r', ['r'])
502 self
.add_tests('fdim', 'r', ['r', 'r'])
503 self
.add_tests('floor', 'r', ['r'])
504 self
.add_tests('fma', 'r', ['r', 'r', 'r'])
505 self
.add_tests('fmax', 'r', ['r', 'r'])
506 self
.add_tests('fmin', 'r', ['r', 'r'])
507 self
.add_tests('fmod', 'r', ['r', 'r'])
508 self
.add_tests('frexp', 'r', ['r', 'int *'])
509 self
.add_tests('hypot', 'r', ['r', 'r'])
510 self
.add_tests('ilogb', 'int', ['r'])
511 self
.add_tests('ldexp', 'r', ['r', 'int'])
512 self
.add_tests('lgamma', 'r', ['r'])
513 self
.add_tests('llrint', 'long long int', ['r'])
514 self
.add_tests('llround', 'long long int', ['r'])
515 # log10 is real-only in ISO C, but supports complex arguments
516 # as a GNU extension.
517 self
.add_tests('log10', 'g', ['g'])
518 self
.add_tests('log1p', 'r', ['r'])
519 self
.add_tests('log2', 'r', ['r'])
520 self
.add_tests('logb', 'r', ['r'])
521 self
.add_tests('lrint', 'long int', ['r'])
522 self
.add_tests('lround', 'long int', ['r'])
523 self
.add_tests('nearbyint', 'r', ['r'])
524 self
.add_tests('nextafter', 'r', ['r', 'r'])
525 self
.add_tests('nexttoward', 's', ['s', 'long double'])
526 self
.add_tests('remainder', 'r', ['r', 'r'])
527 self
.add_tests('remquo', 'r', ['r', 'r', 'int *'])
528 self
.add_tests('rint', 'r', ['r'])
529 self
.add_tests('round', 'r', ['r'])
530 self
.add_tests('scalbn', 'r', ['r', 'int'])
531 self
.add_tests('scalbln', 'r', ['r', 'long int'])
532 self
.add_tests('tgamma', 'r', ['r'])
533 self
.add_tests('trunc', 'r', ['r'])
534 # C99/C11 real-and-complex functions.
535 self
.add_tests('acos', 'g', ['g'])
536 self
.add_tests('asin', 'g', ['g'])
537 self
.add_tests('atan', 'g', ['g'])
538 self
.add_tests('acosh', 'g', ['g'])
539 self
.add_tests('asinh', 'g', ['g'])
540 self
.add_tests('atanh', 'g', ['g'])
541 self
.add_tests('cos', 'g', ['g'])
542 self
.add_tests('sin', 'g', ['g'])
543 self
.add_tests('tan', 'g', ['g'])
544 self
.add_tests('cosh', 'g', ['g'])
545 self
.add_tests('sinh', 'g', ['g'])
546 self
.add_tests('tanh', 'g', ['g'])
547 self
.add_tests('exp', 'g', ['g'])
548 self
.add_tests('log', 'g', ['g'])
549 self
.add_tests('pow', 'g', ['g', 'g'])
550 self
.add_tests('sqrt', 'g', ['g'])
551 self
.add_tests('fabs', 'r', ['g'], 'cabs')
552 # C99/C11 complex-only functions.
553 self
.add_tests('carg', 'r', ['c'])
554 self
.add_tests('cimag', 'r', ['c'])
555 self
.add_tests('conj', 'c', ['c'])
556 self
.add_tests('cproj', 'c', ['c'])
557 self
.add_tests('creal', 'r', ['c'])
558 # TS 18661-1 functions.
559 self
.add_tests('roundeven', 'r', ['r'])
560 self
.add_tests('nextup', 'r', ['r'])
561 self
.add_tests('nextdown', 'r', ['r'])
562 self
.add_tests('fminmag', 'r', ['r', 'r'])
563 self
.add_tests('fmaxmag', 'r', ['r', 'r'])
564 self
.add_tests('llogb', 'long int', ['r'])
565 self
.add_tests('fromfp', 'intmax_t', ['r', 'int', 'unsigned int'])
566 self
.add_tests('fromfpx', 'intmax_t', ['r', 'int', 'unsigned int'])
567 self
.add_tests('ufromfp', 'uintmax_t', ['r', 'int', 'unsigned int'])
568 self
.add_tests('ufromfpx', 'uintmax_t', ['r', 'int', 'unsigned int'])
569 self
.add_tests('totalorder', 'int', ['r', 'r'])
570 self
.add_tests('totalordermag', 'int', ['r', 'r'])
571 # The functions that round their result to a narrower type,
572 # and the associated type-generic macros, are not yet
573 # supported by this script or by glibc.
574 # Miscellaneous functions.
575 self
.add_tests('scalb', 's', ['s', 's'])
577 def tests_text(self
):
578 """Return the text of the generated testcase."""
579 test_list
= [''.join(self
.test_text_list
),
580 'static const struct test tests[] =\n'
582 ''.join(self
.test_array_list
),
584 footer_list
= ['static int\n'
587 ' for (size_t i = 0;\n'
588 ' i < sizeof (tests) / sizeof (tests[0]);\n'
591 ' called_mant_dig = 0;\n'
592 ' called_func_name = "";\n'
593 ' tests[i].func ();\n'
594 ' if (called_mant_dig == tests[i].mant_dig\n'
595 ' && strcmp (called_func_name,\n'
596 ' tests[i].func_name) == 0)\n'
601 ' printf ("Test %zu (%s):\\n"\n'
602 ' " Expected: %s precision %d\\n"\n'
603 ' " Actual: %s precision %d\\n\\n",\n'
604 ' i, tests[i].test_name,\n'
605 ' tests[i].func_name,\n'
606 ' tests[i].mant_dig,\n'
607 ' called_func_name, called_mant_dig);\n'
610 ' printf ("%d pass, %d fail\\n", num_pass, num_fail);\n'
611 ' return num_fail != 0;\n'
614 '#include <support/test-driver.c>']
615 return ''.join(self
.header_list
+ test_list
+ footer_list
)
618 """The main entry point."""
622 print(t
.tests_text())
624 if __name__
== '__main__':