-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
gh-130599: precompute conversion constants for long()
#130714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6888024
gh-130599: precompute conversion constants for ``long()``
nascheme 4f8cd77
Code tidy for assert().
nascheme 7912268
Update c-analyzer/cpython/ignored.tsv.
nascheme f06786e
Use static tables rather than computing on startup.
nascheme 6ce699d
Remove unneeded func declaration, fix comment.
nascheme d8b044c
Fix array declarations.
nascheme 3f9e1e7
Remove unused 'steps' variable.
nascheme 8c703fb
Merge 'origin/main' into gh-130599-long-conv-ts-fix
nascheme f4388ba
Don't compute unneeded constants.
nascheme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# Compute tables for longobject.c long_from_non_binary_base(). They are used | ||
# for conversions of strings to integers with a non-binary base. | ||
|
||
import math | ||
import textwrap | ||
|
||
|
||
def format_array(name, values): | ||
values = [str(v) for v in values] | ||
values = ', '.join(values) | ||
result = f'{name} = {{{values}}};' | ||
result = textwrap.wrap( | ||
result, | ||
initial_indent=' ' * 4, | ||
subsequent_indent=' ' * 8, | ||
) | ||
return '\n'.join(result) | ||
|
||
|
||
def conv_tables(long_bits): | ||
PyLong_BASE = 1 << long_bits | ||
log_base_BASE = [0.0] * 37 | ||
convmultmax_base = [0] * 37 | ||
convwidth_base = [0] * 37 | ||
for base in range(2, 37): | ||
is_binary_base = (base & (base - 1)) == 0 | ||
if is_binary_base: | ||
continue # don't need, leave as zero | ||
convmax = base | ||
i = 1 | ||
log_base_BASE[base] = math.log(base) / math.log(PyLong_BASE) | ||
while True: | ||
next = convmax * base | ||
if next > PyLong_BASE: | ||
break | ||
convmax = next | ||
i += 1 | ||
convmultmax_base[base] = convmax | ||
assert i > 0 | ||
convwidth_base[base] = i | ||
return '\n'.join( | ||
[ | ||
format_array( | ||
'static const double log_base_BASE[37]', log_base_BASE | ||
), | ||
format_array( | ||
'static const int convwidth_base[37]', convwidth_base | ||
), | ||
format_array( | ||
'static const twodigits convmultmax_base[37]', | ||
convmultmax_base, | ||
), | ||
] | ||
) | ||
|
||
|
||
def main(): | ||
print( | ||
f'''\ | ||
// Tables are computed by Tools/scripts/long_conv_tables.py | ||
#if PYLONG_BITS_IN_DIGIT == 15 | ||
{conv_tables(15)} | ||
#elif PYLONG_BITS_IN_DIGIT == 30 | ||
{conv_tables(30)} | ||
#else | ||
#error "invalid PYLONG_BITS_IN_DIGIT value" | ||
#endif | ||
''' | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.