Skip to main content

ftts_kernels/
route.rs

1//! The one process-wide switch between the optimized product route and the f32 reference.
2//!
3//! The optimized route (int8 projections, the SLEEF SnakeBeta, the worker team) is the DEFAULT
4//! everywhere — library and binary alike. Two things override it:
5//!
6//! - **Environment**: `FTTS_INT8=0` (or `off`/`false`) selects the reference route for a run;
7//!   the finer-grained `FTTS_INT8_CODEC` / `FTTS_FAST_SNAKE` variables tune individual pieces.
8//! - **[`pin_reference`]**: a programmatic, first-call-wins pin used by the conformance
9//!   harness, so every oracle-parity test measures the reference numerics no matter what the
10//!   surrounding environment says. A parity suite that silently tested the optimized route
11//!   would be comparing the wrong thing against the oracle.
12//!
13//! The pin is sticky for the process (OnceLock): parity test binaries pin once, product
14//! processes never call it.
15
16use std::sync::OnceLock;
17
18static REFERENCE_PIN: OnceLock<()> = OnceLock::new();
19
20/// Pins this process to the f32 reference route, regardless of environment defaults.
21///
22/// First call wins and the pin never lifts; call it before any synthesis or decode work so
23/// no armed route has cached its decision yet.
24pub fn pin_reference() {
25    let _ = REFERENCE_PIN.set(());
26}
27
28/// Whether [`pin_reference`] has been called.
29#[must_use]
30pub fn reference_pinned() -> bool {
31    REFERENCE_PIN.get().is_some()
32}
33
34/// The shared default rule for optimized-route switches.
35///
36/// Returns `false` (reference) when the process is pinned or `variable` is set to a disabling
37/// value; returns `true` otherwise — including when the variable is unset, which is what makes
38/// the optimized route the default everywhere.
39#[must_use]
40pub fn optimized_default(variable: &str) -> bool {
41    if reference_pinned() {
42        return false;
43    }
44    !matches!(
45        std::env::var(variable).as_deref(),
46        Ok("0" | "off" | "false")
47    )
48}