Skip to content

Commit b52465f

Browse files
Fix scientific and compact formatting of zero (#682)
`format_c` turns `%e` with `0.0` into `NaN.000000e-2147483648` instead of `0.000000e+00`. For the same zero argument, `%g` and `%G` panic in debug builds and emit `NaN.` in release builds. The refcount `printf`, `fprintf` and `snprintf` paths now format scientific (`%e`/`%E`) and compact (`%g`/`%G`) zeros directly, avoiding logarithmic normalization. Nonzero, subnormal and non-finite values, and `%f`/`%F`, retain the existing formatter; this is not a general normalization or rounding repair. --- Made by [Perfloop](https://app.perfloop.ai). Human sponsor: [Tomás Senart](https://github.com/tsenart). --------- Signed-off-by: Perfloop Agent <agent@perfloop.ai>
1 parent a7d4cc9 commit b52465f

4 files changed

Lines changed: 201 additions & 1 deletion

File tree

‎libcc2rs/src/format.rs‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,54 @@ fn pad(out: &mut Vec<u8>, spec: &ConversionSpecifier, s: &[u8]) {
5959
}
6060
}
6161

62+
// Zero has no logarithmic exponent. Format it without sprintf's normalization.
63+
fn float_zero(out: &mut Vec<u8>, spec: &ConversionSpecifier, negative: bool) {
64+
let NumericParam::Literal(precision) = spec.precision else {
65+
unreachable!()
66+
};
67+
let scientific = matches!(
68+
spec.conversion_type,
69+
ConversionType::SciFloatLower | ConversionType::SciFloatUpper
70+
);
71+
let precision = if scientific {
72+
precision as usize
73+
} else if spec.alt_form {
74+
precision.max(1) as usize - 1
75+
} else {
76+
0
77+
};
78+
let mut number = Vec::new();
79+
if negative {
80+
number.push(b'-');
81+
} else if spec.force_sign {
82+
number.push(b'+');
83+
} else if spec.space_sign {
84+
number.push(b' ');
85+
}
86+
let point = precision > 0 || spec.alt_form;
87+
if spec.zero_pad && !spec.left_adj {
88+
let NumericParam::Literal(width) = spec.width else {
89+
unreachable!()
90+
};
91+
let len =
92+
number.len() + 1 + usize::from(point) + precision + if scientific { 4 } else { 0 };
93+
number.resize(number.len() + (width as usize).saturating_sub(len), b'0');
94+
}
95+
number.push(b'0');
96+
if point {
97+
number.push(b'.');
98+
}
99+
number.resize(number.len() + precision, b'0');
100+
if scientific {
101+
number.extend_from_slice(if spec.conversion_type == ConversionType::SciFloatUpper {
102+
b"E+00"
103+
} else {
104+
b"e+00"
105+
});
106+
}
107+
pad(out, spec, &number);
108+
}
109+
62110
fn numeric_arg(conversion_type: ConversionType, arg: &VaArg) -> Arg {
63111
match conversion_type {
64112
ConversionType::DecInt => match arg {
@@ -87,7 +135,7 @@ fn numeric_arg(conversion_type: ConversionType, arg: &VaArg) -> Arg {
87135

88136
// Formats like C's printf. The format, the strings and the result are bytes,
89137
// which need not be UTF-8, so %s and %c are formatted here; sprintf formats
90-
// the numbers, which are ASCII.
138+
// the numbers, which are ASCII, except for scientific and compact zero.
91139
pub fn format_c(fmt: &[u8], va: &[VaArg]) -> Vec<u8> {
92140
let mut out = Vec::with_capacity(fmt.len());
93141
let mut va = va.iter();
@@ -121,6 +169,19 @@ pub fn format_c(fmt: &[u8], va: &[VaArg]) -> Vec<u8> {
121169
}
122170
conversion_type => {
123171
let arg = numeric_arg(conversion_type, arg);
172+
if let Arg::Double(value) = arg
173+
&& value == 0.0
174+
&& matches!(
175+
conversion_type,
176+
ConversionType::SciFloatLower
177+
| ConversionType::SciFloatUpper
178+
| ConversionType::CompactFloatLower
179+
| ConversionType::CompactFloatUpper
180+
)
181+
{
182+
float_zero(&mut out, &spec, value.is_sign_negative());
183+
continue;
184+
}
124185
match vsprintfp(&[FormatElement::Format(spec)], &[arg.as_printf()]) {
125186
Ok(s) => out.extend_from_slice(s.as_bytes()),
126187
Err(e) => panic!("format_c: cannot format {spec:?}: {e:?}"),
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn main() {
10+
__cpp2rust_init_globals();
11+
libcc2rs::cpp2rust_exit(main_0());
12+
}
13+
fn main_0() -> i32 {
14+
printf!(b"%e|%E\n", 0_f64, -0_f64);
15+
printf!(b"%.0e|%#.0E|%02.2e\n", 0_f64, -0_f64, 0_f64);
16+
printf!(b"%+ 012.2e|%-#012.0E|% 12.1e\n", 0_f64, -0_f64, 0_f64);
17+
printf!(b"% .1e|% 012.1e\n", 0_f64, 0_f64);
18+
printf!(b"%g|%G|%#g\n", 0_f64, -0_f64, 0_f64);
19+
printf!(
20+
b"%.0g|%.1g|%#.0g|%#.1G|%#.4g\n",
21+
0_f64,
22+
-0_f64,
23+
0_f64,
24+
-0_f64,
25+
0_f64
26+
);
27+
printf!(
28+
b"%+ 08g|% g|% 08G|%-#08.4G|%02.4g\n",
29+
0_f64,
30+
0_f64,
31+
0_f64,
32+
-0_f64,
33+
0_f64
34+
);
35+
printf!(b"%f|%F|%e|%g\n", 0_f64, -0_f64, 1500_f64, 0.5_f64);
36+
return 0;
37+
}
38+
pub fn __cpp2rust_init_globals() {}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub fn main() {
10+
unsafe {
11+
__cpp2rust_init_globals();
12+
libcc2rs::cpp2rust_exit(main_0() as i32);
13+
}
14+
}
15+
unsafe fn main_0() -> i32 {
16+
(unsafe {
17+
libc::printf(
18+
(c"%e|%E\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char,
19+
(0_f64),
20+
(-0_f64),
21+
)
22+
});
23+
(unsafe {
24+
libc::printf(
25+
(c"%.0e|%#.0E|%02.2e\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char,
26+
(0_f64),
27+
(-0_f64),
28+
(0_f64),
29+
)
30+
});
31+
(unsafe {
32+
libc::printf(
33+
(c"%+ 012.2e|%-#012.0E|% 12.1e\n".as_ptr().cast_mut()).cast_const()
34+
as *const libc::c_char,
35+
(0_f64),
36+
(-0_f64),
37+
(0_f64),
38+
)
39+
});
40+
(unsafe {
41+
libc::printf(
42+
(c"% .1e|% 012.1e\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char,
43+
(0_f64),
44+
(0_f64),
45+
)
46+
});
47+
(unsafe {
48+
libc::printf(
49+
(c"%g|%G|%#g\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char,
50+
(0_f64),
51+
(-0_f64),
52+
(0_f64),
53+
)
54+
});
55+
(unsafe {
56+
libc::printf(
57+
(c"%.0g|%.1g|%#.0g|%#.1G|%#.4g\n".as_ptr().cast_mut()).cast_const()
58+
as *const libc::c_char,
59+
(0_f64),
60+
(-0_f64),
61+
(0_f64),
62+
(-0_f64),
63+
(0_f64),
64+
)
65+
});
66+
(unsafe {
67+
libc::printf(
68+
(c"%+ 08g|% g|% 08G|%-#08.4G|%02.4g\n".as_ptr().cast_mut()).cast_const()
69+
as *const libc::c_char,
70+
(0_f64),
71+
(0_f64),
72+
(0_f64),
73+
(-0_f64),
74+
(0_f64),
75+
)
76+
});
77+
(unsafe {
78+
libc::printf(
79+
(c"%f|%F|%e|%g\n".as_ptr().cast_mut()).cast_const() as *const libc::c_char,
80+
(0_f64),
81+
(-0_f64),
82+
(1500_f64),
83+
(0.5_f64),
84+
)
85+
});
86+
return 0;
87+
}
88+
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/printf_zero.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
printf("%e|%E\n", 0.0, -0.0);
5+
printf("%.0e|%#.0E|%02.2e\n", 0.0, -0.0, 0.0);
6+
printf("%+ 012.2e|%-#012.0E|% 12.1e\n", 0.0, -0.0, 0.0);
7+
printf("% .1e|% 012.1e\n", 0.0, 0.0);
8+
printf("%g|%G|%#g\n", 0.0, -0.0, 0.0);
9+
printf("%.0g|%.1g|%#.0g|%#.1G|%#.4g\n", 0.0, -0.0, 0.0, -0.0, 0.0);
10+
printf("%+ 08g|% g|% 08G|%-#08.4G|%02.4g\n", 0.0, 0.0, 0.0, -0.0, 0.0);
11+
printf("%f|%F|%e|%g\n", 0.0, -0.0, 1500.0, 0.5);
12+
return 0;
13+
}

0 commit comments

Comments
 (0)