From 70f767370feb9ca056e4baf32f37c6d8d8235e0c Mon Sep 17 00:00:00 2001 From: iximeow Date: Thu, 20 Jun 2024 13:18:13 -0700 Subject: swap printed size check and lzcnt if printed_size == 0 then the value must be 0, but we can check if the value is 0 before doing all that stuff --- src/long_mode/display.rs | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) (limited to 'src/long_mode') diff --git a/src/long_mode/display.rs b/src/long_mode/display.rs index c69e648..ca5e580 100644 --- a/src/long_mode/display.rs +++ b/src/long_mode/display.rs @@ -891,12 +891,12 @@ impl DisplaySink for alloc::string::String { /// followup step) #[inline(always)] fn write_u8(&mut self, mut v: u8) -> Result<(), core::fmt::Error> { + if v == 0 { + return self.write_fixed_size("0"); + } // we can fairly easily predict the size of a formatted string here with lzcnt, which also // means we can write directly into the correct offsets of the output string. let printed_size = ((8 - v.leading_zeros() + 3) >> 2) as usize; - if printed_size == 0 { - return self.write_fixed_size("0"); - } self.reserve(printed_size); @@ -930,12 +930,12 @@ impl DisplaySink for alloc::string::String { /// followup step) #[inline(always)] fn write_u16(&mut self, mut v: u16) -> Result<(), core::fmt::Error> { + if v == 0 { + return self.write_fixed_size("0"); + } // we can fairly easily predict the size of a formatted string here with lzcnt, which also // means we can write directly into the correct offsets of the output string. let printed_size = ((16 - v.leading_zeros() + 3) >> 2) as usize; - if printed_size == 0 { - return self.write_fixed_size("0"); - } self.reserve(printed_size); @@ -969,12 +969,12 @@ impl DisplaySink for alloc::string::String { /// followup step) #[inline(always)] fn write_u32(&mut self, mut v: u32) -> Result<(), core::fmt::Error> { + if v == 0 { + return self.write_fixed_size("0"); + } // we can fairly easily predict the size of a formatted string here with lzcnt, which also // means we can write directly into the correct offsets of the output string. let printed_size = ((32 - v.leading_zeros() + 3) >> 2) as usize; - if printed_size == 0 { - return self.write_fixed_size("0"); - } self.reserve(printed_size); @@ -1008,12 +1008,12 @@ impl DisplaySink for alloc::string::String { /// followup step) #[inline(always)] fn write_u64(&mut self, mut v: u64) -> Result<(), core::fmt::Error> { + if v == 0 { + return self.write_fixed_size("0"); + } // we can fairly easily predict the size of a formatted string here with lzcnt, which also // means we can write directly into the correct offsets of the output string. let printed_size = ((64 - v.leading_zeros() + 3) >> 2) as usize; - if printed_size == 0 { - return self.write_fixed_size("0"); - } self.reserve(printed_size); @@ -1281,12 +1281,12 @@ impl<'buf> DisplaySink for InstructionTextSink<'buf> { /// followup step) #[inline(always)] fn write_u8(&mut self, mut v: u8) -> Result<(), core::fmt::Error> { + if v == 0 { + return self.write_fixed_size("0"); + } // we can fairly easily predict the size of a formatted string here with lzcnt, which also // means we can write directly into the correct offsets of the output string. let printed_size = ((8 - v.leading_zeros() + 3) >> 2) as usize; - if printed_size == 0 { - return self.write_fixed_size("0"); - } let buf = unsafe { self.buf.as_mut_vec() }; let new_len = buf.len() + printed_size; @@ -1318,12 +1318,12 @@ impl<'buf> DisplaySink for InstructionTextSink<'buf> { /// followup step) #[inline(always)] fn write_u16(&mut self, mut v: u16) -> Result<(), core::fmt::Error> { + if v == 0 { + return self.write_fixed_size("0"); + } // we can fairly easily predict the size of a formatted string here with lzcnt, which also // means we can write directly into the correct offsets of the output string. let printed_size = ((16 - v.leading_zeros() + 3) >> 2) as usize; - if printed_size == 0 { - return self.write_fixed_size("0"); - } let buf = unsafe { self.buf.as_mut_vec() }; let new_len = buf.len() + printed_size; @@ -1355,12 +1355,12 @@ impl<'buf> DisplaySink for InstructionTextSink<'buf> { /// followup step) #[inline(always)] fn write_u32(&mut self, mut v: u32) -> Result<(), core::fmt::Error> { + if v == 0 { + return self.write_fixed_size("0"); + } // we can fairly easily predict the size of a formatted string here with lzcnt, which also // means we can write directly into the correct offsets of the output string. let printed_size = ((32 - v.leading_zeros() + 3) >> 2) as usize; - if printed_size == 0 { - return self.write_fixed_size("0"); - } let buf = unsafe { self.buf.as_mut_vec() }; let new_len = buf.len() + printed_size; @@ -1392,12 +1392,12 @@ impl<'buf> DisplaySink for InstructionTextSink<'buf> { /// followup step) #[inline(always)] fn write_u64(&mut self, mut v: u64) -> Result<(), core::fmt::Error> { + if v == 0 { + return self.write_fixed_size("0"); + } // we can fairly easily predict the size of a formatted string here with lzcnt, which also // means we can write directly into the correct offsets of the output string. let printed_size = ((64 - v.leading_zeros() + 3) >> 2) as usize; - if printed_size == 0 { - return self.write_fixed_size("0"); - } let buf = unsafe { self.buf.as_mut_vec() }; let new_len = buf.len() + printed_size; -- cgit v1.1