gen-tgmath-tests.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. #!/usr/bin/python
  2. # Generate tests for <tgmath.h> macros.
  3. # Copyright (C) 2017-2019 Free Software Foundation, Inc.
  4. # This file is part of the GNU C Library.
  5. #
  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.
  10. #
  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.
  15. #
  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/>.
  19. # As glibc does not support decimal floating point, the types to
  20. # consider for generic parameters are standard and binary
  21. # floating-point types, and integer types which are treated as double.
  22. # The corresponding complex types may also be used (including complex
  23. # integer types, which are a GNU extension, but are currently disabled
  24. # here because they do not work properly with tgmath.h).
  25. # The proposed resolution to TS 18661-1 DR#9
  26. # <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2149.htm#dr_9>
  27. # makes the <tgmath.h> rules for selecting a function to call
  28. # correspond to the usual arithmetic conversions (applied successively
  29. # to the arguments for generic parameters in order), which choose the
  30. # type whose set of values contains that of the other type (undefined
  31. # behavior if neither type's set of values is a superset of the
  32. # other), with interchange types being preferred to standard types
  33. # (long double, double, float), being preferred to extended types
  34. # (_Float128x, _Float64x, _Float32x).
  35. # For the standard and binary floating-point types supported by GCC 7
  36. # on any platform, this means the resulting type is the last of the
  37. # given types in one of the following orders, or undefined behavior if
  38. # types with both ibm128 and binary128 representation are specified.
  39. # If double = long double: _Float16, float, _Float32, _Float32x,
  40. # double, long double, _Float64, _Float64x, _Float128.
  41. # Otherwise: _Float16, float, _Float32, _Float32x, double, _Float64,
  42. # _Float64x, long double, _Float128.
  43. # We generate tests to verify the return type is exactly as expected.
  44. # We also verify that the function called is real or complex as
  45. # expected, and that it is called for the right floating-point format
  46. # (but it is OK to call a double function instead of a long double one
  47. # if they have the same format, for example). For all the formats
  48. # supported on any given configuration of glibc, the MANT_DIG value
  49. # uniquely determines the format.
  50. import string
  51. import sys
  52. class Type(object):
  53. """A type that may be used as an argument for generic parameters."""
  54. # All possible argument or result types.
  55. all_types_list = []
  56. # All argument types.
  57. argument_types_list = []
  58. # All real argument types.
  59. real_argument_types_list = []
  60. # Real argument types that correspond to a standard floating type
  61. # (float, double or long double; not _FloatN or _FloatNx).
  62. standard_real_argument_types_list = []
  63. # The real floating types by their order properties (which are
  64. # tuples giving the positions in both the possible orders above).
  65. real_types_order = {}
  66. # The type double.
  67. double_type = None
  68. # The type _Complex double.
  69. complex_double_type = None
  70. # The type _Float64.
  71. float64_type = None
  72. # The type _Float64x.
  73. float64x_type = None
  74. def __init__(self, name, suffix=None, mant_dig=None, condition='1',
  75. order=None, integer=False, complex=False, real_type=None):
  76. """Initialize a Type object, creating any corresponding complex type
  77. in the process."""
  78. self.name = name
  79. self.suffix = suffix
  80. self.mant_dig = mant_dig
  81. self.condition = condition
  82. self.order = order
  83. self.integer = integer
  84. self.complex = complex
  85. if complex:
  86. self.complex_type = self
  87. self.real_type = real_type
  88. else:
  89. # complex_type filled in by the caller once created.
  90. self.complex_type = None
  91. self.real_type = self
  92. def register_type(self, internal):
  93. """Record a type in the lists of all types."""
  94. Type.all_types_list.append(self)
  95. if not internal:
  96. Type.argument_types_list.append(self)
  97. if not self.complex:
  98. Type.real_argument_types_list.append(self)
  99. if not self.name.startswith('_Float'):
  100. Type.standard_real_argument_types_list.append(self)
  101. if self.order is not None:
  102. Type.real_types_order[self.order] = self
  103. if self.name == 'double':
  104. Type.double_type = self
  105. if self.name == '_Complex double':
  106. Type.complex_double_type = self
  107. if self.name == '_Float64':
  108. Type.float64_type = self
  109. if self.name == '_Float64x':
  110. Type.float64x_type = self
  111. @staticmethod
  112. def create_type(name, suffix=None, mant_dig=None, condition='1', order=None,
  113. integer=False, complex_name=None, complex_ok=True,
  114. internal=False):
  115. """Create and register a Type object for a real type, creating any
  116. corresponding complex type in the process."""
  117. real_type = Type(name, suffix=suffix, mant_dig=mant_dig,
  118. condition=condition, order=order, integer=integer,
  119. complex=False)
  120. if complex_ok:
  121. if complex_name is None:
  122. complex_name = '_Complex %s' % name
  123. complex_type = Type(complex_name, condition=condition,
  124. integer=integer, complex=True,
  125. real_type=real_type)
  126. else:
  127. complex_type = None
  128. real_type.complex_type = complex_type
  129. real_type.register_type(internal)
  130. if complex_type is not None:
  131. complex_type.register_type(internal)
  132. def floating_type(self):
  133. """Return the corresponding floating type."""
  134. if self.integer:
  135. return (Type.complex_double_type
  136. if self.complex
  137. else Type.double_type)
  138. else:
  139. return self
  140. def real_floating_type(self):
  141. """Return the corresponding real floating type."""
  142. return self.real_type.floating_type()
  143. def __str__(self):
  144. """Return string representation of a type."""
  145. return self.name
  146. @staticmethod
  147. def init_types():
  148. """Initialize all the known types."""
  149. Type.create_type('_Float16', 'f16', 'FLT16_MANT_DIG',
  150. complex_name='__CFLOAT16',
  151. condition='defined HUGE_VAL_F16', order=(0, 0))
  152. Type.create_type('float', 'f', 'FLT_MANT_DIG', order=(1, 1))
  153. Type.create_type('_Float32', 'f32', 'FLT32_MANT_DIG',
  154. complex_name='__CFLOAT32',
  155. condition='defined HUGE_VAL_F32', order=(2, 2))
  156. Type.create_type('_Float32x', 'f32x', 'FLT32X_MANT_DIG',
  157. complex_name='__CFLOAT32X',
  158. condition='defined HUGE_VAL_F32X', order=(3, 3))
  159. Type.create_type('double', '', 'DBL_MANT_DIG', order=(4, 4))
  160. Type.create_type('long double', 'l', 'LDBL_MANT_DIG', order=(5, 7))
  161. Type.create_type('_Float64', 'f64', 'FLT64_MANT_DIG',
  162. complex_name='__CFLOAT64',
  163. condition='defined HUGE_VAL_F64', order=(6, 5))
  164. Type.create_type('_Float64x', 'f64x', 'FLT64X_MANT_DIG',
  165. complex_name='__CFLOAT64X',
  166. condition='defined HUGE_VAL_F64X', order=(7, 6))
  167. Type.create_type('_Float128', 'f128', 'FLT128_MANT_DIG',
  168. complex_name='__CFLOAT128',
  169. condition='defined HUGE_VAL_F128', order=(8, 8))
  170. Type.create_type('char', integer=True)
  171. Type.create_type('signed char', integer=True)
  172. Type.create_type('unsigned char', integer=True)
  173. Type.create_type('short int', integer=True)
  174. Type.create_type('unsigned short int', integer=True)
  175. Type.create_type('int', integer=True)
  176. Type.create_type('unsigned int', integer=True)
  177. Type.create_type('long int', integer=True)
  178. Type.create_type('unsigned long int', integer=True)
  179. Type.create_type('long long int', integer=True)
  180. Type.create_type('unsigned long long int', integer=True)
  181. Type.create_type('__int128', integer=True,
  182. condition='defined __SIZEOF_INT128__')
  183. Type.create_type('unsigned __int128', integer=True,
  184. condition='defined __SIZEOF_INT128__')
  185. Type.create_type('enum e', integer=True, complex_ok=False)
  186. Type.create_type('_Bool', integer=True, complex_ok=False)
  187. Type.create_type('bit_field', integer=True, complex_ok=False)
  188. # Internal types represent the combination of long double with
  189. # _Float64 or _Float64x, for which the ordering depends on
  190. # whether long double has the same format as double.
  191. Type.create_type('long_double_Float64', None, 'LDBL_MANT_DIG',
  192. complex_name='complex_long_double_Float64',
  193. condition='defined HUGE_VAL_F64', order=(6, 7),
  194. internal=True)
  195. Type.create_type('long_double_Float64x', None, 'FLT64X_MANT_DIG',
  196. complex_name='complex_long_double_Float64x',
  197. condition='defined HUGE_VAL_F64X', order=(7, 7),
  198. internal=True)
  199. @staticmethod
  200. def can_combine_types(types):
  201. """Return a C preprocessor conditional for whether the given list of
  202. types can be used together as type-generic macro arguments."""
  203. have_long_double = False
  204. have_float128 = False
  205. for t in types:
  206. t = t.real_floating_type()
  207. if t.name == 'long double':
  208. have_long_double = True
  209. if t.name == '_Float128' or t.name == '_Float64x':
  210. have_float128 = True
  211. if have_long_double and have_float128:
  212. # If ibm128 format is in use for long double, both
  213. # _Float64x and _Float128 are binary128 and the types
  214. # cannot be combined.
  215. return '(LDBL_MANT_DIG != 106)'
  216. return '1'
  217. @staticmethod
  218. def combine_types(types):
  219. """Return the result of combining a set of types."""
  220. have_complex = False
  221. combined = None
  222. for t in types:
  223. if t.complex:
  224. have_complex = True
  225. t = t.real_floating_type()
  226. if combined is None:
  227. combined = t
  228. else:
  229. order = (max(combined.order[0], t.order[0]),
  230. max(combined.order[1], t.order[1]))
  231. combined = Type.real_types_order[order]
  232. return combined.complex_type if have_complex else combined
  233. def list_product_initial(initial, lists):
  234. """Return a list of lists, with an initial sequence from the first
  235. argument (a list of lists) followed by each sequence of one
  236. element from each successive element of the second argument."""
  237. if not lists:
  238. return initial
  239. return list_product_initial([a + [b] for a in initial for b in lists[0]],
  240. lists[1:])
  241. def list_product(lists):
  242. """Return a list of lists, with each sequence of one element from each
  243. successive element of the argument."""
  244. return list_product_initial([[]], lists)
  245. try:
  246. trans_id = str.maketrans(' *', '_p')
  247. except AttributeError:
  248. trans_id = string.maketrans(' *', '_p')
  249. def var_for_type(name):
  250. """Return the name of a variable with a given type (name)."""
  251. return 'var_%s' % name.translate(trans_id)
  252. def vol_var_for_type(name):
  253. """Return the name of a variable with a given volatile type (name)."""
  254. return 'vol_var_%s' % name.translate(trans_id)
  255. def define_vars_for_type(name):
  256. """Return the definitions of variables with a given type (name)."""
  257. if name == 'bit_field':
  258. struct_vars = define_vars_for_type('struct s');
  259. return '%s#define %s %s.bf\n' % (struct_vars,
  260. vol_var_for_type(name),
  261. vol_var_for_type('struct s'))
  262. return ('%s %s __attribute__ ((unused));\n'
  263. '%s volatile %s __attribute__ ((unused));\n'
  264. % (name, var_for_type(name), name, vol_var_for_type(name)))
  265. def if_cond_text(conds, text):
  266. """Return the result of making some text conditional under #if. The
  267. text ends with a newline, as does the return value if not empty."""
  268. if '0' in conds:
  269. return ''
  270. conds = [c for c in conds if c != '1']
  271. conds = sorted(set(conds))
  272. if not conds:
  273. return text
  274. return '#if %s\n%s#endif\n' % (' && '.join(conds), text)
  275. class Tests(object):
  276. """The state associated with testcase generation."""
  277. def __init__(self):
  278. """Initialize a Tests object."""
  279. self.header_list = ['#define __STDC_WANT_IEC_60559_TYPES_EXT__\n'
  280. '#include <float.h>\n'
  281. '#include <stdbool.h>\n'
  282. '#include <stdint.h>\n'
  283. '#include <stdio.h>\n'
  284. '#include <string.h>\n'
  285. '#include <tgmath.h>\n'
  286. '\n'
  287. 'struct test\n'
  288. ' {\n'
  289. ' void (*func) (void);\n'
  290. ' const char *func_name;\n'
  291. ' const char *test_name;\n'
  292. ' int mant_dig;\n'
  293. ' };\n'
  294. 'int num_pass, num_fail;\n'
  295. 'volatile int called_mant_dig;\n'
  296. 'const char *volatile called_func_name;\n'
  297. 'enum e { E, F };\n'
  298. 'struct s\n'
  299. ' {\n'
  300. ' int bf:2;\n'
  301. ' };\n']
  302. float64_text = ('# if LDBL_MANT_DIG == DBL_MANT_DIG\n'
  303. 'typedef _Float64 long_double_Float64;\n'
  304. 'typedef __CFLOAT64 complex_long_double_Float64;\n'
  305. '# else\n'
  306. 'typedef long double long_double_Float64;\n'
  307. 'typedef _Complex long double '
  308. 'complex_long_double_Float64;\n'
  309. '# endif\n')
  310. float64_text = if_cond_text([Type.float64_type.condition],
  311. float64_text)
  312. float64x_text = ('# if LDBL_MANT_DIG == DBL_MANT_DIG\n'
  313. 'typedef _Float64x long_double_Float64x;\n'
  314. 'typedef __CFLOAT64X complex_long_double_Float64x;\n'
  315. '# else\n'
  316. 'typedef long double long_double_Float64x;\n'
  317. 'typedef _Complex long double '
  318. 'complex_long_double_Float64x;\n'
  319. '# endif\n')
  320. float64x_text = if_cond_text([Type.float64x_type.condition],
  321. float64x_text)
  322. self.header_list.append(float64_text)
  323. self.header_list.append(float64x_text)
  324. self.types_seen = set()
  325. for t in Type.all_types_list:
  326. self.add_type_var(t.name, t.condition)
  327. self.test_text_list = []
  328. self.test_array_list = []
  329. self.macros_seen = set()
  330. def add_type_var(self, name, cond):
  331. """Add declarations of variables for a type."""
  332. if name in self.types_seen:
  333. return
  334. t_vars = define_vars_for_type(name)
  335. self.header_list.append(if_cond_text([cond], t_vars))
  336. self.types_seen.add(name)
  337. def add_tests(self, macro, ret, args, complex_func=None):
  338. """Add tests for a given tgmath.h macro, if that is the macro for
  339. which tests are to be generated; otherwise just add it to the
  340. list of macros for which test generation is supported."""
  341. # 'c' means the function argument or return type is
  342. # type-generic and complex only (a complex function argument
  343. # may still have a real macro argument). 'g' means it is
  344. # type-generic and may be real or complex; 'r' means it is
  345. # type-generic and may only be real; 's' means the same as
  346. # 'r', but restricted to float, double and long double.
  347. self.macros_seen.add(macro)
  348. if macro != self.macro:
  349. return
  350. have_complex = False
  351. func = macro
  352. if ret == 'c' or 'c' in args:
  353. # Complex-only.
  354. have_complex = True
  355. complex_func = func
  356. func = None
  357. elif ret == 'g' or 'g' in args:
  358. # Real and complex.
  359. have_complex = True
  360. if complex_func == None:
  361. complex_func = 'c%s' % func
  362. types = [ret] + args
  363. for t in types:
  364. if t != 'c' and t != 'g' and t != 'r' and t != 's':
  365. self.add_type_var(t, '1')
  366. for t in Type.argument_types_list:
  367. if t.integer:
  368. continue
  369. if t.complex and not have_complex:
  370. continue
  371. if func == None and not t.complex:
  372. continue
  373. if ret == 's' and t.name.startswith('_Float'):
  374. continue
  375. if ret == 'c':
  376. ret_name = t.complex_type.name
  377. elif ret == 'g':
  378. ret_name = t.name
  379. elif ret == 'r' or ret == 's':
  380. ret_name = t.real_type.name
  381. else:
  382. ret_name = ret
  383. dummy_func_name = complex_func if t.complex else func
  384. arg_list = []
  385. arg_num = 0
  386. for a in args:
  387. if a == 'c':
  388. arg_name = t.complex_type.name
  389. elif a == 'g':
  390. arg_name = t.name
  391. elif a == 'r' or a == 's':
  392. arg_name = t.real_type.name
  393. else:
  394. arg_name = a
  395. arg_list.append('%s arg%d __attribute__ ((unused))'
  396. % (arg_name, arg_num))
  397. arg_num += 1
  398. dummy_func = ('%s\n'
  399. '(%s%s) (%s)\n'
  400. '{\n'
  401. ' called_mant_dig = %s;\n'
  402. ' called_func_name = "%s";\n'
  403. ' return 0;\n'
  404. '}\n' % (ret_name, dummy_func_name,
  405. t.real_type.suffix, ', '.join(arg_list),
  406. t.real_type.mant_dig, dummy_func_name))
  407. dummy_func = if_cond_text([t.condition], dummy_func)
  408. self.test_text_list.append(dummy_func)
  409. arg_types = []
  410. for t in args:
  411. if t == 'g' or t == 'c':
  412. arg_types.append(Type.argument_types_list)
  413. elif t == 'r':
  414. arg_types.append(Type.real_argument_types_list)
  415. elif t == 's':
  416. arg_types.append(Type.standard_real_argument_types_list)
  417. arg_types_product = list_product(arg_types)
  418. test_num = 0
  419. for this_args in arg_types_product:
  420. comb_type = Type.combine_types(this_args)
  421. can_comb = Type.can_combine_types(this_args)
  422. all_conds = [t.condition for t in this_args]
  423. all_conds.append(can_comb)
  424. any_complex = func == None
  425. for t in this_args:
  426. if t.complex:
  427. any_complex = True
  428. func_name = complex_func if any_complex else func
  429. test_name = '%s (%s)' % (macro,
  430. ', '.join([t.name for t in this_args]))
  431. test_func_name = 'test_%s_%d' % (macro, test_num)
  432. test_num += 1
  433. mant_dig = comb_type.real_type.mant_dig
  434. test_text = '%s, "%s", "%s", %s' % (test_func_name, func_name,
  435. test_name, mant_dig)
  436. test_text = ' { %s },\n' % test_text
  437. test_text = if_cond_text(all_conds, test_text)
  438. self.test_array_list.append(test_text)
  439. call_args = []
  440. call_arg_pos = 0
  441. for t in args:
  442. if t == 'g' or t == 'c' or t == 'r' or t == 's':
  443. type = this_args[call_arg_pos].name
  444. call_arg_pos += 1
  445. else:
  446. type = t
  447. call_args.append(vol_var_for_type(type))
  448. call_args_text = ', '.join(call_args)
  449. if ret == 'g':
  450. ret_type = comb_type.name
  451. elif ret == 'r' or ret == 's':
  452. ret_type = comb_type.real_type.name
  453. elif ret == 'c':
  454. ret_type = comb_type.complex_type.name
  455. else:
  456. ret_type = ret
  457. call_text = '%s (%s)' % (macro, call_args_text)
  458. test_func_text = ('static void\n'
  459. '%s (void)\n'
  460. '{\n'
  461. ' extern typeof (%s) %s '
  462. '__attribute__ ((unused));\n'
  463. ' %s = %s;\n'
  464. '}\n' % (test_func_name, call_text,
  465. var_for_type(ret_type),
  466. vol_var_for_type(ret_type), call_text))
  467. test_func_text = if_cond_text(all_conds, test_func_text)
  468. self.test_text_list.append(test_func_text)
  469. def add_all_tests(self, macro):
  470. """Add tests for the given tgmath.h macro, if any, and generate the
  471. list of all supported macros."""
  472. self.macro = macro
  473. # C99/C11 real-only functions.
  474. self.add_tests('atan2', 'r', ['r', 'r'])
  475. self.add_tests('cbrt', 'r', ['r'])
  476. self.add_tests('ceil', 'r', ['r'])
  477. self.add_tests('copysign', 'r', ['r', 'r'])
  478. self.add_tests('erf', 'r', ['r'])
  479. self.add_tests('erfc', 'r', ['r'])
  480. self.add_tests('exp2', 'r', ['r'])
  481. self.add_tests('expm1', 'r', ['r'])
  482. self.add_tests('fdim', 'r', ['r', 'r'])
  483. self.add_tests('floor', 'r', ['r'])
  484. self.add_tests('fma', 'r', ['r', 'r', 'r'])
  485. self.add_tests('fmax', 'r', ['r', 'r'])
  486. self.add_tests('fmin', 'r', ['r', 'r'])
  487. self.add_tests('fmod', 'r', ['r', 'r'])
  488. self.add_tests('frexp', 'r', ['r', 'int *'])
  489. self.add_tests('hypot', 'r', ['r', 'r'])
  490. self.add_tests('ilogb', 'int', ['r'])
  491. self.add_tests('ldexp', 'r', ['r', 'int'])
  492. self.add_tests('lgamma', 'r', ['r'])
  493. self.add_tests('llrint', 'long long int', ['r'])
  494. self.add_tests('llround', 'long long int', ['r'])
  495. # log10 is real-only in ISO C, but supports complex arguments
  496. # as a GNU extension.
  497. self.add_tests('log10', 'g', ['g'])
  498. self.add_tests('log1p', 'r', ['r'])
  499. self.add_tests('log2', 'r', ['r'])
  500. self.add_tests('logb', 'r', ['r'])
  501. self.add_tests('lrint', 'long int', ['r'])
  502. self.add_tests('lround', 'long int', ['r'])
  503. self.add_tests('nearbyint', 'r', ['r'])
  504. self.add_tests('nextafter', 'r', ['r', 'r'])
  505. self.add_tests('nexttoward', 's', ['s', 'long double'])
  506. self.add_tests('remainder', 'r', ['r', 'r'])
  507. self.add_tests('remquo', 'r', ['r', 'r', 'int *'])
  508. self.add_tests('rint', 'r', ['r'])
  509. self.add_tests('round', 'r', ['r'])
  510. self.add_tests('scalbn', 'r', ['r', 'int'])
  511. self.add_tests('scalbln', 'r', ['r', 'long int'])
  512. self.add_tests('tgamma', 'r', ['r'])
  513. self.add_tests('trunc', 'r', ['r'])
  514. # C99/C11 real-and-complex functions.
  515. self.add_tests('acos', 'g', ['g'])
  516. self.add_tests('asin', 'g', ['g'])
  517. self.add_tests('atan', 'g', ['g'])
  518. self.add_tests('acosh', 'g', ['g'])
  519. self.add_tests('asinh', 'g', ['g'])
  520. self.add_tests('atanh', 'g', ['g'])
  521. self.add_tests('cos', 'g', ['g'])
  522. self.add_tests('sin', 'g', ['g'])
  523. self.add_tests('tan', 'g', ['g'])
  524. self.add_tests('cosh', 'g', ['g'])
  525. self.add_tests('sinh', 'g', ['g'])
  526. self.add_tests('tanh', 'g', ['g'])
  527. self.add_tests('exp', 'g', ['g'])
  528. self.add_tests('log', 'g', ['g'])
  529. self.add_tests('pow', 'g', ['g', 'g'])
  530. self.add_tests('sqrt', 'g', ['g'])
  531. self.add_tests('fabs', 'r', ['g'], 'cabs')
  532. # C99/C11 complex-only functions.
  533. self.add_tests('carg', 'r', ['c'])
  534. self.add_tests('cimag', 'r', ['c'])
  535. self.add_tests('conj', 'c', ['c'])
  536. self.add_tests('cproj', 'c', ['c'])
  537. self.add_tests('creal', 'r', ['c'])
  538. # TS 18661-1 functions.
  539. self.add_tests('roundeven', 'r', ['r'])
  540. self.add_tests('nextup', 'r', ['r'])
  541. self.add_tests('nextdown', 'r', ['r'])
  542. self.add_tests('fminmag', 'r', ['r', 'r'])
  543. self.add_tests('fmaxmag', 'r', ['r', 'r'])
  544. self.add_tests('llogb', 'long int', ['r'])
  545. self.add_tests('fromfp', 'intmax_t', ['r', 'int', 'unsigned int'])
  546. self.add_tests('fromfpx', 'intmax_t', ['r', 'int', 'unsigned int'])
  547. self.add_tests('ufromfp', 'uintmax_t', ['r', 'int', 'unsigned int'])
  548. self.add_tests('ufromfpx', 'uintmax_t', ['r', 'int', 'unsigned int'])
  549. self.add_tests('totalorder', 'int', ['r', 'r'])
  550. self.add_tests('totalordermag', 'int', ['r', 'r'])
  551. # The functions that round their result to a narrower type,
  552. # and the associated type-generic macros, are not yet
  553. # supported by this script or by glibc.
  554. # Miscellaneous functions.
  555. self.add_tests('scalb', 's', ['s', 's'])
  556. def tests_text(self):
  557. """Return the text of the generated testcase."""
  558. test_list = [''.join(self.test_text_list),
  559. 'static const struct test tests[] =\n'
  560. ' {\n',
  561. ''.join(self.test_array_list),
  562. ' };\n']
  563. footer_list = ['static int\n'
  564. 'do_test (void)\n'
  565. '{\n'
  566. ' for (size_t i = 0;\n'
  567. ' i < sizeof (tests) / sizeof (tests[0]);\n'
  568. ' i++)\n'
  569. ' {\n'
  570. ' called_mant_dig = 0;\n'
  571. ' called_func_name = "";\n'
  572. ' tests[i].func ();\n'
  573. ' if (called_mant_dig == tests[i].mant_dig\n'
  574. ' && strcmp (called_func_name,\n'
  575. ' tests[i].func_name) == 0)\n'
  576. ' num_pass++;\n'
  577. ' else\n'
  578. ' {\n'
  579. ' num_fail++;\n'
  580. ' printf ("Test %zu (%s):\\n"\n'
  581. ' " Expected: %s precision %d\\n"\n'
  582. ' " Actual: %s precision %d\\n\\n",\n'
  583. ' i, tests[i].test_name,\n'
  584. ' tests[i].func_name,\n'
  585. ' tests[i].mant_dig,\n'
  586. ' called_func_name, called_mant_dig);\n'
  587. ' }\n'
  588. ' }\n'
  589. ' printf ("%d pass, %d fail\\n", num_pass, num_fail);\n'
  590. ' return num_fail != 0;\n'
  591. '}\n'
  592. '\n'
  593. '#include <support/test-driver.c>']
  594. return ''.join(self.header_list + test_list + footer_list)
  595. def check_macro_list(self, macro_list):
  596. """Check the list of macros that can be tested."""
  597. if self.macros_seen != set(macro_list):
  598. print('error: macro list mismatch')
  599. sys.exit(1)
  600. def main():
  601. """The main entry point."""
  602. Type.init_types()
  603. t = Tests()
  604. if sys.argv[1] == 'check-list':
  605. macro = None
  606. macro_list = sys.argv[2:]
  607. else:
  608. macro = sys.argv[1]
  609. macro_list = []
  610. t.add_all_tests(macro)
  611. if macro:
  612. print(t.tests_text())
  613. else:
  614. t.check_macro_list(macro_list)
  615. if __name__ == '__main__':
  616. main()