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
,
131 # Complex integer types currently disabled because of problems
133 if complex_ok
and not integer
:
134 if complex_name
is None:
135 complex_name
= '_Complex %s' % name
136 complex_type
= Type(complex_name
, condition
=condition
,
137 integer
=integer
, complex=True,
141 real_type
.complex_type
= complex_type
142 real_type
.register_type(internal
)
143 if complex_type
is not None:
144 complex_type
.register_type(internal
)
146 def floating_type(self
):
147 """Return the corresponding floating type."""
149 return (Type
.complex_double_type
151 else Type
.double_type
)
155 def real_floating_type(self
):
156 """Return the corresponding real floating type."""
157 return self
.real_type
.floating_type()
160 """Return string representation of a type."""
165 """Initialize all the known types."""
166 Type
.create_type('_Float16', 'f16', 'FLT16_MANT_DIG',
167 complex_name
='__CFLOAT16',
168 condition
='defined HUGE_VAL_F16', order
=(0, 0))
169 Type
.create_type('float', 'f', 'FLT_MANT_DIG', order
=(1, 1))
170 Type
.create_type('_Float32', 'f32', 'FLT32_MANT_DIG',
171 complex_name
='__CFLOAT32',
172 condition
='defined HUGE_VAL_F32', order
=(2, 2))
173 Type
.create_type('_Float32x', 'f32x', 'FLT32X_MANT_DIG',
174 complex_name
='__CFLOAT32X',
175 condition
='defined HUGE_VAL_F32X', order
=(3, 3))
176 Type
.create_type('double', '', 'DBL_MANT_DIG', order
=(4, 4))
177 Type
.create_type('long double', 'l', 'LDBL_MANT_DIG', order
=(5, 7))
178 Type
.create_type('_Float64', 'f64', 'FLT64_MANT_DIG',
179 complex_name
='__CFLOAT64',
180 condition
='defined HUGE_VAL_F64', order
=(6, 5))
181 Type
.create_type('_Float64x', 'f64x', 'FLT64X_MANT_DIG',
182 complex_name
='__CFLOAT64X',
183 condition
='defined HUGE_VAL_F64X', order
=(7, 6))
184 Type
.create_type('_Float128', 'f128', 'FLT128_MANT_DIG',
185 complex_name
='__CFLOAT128',
186 condition
='defined HUGE_VAL_F128', order
=(8, 8))
187 Type
.create_type('char', integer
=True)
188 Type
.create_type('signed char', integer
=True)
189 Type
.create_type('unsigned char', integer
=True)
190 Type
.create_type('short int', integer
=True)
191 Type
.create_type('unsigned short int', integer
=True)
192 Type
.create_type('int', integer
=True)
193 Type
.create_type('unsigned int', integer
=True)
194 Type
.create_type('long int', integer
=True)
195 Type
.create_type('unsigned long int', integer
=True)
196 Type
.create_type('long long int', integer
=True)
197 Type
.create_type('unsigned long long int', integer
=True)
198 Type
.create_type('__int128', integer
=True,
199 condition
='defined __SIZEOF_INT128__')
200 Type
.create_type('unsigned __int128', integer
=True,
201 condition
='defined __SIZEOF_INT128__')
202 Type
.create_type('enum e', integer
=True, complex_ok
=False)
203 Type
.create_type('_Bool', integer
=True, complex_ok
=False)
204 Type
.create_type('bit_field', integer
=True, complex_ok
=False)
205 # Internal types represent the combination of long double with
206 # _Float64 or _Float64x, for which the ordering depends on
207 # whether long double has the same format as double.
208 Type
.create_type('long_double_Float64', 'LDBL_MANT_DIG',
209 complex_name
='complex_long_double_Float64',
210 condition
='defined HUGE_VAL_F64', order
=(6, 7),
212 Type
.create_type('long_double_Float64x', 'FLT64X_MANT_DIG',
213 complex_name
='complex_long_double_Float64x',
214 condition
='defined HUGE_VAL_F64X', order
=(7, 7),
218 def can_combine_types(types
):
219 """Return a C preprocessor conditional for whether the given list of
220 types can be used together as type-generic macro arguments."""
221 have_long_double
= False
222 have_float128
= False
224 t
= t
.real_floating_type()
225 if t
.name
== 'long double':
226 have_long_double
= True
227 if t
.name
== '_Float128' or t
.name
== '_Float64x':
229 if have_long_double
and have_float128
:
230 # If ibm128 format is in use for long double, both
231 # _Float64x and _Float128 are binary128 and the types
232 # cannot be combined.
233 return '(LDBL_MANT_DIG != 106)'
237 def combine_types(types
):
238 """Return the result of combining a set of types."""
244 t
= t
.real_floating_type()
248 order
= (max(combined
.order
[0], t
.order
[0]),
249 max(combined
.order
[1], t
.order
[1]))
250 combined
= Type
.real_types_order
[order
]
251 return combined
.complex_type
if have_complex
else combined
253 def list_product_initial(initial
, lists
):
254 """Return a list of lists, with an initial sequence from the first
255 argument (a list of lists) followed by each sequence of one
256 element from each successive element of the second argument."""
259 return list_product_initial([a
+ [b
] for a
in initial
for b
in lists
[0]],
262 def list_product(lists
):
263 """Return a list of lists, with each sequence of one element from each
264 successive element of the argument."""
265 return list_product_initial([[]], lists
)
268 trans_id
= str.maketrans(' *', '_p')
269 except AttributeError:
270 trans_id
= string
.maketrans(' *', '_p')
271 def var_for_type(name
):
272 """Return the name of a variable with a given type (name)."""
273 return 'var_%s' % name
.translate(trans_id
)
275 def vol_var_for_type(name
):
276 """Return the name of a variable with a given volatile type (name)."""
277 return 'vol_var_%s' % name
.translate(trans_id
)
279 def define_vars_for_type(name
):
280 """Return the definitions of variables with a given type (name)."""
281 if name
== 'bit_field':
282 struct_vars
= define_vars_for_type('struct s');
283 return '%s#define %s %s.bf\n' % (struct_vars
,
284 vol_var_for_type(name
),
285 vol_var_for_type('struct s'))
286 return ('%s %s __attribute__ ((unused));\n'
287 '%s volatile %s __attribute__ ((unused));\n'
288 % (name
, var_for_type(name
), name
, vol_var_for_type(name
)))
290 def if_cond_text(conds
, text
):
291 """Return the result of making some text conditional under #if. The
292 text ends with a newline, as does the return value if not empty."""
295 conds
= [c
for c
in conds
if c
!= '1']
296 conds
= sorted(set(conds
))
299 return '#if %s\n%s#endif\n' % (' && '.join(conds
), text
)
302 """The state associated with testcase generation."""
305 """Initialize a Tests object."""
306 self
.header_list
= ['#define __STDC_WANT_IEC_60559_TYPES_EXT__\n'
307 '#include <float.h>\n'
308 '#include <stdbool.h>\n'
309 '#include <stdint.h>\n'
310 '#include <stdio.h>\n'
311 '#include <string.h>\n'
312 '#include <tgmath.h>\n'
316 ' void (*func) (void);\n'
317 ' const char *func_name;\n'
318 ' const char *test_name;\n'
321 'int num_pass, num_fail;\n'
322 'volatile int called_mant_dig;\n'
323 'const char *volatile called_func_name;\n'
329 float64_text
= ('# if LDBL_MANT_DIG == DBL_MANT_DIG\n'
330 'typedef _Float64 long_double_Float64;\n'
331 'typedef __CFLOAT64 complex_long_double_Float64;\n'
333 'typedef long double long_double_Float64;\n'
334 'typedef _Complex long double '
335 'complex_long_double_Float64;\n'
337 float64_text
= if_cond_text([Type
.float64_type
.condition
],
339 float64x_text
= ('# if LDBL_MANT_DIG == DBL_MANT_DIG\n'
340 'typedef _Float64x long_double_Float64x;\n'
341 'typedef __CFLOAT64X complex_long_double_Float64x;\n'
343 'typedef long double long_double_Float64x;\n'
344 'typedef _Complex long double '
345 'complex_long_double_Float64x;\n'
347 float64x_text
= if_cond_text([Type
.float64x_type
.condition
],
349 self
.header_list
.append(float64_text
)
350 self
.header_list
.append(float64x_text
)
351 self
.types_seen
= set()
352 for t
in Type
.all_types_list
:
353 self
.add_type_var(t
.name
, t
.condition
)
354 self
.test_text_list
= []
355 self
.test_array_list
= []
357 def add_type_var(self
, name
, cond
):
358 """Add declarations of variables for a type."""
359 if name
in self
.types_seen
:
361 t_vars
= define_vars_for_type(name
)
362 self
.header_list
.append(if_cond_text([cond
], t_vars
))
363 self
.types_seen
.add(name
)
365 def add_tests(self
, macro
, ret
, args
, complex_func
=None):
366 """Add tests for a given tgmath.h macro."""
367 # 'c' means the function argument or return type is
368 # type-generic and complex only (a complex function argument
369 # may still have a real macro argument). 'g' means it is
370 # type-generic and may be real or complex; 'r' means it is
371 # type-generic and may only be real; 's' means the same as
372 # 'r', but restricted to float, double and long double.
375 if ret
== 'c' or 'c' in args
:
380 elif ret
== 'g' or 'g' in args
:
383 if complex_func
== None:
384 complex_func
= 'c%s' % func
387 if t
!= 'c' and t
!= 'g' and t
!= 'r' and t
!= 's':
388 self
.add_type_var(t
, '1')
389 for t
in Type
.argument_types_list
:
392 if t
.complex and not have_complex
:
394 if func
== None and not t
.complex:
396 if ret
== 's' and t
.name
.startswith('_Float'):
399 ret_name
= t
.complex_type
.name
402 elif ret
== 'r' or ret
== 's':
403 ret_name
= t
.real_type
.name
406 dummy_func_name
= complex_func
if t
.complex else func
411 arg_name
= t
.complex_type
.name
414 elif a
== 'r' or a
== 's':
415 arg_name
= t
.real_type
.name
418 arg_list
.append('%s arg%d __attribute__ ((unused))'
419 % (arg_name
, arg_num
))
424 ' called_mant_dig = %s;\n'
425 ' called_func_name = "%s";\n'
427 '}\n' % (ret_name
, dummy_func_name
,
428 t
.real_type
.suffix
, ', '.join(arg_list
),
429 t
.real_type
.mant_dig
, dummy_func_name
))
430 dummy_func
= if_cond_text([t
.condition
], dummy_func
)
431 self
.test_text_list
.append(dummy_func
)
434 if t
== 'g' or t
== 'c':
435 arg_types
.append(Type
.argument_types_list
)
437 arg_types
.append(Type
.real_argument_types_list
)
439 arg_types
.append(Type
.standard_real_argument_types_list
)
440 arg_types_product
= list_product(arg_types
)
442 for this_args
in arg_types_product
:
443 comb_type
= Type
.combine_types(this_args
)
444 can_comb
= Type
.can_combine_types(this_args
)
445 all_conds
= [t
.condition
for t
in this_args
]
446 all_conds
.append(can_comb
)
447 any_complex
= func
== None
451 func_name
= complex_func
if any_complex
else func
452 test_name
= '%s (%s)' % (macro
,
453 ', '.join([t
.name
for t
in this_args
]))
454 test_func_name
= 'test_%s_%d' % (macro
, test_num
)
456 mant_dig
= comb_type
.real_type
.mant_dig
457 test_text
= '%s, "%s", "%s", %s' % (test_func_name
, func_name
,
459 test_text
= ' { %s },\n' % test_text
460 test_text
= if_cond_text(all_conds
, test_text
)
461 self
.test_array_list
.append(test_text
)
465 if t
== 'g' or t
== 'c' or t
== 'r' or t
== 's':
466 type = this_args
[call_arg_pos
].name
470 call_args
.append(vol_var_for_type(type))
471 call_args_text
= ', '.join(call_args
)
473 ret_type
= comb_type
.name
474 elif ret
== 'r' or ret
== 's':
475 ret_type
= comb_type
.real_type
.name
477 ret_type
= comb_type
.complex_type
.name
480 call_text
= '%s (%s)' % (macro
, call_args_text
)
481 test_func_text
= ('static void\n'
484 ' extern typeof (%s) %s '
485 '__attribute__ ((unused));\n'
487 '}\n' % (test_func_name
, call_text
,
488 var_for_type(ret_type
),
489 vol_var_for_type(ret_type
), call_text
))
490 test_func_text
= if_cond_text(all_conds
, test_func_text
)
491 self
.test_text_list
.append(test_func_text
)
493 def add_all_tests(self
):
494 """Add tests for all tgmath.h macros."""
495 # C99/C11 real-only functions.
496 self
.add_tests('atan2', 'r', ['r', 'r'])
497 self
.add_tests('cbrt', 'r', ['r'])
498 self
.add_tests('ceil', 'r', ['r'])
499 self
.add_tests('copysign', 'r', ['r', 'r'])
500 self
.add_tests('erf', 'r', ['r'])
501 self
.add_tests('erfc', 'r', ['r'])
502 self
.add_tests('exp2', 'r', ['r'])
503 self
.add_tests('expm1', 'r', ['r'])
504 self
.add_tests('fdim', 'r', ['r', 'r'])
505 self
.add_tests('floor', 'r', ['r'])
506 self
.add_tests('fma', 'r', ['r', 'r', 'r'])
507 self
.add_tests('fmax', 'r', ['r', 'r'])
508 self
.add_tests('fmin', 'r', ['r', 'r'])
509 self
.add_tests('fmod', 'r', ['r', 'r'])
510 self
.add_tests('frexp', 'r', ['r', 'int *'])
511 self
.add_tests('hypot', 'r', ['r', 'r'])
512 self
.add_tests('ilogb', 'int', ['r'])
513 self
.add_tests('ldexp', 'r', ['r', 'int'])
514 self
.add_tests('lgamma', 'r', ['r'])
515 self
.add_tests('llrint', 'long long int', ['r'])
516 self
.add_tests('llround', 'long long int', ['r'])
517 # log10 is real-only in ISO C, but supports complex arguments
518 # as a GNU extension.
519 self
.add_tests('log10', 'g', ['g'])
520 self
.add_tests('log1p', 'r', ['r'])
521 self
.add_tests('log2', 'r', ['r'])
522 self
.add_tests('logb', 'r', ['r'])
523 self
.add_tests('lrint', 'long int', ['r'])
524 self
.add_tests('lround', 'long int', ['r'])
525 self
.add_tests('nearbyint', 'r', ['r'])
526 self
.add_tests('nextafter', 'r', ['r', 'r'])
527 self
.add_tests('nexttoward', 's', ['s', 'long double'])
528 self
.add_tests('remainder', 'r', ['r', 'r'])
529 self
.add_tests('remquo', 'r', ['r', 'r', 'int *'])
530 self
.add_tests('rint', 'r', ['r'])
531 self
.add_tests('round', 'r', ['r'])
532 self
.add_tests('scalbn', 'r', ['r', 'int'])
533 self
.add_tests('scalbln', 'r', ['r', 'long int'])
534 self
.add_tests('tgamma', 'r', ['r'])
535 self
.add_tests('trunc', 'r', ['r'])
536 # C99/C11 real-and-complex functions.
537 self
.add_tests('acos', 'g', ['g'])
538 self
.add_tests('asin', 'g', ['g'])
539 self
.add_tests('atan', 'g', ['g'])
540 self
.add_tests('acosh', 'g', ['g'])
541 self
.add_tests('asinh', 'g', ['g'])
542 self
.add_tests('atanh', 'g', ['g'])
543 self
.add_tests('cos', 'g', ['g'])
544 self
.add_tests('sin', 'g', ['g'])
545 self
.add_tests('tan', 'g', ['g'])
546 self
.add_tests('cosh', 'g', ['g'])
547 self
.add_tests('sinh', 'g', ['g'])
548 self
.add_tests('tanh', 'g', ['g'])
549 self
.add_tests('exp', 'g', ['g'])
550 self
.add_tests('log', 'g', ['g'])
551 self
.add_tests('pow', 'g', ['g', 'g'])
552 self
.add_tests('sqrt', 'g', ['g'])
553 self
.add_tests('fabs', 'r', ['g'], 'cabs')
554 # C99/C11 complex-only functions.
555 self
.add_tests('carg', 'r', ['c'])
556 self
.add_tests('cimag', 'r', ['c'])
557 self
.add_tests('conj', 'c', ['c'])
558 self
.add_tests('cproj', 'c', ['c'])
559 self
.add_tests('creal', 'r', ['c'])
560 # TS 18661-1 functions.
561 self
.add_tests('roundeven', 'r', ['r'])
562 self
.add_tests('nextup', 'r', ['r'])
563 self
.add_tests('nextdown', 'r', ['r'])
564 self
.add_tests('fminmag', 'r', ['r', 'r'])
565 self
.add_tests('fmaxmag', 'r', ['r', 'r'])
566 self
.add_tests('llogb', 'long int', ['r'])
567 self
.add_tests('fromfp', 'intmax_t', ['r', 'int', 'unsigned int'])
568 self
.add_tests('fromfpx', 'intmax_t', ['r', 'int', 'unsigned int'])
569 self
.add_tests('ufromfp', 'uintmax_t', ['r', 'int', 'unsigned int'])
570 self
.add_tests('ufromfpx', 'uintmax_t', ['r', 'int', 'unsigned int'])
571 self
.add_tests('totalorder', 'int', ['r', 'r'])
572 self
.add_tests('totalordermag', 'int', ['r', 'r'])
573 # The functions that round their result to a narrower type,
574 # and the associated type-generic macros, are not yet
575 # supported by this script or by glibc.
576 # Miscellaneous functions.
577 self
.add_tests('scalb', 's', ['s', 's'])
579 def tests_text(self
):
580 """Return the text of the generated testcase."""
581 test_list
= [''.join(self
.test_text_list
),
582 'static const struct test tests[] =\n'
584 ''.join(self
.test_array_list
),
586 footer_list
= ['static int\n'
589 ' for (size_t i = 0;\n'
590 ' i < sizeof (tests) / sizeof (tests[0]);\n'
593 ' called_mant_dig = 0;\n'
594 ' called_func_name = "";\n'
595 ' tests[i].func ();\n'
596 ' if (called_mant_dig == tests[i].mant_dig\n'
597 ' && strcmp (called_func_name,\n'
598 ' tests[i].func_name) == 0)\n'
603 ' printf ("Test %zu (%s):\\n"\n'
604 ' " Expected: %s precision %d\\n"\n'
605 ' " Actual: %s precision %d\\n\\n",\n'
606 ' i, tests[i].test_name,\n'
607 ' tests[i].func_name,\n'
608 ' tests[i].mant_dig,\n'
609 ' called_func_name, called_mant_dig);\n'
612 ' printf ("%d pass, %d fail\\n", num_pass, num_fail);\n'
613 ' return num_fail != 0;\n'
616 '#include <support/test-driver.c>']
617 return ''.join(self
.header_list
+ test_list
+ footer_list
)
620 """The main entry point."""
624 print(t
.tests_text())
626 if __name__
== '__main__':