-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.py
135 lines (108 loc) · 6.39 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Copyright (c) 2021 Arm Limited
# SPDX-License-Identifier: MIT
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys, argparse
import itertools
from standalone import Toom4Standalone
from evaluation import Toom4Evaluation
from interpolation import Toom4Interpolation
def main(argv):
outfile = None
degree = None
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--dim", type=int, default=32,
help="The dimension of the polynomials to be multiplied. Equivalently, one less than the polynomial degree.")
parser.add_argument("--out", type=str, default=None,
help="The name of the file to write the generated assembly to. If unspecified, the assembly will be printed on the standard output.")
parser.add_argument("--padding", dest='padding', action='store_true',
help="Add padding between the Toom4 evaluations before calling the inner multiplication. Padding may be useful assumption for the inner multiplication routine to simplify the code. This only makes sense in mode 'standalone'.")
parser.add_argument("--keep-no-op-evals-on-stack", dest='keep_no_op_evals_on_stack', action='store_true')
parser.add_argument("--no-comments", dest='no_comments', action='store_true')
parser.add_argument("--no-overlap", dest='no_overlap', action='store_true')
parser.add_argument("--toom4-interpolation-save-gprs", dest='toom4_save_gprs', action='store_true')
parser.add_argument("--striding", dest='striding', action='store_true')
parser.add_argument("--layout", type=str, choices=["top","bottom","standard","karatsuba_x1","karatsuba_x2"], default="standard")
parser.add_argument("--out-of-place", action='store_true', default=False)
parser.add_argument("--inner", type=str, default=None,
help="The name of the inner polynomial multiplication routine to call. This must be specified in mode 'standalone' and is meaningless in mode 'evaluation'")
parser.add_argument("mode", type=str, choices=['standalone','evaluation','interpolation'],
help="What functionality to generate code for: 'standalone' for a standalone polynomial multiplication routine based on Toom4 and an inner multiplication specified by --inner. 'evaluation' for a standalone evaluation routine which performs the Toom4 evaluation step in-place, similar to the forward-NTT. 'interpolation' for a standalone interpolation routine which performs the Toom4 interpolation step in-place, similar to the inverse-NTT. ")
parser.add_argument("symbol", type=str,
help="The name of the function symbol to emit.")
args = parser.parse_args()
src_a = 'r1'
src_b = 'r2'
dst = 'r0'
code_all = []
code_essential = []
line_count = 0;
def is_code_line(line):
if len(line) < 2:
return False
if line[0:2] == '//':
return False
return True
if args.mode == 'standalone':
if args.inner == None:
raise Exception("In standalone mode, an inner multiplication routine must be specified via --inner.")
toom4 = Toom4Standalone(src_a,src_b,dst,args.dim,
args.inner, args.symbol,
add_padding=args.padding,
keep_no_op_evals_on_stack=args.keep_no_op_evals_on_stack,
toom4_interpolation_save_gprs=args.toom4_save_gprs)
code_gen = toom4.run()
elif args.mode == 'evaluation':
toom4 = Toom4Evaluation(args.dim, striding=args.striding,
layout=args.layout,
out_of_place=args.out_of_place)
code_gen = toom4.standalone(args.symbol)
elif args.mode == 'interpolation':
if args.no_overlap:
toom4 = Toom4Interpolation(int(args.dim / 4), no_overlap=True, dual=False,
save_gprs=args.toom4_save_gprs)
elif args.striding:
toom4 = Toom4Interpolation(int(args.dim / 4), no_overlap=False, dual=True,
dual_layout=args.layout,
save_gprs=args.toom4_save_gprs,
out_of_place=args.out_of_place)
else:
toom4 = Toom4Interpolation(2*int(args.dim / 4), no_overlap=False, dual=False,
save_gprs=args.toom4_save_gprs)
code_gen = toom4.standalone('r0', args.symbol)
for line in code_gen:
code_all.append(line)
# The code generation sometimes emits multiple lines -- split those
code_all = list(itertools.chain.from_iterable(map(lambda s: s.split("\n"), code_all)))
code_essential = list(filter(is_code_line, code_all))
line_count_total = len(code_all)
line_count_essential = len(list(code_essential))
code_all.append(f'')
code_all.append(f'// Instruction count: {line_count_essential}')
if not args.no_comments:
code_all.append(f'// Line count: {line_count_total}')
code_all_str = "\n".join(code_all)
else:
code_all_str = "\n".join(list(code_essential))
if not args.out == None:
f = open(args.out,"w")
f.write(code_all_str)
f.close()
else:
print(code_all_str)
if __name__ == "__main__":
main(sys.argv[1:])