diff --git a/crates/core_simd/src/fmt.rs b/crates/core_simd/src/fmt.rs new file mode 100644 index 00000000000..62a21b442d2 --- /dev/null +++ b/crates/core_simd/src/fmt.rs @@ -0,0 +1,106 @@ +macro_rules! debug_wrapper { + { $($trait:ident => $name:ident,)* } => { + $( + pub(crate) fn $name(slice: &[T], f: &mut core::fmt::Formatter) -> core::fmt::Result { + #[repr(transparent)] + struct Wrapper<'a, T: core::fmt::$trait>(&'a T); + + impl core::fmt::Debug for Wrapper<'_, T> { + fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { + self.0.fmt(f) + } + } + + f.debug_list() + .entries(slice.iter().map(|x| Wrapper(x))) + .finish() + } + )* + } +} + +debug_wrapper! { + Debug => format, + Binary => format_binary, + LowerExp => format_lower_exp, + UpperExp => format_upper_exp, + Octal => format_octal, + LowerHex => format_lower_hex, + UpperHex => format_upper_hex, + Pointer => format_pointer, +} + +macro_rules! impl_fmt_trait { + { $($type:ty => $(($trait:ident, $format:ident)),*;)* } => { + $( // repeat type + $( // repeat trait + impl core::fmt::$trait for $type { + fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { + $format(self.as_ref(), f) + } + } + )* + )* + }; + { integers: $($type:ty,)* } => { + impl_fmt_trait! { + $($type => + (Debug, format), + (Binary, format_binary), + (LowerExp, format_lower_exp), + (UpperExp, format_upper_exp), + (Octal, format_octal), + (LowerHex, format_lower_hex), + (UpperHex, format_upper_hex); + )* + } + }; + { floats: $($type:ty,)* } => { + impl_fmt_trait! { + $($type => + (Debug, format), + (LowerExp, format_lower_exp), + (UpperExp, format_upper_exp); + )* + } + }; + { masks: $($type:ty,)* } => { + impl_fmt_trait! { + $($type => + (Debug, format); + )* + } + } +} + +impl_fmt_trait! { + integers: + crate::u8x2, crate::u8x4, crate::u8x8, crate::u8x16, crate::u8x32, crate::u8x64, + crate::i8x2, crate::i8x4, crate::i8x8, crate::i8x16, crate::i8x32, crate::i8x64, + crate::u16x2, crate::u16x4, crate::u16x8, crate::u16x16, crate::u16x32, + crate::i16x2, crate::i16x4, crate::i16x8, crate::i16x16, crate::i16x32, + crate::u32x2, crate::u32x4, crate::u32x8, crate::u32x16, + crate::i32x2, crate::i32x4, crate::i32x8, crate::i32x16, + crate::u64x2, crate::u64x4, crate::u64x8, + crate::i64x2, crate::i64x4, crate::i64x8, + crate::u128x2, crate::u128x4, + crate::i128x2, crate::i128x4, + crate::usizex2, crate::usizex4, crate::usizex8, + crate::isizex2, crate::isizex4, crate::isizex8, +} + +impl_fmt_trait! { + floats: + crate::f32x2, crate::f32x4, crate::f32x8, crate::f32x16, + crate::f64x2, crate::f64x4, crate::f64x8, +} + +impl_fmt_trait! { + masks: + crate::mask8x2, crate::mask8x4, crate::mask8x8, crate::mask8x16, crate::mask8x32, crate::mask8x64, + crate::mask16x2, crate::mask16x4, crate::mask16x8, crate::mask16x16, crate::mask16x32, + crate::mask32x2, crate::mask32x4, crate::mask32x8, crate::mask32x16, + crate::mask64x2, crate::mask64x4, crate::mask64x8, + crate::mask128x2, crate::mask128x4, + crate::masksizex2, crate::masksizex4, crate::masksizex8, +} diff --git a/crates/core_simd/src/lib.rs b/crates/core_simd/src/lib.rs index 34f2b90f767..f6ddc4c6cfd 100644 --- a/crates/core_simd/src/lib.rs +++ b/crates/core_simd/src/lib.rs @@ -6,6 +6,8 @@ #[macro_use] mod macros; +mod fmt; + mod masks; pub use masks::*; diff --git a/crates/core_simd/src/macros.rs b/crates/core_simd/src/macros.rs index 6b5599f0170..33541899ca3 100644 --- a/crates/core_simd/src/macros.rs +++ b/crates/core_simd/src/macros.rs @@ -247,7 +247,7 @@ macro_rules! define_vector { { def $(#[$attr:meta])* | $name:ident | $($itype:ty)* } => { $(#[$attr])* #[allow(non_camel_case_types)] - #[derive(Copy, Clone, Debug, Default, PartialEq, PartialOrd)] + #[derive(Copy, Clone, Default, PartialEq, PartialOrd)] #[repr(simd)] pub struct $name($($itype),*); }; @@ -284,7 +284,7 @@ macro_rules! define_mask_vector { { def $(#[$attr:meta])* | $name:ident | $($itype:ty)* } => { $(#[$attr])* #[allow(non_camel_case_types)] - #[derive(Copy, Clone, Debug, Default, PartialEq, PartialOrd, Eq, Ord)] + #[derive(Copy, Clone, Default, PartialEq, PartialOrd, Eq, Ord)] #[repr(simd)] pub struct $name($($itype),*); }; diff --git a/crates/core_simd/src/masks.rs b/crates/core_simd/src/masks.rs index 970047aabde..ceefbfc9444 100644 --- a/crates/core_simd/src/masks.rs +++ b/crates/core_simd/src/masks.rs @@ -2,7 +2,7 @@ macro_rules! define_mask { { $(#[$attr:meta])* struct $name:ident($type:ty); } => { $(#[$attr])* #[allow(non_camel_case_types)] - #[derive(Copy, Clone, Debug, Default, PartialEq, PartialOrd, Eq, Ord, Hash)] + #[derive(Copy, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash)] #[repr(transparent)] pub struct $name(pub(crate) $type); @@ -33,6 +33,12 @@ macro_rules! define_mask { mask.test() } } + + impl core::fmt::Debug for $name { + fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { + self.test().fmt(f) + } + } } } diff --git a/crates/core_simd/src/pointers.rs b/crates/core_simd/src/pointers.rs index ecd78fb0cb1..a7c514aae7a 100644 --- a/crates/core_simd/src/pointers.rs +++ b/crates/core_simd/src/pointers.rs @@ -100,9 +100,12 @@ macro_rules! define_pointer_vector { { debug $name:ident | $type:ty | $($index:tt)* } => { impl core::fmt::Debug for $name { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { - f.debug_tuple(stringify!($name)) - $(.field(&(AsRef::<[isize]>::as_ref(&self.0)[$index] as $type)))* - .finish() + crate::fmt::format(self.as_ref(), f) + } + } + impl core::fmt::Pointer for $name { + fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { + crate::fmt::format_pointer(self.as_ref(), f) } } }