5
5
ExponentPrecisionType ,
6
6
ExponentType ,
7
7
FixedPrecisionType ,
8
+ FloatPrecisionType ,
8
9
IntegerPrecisionType ,
9
10
NamedType ,
10
11
PackedType ,
12
+ StandardFloatPrecisionType ,
11
13
XnorPrecisionType ,
12
14
)
13
15
@@ -51,6 +53,25 @@ def definition_cpp(self):
51
53
return typestring
52
54
53
55
56
+ class APFloatPrecisionDefinition (PrecisionDefinition ):
57
+ def definition_cpp (self ):
58
+ raise NotImplementedError (
59
+ 'FloatPrecisionType is not supported in AP type precision definitions. Use StandardFloatPrecisionType instead.'
60
+ )
61
+
62
+
63
+ class APStandardFloatPrecisionDefinition (PrecisionDefinition ):
64
+ def definition_cpp (self ):
65
+ typestring = str (self )
66
+ if typestring .startswith ('std_float' ):
67
+ typestring = typestring .replace ('std_float' , 'ap_float' )
68
+ elif typestring == 'half' :
69
+ typestring = 'std::float16_t'
70
+ elif typestring == 'bfloat16' :
71
+ typestring = 'std::bfloat16_t'
72
+ return typestring
73
+
74
+
54
75
class ACIntegerPrecisionDefinition (PrecisionDefinition ):
55
76
def definition_cpp (self ):
56
77
typestring = f'ac_int<{ self .width } , { str (self .signed ).lower ()} >'
@@ -94,12 +115,40 @@ def definition_cpp(self):
94
115
return typestring
95
116
96
117
118
+ class ACFloatPrecisionDefinition (PrecisionDefinition ):
119
+ def _rounding_mode_cpp (self , mode ):
120
+ if mode is not None :
121
+ return 'AC_' + str (mode )
122
+
123
+ def definition_cpp (self ):
124
+ args = [
125
+ self .width ,
126
+ self .integer ,
127
+ self .exponent ,
128
+ self ._rounding_mode_cpp (self .rounding_mode ),
129
+ ]
130
+ if args [3 ] == 'AC_TRN' :
131
+ # This is the default, so we won't write the full definition for brevity
132
+ args [3 ] = None
133
+ args = ',' .join ([str (arg ) for arg in args [:5 ] if arg is not None ])
134
+ typestring = f'ac_float<{ args } >'
135
+ return typestring
136
+
137
+
138
+ class ACStandardFloatPrecisionDefinition (PrecisionDefinition ):
139
+ def definition_cpp (self ):
140
+ typestring = str (self )
141
+ if typestring .startswith ('std_float' ):
142
+ typestring = 'ac_' + typestring
143
+ return typestring
144
+
145
+
97
146
class PrecisionConverter :
98
147
def convert (self , precision_type ):
99
148
raise NotImplementedError
100
149
101
150
102
- class FixedPrecisionConverter (PrecisionConverter ):
151
+ class FPGAPrecisionConverter (PrecisionConverter ):
103
152
def __init__ (self , type_map , prefix ):
104
153
self .type_map = type_map
105
154
self .prefix = prefix
@@ -124,25 +173,29 @@ def convert(self, precision_type):
124
173
raise Exception (f'Cannot convert precision type to { self .prefix } : { precision_type .__class__ .__name__ } ' )
125
174
126
175
127
- class APTypeConverter (FixedPrecisionConverter ):
176
+ class APTypeConverter (FPGAPrecisionConverter ):
128
177
def __init__ (self ):
129
178
super ().__init__ (
130
179
type_map = {
131
180
FixedPrecisionType : APFixedPrecisionDefinition ,
132
181
IntegerPrecisionType : APIntegerPrecisionDefinition ,
182
+ FloatPrecisionType : APFloatPrecisionDefinition ,
183
+ StandardFloatPrecisionType : APStandardFloatPrecisionDefinition ,
133
184
ExponentPrecisionType : APIntegerPrecisionDefinition ,
134
185
XnorPrecisionType : APIntegerPrecisionDefinition ,
135
186
},
136
187
prefix = 'AP' ,
137
188
)
138
189
139
190
140
- class ACTypeConverter (FixedPrecisionConverter ):
191
+ class ACTypeConverter (FPGAPrecisionConverter ):
141
192
def __init__ (self ):
142
193
super ().__init__ (
143
194
type_map = {
144
195
FixedPrecisionType : ACFixedPrecisionDefinition ,
145
196
IntegerPrecisionType : ACIntegerPrecisionDefinition ,
197
+ FloatPrecisionType : ACFloatPrecisionDefinition ,
198
+ StandardFloatPrecisionType : ACStandardFloatPrecisionDefinition ,
146
199
ExponentPrecisionType : ACIntegerPrecisionDefinition ,
147
200
XnorPrecisionType : ACIntegerPrecisionDefinition ,
148
201
},
0 commit comments