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