euv-core 0.48.3

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Converts a display refresh rate in Hertz into deterministic integer millihertz (`144.0 Hz -> 144_000`).
///
/// # Arguments
///
/// - `f64` - Display refresh rate in Hertz.
///
/// # Returns
///
/// - `u32` - Quantized millihertz value (`0` when non-positive or non-finite).
#[must_use]
pub fn hz_to_millihz(hz: f64) -> u32 {
    if !hz.is_finite() || hz <= 0.0 {
        return 0;
    }
    (hz * HZ_MILLIHZ_SCALE).round().clamp(0.0, u32::MAX as f64) as u32
}

/// Computes an exact nearest-rank percentile over a pre-sorted non-empty `f64` slice.
///
/// # Arguments
///
/// - `&[f64]` - Ascending sorted samples.
/// - `f64` - Percentile fraction in `[0.0, 1.0]` (e.g., `0.95` for p95).
///
/// # Returns
///
/// - `f64` - Nearest-rank percentile value (`0.0` when slice is empty).
#[must_use]
pub(crate) fn sorted_percentile_f64(sorted: &[f64], percentile: f64) -> f64 {
    if sorted.is_empty() {
        return 0.0;
    }
    let clamped: f64 = percentile.clamp(0.0, 1.0);
    let max_idx: usize = sorted.len().saturating_sub(1);
    let idx: usize = ((clamped * (max_idx as f64)).ceil() as usize).min(max_idx);
    sorted[idx]
}

/// Computes a unified lower-is-better composite latency cost combining real missed frames, p95/p99 tail time, and input-to-present latency.
///
/// # Arguments
///
/// - `&CandidateScore` - Measured candidate score.
///
/// # Returns
///
/// - `f64` - Lower-is-better composite score in millisecond equivalents.
#[must_use]
pub fn compute_candidate_composite_cost_ms(score: &CandidateScore) -> f64 {
    let missed_penalty: f64 = score.get_missed_frame_ratio().max(0.0) * SCORE_MISSED_WEIGHT_MS;
    let p95_term: f64 = score.get_p95_frame_ms().max(0.0) * SCORE_P95_WEIGHT;
    let p99_term: f64 = score.get_p99_frame_ms().max(0.0) * SCORE_P99_WEIGHT;
    let input_term: f64 = score.get_input_to_present_ms().max(0.0) * SCORE_INPUT_WEIGHT;
    missed_penalty + p95_term + p99_term + input_term
}