| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# Compute tables for longobject.c long_from_non_binary_base(). They are used |
| 4 |
# for conversions of strings to integers with a non-binary base. |
| 5 |
|
| 6 |
import math |
| 7 |
import textwrap |
| 8 |
|
| 9 |
|
| 10 |
def 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 |
|
| 22 |
def conv_tables(long_bits): |
| 23 |
PyLong_BASE = 1 << long_bits |
| 24 |
log_base_BASE = [0.0] * 37 |
| 25 |
convmultmax_base = [0] * 37 |
| 26 |
convwidth_base = [0] * 37 |
| 27 |
for base in range(2, 37): |
| 28 |
is_binary_base = (base & (base - 1)) == 0 |
| 29 |
if is_binary_base: |
| 30 |
continue # don't need, leave as zero |
| 31 |
convmax = base |
| 32 |
i = 1 |
| 33 |
log_base_BASE[base] = math.log(base) / math.log(PyLong_BASE) |
| 34 |
while True: |
| 35 |
next = convmax * base |
| 36 |
if next > PyLong_BASE: |
| 37 |
break |
| 38 |
convmax = next |
| 39 |
i += 1 |
| 40 |
convmultmax_base[base] = convmax |
| 41 |
assert i > 0 |
| 42 |
convwidth_base[base] = i |
| 43 |
return '\n'.join( |
| 44 |
[ |
| 45 |
format_array( |
| 46 |
'static const double log_base_BASE[37]', log_base_BASE |
| 47 |
), |
| 48 |
format_array( |
| 49 |
'static const int convwidth_base[37]', convwidth_base |
| 50 |
), |
| 51 |
format_array( |
| 52 |
'static const twodigits convmultmax_base[37]', |
| 53 |
convmultmax_base, |
| 54 |
), |
| 55 |
] |
| 56 |
) |
| 57 |
|
| 58 |
|
| 59 |
def main(): |
| 60 |
print( |
| 61 |
f'''\ |
| 62 |
// Tables are computed by Tools/scripts/long_conv_tables.py |
| 63 |
#if PYLONG_BITS_IN_DIGIT == 15 |
| 64 |
{conv_tables(15)} |
| 65 |
#elif PYLONG_BITS_IN_DIGIT == 30 |
| 66 |
{conv_tables(30)} |
| 67 |
#else |
| 68 |
#error "invalid PYLONG_BITS_IN_DIGIT value" |
| 69 |
#endif |
| 70 |
''' |
| 71 |
) |
| 72 |
|
| 73 |
|
| 74 |
if __name__ == '__main__': |
| 75 |
main() |