From 4bf015f50057d585f1c57ad474d4fb38aa351a7d Mon Sep 17 00:00:00 2001 From: Perfloop Agent Date: Wed, 25 Nov 2026 04:49:02 +0000 Subject: [PATCH 1/4] Fix scientific formatting of zero Signed-off-by: Perfloop Agent --- libcc2rs/src/format.rs | 69 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/libcc2rs/src/format.rs b/libcc2rs/src/format.rs index 0304e3d3ba..e8de0c37bd 100644 --- a/libcc2rs/src/format.rs +++ b/libcc2rs/src/format.rs @@ -59,6 +59,41 @@ fn pad(out: &mut Vec, spec: &ConversionSpecifier, s: &[u8]) { } } +// Zero has no logarithmic exponent. Format it without sprintf's normalization. +fn scientific_zero(out: &mut Vec, spec: &ConversionSpecifier, negative: bool) { + let NumericParam::Literal(precision) = spec.precision else { + unreachable!() + }; + let precision = precision as usize; + let mut number = Vec::new(); + if negative { + number.push(b'-'); + } else if spec.force_sign { + number.push(b'+'); + } else if spec.space_sign { + number.push(b' '); + } + let point = precision > 0 || spec.alt_form; + if spec.zero_pad && !spec.left_adj { + let NumericParam::Literal(width) = spec.width else { + unreachable!() + }; + let len = number.len() + 1 + usize::from(point) + precision + 4; + number.resize(number.len() + (width as usize).saturating_sub(len), b'0'); + } + number.push(b'0'); + if point { + number.push(b'.'); + } + number.resize(number.len() + precision, b'0'); + number.extend_from_slice(if spec.conversion_type == ConversionType::SciFloatUpper { + b"E+00" + } else { + b"e+00" + }); + pad(out, spec, &number); +} + fn numeric_arg(conversion_type: ConversionType, arg: &VaArg) -> Arg { match conversion_type { ConversionType::DecInt => match arg { @@ -87,7 +122,7 @@ fn numeric_arg(conversion_type: ConversionType, arg: &VaArg) -> Arg { // Formats like C's printf. The format, the strings and the result are bytes, // which need not be UTF-8, so %s and %c are formatted here; sprintf formats -// the numbers, which are ASCII. +// the numbers, which are ASCII, except for scientific zero. pub fn format_c(fmt: &[u8], va: &[VaArg]) -> Vec { let mut out = Vec::with_capacity(fmt.len()); let mut va = va.iter(); @@ -121,6 +156,16 @@ pub fn format_c(fmt: &[u8], va: &[VaArg]) -> Vec { } conversion_type => { let arg = numeric_arg(conversion_type, arg); + if let Arg::Double(value) = arg + && value == 0.0 + && matches!( + conversion_type, + ConversionType::SciFloatLower | ConversionType::SciFloatUpper + ) + { + scientific_zero(&mut out, &spec, value.is_sign_negative()); + continue; + } match vsprintfp(&[FormatElement::Format(spec)], &[arg.as_printf()]) { Ok(s) => out.extend_from_slice(s.as_bytes()), Err(e) => panic!("format_c: cannot format {spec:?}: {e:?}"), @@ -168,6 +213,28 @@ mod tests { assert_eq!(format_c(b"%g", &[0.5f64.into()]), b"0.5"); } + #[test] + fn scientific_zero() { + assert_eq!( + format_c(b"%e|%E", &[0.0f64.into(), (-0.0f64).into()]), + b"0.000000e+00|-0.000000E+00" + ); + assert_eq!( + format_c( + b"%.0e|%#.0E|%02.2e", + &[0.0f64.into(), (-0.0f64).into(), 0.0f64.into()] + ), + b"0e+00|-0.E+00|0.00e+00" + ); + assert_eq!( + format_c( + b"%+ 012.2e|%-#012.0E|% 12.1e", + &[0.0f64.into(), (-0.0f64).into(), 0.0f64.into()] + ), + b"+0000.00e+00|-0.E+00 | 0.0e+00" + ); + } + #[test] fn bytes_need_not_be_utf8() { // Widths and precisions count bytes, and invalid UTF-8 is copied. From dafc032a4c5072fb1a3a623dc1d3e2b00b2c353b Mon Sep 17 00:00:00 2001 From: Perfloop Agent Date: Wed, 25 Nov 2026 05:05:00 +0000 Subject: [PATCH 2/4] Keep fixed-format zero on the existing formatter Signed-off-by: Perfloop Agent --- libcc2rs/src/format.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libcc2rs/src/format.rs b/libcc2rs/src/format.rs index e8de0c37bd..18ea657ceb 100644 --- a/libcc2rs/src/format.rs +++ b/libcc2rs/src/format.rs @@ -211,6 +211,10 @@ mod tests { ); assert_eq!(format_c(b"%e", &[1500.0f64.into()]), b"1.500000e+03"); assert_eq!(format_c(b"%g", &[0.5f64.into()]), b"0.5"); + assert_eq!( + format_c(b"%f|%F", &[0.0f64.into(), (-0.0f64).into()]), + b"0.000000|-0.000000" + ); } #[test] From 8c7eefbb09cf357878dcd8323874d3d0215f04f4 Mon Sep 17 00:00:00 2001 From: Perfloop Agent Date: Wed, 25 Nov 2026 05:44:31 +0000 Subject: [PATCH 3/4] Fix compact formatting of zero Signed-off-by: Perfloop Agent --- libcc2rs/src/format.rs | 79 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 11 deletions(-) diff --git a/libcc2rs/src/format.rs b/libcc2rs/src/format.rs index 18ea657ceb..f04cb1a040 100644 --- a/libcc2rs/src/format.rs +++ b/libcc2rs/src/format.rs @@ -60,11 +60,21 @@ fn pad(out: &mut Vec, spec: &ConversionSpecifier, s: &[u8]) { } // Zero has no logarithmic exponent. Format it without sprintf's normalization. -fn scientific_zero(out: &mut Vec, spec: &ConversionSpecifier, negative: bool) { +fn float_zero(out: &mut Vec, spec: &ConversionSpecifier, negative: bool) { let NumericParam::Literal(precision) = spec.precision else { unreachable!() }; - let precision = precision as usize; + let scientific = matches!( + spec.conversion_type, + ConversionType::SciFloatLower | ConversionType::SciFloatUpper + ); + let precision = if scientific { + precision as usize + } else if spec.alt_form { + precision.max(1) as usize - 1 + } else { + 0 + }; let mut number = Vec::new(); if negative { number.push(b'-'); @@ -78,7 +88,8 @@ fn scientific_zero(out: &mut Vec, spec: &ConversionSpecifier, negative: bool let NumericParam::Literal(width) = spec.width else { unreachable!() }; - let len = number.len() + 1 + usize::from(point) + precision + 4; + let len = + number.len() + 1 + usize::from(point) + precision + if scientific { 4 } else { 0 }; number.resize(number.len() + (width as usize).saturating_sub(len), b'0'); } number.push(b'0'); @@ -86,11 +97,13 @@ fn scientific_zero(out: &mut Vec, spec: &ConversionSpecifier, negative: bool number.push(b'.'); } number.resize(number.len() + precision, b'0'); - number.extend_from_slice(if spec.conversion_type == ConversionType::SciFloatUpper { - b"E+00" - } else { - b"e+00" - }); + if scientific { + number.extend_from_slice(if spec.conversion_type == ConversionType::SciFloatUpper { + b"E+00" + } else { + b"e+00" + }); + } pad(out, spec, &number); } @@ -122,7 +135,7 @@ fn numeric_arg(conversion_type: ConversionType, arg: &VaArg) -> Arg { // Formats like C's printf. The format, the strings and the result are bytes, // which need not be UTF-8, so %s and %c are formatted here; sprintf formats -// the numbers, which are ASCII, except for scientific zero. +// the numbers, which are ASCII, except for scientific and compact zero. pub fn format_c(fmt: &[u8], va: &[VaArg]) -> Vec { let mut out = Vec::with_capacity(fmt.len()); let mut va = va.iter(); @@ -160,10 +173,13 @@ pub fn format_c(fmt: &[u8], va: &[VaArg]) -> Vec { && value == 0.0 && matches!( conversion_type, - ConversionType::SciFloatLower | ConversionType::SciFloatUpper + ConversionType::SciFloatLower + | ConversionType::SciFloatUpper + | ConversionType::CompactFloatLower + | ConversionType::CompactFloatUpper ) { - scientific_zero(&mut out, &spec, value.is_sign_negative()); + float_zero(&mut out, &spec, value.is_sign_negative()); continue; } match vsprintfp(&[FormatElement::Format(spec)], &[arg.as_printf()]) { @@ -237,6 +253,47 @@ mod tests { ), b"+0000.00e+00|-0.E+00 | 0.0e+00" ); + assert_eq!( + format_c(b"% .1e|% 012.1e", &[0.0f64.into(), 0.0f64.into()]), + b" 0.0e+00| 00000.0e+00" + ); + } + + #[test] + fn compact_zero() { + assert_eq!( + format_c( + b"%g|%G|%#g", + &[0.0f64.into(), (-0.0f64).into(), 0.0f64.into()] + ), + b"0|-0|0.00000" + ); + assert_eq!( + format_c( + b"%.0g|%.1g|%#.0g|%#.1G|%#.4g", + &[ + 0.0f64.into(), + (-0.0f64).into(), + 0.0f64.into(), + (-0.0f64).into(), + 0.0f64.into() + ] + ), + b"0|-0|0.|-0.|0.000" + ); + assert_eq!( + format_c( + b"%+ 08g|% g|% 08G|%-#08.4G|%02.4g", + &[ + 0.0f64.into(), + 0.0f64.into(), + 0.0f64.into(), + (-0.0f64).into(), + 0.0f64.into() + ] + ), + b"+0000000| 0| 0000000|-0.000 |00" + ); } #[test] From b75d220d1f840adef8525859dea9daf2d8ac62c4 Mon Sep 17 00:00:00 2001 From: Perfloop Agent Date: Wed, 25 Nov 2026 19:29:29 +0000 Subject: [PATCH 4/4] Replace zero-formatting Rust tests with a C fixture Signed-off-by: Perfloop Agent --- libcc2rs/src/format.rs | 67 -------------------- tests/unit/out/refcount/printf_zero.rs | 38 +++++++++++ tests/unit/out/unsafe/printf_zero.rs | 88 ++++++++++++++++++++++++++ tests/unit/printf_zero.c | 13 ++++ 4 files changed, 139 insertions(+), 67 deletions(-) create mode 100644 tests/unit/out/refcount/printf_zero.rs create mode 100644 tests/unit/out/unsafe/printf_zero.rs create mode 100644 tests/unit/printf_zero.c diff --git a/libcc2rs/src/format.rs b/libcc2rs/src/format.rs index f04cb1a040..b120dae25f 100644 --- a/libcc2rs/src/format.rs +++ b/libcc2rs/src/format.rs @@ -227,73 +227,6 @@ mod tests { ); assert_eq!(format_c(b"%e", &[1500.0f64.into()]), b"1.500000e+03"); assert_eq!(format_c(b"%g", &[0.5f64.into()]), b"0.5"); - assert_eq!( - format_c(b"%f|%F", &[0.0f64.into(), (-0.0f64).into()]), - b"0.000000|-0.000000" - ); - } - - #[test] - fn scientific_zero() { - assert_eq!( - format_c(b"%e|%E", &[0.0f64.into(), (-0.0f64).into()]), - b"0.000000e+00|-0.000000E+00" - ); - assert_eq!( - format_c( - b"%.0e|%#.0E|%02.2e", - &[0.0f64.into(), (-0.0f64).into(), 0.0f64.into()] - ), - b"0e+00|-0.E+00|0.00e+00" - ); - assert_eq!( - format_c( - b"%+ 012.2e|%-#012.0E|% 12.1e", - &[0.0f64.into(), (-0.0f64).into(), 0.0f64.into()] - ), - b"+0000.00e+00|-0.E+00 | 0.0e+00" - ); - assert_eq!( - format_c(b"% .1e|% 012.1e", &[0.0f64.into(), 0.0f64.into()]), - b" 0.0e+00| 00000.0e+00" - ); - } - - #[test] - fn compact_zero() { - assert_eq!( - format_c( - b"%g|%G|%#g", - &[0.0f64.into(), (-0.0f64).into(), 0.0f64.into()] - ), - b"0|-0|0.00000" - ); - assert_eq!( - format_c( - b"%.0g|%.1g|%#.0g|%#.1G|%#.4g", - &[ - 0.0f64.into(), - (-0.0f64).into(), - 0.0f64.into(), - (-0.0f64).into(), - 0.0f64.into() - ] - ), - b"0|-0|0.|-0.|0.000" - ); - assert_eq!( - format_c( - b"%+ 08g|% g|% 08G|%-#08.4G|%02.4g", - &[ - 0.0f64.into(), - 0.0f64.into(), - 0.0f64.into(), - (-0.0f64).into(), - 0.0f64.into() - ] - ), - b"+0000000| 0| 0000000|-0.000 |00" - ); } #[test] diff --git a/tests/unit/out/refcount/printf_zero.rs b/tests/unit/out/refcount/printf_zero.rs new file mode 100644 index 0000000000..a7ccac4166 --- /dev/null +++ b/tests/unit/out/refcount/printf_zero.rs @@ -0,0 +1,38 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn main() { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0()); +} +fn main_0() -> i32 { + printf!(b"%e|%E\n", 0_f64, -0_f64); + printf!(b"%.0e|%#.0E|%02.2e\n", 0_f64, -0_f64, 0_f64); + printf!(b"%+ 012.2e|%-#012.0E|% 12.1e\n", 0_f64, -0_f64, 0_f64); + printf!(b"% .1e|% 012.1e\n", 0_f64, 0_f64); + printf!(b"%g|%G|%#g\n", 0_f64, -0_f64, 0_f64); + printf!( + b"%.0g|%.1g|%#.0g|%#.1G|%#.4g\n", + 0_f64, + -0_f64, + 0_f64, + -0_f64, + 0_f64 + ); + printf!( + b"%+ 08g|% g|% 08G|%-#08.4G|%02.4g\n", + 0_f64, + 0_f64, + 0_f64, + -0_f64, + 0_f64 + ); + printf!(b"%f|%F|%e|%g\n", 0_f64, -0_f64, 1500_f64, 0.5_f64); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/printf_zero.rs b/tests/unit/out/unsafe/printf_zero.rs new file mode 100644 index 0000000000..505d337655 --- /dev/null +++ b/tests/unit/out/unsafe/printf_zero.rs @@ -0,0 +1,88 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { + libc::printf( + (c"%e|%E\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char, + (0_f64), + (-0_f64), + ) + }); + (unsafe { + libc::printf( + (c"%.0e|%#.0E|%02.2e\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char, + (0_f64), + (-0_f64), + (0_f64), + ) + }); + (unsafe { + libc::printf( + (c"%+ 012.2e|%-#012.0E|% 12.1e\n".as_ptr().cast_mut()).cast_const() + as *const libc::c_char, + (0_f64), + (-0_f64), + (0_f64), + ) + }); + (unsafe { + libc::printf( + (c"% .1e|% 012.1e\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char, + (0_f64), + (0_f64), + ) + }); + (unsafe { + libc::printf( + (c"%g|%G|%#g\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char, + (0_f64), + (-0_f64), + (0_f64), + ) + }); + (unsafe { + libc::printf( + (c"%.0g|%.1g|%#.0g|%#.1G|%#.4g\n".as_ptr().cast_mut()).cast_const() + as *const libc::c_char, + (0_f64), + (-0_f64), + (0_f64), + (-0_f64), + (0_f64), + ) + }); + (unsafe { + libc::printf( + (c"%+ 08g|% g|% 08G|%-#08.4G|%02.4g\n".as_ptr().cast_mut()).cast_const() + as *const libc::c_char, + (0_f64), + (0_f64), + (0_f64), + (-0_f64), + (0_f64), + ) + }); + (unsafe { + libc::printf( + (c"%f|%F|%e|%g\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char, + (0_f64), + (-0_f64), + (1500_f64), + (0.5_f64), + ) + }); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/printf_zero.c b/tests/unit/printf_zero.c new file mode 100644 index 0000000000..274addbac2 --- /dev/null +++ b/tests/unit/printf_zero.c @@ -0,0 +1,13 @@ +#include + +int main() { + printf("%e|%E\n", 0.0, -0.0); + printf("%.0e|%#.0E|%02.2e\n", 0.0, -0.0, 0.0); + printf("%+ 012.2e|%-#012.0E|% 12.1e\n", 0.0, -0.0, 0.0); + printf("% .1e|% 012.1e\n", 0.0, 0.0); + printf("%g|%G|%#g\n", 0.0, -0.0, 0.0); + printf("%.0g|%.1g|%#.0g|%#.1G|%#.4g\n", 0.0, -0.0, 0.0, -0.0, 0.0); + printf("%+ 08g|% g|% 08G|%-#08.4G|%02.4g\n", 0.0, 0.0, 0.0, -0.0, 0.0); + printf("%f|%F|%e|%g\n", 0.0, -0.0, 1500.0, 0.5); + return 0; +}