pub(in crate::inspect::smp) fn probable_normal_f32_string(value: u32) -> Option<String> {
    let exponent = (value >> 23) & 0xff;
    if exponent == 0 || exponent == 0xff {
        return None;
    }
    let scalar = f32::from_bits(value);
    if !scalar.is_finite() {
        return None;
    }
    Some(format!("{scalar:.6}"))
}

pub(in crate::inspect::smp) fn probable_recipe_token_high16_ascii_stem(
    value: u32,
) -> Option<String> {
    if value & 0xffff != 0 {
        return None;
    }
    let high = ((value >> 16) & 0xffff) as u16;
    if high == 0 {
        return None;
    }
    let low_byte = (high & 0x00ff) as u8;
    let high_byte = (high >> 8) as u8;
    if !low_byte.is_ascii_alphabetic() || !high_byte.is_ascii_alphabetic() {
        return None;
    }
    Some(format!("{}{}", low_byte as char, high_byte as char))
}

pub(in crate::inspect::smp) fn classify_recipe_token_layout(value: u32) -> &'static str {
    if value == 0 {
        return "zero";
    }
    if probable_recipe_token_high16_ascii_stem(value).is_some() {
        return "high16-ascii-stem";
    }
    if value & 0xffff == 0 {
        return "high16-numeric";
    }
    if value >> 16 == 0 {
        return "low16-marker";
    }
    "mixed"
}

pub(in crate::inspect::smp) fn classify_recipe_line_signature(
    mode_word: u32,
    supplied_cargo_token_word: u32,
    demanded_cargo_token_word: u32,
) -> &'static str {
    let supplied_layout = classify_recipe_token_layout(supplied_cargo_token_word);
    let demanded_layout = classify_recipe_token_layout(demanded_cargo_token_word);
    if mode_word == 0 && supplied_cargo_token_word == 0 && demanded_layout == "high16-numeric" {
        return "demand-numeric-entry";
    }
    if mode_word == 0 && supplied_cargo_token_word == 0 && demanded_layout == "high16-ascii-stem" {
        return "demand-stem-entry";
    }
    if mode_word == 0 && demanded_cargo_token_word == 0 && supplied_layout == "high16-numeric" {
        return "supply-numeric-entry";
    }
    if mode_word != 0 && demanded_cargo_token_word == 0 && supplied_layout == "low16-marker" {
        return "supply-marker-entry";
    }
    if mode_word == 0 && supplied_cargo_token_word == 0 && demanded_cargo_token_word == 0 {
        return "zero";
    }
    "mixed"
}

pub(in crate::inspect::smp) fn classify_recipe_runtime_import_branch(
    mode_word: u32,
) -> &'static str {
    if mode_word == 0 {
        return "zero-mode-skipped";
    }
    if mode_word == 1 {
        return "mode1-demand-branch";
    }
    if mode_word == 3 {
        return "mode3-dual-branch";
    }
    "nonzero-supply-branch"
}

pub(in crate::inspect::smp) fn classify_recipe_book_region_kind(bytes: &[u8]) -> &'static str {
    if bytes.iter().all(|byte| *byte == 0) {
        "zero"
    } else if bytes.iter().all(|byte| *byte == 0xcd) {
        "cdcd"
    } else {
        "mixed"
    }
}
