#![forbid(unsafe_code)]
use std::collections::{BTreeMap, HashMap};
use std::fs;
use std::io::Write;
use std::path::PathBuf;
use std::process::Stdio;
use std::time::{Instant, SystemTime, UNIX_EPOCH};
use fsci_conformance::{ArmCounts, CompareLedger};
use fsci_special::{
ceil, fabs, floor, fmax, fmin, hypot, ldexp, maximum, minimum, modf, nan_to_num, negative,
positive, power, reciprocal, round, sign, signbit, square, trunc,
};
use serde::{Deserialize, Serialize};
const PACKET_ID: &str = "FSCI-P2C-006";
const ABS_TOL: f64 = 1.0e-12;
const REQUIRE_SCIPY_ENV: &str = "FSCI_REQUIRE_SCIPY_ORACLE";
const ARMS: [&str; 20] = [
"floor",
"ceil",
"fabs",
"trunc",
"round",
"sign",
"reciprocal",
"square",
"positive",
"negative",
"signbit",
"modf",
"maximum",
"minimum",
"fmax",
"fmin",
"hypot",
"power",
"ldexp",
"nan_to_num",
];
#[derive(Debug, Clone, Serialize)]
struct PointCase {
case_id: String,
op: String,
a: f64,
b: f64,
n: i64,
}
#[derive(Debug, Clone, Serialize)]
struct OracleQuery {
points: Vec<PointCase>,
}
#[derive(Debug, Clone, Deserialize)]
struct PointArm {
case_id: String,
value: Option<f64>,
aux: Option<f64>,
bool_value: Option<bool>,
}
#[derive(Debug, Clone, Deserialize)]
struct OracleResult {
points: Vec<PointArm>,
}
#[derive(Debug, Clone, Serialize)]
struct CaseDiff {
case_id: String,
op: String,
abs_diff: f64,
pass: bool,
}
#[derive(Debug, Clone, Serialize)]
struct DiffLog {
test_id: String,
category: String,
case_count: usize,
compared: BTreeMap<String, ArmCounts>,
max_abs_diff: f64,
pass: bool,
timestamp_ms: u128,
duration_ns: u128,
cases: Vec<CaseDiff>,
}
fn output_dir() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(format!("fixtures/artifacts/{PACKET_ID}/diff"))
}
fn ensure_output_dir() {
fs::create_dir_all(output_dir()).expect("create basic_math diff output dir");
}
fn timestamp_ms() -> u128 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0, |d| d.as_millis())
}
fn emit_log(log: &DiffLog) {
ensure_output_dir();
let path = output_dir().join(format!("{}.json", log.test_id));
let json = serde_json::to_string_pretty(log).expect("serialize basic_math diff log");
fs::write(path, json).expect("write basic_math diff log");
}
fn generate_query() -> OracleQuery {
let mut points = Vec::new();
let unary_xs = [-2.7_f64, -1.0, -0.5, 0.0, 0.5, 1.0, 2.7, 10.0];
for x in unary_xs {
for op in [
"floor",
"ceil",
"fabs",
"trunc",
"round",
"sign",
"reciprocal",
"square",
"positive",
"negative",
] {
if op == "reciprocal" && x == 0.0 {
continue; }
points.push(PointCase {
case_id: format!("{op}_{x}"),
op: op.into(),
a: x,
b: 0.0,
n: 0,
});
}
points.push(PointCase {
case_id: format!("signbit_{x}"),
op: "signbit".into(),
a: x,
b: 0.0,
n: 0,
});
points.push(PointCase {
case_id: format!("modf_{x}"),
op: "modf".into(),
a: x,
b: 0.0,
n: 0,
});
}
let binary_pairs: &[(f64, f64)] = &[
(3.0, 4.0),
(-1.0, 1.0),
(5.0, -3.0),
(0.0, 0.0),
(1e-5, 2.0),
];
for &(x, y) in binary_pairs {
for op in ["maximum", "minimum", "fmax", "fmin", "hypot", "power"] {
points.push(PointCase {
case_id: format!("{op}_{x}_{y}"),
op: op.into(),
a: x,
b: y,
n: 0,
});
}
}
for &(x, n) in &[(1.0_f64, 4_i64), (1.5, -3), (-2.0, 10), (0.5, 0), (3.7, -5)] {
points.push(PointCase {
case_id: format!("ldexp_{x}_{n}"),
op: "ldexp".into(),
a: x,
b: 0.0,
n,
});
}
for x in [1.0_f64, -2.0, 0.0, 100.0] {
points.push(PointCase {
case_id: format!("nan_to_num_{x}"),
op: "nan_to_num".into(),
a: x,
b: 1.0e308,
n: 0,
});
}
OracleQuery { points }
}
fn scipy_oracle_or_skip(query: &OracleQuery) -> Option<OracleResult> {
let script = r#"
import json
import math
import sys
import numpy as np
q = json.load(sys.stdin)
points = []
for case in q["points"]:
cid = case["case_id"]; op = case["op"]
a = float(case["a"]); b = float(case["b"]); n = int(case["n"])
v = None; aux = None; bool_v = None
try:
if op == "floor": v = float(math.floor(a))
elif op == "ceil": v = float(math.ceil(a))
elif op == "fabs": v = float(abs(a))
elif op == "trunc": v = float(math.trunc(a))
elif op == "round":
# fsci uses Rust's f64::round which rounds half away from zero;
# Python's built-in round() is banker's. Match Rust convention:
if a > 0:
v = float(math.floor(a + 0.5))
else:
v = float(-math.floor(-a + 0.5))
elif op == "sign":
v = 0.0 if a == 0.0 else (1.0 if a > 0 else -1.0)
if math.isnan(a):
v = float("nan")
elif op == "reciprocal": v = 1.0 / a
elif op == "square": v = a * a
elif op == "positive":
# fsci: max(x, 0) — positive part / ReLU
if math.isnan(a): v = float("nan")
elif a > 0: v = a
else: v = 0.0
elif op == "negative":
# fsci: max(-x, 0) — negative-part magnitude
if math.isnan(a): v = float("nan")
elif a < 0: v = -a
else: v = 0.0
elif op == "maximum":
v = float("nan") if (math.isnan(a) or math.isnan(b)) else max(a, b)
elif op == "minimum":
v = float("nan") if (math.isnan(a) or math.isnan(b)) else min(a, b)
elif op == "fmax":
if math.isnan(a): v = b
elif math.isnan(b): v = a
else: v = max(a, b)
elif op == "fmin":
if math.isnan(a): v = b
elif math.isnan(b): v = a
else: v = min(a, b)
elif op == "hypot": v = math.hypot(a, b)
elif op == "power": v = math.pow(a, b)
elif op == "ldexp": v = math.ldexp(a, n)
elif op == "signbit": bool_v = bool(math.copysign(1.0, a) < 0)
elif op == "modf":
frac, whole = math.modf(a)
v = float(whole); aux = float(frac)
elif op == "nan_to_num":
# fsci: nan_to_num(x, 0.0, 1e308, -1e308) — replaces NaN with 0,
# +inf with posinf arg (b), -inf with -posinf (since we use the
# same magnitude for both via the n arg or hardcoded).
if math.isnan(a): v = 0.0
elif a == float("inf"): v = b # posinf
elif a == float("-inf"): v = -b # neginf
else: v = a
else:
v = None
# finite check (NaN allowed for sign(nan) etc; require values to be float())
if v is None and bool_v is None:
points.append({"case_id": cid, "value": None, "aux": None, "bool_value": None})
elif bool_v is not None:
points.append({"case_id": cid, "value": None, "aux": None, "bool_value": bool_v})
else:
if math.isnan(v):
points.append({"case_id": cid, "value": float("nan"),
"aux": aux, "bool_value": None})
elif not math.isfinite(v):
# Allow ±inf as exact tokens stringified by Rust; we encode None
# and skip via lenient compare below.
points.append({"case_id": cid, "value": None, "aux": aux,
"bool_value": None})
else:
points.append({"case_id": cid, "value": float(v),
"aux": aux, "bool_value": None})
except Exception:
points.append({"case_id": cid, "value": None, "aux": None, "bool_value": None})
print(json.dumps({"points": points}))
"#;
let query_json = serde_json::to_string(query).expect("serialize basic_math query");
let mut child = match fsci_conformance::scipy_oracle_command()
.arg("-c")
.arg(script)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
{
Ok(c) => c,
Err(e) => {
assert!(
std::env::var(REQUIRE_SCIPY_ENV).is_err(),
"failed to spawn python3 for basic_math oracle: {e}"
);
eprintln!("skipping basic_math oracle: python3 not available ({e})");
return None;
}
};
{
let stdin = child.stdin.as_mut().expect("open basic_math oracle stdin");
if let Err(err) = stdin.write_all(query_json.as_bytes()) {
let output = child.wait_with_output().expect("wait for failed oracle");
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
std::env::var(REQUIRE_SCIPY_ENV).is_err(),
"basic_math oracle stdin write failed: {err}; stderr: {stderr}"
);
eprintln!("skipping basic_math oracle: stdin write failed ({err})\n{stderr}");
return None;
}
}
let output = child
.wait_with_output()
.expect("wait for basic_math oracle");
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
std::env::var(REQUIRE_SCIPY_ENV).is_err(),
"basic_math oracle failed: {stderr}"
);
eprintln!("skipping basic_math oracle: numpy not available\n{stderr}");
return None;
}
let stdout = String::from_utf8_lossy(&output.stdout);
Some(serde_json::from_str(&stdout).expect("parse basic_math oracle JSON"))
}
#[test]
fn diff_special_basic_math() {
let query = generate_query();
let Some(oracle) = scipy_oracle_or_skip(&query) else {
return;
};
assert_eq!(oracle.points.len(), query.points.len());
let pmap: HashMap<String, PointArm> = oracle
.points
.into_iter()
.map(|d| (d.case_id.clone(), d))
.collect();
let start = Instant::now();
let mut diffs = Vec::new();
let mut max_overall = 0.0_f64;
let mut ledger = CompareLedger::new("diff_special_basic_math", &ARMS);
for case in &query.points {
let scipy_arm = pmap.get(&case.case_id).expect("validated oracle");
let arm = case.op.as_str();
match arm {
"signbit" => {
let Some((b_exp, fsci_b)) = ledger.both(
arm,
&case.case_id,
scipy_arm.bool_value,
Some(signbit(case.a)),
) else {
continue;
};
let pass = fsci_b == b_exp;
ledger.compared(arm, &case.case_id, pass);
diffs.push(CaseDiff {
case_id: case.case_id.clone(),
op: "signbit".into(),
abs_diff: if pass { 0.0 } else { 1.0 },
pass,
});
}
"modf" => {
let scipy_parts = scipy_arm
.value
.zip(scipy_arm.aux)
.map(|(whole, frac)| vec![whole, frac]);
let (frac_f, whole_f) = modf(case.a);
let fsci_parts = [whole_f, frac_f];
let Some((s, f)) = ledger.slices(
arm,
&case.case_id,
scipy_parts.as_deref(),
Some(fsci_parts.as_slice()),
) else {
continue;
};
let (whole_exp, frac_exp) = (s[0], s[1]);
let (whole_f, frac_f) = (f[0], f[1]);
let d = (whole_f - whole_exp).abs().max((frac_f - frac_exp).abs());
max_overall = max_overall.max(d);
ledger.compared(arm, &case.case_id, d <= ABS_TOL);
diffs.push(CaseDiff {
case_id: case.case_id.clone(),
op: "modf".into(),
abs_diff: d,
pass: d <= ABS_TOL,
});
}
_ => {
let fsci_v = match arm {
"floor" => floor(case.a),
"ceil" => ceil(case.a),
"fabs" => fabs(case.a),
"trunc" => trunc(case.a),
"round" => round(case.a),
"sign" => sign(case.a),
"reciprocal" => reciprocal(case.a),
"square" => square(case.a),
"positive" => positive(case.a),
"negative" => negative(case.a),
"maximum" => maximum(case.a, case.b),
"minimum" => minimum(case.a, case.b),
"fmax" => fmax(case.a, case.b),
"fmin" => fmin(case.a, case.b),
"hypot" => hypot(case.a, case.b),
"power" => power(case.a, case.b),
"ldexp" => ldexp(case.a, case.n as i32),
"nan_to_num" => nan_to_num(case.a, 0.0, case.b, -case.b),
other => panic!("unknown op {other} in {}", case.case_id),
};
let Some((scipy_v, fsci_v)) =
ledger.pair(arm, &case.case_id, scipy_arm.value, Some(fsci_v))
else {
continue;
};
let abs_d = if scipy_v.is_nan() && fsci_v.is_nan() {
0.0
} else {
(fsci_v - scipy_v).abs()
};
max_overall = max_overall.max(abs_d);
ledger.compared(arm, &case.case_id, abs_d <= ABS_TOL);
diffs.push(CaseDiff {
case_id: case.case_id.clone(),
op: case.op.clone(),
abs_diff: abs_d,
pass: abs_d <= ABS_TOL,
});
}
}
}
let all_pass = diffs.iter().all(|d| d.pass);
let log = DiffLog {
test_id: "diff_special_basic_math".into(),
category: "fsci_special basic math wrappers".into(),
case_count: diffs.len(),
compared: ledger.counts().clone(),
max_abs_diff: max_overall,
pass: all_pass,
timestamp_ms: timestamp_ms(),
duration_ns: start.elapsed().as_nanos(),
cases: diffs.clone(),
};
emit_log(&log);
for d in &diffs {
if !d.pass {
eprintln!("{} mismatch: {} abs_diff={}", d.op, d.case_id, d.abs_diff);
}
}
assert!(
all_pass,
"basic_math conformance failed: {} cases, max_diff={}",
diffs.len(),
max_overall
);
let min_per_arm = ARMS
.iter()
.map(|arm| query.points.iter().filter(|c| c.op == *arm).count())
.min()
.expect("ARMS is non-empty");
ledger.finish(min_per_arm);
}