Skip to content

Commit 9dbc078

Browse files
feat: make the debug_i16 into a generic debug_as function that adapts to
base type
1 parent 3b77dc5 commit 9dbc078

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -207,45 +207,45 @@ impl DebugHexF16 for __m512i {
207207
}
208208
}
209209
210-
trait DebugI16 {
210+
trait DebugAs<T> {
211211
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result;
212212
}
213213
214-
impl DebugI16 for i16 {
214+
impl<T: core::fmt::Display> DebugAs<T> for T {
215215
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
216216
write!(f, "{}", self)
217217
}
218218
}
219219
220-
impl DebugI16 for __m128i {
221-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
222-
let array = unsafe { core::mem::transmute::<_, [i16; 8]>(*self) };
223-
debug_simd_finish(f, "__m128i", &array)
224-
}
225-
}
226-
227-
impl DebugI16 for __m256i {
228-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
229-
let array = unsafe { core::mem::transmute::<_, [i16; 16]>(*self) };
230-
debug_simd_finish(f, "__m256i", &array)
231-
}
232-
}
233-
234-
impl DebugI16 for __m512i {
235-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
236-
let array = unsafe { core::mem::transmute::<_, [i16; 32]>(*self) };
237-
debug_simd_finish(f, "__m512i", &array)
238-
}
239-
}
240-
241-
fn debug_i16<T: DebugI16>(x: T) -> impl core::fmt::Debug {
242-
struct DebugWrapper<T>(T);
243-
impl<T: DebugI16> core::fmt::Debug for DebugWrapper<T> {
220+
macro_rules! impl_debug_as {
221+
($simd:ty, $name:expr, $bits:expr, [$($type:ty),+]) => {
222+
$(
223+
impl DebugAs<$type> for $simd {
224+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
225+
const ELEMENT_BITS: usize = core::mem::size_of::<$type>() * 8;
226+
const NUM_ELEMENTS: usize = $bits / ELEMENT_BITS;
227+
let array = unsafe { core::mem::transmute::<_, [$type; NUM_ELEMENTS]>(*self) };
228+
debug_simd_finish(f, $name, &array)
229+
}
230+
}
231+
)+
232+
};
233+
}
234+
235+
impl_debug_as!(__m128i, "__m128i", 128, [u8, i8, u16, i16, u32, i32, u64, i64]);
236+
impl_debug_as!(__m256i, "__m256i", 256, [u8, i8, u16, i16, u32, i32, u64, i64]);
237+
impl_debug_as!(__m512i, "__m512i", 512, [u8, i8, u16, i16, u32, i32, u64, i64]);
238+
239+
fn debug_as<V, T>(x: V) -> impl core::fmt::Debug
240+
where V: DebugAs<T>
241+
{
242+
struct DebugWrapper<V, T>(V, core::marker::PhantomData<T>);
243+
impl<V: DebugAs<T>, T> core::fmt::Debug for DebugWrapper<V, T> {
244244
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
245245
self.0.fmt(f)
246246
}
247247
}
248-
DebugWrapper(x)
248+
DebugWrapper(x, core::marker::PhantomData)
249249
}
250250
251251
"#;

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,14 +295,16 @@ impl IntrinsicTypeDefinition for X86IntrinsicType {
295295

296296
fn print_result_rust(&self) -> String {
297297
let return_value = match self.kind() {
298-
TypeKind::Float if self.inner_size() == 16 => "debug_f16(__return_value)",
299-
_ if ["__m128i", "__m256i", "__m512i"].contains(&self.param.type_data.as_str()) => {
300-
"debug_i16(__return_value)"
298+
TypeKind::Float if self.inner_size() == 16 => "debug_f16(__return_value)".to_string(),
299+
TypeKind::Int(_)
300+
if ["__m128i", "__m256i", "__m512i"].contains(&self.param.type_data.as_str()) =>
301+
{
302+
format!("debug_as::<_, u{}>(__return_value)", self.inner_size())
301303
}
302-
_ => "format_args!(\"{__return_value:.150?}\")",
304+
_ => "format_args!(\"{__return_value:.150?}\")".to_string(),
303305
};
304306

305-
String::from(return_value)
307+
return_value
306308
}
307309
}
308310

0 commit comments

Comments
 (0)