-
Notifications
You must be signed in to change notification settings - Fork 18
/
vc_wrapper_generator.py
171 lines (142 loc) · 5.18 KB
/
vc_wrapper_generator.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#! /usr/bin/python
"""
Generates vc wrapper - both header and .cc file
"""
RESTRICT="__restrict__"
VC_PREF="vc_"
VC_SCOPE_OP="Vc::"
FUNCTIONS_LIST=["asin",
"atan",
"cos",
"inv",
"log",
"sin",
"isqrt",
"identity"]
VC_WRAPPER_HEADER='vdtdiag_vcWrapper.h'
VC_WRAPPER_IMPL='vdtdiag_vcWrapper.cc'
#------------------------------------------------------------------
def get_type_dependent_parts(is_double, is_vector):
suffix=""
type="double"
if(is_double):
if(is_vector):
suffix="v"
else:
type="float"
suffix="f"
if(is_vector):
suffix="fv"
data_type="%s" %(type)
if(is_vector):
data_type="%s* %s" %(type, RESTRICT)
vc_data_type="%s%s_v" %(VC_SCOPE_OP,type)
return (type, data_type, vc_data_type, suffix)
#------------------------------------------------------------------
def get_function_prototype_and_vctype(fcn_name,is_double,is_vector):
(type,data_type,vc_type,suffix)=get_type_dependent_parts(is_double,is_vector)
prototype="%s %s%s%s(%s x)" %(type,VC_PREF,fcn_name,suffix,data_type)
if(is_vector):
prototype="void %s%s%s(const uint32_t size, %s iarray, %s oarray)" %(VC_PREF,fcn_name,suffix,data_type,data_type)
return (prototype,vc_type)
#-------------------------------------------------------------------
# translation of raw name withnout suffixes
def get_vc_fcnname_translation(fcn_name):
if(fcn_name == "inv"):
vc_name = "reciprocal"
elif(fcn_name == "isqrt"):
vc_name = "rsqrt"
else:
vc_name = fcn_name
return vc_name
#-------------------------------------------------------------------
def get_function_code(fcn_name,vc_data_type,is_vector):
if(is_vector):
code = "{\n" +\
"\t%s in,out;\n" %(vc_data_type) +\
"\tconst uint32_t vcSize = %s::Size;\n" %(vc_data_type) +\
"\tuint32_t step,i;\n"\
"\tfor(step=0;step < size-(size%vcSize); step += vcSize){\n" +\
"\t in.load(iarray+step, Vc::Unaligned);\n"
if(fcn_name == "identity"):
code += "\t in.store(oarray+step,Vc::Unaligned);\n"
else:
code += "\t Vc::%s(in).store(oarray+step,Vc::Unaligned);\n" %(get_vc_fcnname_translation(fcn_name))
code += "\t}\n"+\
"\tuint32_t tail = size-step;\n" +\
"\tfor(i=0;i<tail;i++)\n" +\
"\t in[i]=iarray[step+i];\n"
if(fcn_name == "identity"):
code += "\tout = in;\n"
else:
code += "\tout = Vc::%s(in);\n" %(get_vc_fcnname_translation(fcn_name))
code += "\tfor(i=0;i<tail;i++)\n" +\
"\t oarray[step+i]=out[i];\n}\n\n"
else:
code = "{\n" +\
"\t%s in;\n" %(vc_data_type) +\
"\tin[0] = x;\n"
if(fcn_name == "identity"):
code += "\t%s out = in;\n\treturn out[0];\n}\n\n" %(vc_data_type)
else:
code += "\treturn Vc::%s(in)[0];\n}\n\n" %(get_vc_fcnname_translation(fcn_name))
return code
#---------------------------------------------------------------------
def create_vcWrapper_signature(fcn_name,is_double=False,is_vector=False,is_impl=False):
(code,vc_type) = get_function_prototype_and_vctype(fcn_name,is_double,is_vector)
if is_impl:
code += get_function_code(fcn_name,vc_type,is_vector)
else:
code += ";\n"
return code
#------------------------------------------------------------------
def create_vcWrapper_signatures(is_impl=False):
code=""
for is_vector in (False,True):
for is_double in (True,False):
for fcn_name in sorted(FUNCTIONS_LIST):
code+=create_vcWrapper_signature(fcn_name,is_double,is_vector,is_impl)
return code
#------------------------------------------------------------------
def get_header_file():
code= "// Automatically generated\n"\
'#ifndef __VDT_VC_WRAPPER__\n'\
'#define __VDT_VC_WRAPPER__\n'\
'#include "inttypes.h"\n'+\
'//#ifdef __cplusplus\n'\
'extern "C"{\n'\
'//#endif\n'+\
create_vcWrapper_signatures(is_impl=False)+\
'//#ifdef __cplusplus\n'\
'}\n'\
'//#endif\n'\
'\n'\
'#endif\n'
return code
#------------------------------------------------------------------
def get_impl_file():
code= "// Automatically generated\n"+\
'#include "%s"\n' %VC_WRAPPER_HEADER+\
'#include "Vc/Vc"\n'+\
'//#ifdef __cplusplus\n'\
'extern "C"{\n'\
'//#endif\n'+\
create_vcWrapper_signatures(is_impl=True)+\
'//#ifdef __cplusplus\n'\
'}\n'\
'//#endif\n'
return code
#------------------------------------------------------------------
def create_header():
ofile=file(VC_WRAPPER_HEADER,'w')
ofile.write(get_header_file())
ofile.close()
#------------------------------------------------------------------
def create_impl():
ofile=file(VC_WRAPPER_IMPL,'w')
ofile.write(get_impl_file())
ofile.close()
#------------------------------------------------------------------
if __name__ == "__main__":
create_header()
create_impl()