codekingpro/portable-devtools
114k
1#!/usr/bin/env python32#3# Compute tables for longobject.c long_from_non_binary_base(). They are used4# for conversions of strings to integers with a non-binary base.5 6import math7import textwrap8 9 10def format_array(name, values):11 values = [str(v) for v in values]12 values = ', '.join(values)13 result = f'{name} = {{{values}}};'14 result = textwrap.wrap(15 result,16 initial_indent=' ' * 4,17 subsequent_indent=' ' * 8,18 )19 return '\n'.join(result)20 21 22def conv_tables(long_bits):23 PyLong_BASE = 1 << long_bits24 log_base_BASE = [0.0] * 3725 convmultmax_base = [0] * 3726 convwidth_base = [0] * 3727 for base in range(2, 37):28 is_binary_base = (base & (base - 1)) == 029 if is_binary_base:30 continue # don't need, leave as zero31 convmax = base32 i = 133 log_base_BASE[base] = math.log(base) / math.log(PyLong_BASE)34 while True:35 next = convmax * base36 if next > PyLong_BASE:37 break38 convmax = next39 i += 140 convmultmax_base[base] = convmax41 assert i > 042 convwidth_base[base] = i43 return '\n'.join(44 [45 format_array(46 'static const double log_base_BASE[37]', log_base_BASE47 ),48 format_array(49 'static const int convwidth_base[37]', convwidth_base50 ),51 format_array(52 'static const twodigits convmultmax_base[37]',53 convmultmax_base,54 ),55 ]56 )57 58 59def main():60 print(61 f'''\62// Tables are computed by Tools/scripts/long_conv_tables.py63#if PYLONG_BITS_IN_DIGIT == 1564{conv_tables(15)}65#elif PYLONG_BITS_IN_DIGIT == 3066{conv_tables(30)}67#else68 #error "invalid PYLONG_BITS_IN_DIGIT value"69#endif70'''71 )72 73 74if __name__ == '__main__':75 main()76 