canic_utils/instructions.rs
1//! Helpers for formatting raw instruction counts into friendly strings.
2
3/// Format an instruction count using engineering suffixes (K/M/B/T).
4#[must_use]
5#[allow(clippy::cast_precision_loss)]
6pub fn format_instructions(n: u64) -> String {
7 const TABLE: &[(u64, &str)] = &[
8 (1_000_000_000_000, "T"),
9 (1_000_000_000, "B"),
10 (1_000_000, "M"),
11 (1_000, "K"),
12 ];
13
14 for &(div, suffix) in TABLE {
15 if n >= div {
16 return format!("{:.2}{}", n as f64 / div as f64, suffix);
17 }
18 }
19
20 n.to_string()
21}