update-quadmath.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #!/usr/bin/python3
  2. # Update libquadmath code from glibc sources.
  3. # Copyright (C) 2018 Free Software Foundation, Inc.
  4. # This file is part of the libquadmath library.
  5. #
  6. # Libquadmath 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. # Libquadmath 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 libquadmath; if not, see
  18. # <https://www.gnu.org/licenses/>.
  19. # Usage: update-quadmath.py glibc_srcdir quadmath_srcdir
  20. import argparse
  21. from collections import defaultdict
  22. import os.path
  23. import re
  24. def replace_in_file(repl_map, extra_map, src, dest):
  25. """Apply the replacements in repl_map, then those in extra_map, to the
  26. file src, producing dest."""
  27. with open(src, 'r') as src_file:
  28. text = src_file.read()
  29. for re_src, re_repl in sorted(repl_map.items()):
  30. text = re.sub(re_src, re_repl, text)
  31. for re_src, re_repl in sorted(extra_map.items()):
  32. text = re.sub(re_src, re_repl, text)
  33. text = text.rstrip() + '\n'
  34. with open(dest, 'w') as dest_file:
  35. dest_file.write(text)
  36. def update_sources(glibc_srcdir, quadmath_srcdir):
  37. """Update libquadmath sources."""
  38. glibc_ldbl128 = os.path.join(glibc_srcdir, 'sysdeps/ieee754/ldbl-128')
  39. glibc_math = os.path.join(glibc_srcdir, 'math')
  40. quadmath_math = os.path.join(quadmath_srcdir, 'math')
  41. float128_h = os.path.join(glibc_srcdir,
  42. 'sysdeps/ieee754/float128/float128_private.h')
  43. repl_map = {}
  44. # Use float128_private.h to get an initial list of names to
  45. # replace for libquadmath.
  46. repl_names = {}
  47. with open(float128_h, 'r') as header:
  48. for line in header:
  49. line = line.strip()
  50. if not line.startswith('#define '):
  51. continue
  52. match = re.fullmatch('^#define[ \t]+([a-zA-Z0-9_]+)'
  53. '[ \t]+([a-zA-Z0-9_]+)', line)
  54. if not match:
  55. continue
  56. macro = match.group(1)
  57. result = match.group(2)
  58. result = result.replace('f128', 'q')
  59. result = result.replace('__ieee754_', '')
  60. if result not in ('__expq_table', '__sincosq_table',
  61. '__builtin_signbit'):
  62. result = result.replace('__', '')
  63. result = result.replace('_do_not_use', '')
  64. if result in ('rem_pio2q', 'kernel_sincosq', 'kernel_sinq',
  65. 'kernel_cosq', 'kernel_tanq', 'gammaq_r',
  66. 'gamma_productq', 'lgamma_negq', 'lgamma_productq',
  67. 'lgammaq_r', 'x2y2m1q'):
  68. # Internal function names, for which the above removal
  69. # of leading '__' was inappropriate and a leading
  70. # '__quadmath_' needs adding instead. In the
  71. # libquadmath context, lgammaq_r is an internal name.
  72. result = '__quadmath_' + result
  73. if result == 'ieee854_float128_shape_type':
  74. result = 'ieee854_float128'
  75. if result == 'HUGE_VAL_F128':
  76. result = 'HUGE_VALQ'
  77. repl_names[macro] = result
  78. # More such names that aren't simply defined as object-like macros
  79. # in float128_private.h.
  80. repl_names['_Float128'] = '__float128'
  81. repl_names['SET_RESTORE_ROUNDL'] = 'SET_RESTORE_ROUNDF128'
  82. repl_names['parts32'] = 'words32'
  83. for macro in ('GET_LDOUBLE_LSW64', 'GET_LDOUBLE_MSW64',
  84. 'GET_LDOUBLE_WORDS64', 'SET_LDOUBLE_LSW64',
  85. 'SET_LDOUBLE_MSW64', 'SET_LDOUBLE_WORDS64'):
  86. repl_names[macro] = macro.replace('LDOUBLE', 'FLT128')
  87. # The classication macros are replaced.
  88. for macro in ('FP_NAN', 'FP_INFINITE', 'FP_ZERO', 'FP_SUBNORMAL',
  89. 'FP_NORMAL'):
  90. repl_names[macro] = 'QUAD' + macro
  91. for macro in ('fpclassify', 'signbit', 'isnan', 'isinf', 'issignaling'):
  92. repl_names[macro] = macro + 'q'
  93. repl_names['isfinite'] = 'finiteq'
  94. # Map comparison macros to the __builtin forms.
  95. for macro in ('isgreater', 'isgreaterequal', 'isless', 'islessequal',
  96. 'islessgreater', 'isunordered'):
  97. repl_names[macro] = '__builtin_' + macro
  98. # Replace macros used in type-generic templates in glibc.
  99. repl_names['FLOAT'] = '__float128'
  100. repl_names['CFLOAT'] = '__complex128'
  101. repl_names['M_NAN'] = 'nanq ("")'
  102. repl_names['M_HUGE_VAL'] = 'HUGE_VALQ'
  103. repl_names['INFINITY'] = '__builtin_inf ()'
  104. for macro in ('MIN_EXP', 'MAX_EXP', 'MIN', 'MAX', 'MANT_DIG', 'EPSILON'):
  105. repl_names['M_%s' % macro] = 'FLT128_%s' % macro
  106. for macro in ('COPYSIGN', 'FABS', 'SINCOS', 'SCALBN', 'LOG1P', 'ATAN2',
  107. 'COSH', 'EXP', 'HYPOT', 'LOG', 'SINH', 'SQRT'):
  108. repl_names['M_%s' % macro] = macro.lower() + 'q'
  109. # Each such name is replaced when it appears as a whole word.
  110. for macro in repl_names:
  111. repl_map[r'\b%s\b' % macro] = repl_names[macro]
  112. # Also replace the L macro for constants; likewise M_LIT and M_MLIT.
  113. repl_map[r'\bL *\((.*?)\)'] = r'\1Q'
  114. repl_map[r'\bM_LIT *\((.*?)\)'] = r'\1Q'
  115. repl_map[r'\bM_MLIT *\((.*?)\)'] = r'\1q'
  116. # M_DECL_FUNC and M_SUF need similar replacements.
  117. repl_map[r'\bM_DECL_FUNC *\((?:__)?(?:ieee754_)?(.*?)\)'] = r'\1q'
  118. repl_map[r'\bM_SUF *\((?:__)?(?:ieee754_)?(.*?)\)'] = r'\1q'
  119. # Further adjustments are then needed for certain internal
  120. # functions called via M_SUF.
  121. repl_map[r'\bx2y2m1q\b'] = '__quadmath_x2y2m1q'
  122. repl_map[r'\bkernel_casinhq\b'] = '__quadmath_kernel_casinhq'
  123. # Replace calls to __set_errno.
  124. repl_map[r'\b__set_errno *\((.*?)\)'] = r'errno = \1'
  125. # Eliminate glibc diagnostic macros.
  126. repl_map[r' *\bDIAG_PUSH_NEEDS_COMMENT;'] = ''
  127. repl_map[r' *\bDIAG_IGNORE_NEEDS_COMMENT *\(.*?\);'] = ''
  128. repl_map[r' *\bDIAG_POP_NEEDS_COMMENT;'] = ''
  129. # Different names used in union.
  130. repl_map[r'\.d\b'] = '.value'
  131. repl_map[r'\bunion ieee854_float128\b'] = 'ieee854_float128'
  132. # Calls to alias and hidden_def macros are all eliminated.
  133. for macro in ('strong_alias', 'weak_alias', 'libm_alias_ldouble',
  134. 'declare_mgen_alias', 'declare_mgen_finite_alias',
  135. 'libm_hidden_def', 'mathx_hidden_def'):
  136. repl_map[r'\b%s *\(.*?\);?' % macro] = ''
  137. # Replace all #includes with a single include of quadmath-imp.h.
  138. repl_map['(\n+#include[^\n]*)+\n+'] = '\n\n#include "quadmath-imp.h"\n\n'
  139. # Omitted from this list because code comes from more than one
  140. # glibc source file: rem_pio2.
  141. ldbl_files = {
  142. 'e_acoshl.c': 'acoshq.c', 'e_acosl.c': 'acosq.c',
  143. 's_asinhl.c': 'asinhq.c', 'e_asinl.c': 'asinq.c',
  144. 'e_atan2l.c': 'atan2q.c', 'e_atanhl.c': 'atanhq.c',
  145. 's_atanl.c': 'atanq.c', 's_cbrtl.c': 'cbrtq.c', 's_ceill.c': 'ceilq.c',
  146. 's_copysignl.c': 'copysignq.c', 'e_coshl.c': 'coshq.c',
  147. 's_cosl.c': 'cosq.c', 'k_cosl.c': 'cosq_kernel.c',
  148. 's_erfl.c': 'erfq.c', 's_expm1l.c': 'expm1q.c', 'e_expl.c': 'expq.c',
  149. 't_expl.h': 'expq_table.h', 's_fabsl.c': 'fabsq.c',
  150. 's_finitel.c': 'finiteq.c', 's_floorl.c': 'floorq.c',
  151. 's_fmal.c': 'fmaq.c', 'e_fmodl.c': 'fmodq.c', 's_frexpl.c': 'frexpq.c',
  152. 'e_lgammal_r.c': 'lgammaq.c', 'lgamma_negl.c': 'lgammaq_neg.c',
  153. 'lgamma_productl.c': 'lgammaq_product.c', 'e_hypotl.c': 'hypotq.c',
  154. 'e_ilogbl.c': 'ilogbq.c', 's_isinfl.c': 'isinfq.c',
  155. 's_isnanl.c': 'isnanq.c', 's_issignalingl.c': 'issignalingq.c',
  156. 'e_j0l.c': 'j0q.c', 'e_j1l.c': 'j1q.c', 'e_jnl.c': 'jnq.c',
  157. 's_llrintl.c': 'llrintq.c', 's_llroundl.c': 'llroundq.c',
  158. 'e_log10l.c': 'log10q.c', 's_log1pl.c': 'log1pq.c',
  159. 'e_log2l.c': 'log2q.c', 's_logbl.c': 'logbq.c', 'e_logl.c': 'logq.c',
  160. 's_lrintl.c': 'lrintq.c', 's_lroundl.c': 'lroundq.c',
  161. 's_modfl.c': 'modfq.c', 's_nearbyintl.c': 'nearbyintq.c',
  162. 's_nextafterl.c': 'nextafterq.c', 'e_powl.c': 'powq.c',
  163. 'e_remainderl.c': 'remainderq.c', 's_remquol.c': 'remquoq.c',
  164. 's_rintl.c': 'rintq.c', 's_roundl.c': 'roundq.c',
  165. 's_scalblnl.c': 'scalblnq.c', 's_scalbnl.c': 'scalbnq.c',
  166. 's_signbitl.c': 'signbitq.c', 't_sincosl.c': 'sincos_table.c',
  167. 's_sincosl.c': 'sincosq.c', 'k_sincosl.c': 'sincosq_kernel.c',
  168. 'e_sinhl.c': 'sinhq.c', 's_sinl.c': 'sinq.c',
  169. 'k_sinl.c': 'sinq_kernel.c', 's_tanhl.c': 'tanhq.c',
  170. 's_tanl.c': 'tanq.c', 'k_tanl.c': 'tanq_kernel.c',
  171. 'e_gammal_r.c': 'tgammaq.c', 'gamma_productl.c': 'tgammaq_product.c',
  172. 's_truncl.c': 'truncq.c', 'x2y2m1l.c': 'x2y2m1q.c'
  173. }
  174. template_files = {
  175. 's_cacosh_template.c': 'cacoshq.c', 's_cacos_template.c': 'cacosq.c',
  176. 's_casinh_template.c': 'casinhq.c',
  177. 'k_casinh_template.c': 'casinhq_kernel.c',
  178. 's_casin_template.c': 'casinq.c', 's_catanh_template.c': 'catanhq.c',
  179. 's_catan_template.c': 'catanq.c', 's_ccosh_template.c': 'ccoshq.c',
  180. 's_cexp_template.c': 'cexpq.c', 'cimag_template.c': 'cimagq.c',
  181. 's_clog10_template.c': 'clog10q.c', 's_clog_template.c': 'clogq.c',
  182. 'conj_template.c': 'conjq.c', 's_cproj_template.c': 'cprojq.c',
  183. 'creal_template.c': 'crealq.c', 's_csinh_template.c': 'csinhq.c',
  184. 's_csin_template.c': 'csinq.c', 's_csqrt_template.c': 'csqrtq.c',
  185. 's_ctanh_template.c': 'ctanhq.c', 's_ctan_template.c': 'ctanq.c',
  186. 'e_exp2_template.c': 'exp2q.c', 's_fdim_template.c': 'fdimq.c',
  187. 's_fmax_template.c': 'fmaxq.c', 's_fmin_template.c': 'fminq.c',
  188. 's_ldexp_template.c': 'ldexpq.c'
  189. }
  190. # Some files have extra substitutions to apply.
  191. extra_maps = defaultdict(dict)
  192. extra_maps['expq.c'] = {r'#include "quadmath-imp\.h"\n':
  193. '#include "quadmath-imp.h"\n'
  194. '#include "expq_table.h"\n'}
  195. extra_maps['ilogbq.c'] = {r'#include "quadmath-imp\.h"\n':
  196. '#include <math.h>\n'
  197. '#include "quadmath-imp.h"\n'
  198. '#ifndef FP_ILOGB0\n'
  199. '# define FP_ILOGB0 INT_MIN\n'
  200. '#endif\n'
  201. '#ifndef FP_ILOGBNAN\n'
  202. '# define FP_ILOGBNAN INT_MAX\n'
  203. '#endif\n',
  204. r'return ([A-Z0-9_]+);':
  205. r'{ errno = EDOM; feraiseexcept (FE_INVALID); '
  206. r'return \1; }'}
  207. extra_maps['lgammaq.c'] = {r'#include "quadmath-imp\.h"\n':
  208. '#include "quadmath-imp.h"\n'
  209. '#ifdef HAVE_MATH_H_SIGNGAM\n'
  210. '# include <math.h>\n'
  211. '#endif\n'
  212. '__float128\n'
  213. 'lgammaq (__float128 x)\n'
  214. '{\n'
  215. '#ifndef HAVE_MATH_H_SIGNGAM\n'
  216. ' int signgam;\n'
  217. '#endif\n'
  218. ' return __quadmath_lgammaq_r (x, &signgam);\n'
  219. '}\n'}
  220. extra_maps['tgammaq.c'] = {r'#include "quadmath-imp\.h"\n':
  221. '#include "quadmath-imp.h"\n'
  222. '__float128\n'
  223. 'tgammaq (__float128 x)\n'
  224. '{\n'
  225. ' int sign;\n'
  226. ' __float128 ret;\n'
  227. ' ret = __quadmath_gammaq_r (x, &sign);\n'
  228. ' return sign < 0 ? -ret : ret;\n'
  229. '}\n'}
  230. for src, dest in ldbl_files.items():
  231. replace_in_file(repl_map, extra_maps[dest],
  232. os.path.join(glibc_ldbl128, src),
  233. os.path.join(quadmath_math, dest))
  234. for src, dest in template_files.items():
  235. replace_in_file(repl_map, extra_maps[dest],
  236. os.path.join(glibc_math, src),
  237. os.path.join(quadmath_math, dest))
  238. def main():
  239. parser = argparse.ArgumentParser(description='Update libquadmath code.')
  240. parser.add_argument('glibc_srcdir', help='glibc source directory')
  241. parser.add_argument('quadmath_srcdir', help='libquadmath source directory')
  242. args = parser.parse_args()
  243. update_sources(args.glibc_srcdir, args.quadmath_srcdir)
  244. if __name__ == '__main__':
  245. main()