@@ -8,27 +8,42 @@ use super::cli::Language;
8
8
use super :: indentation:: Indentation ;
9
9
use super :: values:: value_for_array;
10
10
11
+ #[ derive( Debug , PartialEq , Copy , Clone ) ]
12
+ pub enum Sign {
13
+ Signed ,
14
+ Unsigned ,
15
+ }
16
+
11
17
#[ derive( Debug , PartialEq , Copy , Clone ) ]
12
18
pub enum TypeKind {
13
19
BFloat ,
14
20
Float ,
15
- Int ,
16
- UInt ,
21
+
22
+ // if signed, then the inner value is true
23
+ Int ( Sign ) ,
24
+ Char ( Sign ) ,
17
25
Poly ,
18
26
Void ,
27
+ Mask ,
28
+ Vector ,
19
29
}
20
30
21
31
impl FromStr for TypeKind {
22
32
type Err = String ;
23
33
24
34
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
25
35
match s {
26
- "bfloat" => Ok ( Self :: BFloat ) ,
27
- "float" => Ok ( Self :: Float ) ,
28
- "int" => Ok ( Self :: Int ) ,
36
+ "bfloat" | "BF16" => Ok ( Self :: BFloat ) ,
37
+ "float" | "double" | "FP16" | "FP32" | "FP64" => Ok ( Self :: Float ) ,
38
+ "int" | "long" | "short" | "SI8" | "SI16" | "SI32" | "SI64" => {
39
+ Ok ( Self :: Int ( Sign :: Signed ) )
40
+ }
29
41
"poly" => Ok ( Self :: Poly ) ,
30
- "uint" | "unsigned" => Ok ( Self :: UInt ) ,
42
+ "char" => Ok ( Self :: Char ( Sign :: Signed ) ) ,
43
+ "uint" | "unsigned" | "UI8" | "UI16" | "UI32" | "UI64" => Ok ( Self :: Int ( Sign :: Unsigned ) ) ,
31
44
"void" => Ok ( Self :: Void ) ,
45
+ "MASK" => Ok ( Self :: Mask ) ,
46
+ "M64" | "M128" | "M256" | "M512" => Ok ( Self :: Vector ) ,
32
47
_ => Err ( format ! ( "Impossible to parse argument kind {s}" ) ) ,
33
48
}
34
49
}
@@ -42,10 +57,14 @@ impl fmt::Display for TypeKind {
42
57
match self {
43
58
Self :: BFloat => "bfloat" ,
44
59
Self :: Float => "float" ,
45
- Self :: Int => "int" ,
46
- Self :: UInt => "uint" ,
60
+ Self :: Int ( Sign :: Signed ) => "int" ,
61
+ Self :: Int ( Sign :: Unsigned ) => "uint" ,
47
62
Self :: Poly => "poly" ,
48
63
Self :: Void => "void" ,
64
+ Self :: Char ( Sign :: Signed ) => "char" ,
65
+ Self :: Char ( Sign :: Unsigned ) => "unsigned char" ,
66
+ Self :: Mask => "mask" ,
67
+ Self :: Vector => "vector" ,
49
68
}
50
69
)
51
70
}
@@ -56,20 +75,24 @@ impl TypeKind {
56
75
pub fn c_prefix ( & self ) -> & str {
57
76
match self {
58
77
Self :: Float => "float" ,
59
- Self :: Int => "int" ,
60
- Self :: UInt => "uint" ,
78
+ Self :: Int ( Sign :: Signed ) => "int" ,
79
+ Self :: Int ( Sign :: Unsigned ) => "uint" ,
61
80
Self :: Poly => "poly" ,
81
+ Self :: Char ( Sign :: Signed ) => "char" ,
62
82
_ => unreachable ! ( "Not used: {:#?}" , self ) ,
63
83
}
64
84
}
65
85
66
86
/// Gets the rust prefix for the type kind i.e. i, u, f.
67
87
pub fn rust_prefix ( & self ) -> & str {
68
88
match self {
89
+ Self :: BFloat => "bf" ,
69
90
Self :: Float => "f" ,
70
- Self :: Int => "i" ,
71
- Self :: UInt => "u" ,
91
+ Self :: Int ( Sign :: Signed ) => "i" ,
92
+ Self :: Int ( Sign :: Unsigned ) => "u" ,
72
93
Self :: Poly => "u" ,
94
+ Self :: Char ( Sign :: Unsigned ) => "u" ,
95
+ Self :: Char ( Sign :: Signed ) => "i" ,
73
96
_ => unreachable ! ( "Unused type kind: {:#?}" , self ) ,
74
97
}
75
98
}
@@ -133,11 +156,17 @@ impl IntrinsicType {
133
156
}
134
157
135
158
pub fn c_scalar_type ( & self ) -> String {
136
- format ! (
137
- "{prefix}{bits}_t" ,
138
- prefix = self . kind( ) . c_prefix( ) ,
139
- bits = self . inner_size( )
140
- )
159
+ match self {
160
+ IntrinsicType {
161
+ kind : TypeKind :: Char ( _) ,
162
+ ..
163
+ } => String :: from ( "char" ) ,
164
+ _ => format ! (
165
+ "{prefix}{bits}_t" ,
166
+ prefix = self . kind( ) . c_prefix( ) ,
167
+ bits = self . inner_size( )
168
+ ) ,
169
+ }
141
170
}
142
171
143
172
pub fn rust_scalar_type ( & self ) -> String {
@@ -155,8 +184,8 @@ impl IntrinsicType {
155
184
bit_len : Some ( 8 ) ,
156
185
..
157
186
} => match kind {
158
- TypeKind :: Int => "(int)" ,
159
- TypeKind :: UInt => "(unsigned int)" ,
187
+ TypeKind :: Int ( Sign :: Signed ) => "(int)" ,
188
+ TypeKind :: Int ( Sign :: Unsigned ) => "(unsigned int)" ,
160
189
TypeKind :: Poly => "(unsigned int)(uint8_t)" ,
161
190
_ => "" ,
162
191
} ,
@@ -172,6 +201,21 @@ impl IntrinsicType {
172
201
128 => "" ,
173
202
_ => panic ! ( "invalid bit_len" ) ,
174
203
} ,
204
+ IntrinsicType {
205
+ kind : TypeKind :: Float ,
206
+ bit_len : Some ( bit_len) ,
207
+ ..
208
+ } => match bit_len {
209
+ 16 => "(float16_t)" ,
210
+ 32 => "(float)" ,
211
+ 64 => "(double)" ,
212
+ 128 => "" ,
213
+ _ => panic ! ( "invalid bit_len" ) ,
214
+ } ,
215
+ IntrinsicType {
216
+ kind : TypeKind :: Char ( _) ,
217
+ ..
218
+ } => "(char)" ,
175
219
_ => "" ,
176
220
}
177
221
}
@@ -185,7 +229,7 @@ impl IntrinsicType {
185
229
match self {
186
230
IntrinsicType {
187
231
bit_len : Some ( bit_len @ ( 8 | 16 | 32 | 64 ) ) ,
188
- kind : kind @ ( TypeKind :: Int | TypeKind :: UInt | TypeKind :: Poly ) ,
232
+ kind : kind @ ( TypeKind :: Int ( _ ) | TypeKind :: Poly | TypeKind :: Char ( _ ) ) ,
189
233
simd_len,
190
234
vec_len,
191
235
..
@@ -201,7 +245,8 @@ impl IntrinsicType {
201
245
. format_with( ",\n " , |i, fmt| {
202
246
let src = value_for_array( * bit_len, i) ;
203
247
assert!( src == 0 || src. ilog2( ) < * bit_len) ;
204
- if * kind == TypeKind :: Int && ( src >> ( * bit_len - 1 ) ) != 0 {
248
+ if * kind == TypeKind :: Int ( Sign :: Signed ) && ( src >> ( * bit_len - 1 ) ) != 0
249
+ {
205
250
// `src` is a two's complement representation of a negative value.
206
251
let mask = !0u64 >> ( 64 - * bit_len) ;
207
252
let ones_compl = src ^ mask;
@@ -257,7 +302,7 @@ impl IntrinsicType {
257
302
..
258
303
} => false ,
259
304
IntrinsicType {
260
- kind : TypeKind :: Int | TypeKind :: UInt | TypeKind :: Poly ,
305
+ kind : TypeKind :: Int ( _ ) | TypeKind :: Poly ,
261
306
..
262
307
} => true ,
263
308
_ => unimplemented ! ( ) ,
0 commit comments