Skip to content

Commit c2294ff

Browse files
feat: casting the results of the lane function by preserving the bits
instead of letting C++ do it (and potentially change the bits)
1 parent e139299 commit c2294ff

File tree

3 files changed

+34
-36
lines changed

3 files changed

+34
-36
lines changed

crates/intrinsic-test/src/arm/types.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,10 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
112112
ty = self.c_single_vector_type(),
113113
lanes = (0..self.num_lanes())
114114
.map(move |idx| -> std::string::String {
115+
let lane_fn = self.get_lane_function();
116+
let final_cast = self.generate_final_type_cast();
115117
format!(
116-
"{cast}{lane_fn}(__return_value.val[{vector}], {lane})",
117-
cast = self.c_promotion(),
118-
lane_fn = self.get_lane_function(),
119-
lane = idx,
120-
vector = vector,
118+
"{final_cast}{lane_fn}(__return_value.val[{vector}], {idx})"
121119
)
122120
})
123121
.collect::<Vec<_>>()
@@ -129,12 +127,9 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
129127
} else if self.num_lanes() > 1 {
130128
(0..self.num_lanes())
131129
.map(|idx| -> std::string::String {
132-
format!(
133-
"{cast}{lane_fn}(__return_value, {lane})",
134-
cast = self.c_promotion(),
135-
lane_fn = self.get_lane_function(),
136-
lane = idx
137-
)
130+
let lane_fn = self.get_lane_function();
131+
let final_cast = self.generate_final_type_cast();
132+
format!("{final_cast}{lane_fn}(__return_value, {idx})")
138133
})
139134
.collect::<Vec<_>>()
140135
.join(r#" << ", " << "#)
@@ -150,7 +145,7 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
150145
TypeKind::Poly => format!("poly{}_t", self.inner_size()),
151146
ty => todo!("print_result_c - Unknown type: {:#?}", ty),
152147
},
153-
promote = self.c_promotion(),
148+
promote = self.generate_final_type_cast(),
154149
)
155150
};
156151

crates/intrinsic-test/src/common/intrinsic_helpers.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ impl IntrinsicType {
173173
bit_len: Some(8),
174174
..
175175
} => match kind {
176-
TypeKind::Int(Sign::Signed) => "(int)",
177-
TypeKind::Int(Sign::Unsigned) => "(unsigned int)",
178-
TypeKind::Poly => "(unsigned int)(uint8_t)",
176+
TypeKind::Int(Sign::Signed) => "int",
177+
TypeKind::Int(Sign::Unsigned) => "unsigned int",
178+
TypeKind::Poly => "uint8_t",
179179
_ => "",
180180
},
181181
IntrinsicType {
@@ -184,9 +184,9 @@ impl IntrinsicType {
184184
..
185185
} => match bit_len {
186186
8 => unreachable!("handled above"),
187-
16 => "(uint16_t)",
188-
32 => "(uint32_t)",
189-
64 => "(uint64_t)",
187+
16 => "uint16_t",
188+
32 => "uint32_t",
189+
64 => "uint64_t",
190190
128 => "",
191191
_ => panic!("invalid bit_len"),
192192
},
@@ -195,16 +195,16 @@ impl IntrinsicType {
195195
bit_len: Some(bit_len),
196196
..
197197
} => match bit_len {
198-
16 => "(float16_t)",
199-
32 => "(float)",
200-
64 => "(double)",
198+
16 => "float16_t",
199+
32 => "float",
200+
64 => "double",
201201
128 => "",
202202
_ => panic!("invalid bit_len"),
203203
},
204204
IntrinsicType {
205205
kind: TypeKind::Char(_),
206206
..
207-
} => "(char)",
207+
} => "char",
208208
_ => "",
209209
}
210210
}
@@ -391,4 +391,13 @@ pub trait IntrinsicTypeDefinition: Deref<Target = IntrinsicType> {
391391
bits = self.inner_size()
392392
)
393393
}
394+
395+
fn generate_final_type_cast(&self) -> String {
396+
let type_data = self.c_promotion();
397+
if type_data.len() > 2 {
398+
format!("({type_data})")
399+
} else {
400+
String::new()
401+
}
402+
}
394403
}

crates/intrinsic-test/src/x86/types.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ impl IntrinsicTypeDefinition for X86IntrinsicType {
185185
.map(move |idx| -> std::string::String {
186186
format!(
187187
"{cast}{lane_fn}(__return_value.val[{vector}], {lane})",
188-
cast = self.c_promotion(),
188+
cast = self.generate_final_type_cast(),
189189
lane_fn = self.get_lane_function(),
190190
lane = idx,
191191
vector = vector,
@@ -200,12 +200,13 @@ impl IntrinsicTypeDefinition for X86IntrinsicType {
200200
} else if self.num_lanes() > 1 {
201201
(0..self.num_lanes())
202202
.map(|idx| -> std::string::String {
203-
format!(
204-
"{cast}{lane_fn}(__return_value, {lane})",
205-
cast = self.c_promotion(),
206-
lane_fn = self.get_lane_function(),
207-
lane = idx
208-
)
203+
let cast_type = self.c_promotion();
204+
let lane_fn = self.get_lane_function();
205+
if cast_type.len() > 2 {
206+
format!("({cast_type})({lane_fn}(__return_value, {idx}))")
207+
} else {
208+
format!("{lane_fn}(__return_value, {idx})")
209+
}
209210
})
210211
.collect::<Vec<_>>()
211212
.join(r#" << ", " << "#)
@@ -224,13 +225,6 @@ impl IntrinsicTypeDefinition for X86IntrinsicType {
224225
"__m{}i",
225226
self.bit_len.expect(format!("self: {:#?}", self).as_str())
226227
),
227-
// TypeKind::Float if self.results().inner_size() == 16 => "float16_t".to_string(),
228-
// TypeKind::Int(true) if self.results().inner_size() == 64 => "long".to_string(),
229-
// TypeKind::Int(false) if self.results().inner_size() == 64 => "unsigned long".to_string(),
230-
// TypeKind::Int(true) if self.results().inner_size() == 32 => "int".to_string(),
231-
// TypeKind::Int(false) if self.results().inner_size() == 32 => "unsigned int".to_string(),
232-
// TypeKind::Int(true) if self.results().inner_size() == 16 => "short".to_string(),
233-
// TypeKind::Int(false) if self.results().inner_size() == 16 => "unsigned short".to_string(),
234228
_ => self.c_scalar_type(),
235229
},
236230
promote = self.c_promotion(),

0 commit comments

Comments
 (0)