From 49f54724a3b0b2497bd944f7638cb6e8ff1d18dc Mon Sep 17 00:00:00 2001 From: iximeow Date: Tue, 18 Jun 2024 13:52:03 -0700 Subject: less integer formatting in operands --- src/long_mode/display.rs | 160 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 153 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/long_mode/display.rs b/src/long_mode/display.rs index b9c9f79..e3f5b0a 100644 --- a/src/long_mode/display.rs +++ b/src/long_mode/display.rs @@ -927,49 +927,195 @@ struct ColorizingOperandVisitor<'a, T, Y> { f: &'a mut T, } +use core::mem::MaybeUninit; + impl crate::long_mode::OperandVisitor for ColorizingOperandVisitor<'_, T, Y> { type Ok = (); type Error = core::fmt::Error; fn visit_u8(&mut self, imm: u8) -> Result { self.f.span_enter(TokenType::Immediate); - write!(self.f, "{}", u8_hex(imm))?; + self.f.write_fixed_size("0x")?; + let mut buf = [MaybeUninit::::uninit(); 2]; + let mut curr = buf.len(); + let mut v = imm; + loop { + let digit = v % 16; + let c = c_to_hex(digit as u8); + curr -= 1; + buf[curr].write(c); + v = v / 16; + if v == 0 { + break; + } + } + let buf = &buf[curr..]; + let s: &str = unsafe { + core::mem::transmute::<&[MaybeUninit], &str>(buf) + }; + self.f.write_fixed_size(s)?; self.f.span_end(TokenType::Immediate); Ok(()) } fn visit_i8(&mut self, imm: i8) -> Result { self.f.span_enter(TokenType::Immediate); - write!(self.f, "{}", signed_i8_hex(imm))?; + let mut v = imm as u8; + if imm < 0 { + self.f.write_char('-')?; + v = -imm as u8; + } + self.f.write_fixed_size("0x")?; + let mut buf = [core::mem::MaybeUninit::::uninit(); 2]; + let mut curr = buf.len(); + loop { + let digit = v % 16; + let c = c_to_hex(digit as u8); + curr -= 1; + buf[curr].write(c); + v = v / 16; + if v == 0 { + break; + } + } + let buf = &buf[curr..]; + let s = unsafe { + core::mem::transmute::<&[core::mem::MaybeUninit], &str>(buf) + }; + + // not actually fixed size, but this should optimize right i hope.. + self.f.write_fixed_size(s)?; self.f.span_end(TokenType::Immediate); Ok(()) } fn visit_u16(&mut self, imm: u16) -> Result { self.f.span_enter(TokenType::Immediate); - write!(self.f, "{}", u16_hex(imm))?; + self.f.write_fixed_size("0x")?; + let mut buf = [MaybeUninit::::uninit(); 4]; + let mut curr = buf.len(); + let mut v = imm; + loop { + let digit = v % 16; + let c = c_to_hex(digit as u8); + curr -= 1; + buf[curr].write(c); + v = v / 16; + if v == 0 { + break; + } + } + let buf = &buf[curr..]; + let s: &str = unsafe { + core::mem::transmute::<&[MaybeUninit], &str>(buf) + }; + self.f.write_fixed_size(s)?; self.f.span_end(TokenType::Immediate); Ok(()) } fn visit_i16(&mut self, imm: i16) -> Result { self.f.span_enter(TokenType::Immediate); - write!(self.f, "{}", signed_i16_hex(imm))?; + let mut v = imm as u16; + if imm < 0 { + self.f.write_char('-')?; + v = -imm as u16; + } + self.f.write_fixed_size("0x")?; + let mut buf = [core::mem::MaybeUninit::::uninit(); 4]; + let mut curr = buf.len(); + loop { + let digit = v % 16; + let c = c_to_hex(digit as u8); + curr -= 1; + buf[curr].write(c); + v = v / 16; + if v == 0 { + break; + } + } + let buf = &buf[curr..]; + let s = unsafe { + core::mem::transmute::<&[core::mem::MaybeUninit], &str>(buf) + }; + + // not actually fixed size, but this should optimize right i hope.. + self.f.write_fixed_size(s)?; self.f.span_end(TokenType::Immediate); Ok(()) } fn visit_u32(&mut self, imm: u32) -> Result { self.f.span_enter(TokenType::Immediate); - write!(self.f, "{}", u32_hex(imm))?; + self.f.write_fixed_size("0x")?; + let mut buf = [MaybeUninit::::uninit(); 8]; + let mut curr = buf.len(); + let mut v = imm; + loop { + let digit = v % 16; + let c = c_to_hex(digit as u8); + curr -= 1; + buf[curr].write(c); + v = v / 16; + if v == 0 { + break; + } + } + let buf = &buf[curr..]; + let s: &str = unsafe { + core::mem::transmute::<&[MaybeUninit], &str>(buf) + }; + self.f.write_fixed_size(s)?; self.f.span_end(TokenType::Immediate); Ok(()) } fn visit_i32(&mut self, imm: i32) -> Result { self.f.span_enter(TokenType::Immediate); - write!(self.f, "{}", signed_i32_hex(imm))?; + let mut v = imm as u32; + if imm < 0 { + self.f.write_char('-')?; + v = -imm as u32; + } + self.f.write_fixed_size("0x")?; + let mut buf = [core::mem::MaybeUninit::::uninit(); 8]; + let mut curr = buf.len(); + loop { + let digit = v % 16; + let c = c_to_hex(digit as u8); + curr -= 1; + buf[curr].write(c); + v = v / 16; + if v == 0 { + break; + } + } + let buf = &buf[curr..]; + let s = unsafe { + core::mem::transmute::<&[core::mem::MaybeUninit], &str>(buf) + }; + + // not actually fixed size, but this should optimize right i hope.. + self.f.write_fixed_size(s)?; self.f.span_end(TokenType::Immediate); Ok(()) } fn visit_u64(&mut self, imm: u64) -> Result { self.f.span_enter(TokenType::Immediate); - write!(self.f, "{}", u64_hex(imm))?; + self.f.write_fixed_size("0x")?; + let mut buf = [MaybeUninit::::uninit(); 16]; + let mut curr = buf.len(); + let mut v = imm; + loop { + let digit = v % 16; + let c = c_to_hex(digit as u8); + curr -= 1; + buf[curr].write(c); + v = v / 16; + if v == 0 { + break; + } + } + let buf = &buf[curr..]; + let s: &str = unsafe { + core::mem::transmute::<&[MaybeUninit], &str>(buf) + }; + self.f.write_fixed_size(s)?; self.f.span_end(TokenType::Immediate); Ok(()) } -- cgit v1.1