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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
use std::fmt;
use std::ops::Deref;
use std::str::FromStr;
use itertools::Itertools as _;
use super::cli::Language;
use super::indentation::Indentation;
use super::values::value_for_array;
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum TypeKind {
BFloat,
Float,
Int,
UInt,
Poly,
Void,
}
impl FromStr for TypeKind {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"bfloat" => Ok(Self::BFloat),
"float" => Ok(Self::Float),
"int" => Ok(Self::Int),
"poly" => Ok(Self::Poly),
"uint" | "unsigned" => Ok(Self::UInt),
"void" => Ok(Self::Void),
_ => Err(format!("Impossible to parse argument kind {s}")),
}
}
}
impl fmt::Display for TypeKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
Self::BFloat => "bfloat",
Self::Float => "float",
Self::Int => "int",
Self::UInt => "uint",
Self::Poly => "poly",
Self::Void => "void",
}
)
}
}
impl TypeKind {
/// Gets the type part of a c typedef for a type that's in the form of {type}{size}_t.
pub fn c_prefix(&self) -> &str {
match self {
Self::Float => "float",
Self::Int => "int",
Self::UInt => "uint",
Self::Poly => "poly",
_ => unreachable!("Not used: {:#?}", self),
}
}
/// Gets the rust prefix for the type kind i.e. i, u, f.
pub fn rust_prefix(&self) -> &str {
match self {
Self::Float => "f",
Self::Int => "i",
Self::UInt => "u",
Self::Poly => "u",
_ => unreachable!("Unused type kind: {:#?}", self),
}
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct IntrinsicType {
pub constant: bool,
/// whether this object is a const pointer
pub ptr_constant: bool,
pub ptr: bool,
pub kind: TypeKind,
/// The bit length of this type (e.g. 32 for u32).
pub bit_len: Option<u32>,
/// Length of the SIMD vector (i.e. 4 for uint32x4_t), A value of `None`
/// means this is not a simd type. A `None` can be assumed to be 1,
/// although in some places a distinction is needed between `u64` and
/// `uint64x1_t` this signals that.
pub simd_len: Option<u32>,
/// The number of rows for SIMD matrices (i.e. 2 for uint8x8x2_t).
/// A value of `None` represents a type that does not contain any
/// rows encoded in the type (e.g. uint8x8_t).
/// A value of `None` can be assumed to be 1 though.
pub vec_len: Option<u32>,
pub target: String,
}
impl IntrinsicType {
pub fn kind(&self) -> TypeKind {
self.kind
}
pub fn inner_size(&self) -> u32 {
if let Some(bl) = self.bit_len {
bl
} else {
unreachable!("")
}
}
pub fn num_lanes(&self) -> u32 {
if let Some(sl) = self.simd_len { sl } else { 1 }
}
pub fn num_vectors(&self) -> u32 {
if let Some(vl) = self.vec_len { vl } else { 1 }
}
pub fn is_simd(&self) -> bool {
self.simd_len.is_some() || self.vec_len.is_some()
}
pub fn is_ptr(&self) -> bool {
self.ptr
}
pub fn c_scalar_type(&self) -> String {
format!(
"{prefix}{bits}_t",
prefix = self.kind().c_prefix(),
bits = self.inner_size()
)
}
pub fn rust_scalar_type(&self) -> String {
format!(
"{prefix}{bits}",
prefix = self.kind().rust_prefix(),
bits = self.inner_size()
)
}
pub fn c_promotion(&self) -> &str {
match *self {
IntrinsicType {
kind,
bit_len: Some(8),
..
} => match kind {
TypeKind::Int => "(int)",
TypeKind::UInt => "(unsigned int)",
TypeKind::Poly => "(unsigned int)(uint8_t)",
_ => "",
},
IntrinsicType {
kind: TypeKind::Poly,
bit_len: Some(bit_len),
..
} => match bit_len {
8 => unreachable!("handled above"),
16 => "(uint16_t)",
32 => "(uint32_t)",
64 => "(uint64_t)",
128 => "",
_ => panic!("invalid bit_len"),
},
_ => "",
}
}
pub fn populate_random(
&self,
indentation: Indentation,
loads: u32,
language: &Language,
) -> String {
match self {
IntrinsicType {
bit_len: Some(bit_len @ (8 | 16 | 32 | 64)),
kind: kind @ (TypeKind::Int | TypeKind::UInt | TypeKind::Poly),
simd_len,
vec_len,
..
} => {
let (prefix, suffix) = match language {
Language::Rust => ("[", "]"),
Language::C => ("{", "}"),
};
let body_indentation = indentation.nested();
format!(
"{prefix}\n{body}\n{indentation}{suffix}",
body = (0..(simd_len.unwrap_or(1) * vec_len.unwrap_or(1) + loads - 1))
.format_with(",\n", |i, fmt| {
let src = value_for_array(*bit_len, i);
assert!(src == 0 || src.ilog2() < *bit_len);
if *kind == TypeKind::Int && (src >> (*bit_len - 1)) != 0 {
// `src` is a two's complement representation of a negative value.
let mask = !0u64 >> (64 - *bit_len);
let ones_compl = src ^ mask;
let twos_compl = ones_compl + 1;
if (twos_compl == src) && (language == &Language::C) {
// `src` is INT*_MIN. C requires `-0x7fffffff - 1` to avoid
// undefined literal overflow behaviour.
fmt(&format_args!("{body_indentation}-{ones_compl:#x} - 1"))
} else {
fmt(&format_args!("{body_indentation}-{twos_compl:#x}"))
}
} else {
fmt(&format_args!("{body_indentation}{src:#x}"))
}
})
)
}
IntrinsicType {
kind: TypeKind::Float,
bit_len: Some(bit_len @ (16 | 32 | 64)),
simd_len,
vec_len,
..
} => {
let (prefix, cast_prefix, cast_suffix, suffix) = match (language, bit_len) {
(&Language::Rust, 16) => ("[", "f16::from_bits(", ")", "]"),
(&Language::Rust, 32) => ("[", "f32::from_bits(", ")", "]"),
(&Language::Rust, 64) => ("[", "f64::from_bits(", ")", "]"),
(&Language::C, 16) => ("{", "cast<float16_t, uint16_t>(", ")", "}"),
(&Language::C, 32) => ("{", "cast<float, uint32_t>(", ")", "}"),
(&Language::C, 64) => ("{", "cast<double, uint64_t>(", ")", "}"),
_ => unreachable!(),
};
format!(
"{prefix}\n{body}\n{indentation}{suffix}",
body = (0..(simd_len.unwrap_or(1) * vec_len.unwrap_or(1) + loads - 1))
.format_with(",\n", |i, fmt| fmt(&format_args!(
"{indentation}{cast_prefix}{src:#x}{cast_suffix}",
indentation = indentation.nested(),
src = value_for_array(*bit_len, i)
)))
)
}
_ => unimplemented!("populate random: {:#?}", self),
}
}
pub fn is_rust_vals_array_const(&self) -> bool {
match self {
// Floats have to be loaded at runtime for stable NaN conversion.
IntrinsicType {
kind: TypeKind::Float,
..
} => false,
IntrinsicType {
kind: TypeKind::Int | TypeKind::UInt | TypeKind::Poly,
..
} => true,
_ => unimplemented!(),
}
}
pub fn as_call_param_c(&self, name: &String) -> String {
if self.ptr {
format!("&{}", name)
} else {
name.clone()
}
}
}
pub trait IntrinsicTypeDefinition: Deref<Target = IntrinsicType> {
/// Determines the load function for this type.
/// can be implemented in an `impl` block
fn get_load_function(&self, _language: Language) -> String;
/// can be implemented in an `impl` block
fn get_lane_function(&self) -> String;
/// can be implemented in an `impl` block
fn from_c(_s: &str, _target: &String) -> Result<Box<Self>, String>;
/// Gets a string containing the typename for this type in C format.
/// can be directly defined in `impl` blocks
fn c_type(&self) -> String;
/// can be directly defined in `impl` blocks
fn c_single_vector_type(&self) -> String;
/// can be defined in `impl` blocks
fn rust_type(&self) -> String;
}
|