frozen-core 0.0.10

Custom implementations and core utilities for frozen codebases
Documentation
//! Implementation of branch preditor hints

/// Empty function used as a placeholder to influence branch prediction
#[cold]
#[inline]
const fn cold_fn() {}

/// Branch predictor hint, which marks given condition as *likely* to be
///
/// # Example
///
/// ```
/// use frozen_core::hints::likely;
/// assert!(likely(true));
/// assert!(!likely(false));
/// ```
#[inline]
pub const fn likely(b: bool) -> bool {
    if !b {
        cold_fn();
    }
    b
}

/// Branch predictor hint, which marks given condition as *unlikely* to be
///
/// # Example
///
/// ```
/// use frozen_core::hints::unlikely;
/// assert!(unlikely(true));
/// assert!(!unlikely(false));
/// ```
#[inline]
pub const fn unlikely(b: bool) -> bool {
    if b {
        cold_fn();
    }
    b
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sanity_checks_for_likely() {
        assert!(likely(true));
        assert!(!likely(false));
    }

    #[test]
    fn sanity_checks_for_unlikely() {
        assert!(unlikely(true));
        assert!(!unlikely(false));
    }
}