Skip to content

Commit f4388ba

Browse files
committed
Don't compute unneeded constants.
This makes the pre-computed tables match exactly what was previously computed at runtime, if all base conversions were used. This makes them slightly smaller too.
1 parent 8c703fb commit f4388ba

File tree

2 files changed

+56
-48
lines changed

2 files changed

+56
-48
lines changed

Objects/longobject.c

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,51 +2823,50 @@ digit beyond the first.
28232823

28242824
// Tables are computed by Tools/scripts/long_conv_tables.py
28252825
#if PYLONG_BITS_IN_DIGIT == 15
2826-
static const double log_base_BASE[37] = {0.0, 0.0,
2827-
0.06666666666666667, 0.10566416671474375, 0.13333333333333333,
2828-
0.15479520632582416, 0.17233083338141042, 0.18715699480384027,
2829-
0.19999999999999998, 0.2113283334294875, 0.22146187299249084,
2830-
0.23062877457581984, 0.2389975000480771, 0.24669598120940617,
2831-
0.25382366147050694, 0.26045937304056793, 0.26666666666666666,
2832-
0.27249752275002265, 0.27799500009615413, 0.2831951675629057,
2833-
0.28812853965915747, 0.29282116151858406, 0.2972954412424865,
2834-
0.3015707970704675, 0.3056641667147438, 0.30959041265164833,
2835-
0.3133626478760728, 0.31699250014423125, 0.3204903281371736,
2836-
0.3238653996751715, 0.3271260397072346, 0.3302797540257917,
2837-
0.33333333333333337, 0.3362929412905636, 0.3391641894166893,
2838-
0.34195220112966446, 0.34466166676282084};
2839-
static const int convwidth_base[37] = {0, 0, 15, 9, 7, 6, 5, 5, 5,
2840-
4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2841-
3, 3, 3, 2, 2, 2, 2};
2842-
static const twodigits convmultmax_base[37] = {0, 0, 32768, 19683,
2843-
16384, 15625, 7776, 16807, 32768, 6561, 10000, 14641, 20736,
2844-
28561, 2744, 3375, 4096, 4913, 5832, 6859, 8000, 9261, 10648,
2845-
12167, 13824, 15625, 17576, 19683, 21952, 24389, 27000, 29791,
2846-
32768, 1089, 1156, 1225, 1296};
2826+
static const double log_base_BASE[37] = {0.0, 0.0, 0.0,
2827+
0.10566416671474375, 0.0, 0.15479520632582416,
2828+
0.17233083338141042, 0.18715699480384027, 0.0,
2829+
0.2113283334294875, 0.22146187299249084, 0.23062877457581984,
2830+
0.2389975000480771, 0.24669598120940617, 0.25382366147050694,
2831+
0.26045937304056793, 0.0, 0.27249752275002265,
2832+
0.27799500009615413, 0.2831951675629057, 0.28812853965915747,
2833+
0.29282116151858406, 0.2972954412424865, 0.3015707970704675,
2834+
0.3056641667147438, 0.30959041265164833, 0.3133626478760728,
2835+
0.31699250014423125, 0.3204903281371736, 0.3238653996751715,
2836+
0.3271260397072346, 0.3302797540257917, 0.0,
2837+
0.3362929412905636, 0.3391641894166893, 0.34195220112966446,
2838+
0.34466166676282084};
2839+
static const int convwidth_base[37] = {0, 0, 0, 9, 0, 6, 5, 5, 0,
2840+
4, 4, 4, 4, 4, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2841+
3, 3, 0, 2, 2, 2, 2};
2842+
static const twodigits convmultmax_base[37] = {0, 0, 0, 19683, 0,
2843+
15625, 7776, 16807, 0, 6561, 10000, 14641, 20736, 28561, 2744,
2844+
3375, 0, 4913, 5832, 6859, 8000, 9261, 10648, 12167, 13824,
2845+
15625, 17576, 19683, 21952, 24389, 27000, 29791, 0, 1089,
2846+
1156, 1225, 1296};
28472847
#elif PYLONG_BITS_IN_DIGIT == 30
2848-
static const double log_base_BASE[37] = {0.0, 0.0,
2849-
0.03333333333333333, 0.05283208335737188, 0.06666666666666667,
2850-
0.07739760316291208, 0.08616541669070521, 0.09357849740192013,
2851-
0.09999999999999999, 0.10566416671474375, 0.11073093649624542,
2852-
0.11531438728790992, 0.11949875002403855, 0.12334799060470308,
2853-
0.12691183073525347, 0.13022968652028397, 0.13333333333333333,
2854-
0.13624876137501132, 0.13899750004807707, 0.14159758378145285,
2855-
0.14406426982957873, 0.14641058075929203, 0.14864772062124326,
2856-
0.15078539853523376, 0.1528320833573719, 0.15479520632582416,
2857-
0.1566813239380364, 0.15849625007211562, 0.1602451640685868,
2858-
0.16193269983758574, 0.1635630198536173, 0.16513987701289584,
2859-
0.16666666666666669, 0.1681464706452818, 0.16958209470834465,
2860-
0.17097610056483223, 0.17233083338141042};
2861-
static const int convwidth_base[37] = {0, 0, 30, 18, 15, 12, 11,
2862-
10, 10, 9, 9, 8, 8, 8, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6,
2863-
6, 6, 6, 6, 6, 6, 5, 5, 5, 5};
2864-
static const twodigits convmultmax_base[37] = {0, 0, 1073741824,
2865-
387420489, 1073741824, 244140625, 362797056, 282475249,
2866-
1073741824, 387420489, 1000000000, 214358881, 429981696,
2867-
815730721, 105413504, 170859375, 268435456, 410338673,
2868-
612220032, 893871739, 64000000, 85766121, 113379904,
2869-
148035889, 191102976, 244140625, 308915776, 387420489,
2870-
481890304, 594823321, 729000000, 887503681, 1073741824,
2848+
static const double log_base_BASE[37] = {0.0, 0.0, 0.0,
2849+
0.05283208335737188, 0.0, 0.07739760316291208,
2850+
0.08616541669070521, 0.09357849740192013, 0.0,
2851+
0.10566416671474375, 0.11073093649624542, 0.11531438728790992,
2852+
0.11949875002403855, 0.12334799060470308, 0.12691183073525347,
2853+
0.13022968652028397, 0.0, 0.13624876137501132,
2854+
0.13899750004807707, 0.14159758378145285, 0.14406426982957873,
2855+
0.14641058075929203, 0.14864772062124326, 0.15078539853523376,
2856+
0.1528320833573719, 0.15479520632582416, 0.1566813239380364,
2857+
0.15849625007211562, 0.1602451640685868, 0.16193269983758574,
2858+
0.1635630198536173, 0.16513987701289584, 0.0,
2859+
0.1681464706452818, 0.16958209470834465, 0.17097610056483223,
2860+
0.17233083338141042};
2861+
static const int convwidth_base[37] = {0, 0, 0, 18, 0, 12, 11, 10,
2862+
0, 9, 9, 8, 8, 8, 7, 7, 0, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6,
2863+
6, 6, 6, 0, 5, 5, 5, 5};
2864+
static const twodigits convmultmax_base[37] = {0, 0, 0, 387420489,
2865+
0, 244140625, 362797056, 282475249, 0, 387420489, 1000000000,
2866+
214358881, 429981696, 815730721, 105413504, 170859375, 0,
2867+
410338673, 612220032, 893871739, 64000000, 85766121,
2868+
113379904, 148035889, 191102976, 244140625, 308915776,
2869+
387420489, 481890304, 594823321, 729000000, 887503681, 0,
28712870
39135393, 45435424, 52521875, 60466176};
28722871
#else
28732872
#error "invalid PYLONG_BITS_IN_DIGIT value"

Tools/scripts/long_conv_tables.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22
#
3-
# Compute tables for longobject.c integer conversion.
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.
45

56
import math
67
import textwrap
@@ -24,9 +25,12 @@ def conv_tables(long_bits):
2425
convmultmax_base = [0] * 37
2526
convwidth_base = [0] * 37
2627
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
2731
convmax = base
2832
i = 1
29-
log_base_BASE[base] = math.log(float(base)) / math.log(PyLong_BASE)
33+
log_base_BASE[base] = math.log(base) / math.log(PyLong_BASE)
3034
while True:
3135
next = convmax * base
3236
if next > PyLong_BASE:
@@ -38,11 +42,16 @@ def conv_tables(long_bits):
3842
convwidth_base[base] = i
3943
return '\n'.join(
4044
[
41-
format_array('static const double log_base_BASE[37]', log_base_BASE),
45+
format_array(
46+
'static const double log_base_BASE[37]', log_base_BASE
47+
),
4248
format_array(
4349
'static const int convwidth_base[37]', convwidth_base
4450
),
45-
format_array('static const twodigits convmultmax_base[37]', convmultmax_base),
51+
format_array(
52+
'static const twodigits convmultmax_base[37]',
53+
convmultmax_base,
54+
),
4655
]
4756
)
4857

0 commit comments

Comments
 (0)