Skip to main content

hydr_core/capability/
numeric.rs

1//! Precision, arithmetic requirements and runtime support.
2
3/// Floating-point format of a component's state.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum Precision {
7    /// IEEE 754 binary64.
8    F64,
9    /// IEEE 754 binary32 (opt-in fast profile, including WebGPU).
10    F32,
11}
12
13impl Precision {
14    /// Returns the stable kebab-case id. Persisted decisions store it, so it never changes.
15    pub fn id(&self) -> &'static str {
16        match self {
17            Self::F64 => "f64",
18            Self::F32 => "f32",
19        }
20    }
21}
22
23/// Arithmetic property that a runtime must provide for a component.
24#[derive(Clone, Copy, Debug, PartialEq, Eq)]
25#[non_exhaustive]
26pub enum ArithmeticRequirement {
27    /// IEEE 754 binary64 add, subtract, multiply and divide.
28    F64Ieee754Basic,
29    /// IEEE 754 binary32 add, subtract, multiply and divide.
30    F32Ieee754Basic,
31    /// Round-to-nearest-even with gradual underflow to subnormals.
32    NearestEvenGradualUnderflow,
33    /// Square root rounded to nearest.
34    RoundToNearestSqrt,
35    /// Fused multiply-add with one rounding.
36    FusedMulAdd,
37    /// F64 `exp`, `log`, `log1p`, `pow` and `acos` through numr `CorrectlyRoundedOps`.
38    /// `exp`, `log` and `log1p` are correctly rounded to nearest.
39    /// `pow` is correctly rounded modulo the unknown hardest cases.
40    /// `acos` is correctly rounded on `|x| >= 2^-4`, proven by an exhaustive upstream search.
41    /// Below `2^-4` no proof covers it, and numr's MPFR checks find no misrounded input.
42    /// Interval `acos` pads one ulp outward, which stays valid for any faithful result.
43    /// All five are deterministic on every runtime that meets the contracts.
44    /// Needs IEEE basic arithmetic and fused multiply-add.
45    /// Evidence: numr `tests/correctly_rounded.rs` checks every function against MPFR, and
46    /// `tests/backend_parity/correctly_rounded.rs` checks CUDA against CPU bit for bit.
47    CorrectlyRoundedElementary,
48}
49
50impl ArithmeticRequirement {
51    /// Returns the stable kebab-case id. Persisted decisions store it, so it never changes.
52    pub fn id(&self) -> &'static str {
53        match self {
54            Self::F64Ieee754Basic => "f64-ieee754-basic",
55            Self::F32Ieee754Basic => "f32-ieee754-basic",
56            Self::NearestEvenGradualUnderflow => "nearest-even-gradual-underflow",
57            Self::RoundToNearestSqrt => "round-to-nearest-sqrt",
58            Self::FusedMulAdd => "fused-mul-add",
59            Self::CorrectlyRoundedElementary => "correctly-rounded-elementary",
60        }
61    }
62}
63
64/// Numr runtime family.
65#[derive(Clone, Copy, Debug, PartialEq, Eq)]
66#[non_exhaustive]
67pub enum RuntimeKind {
68    /// CPU runtime.
69    Cpu,
70    /// CUDA runtime.
71    Cuda,
72    /// WebGPU runtime.
73    Wgpu,
74}
75
76impl RuntimeKind {
77    /// Returns the stable kebab-case id. Persisted decisions store it, so it never changes.
78    pub fn id(&self) -> &'static str {
79        match self {
80            Self::Cpu => "cpu",
81            Self::Cuda => "cuda",
82            Self::Wgpu => "wgpu",
83        }
84    }
85}
86
87/// Standing of a component on one runtime.
88#[derive(Clone, Copy, Debug, PartialEq, Eq)]
89#[non_exhaustive]
90pub enum RuntimeStatus {
91    /// Reference profile. Other runtimes are checked against it.
92    Reference,
93    /// A test runs the component on this runtime.
94    Executed,
95    /// Admission accepts the runtime, but no test runs the component on it.
96    AdmittedUnexercised,
97    /// Admission refuses the runtime because it lacks F64.
98    RejectedLacksF64,
99}
100
101impl RuntimeStatus {
102    /// Returns the stable kebab-case id. Persisted decisions store it, so it never changes.
103    pub fn id(&self) -> &'static str {
104        match self {
105            Self::Reference => "reference",
106            Self::Executed => "executed",
107            Self::AdmittedUnexercised => "admitted-unexercised",
108            Self::RejectedLacksF64 => "rejected-lacks-f64",
109        }
110    }
111}
112
113/// Standing of a component on one runtime, with its evidence.
114#[derive(Clone, Copy, Debug, PartialEq, Eq)]
115pub struct RuntimeSupport {
116    /// Runtime family.
117    pub runtime: RuntimeKind,
118    /// Standing on that runtime.
119    pub status: RuntimeStatus,
120    /// Name of the test function that shows the status.
121    pub evidence: &'static str,
122}
123
124#[cfg(test)]
125mod tests {
126    use super::*;
127    use crate::capability::id_check::assert_ids;
128
129    #[test]
130    fn precision_ids_are_unique_kebab_case() {
131        assert_ids(&[Precision::F64.id(), Precision::F32.id()]);
132    }
133
134    #[test]
135    fn arithmetic_ids_are_unique_kebab_case() {
136        let all = [
137            ArithmeticRequirement::F64Ieee754Basic,
138            ArithmeticRequirement::F32Ieee754Basic,
139            ArithmeticRequirement::NearestEvenGradualUnderflow,
140            ArithmeticRequirement::RoundToNearestSqrt,
141            ArithmeticRequirement::FusedMulAdd,
142            ArithmeticRequirement::CorrectlyRoundedElementary,
143        ];
144        assert_ids(&all.map(|a| a.id()));
145    }
146
147    #[test]
148    fn runtime_ids_are_unique_kebab_case() {
149        let all = [RuntimeKind::Cpu, RuntimeKind::Cuda, RuntimeKind::Wgpu];
150        assert_ids(&all.map(|r| r.id()));
151    }
152
153    #[test]
154    fn runtime_status_ids_are_unique_kebab_case() {
155        let all = [
156            RuntimeStatus::Reference,
157            RuntimeStatus::Executed,
158            RuntimeStatus::AdmittedUnexercised,
159            RuntimeStatus::RejectedLacksF64,
160        ];
161        assert_ids(&all.map(|s| s.id()));
162    }
163}