Skip to content

Commit b9793e1

Browse files
committed
update scripts
* fixed version updates in release script * formatted Python scripts with `black` - The uncompromising code formatter (https://black.readthedocs.io/) * f-string all the things
1 parent 6801b0c commit b9793e1

File tree

5 files changed

+271
-193
lines changed

5 files changed

+271
-193
lines changed

script/amalgamate.py

+99-77
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,122 @@
11
# text parts
2-
processed_files = { }
2+
processed_files = {}
33

44
# authors
5-
for filename in ['AUTHORS', 'CONTRIBUTORS']:
6-
with open(filename, encoding='utf8') as f:
7-
text = ''
8-
for line in f:
9-
if filename == 'AUTHORS':
10-
text += '// fast_float by ' + line
11-
if filename == 'CONTRIBUTORS':
12-
text += '// with contributions from ' + line
13-
processed_files[filename] = text + '//\n//\n'
5+
for filename in ["AUTHORS", "CONTRIBUTORS"]:
6+
with open(filename, encoding="utf8") as f:
7+
text = ""
8+
for line in f:
9+
if filename == "AUTHORS":
10+
text += "// fast_float by " + line
11+
if filename == "CONTRIBUTORS":
12+
text += "// with contributions from " + line
13+
processed_files[filename] = text + "//\n//\n"
1414

1515
# licenses
16-
for filename in ['LICENSE-MIT', 'LICENSE-APACHE', 'LICENSE-BOOST']:
17-
lines = []
18-
with open(filename, encoding='utf8') as f:
19-
lines = f.readlines()
16+
for filename in ["LICENSE-MIT", "LICENSE-APACHE", "LICENSE-BOOST"]:
17+
lines = []
18+
with open(filename, encoding="utf8") as f:
19+
lines = f.readlines()
2020

21-
# Retrieve subset required for inclusion in source
22-
if filename == 'LICENSE-APACHE':
23-
lines = [
24-
' Copyright 2021 The fast_float authors\n',
25-
*lines[179:-1]
26-
]
21+
# Retrieve subset required for inclusion in source
22+
if filename == "LICENSE-APACHE":
23+
lines = [" Copyright 2021 The fast_float authors\n", *lines[179:-1]]
2724

28-
text = ''
29-
for line in lines:
30-
line = line.strip()
31-
if len(line):
32-
line = ' ' + line
33-
text += '//' + line + '\n'
34-
processed_files[filename] = text
25+
text = ""
26+
for line in lines:
27+
line = line.strip()
28+
if len(line):
29+
line = " " + line
30+
text += "//" + line + "\n"
31+
processed_files[filename] = text
3532

3633
# code
37-
for filename in [ 'constexpr_feature_detect.h', 'float_common.h', 'fast_float.h', 'ascii_number.h',
38-
'fast_table.h', 'decimal_to_binary.h', 'bigint.h', 'digit_comparison.h', 'parse_number.h']:
39-
with open('include/fast_float/' + filename, encoding='utf8') as f:
40-
text = ''
41-
for line in f:
42-
if line.startswith('#include "'): continue
43-
text += line
44-
processed_files[filename] = '\n' + text
34+
for filename in [
35+
"constexpr_feature_detect.h",
36+
"float_common.h",
37+
"fast_float.h",
38+
"ascii_number.h",
39+
"fast_table.h",
40+
"decimal_to_binary.h",
41+
"bigint.h",
42+
"digit_comparison.h",
43+
"parse_number.h",
44+
]:
45+
with open("include/fast_float/" + filename, encoding="utf8") as f:
46+
text = ""
47+
for line in f:
48+
if line.startswith('#include "'):
49+
continue
50+
text += line
51+
processed_files[filename] = "\n" + text
4552

4653
# command line
4754
import argparse
4855

49-
parser = argparse.ArgumentParser(description='Amalgamate fast_float.')
50-
parser.add_argument('--license', default='TRIPLE', choices=['DUAL', 'TRIPLE', 'MIT', 'APACHE', 'BOOST'], help='choose license')
51-
parser.add_argument('--output', default='', help='output file (stdout if none')
56+
parser = argparse.ArgumentParser(description="Amalgamate fast_float.")
57+
parser.add_argument(
58+
"--license",
59+
default="TRIPLE",
60+
choices=["DUAL", "TRIPLE", "MIT", "APACHE", "BOOST"],
61+
help="choose license",
62+
)
63+
parser.add_argument("--output", default="", help="output file (stdout if none")
5264

5365
args = parser.parse_args()
5466

67+
5568
def license_content(license_arg):
56-
result = []
57-
if license_arg == 'TRIPLE':
58-
result += [
59-
'// Licensed under the Apache License, Version 2.0, or the\n',
60-
'// MIT License or the Boost License. This file may not be copied,\n',
61-
'// modified, or distributed except according to those terms.\n',
62-
'//\n'
63-
]
64-
if license_arg == 'DUAL':
65-
result += [
66-
'// Licensed under the Apache License, Version 2.0, or the\n',
67-
'// MIT License at your option. This file may not be copied,\n',
68-
'// modified, or distributed except according to those terms.\n',
69-
'//\n'
70-
]
69+
result = []
70+
if license_arg == "TRIPLE":
71+
result += [
72+
"// Licensed under the Apache License, Version 2.0, or the\n",
73+
"// MIT License or the Boost License. This file may not be copied,\n",
74+
"// modified, or distributed except according to those terms.\n",
75+
"//\n",
76+
]
77+
if license_arg == "DUAL":
78+
result += [
79+
"// Licensed under the Apache License, Version 2.0, or the\n",
80+
"// MIT License at your option. This file may not be copied,\n",
81+
"// modified, or distributed except according to those terms.\n",
82+
"//\n",
83+
]
84+
85+
if license_arg in ("DUAL", "TRIPLE", "MIT"):
86+
result.append("// MIT License Notice\n//\n")
87+
result.append(processed_files["LICENSE-MIT"])
88+
result.append("//\n")
89+
if license_arg in ("DUAL", "TRIPLE", "APACHE"):
90+
result.append("// Apache License (Version 2.0) Notice\n//\n")
91+
result.append(processed_files["LICENSE-APACHE"])
92+
result.append("//\n")
93+
if license_arg in ("TRIPLE", "BOOST"):
94+
result.append("// BOOST License Notice\n//\n")
95+
result.append(processed_files["LICENSE-BOOST"])
96+
result.append("//\n")
7197

72-
if license_arg in ('DUAL', 'TRIPLE', 'MIT'):
73-
result.append('// MIT License Notice\n//\n')
74-
result.append(processed_files['LICENSE-MIT'])
75-
result.append('//\n')
76-
if license_arg in ('DUAL', 'TRIPLE', 'APACHE'):
77-
result.append('// Apache License (Version 2.0) Notice\n//\n')
78-
result.append(processed_files['LICENSE-APACHE'])
79-
result.append('//\n')
80-
if license_arg in ('TRIPLE', 'BOOST'):
81-
result.append('// BOOST License Notice\n//\n')
82-
result.append(processed_files['LICENSE-BOOST'])
83-
result.append('//\n')
98+
return result
8499

85-
return result
86100

87-
text = ''.join([
88-
processed_files['AUTHORS'], processed_files['CONTRIBUTORS'],
89-
*license_content(args.license),
90-
processed_files['constexpr_feature_detect.h'],
91-
processed_files['float_common.h'], processed_files['fast_float.h'],
92-
processed_files['ascii_number.h'], processed_files['fast_table.h'],
93-
processed_files['decimal_to_binary.h'], processed_files['bigint.h'],
94-
processed_files['digit_comparison.h'], processed_files['parse_number.h']])
101+
text = "".join(
102+
[
103+
processed_files["AUTHORS"],
104+
processed_files["CONTRIBUTORS"],
105+
*license_content(args.license),
106+
processed_files["constexpr_feature_detect.h"],
107+
processed_files["float_common.h"],
108+
processed_files["fast_float.h"],
109+
processed_files["ascii_number.h"],
110+
processed_files["fast_table.h"],
111+
processed_files["decimal_to_binary.h"],
112+
processed_files["bigint.h"],
113+
processed_files["digit_comparison.h"],
114+
processed_files["parse_number.h"],
115+
]
116+
)
95117

96118
if args.output:
97-
with open(args.output, 'wt', encoding='utf8') as f:
98-
f.write(text)
119+
with open(args.output, "wt", encoding="utf8") as f:
120+
f.write(text)
99121
else:
100-
print(text)
122+
print(text)

script/analysis.py

+30-28
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
1+
import sys
12
from math import floor
23

4+
35
def log2(x):
4-
"""returns ceil(log2(x)))"""
5-
y = 0
6-
while((1<<y) < x):
7-
y = y + 1
8-
return y
6+
"""returns ceil(log2(x)))"""
7+
y = 0
8+
while (1 << y) < x:
9+
y = y + 1
10+
return y
911

1012

11-
for q in range(1,17+1):
12-
d = 5**q
13+
for q in range(1, 17 + 1):
14+
d = 5 ** q
1315
b = 127 + log2(d)
14-
t = 2** b
15-
c = t//d + 1
16-
assert c < 2**128
17-
assert c >= 2**127
18-
K = 2**127
19-
if(not(c * K * d<=( K + 1) * t)):
20-
print(q)
21-
top = floor(t/(c * d - t))
22-
sys.exit(-1)
16+
t = 2 ** b
17+
c = t // d + 1
18+
assert c < 2 ** 128
19+
assert c >= 2 ** 127
20+
K = 2 ** 127
21+
if not (c * K * d <= (K + 1) * t):
22+
print(q)
23+
top = floor(t / (c * d - t))
24+
sys.exit(-1)
2325

24-
for q in range(18, 344+1):
25-
d = 5**q
26-
b = 64 + 2*log2(d)
27-
t = 2**b
28-
c = t//d + 1
29-
assert c > 2**(64 +log2(d))
30-
K = 2**64
31-
if(not(c * K * d<=( K + 1) * t)):
32-
print(q)
33-
top = floor(t/(c * d - t))
34-
sys.exit(-1)
26+
for q in range(18, 344 + 1):
27+
d = 5 ** q
28+
b = 64 + 2 * log2(d)
29+
t = 2 ** b
30+
c = t // d + 1
31+
assert c > 2 ** (64 + log2(d))
32+
K = 2 ** 64
33+
if not (c * K * d <= (K + 1) * t):
34+
print(q)
35+
top = floor(t / (c * d - t))
36+
sys.exit(-1)
3537

36-
print("all good")
38+
print("all good")

script/mushtak_lemire.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@
99
# Appendix B of Number parsing at a gigabyte per second.
1010
# Software: Practice and Experience 2021;51(8):1700–1727.
1111
for q in range(-342, -27):
12-
power5 = 5**-q
12+
power5 = 5 ** -q
1313
z = 0
1414
while (1 << z) < power5:
1515
z += 1
1616
b = 2 * z + 2 * 64
17-
c = 2**b // power5 + 1
17+
c = 2 ** b // power5 + 1
1818
while c >= (1 << 128):
1919
c //= 2
2020
all_tqs.append(c)
2121
for q in range(-27, 0):
22-
power5 = 5**-q
22+
power5 = 5 ** -q
2323
z = 0
2424
while (1 << z) < power5:
2525
z += 1
2626
b = z + 127
27-
c = 2**b // power5 + 1
27+
c = 2 ** b // power5 + 1
2828
all_tqs.append(c)
2929
for q in range(0, 308 + 1):
30-
power5 = 5**q
30+
power5 = 5 ** q
3131
while power5 < (1 << 127):
3232
power5 *= 2
3333
while power5 >= (1 << 128):
@@ -44,6 +44,7 @@ def continued_fraction(numer, denom):
4444
numer, denom = denom, rem
4545
return cf
4646

47+
4748
# Given a continued fraction [a0; a1, a2, ..., an], returns
4849
# all the convergents of that continued fraction
4950
# as pairs of the form (numer, denom), where numer/denom is
@@ -58,17 +59,22 @@ def convergents(cf):
5859
p_n = a_n * p_n_minus_1 + p_n_minus_2
5960
q_n = a_n * q_n_minus_1 + q_n_minus_2
6061
convergents.append((p_n, q_n))
61-
p_n_minus_2, q_n_minus_2, p_n_minus_1, q_n_minus_1 = p_n_minus_1, q_n_minus_1, p_n, q_n
62+
p_n_minus_2, q_n_minus_2, p_n_minus_1, q_n_minus_1 = (
63+
p_n_minus_1,
64+
q_n_minus_1,
65+
p_n,
66+
q_n,
67+
)
6268
return convergents
6369

6470

6571
# Enumerate through all the convergents of T[q] / 2^137 with denominators < 2^64
6672
found_solution = False
6773
for j, tq in enumerate(all_tqs):
68-
for _, w in convergents(continued_fraction(tq, 2**137)):
69-
if w >= 2**64:
74+
for _, w in convergents(continued_fraction(tq, 2 ** 137)):
75+
if w >= 2 ** 64:
7076
break
71-
if (tq*w) % 2**137 > 2**137 - 2**64:
77+
if (tq * w) % 2 ** 137 > 2 ** 137 - 2 ** 64:
7278
print(f"SOLUTION: q={j-342} T[q]={tq} w={w}")
7379
found_solution = True
7480
if not found_solution:

0 commit comments

Comments
 (0)