use crate::math::common::{InstructionSet, TEST_ISA_OVERRIDE, encode_isa_override};
use std::sync::atomic::Ordering;
#[derive(Debug)]
pub struct ForceAvx2Guard {
prev_override: u8,
}
impl ForceAvx2Guard {
pub fn new() -> Self {
let prev =
TEST_ISA_OVERRIDE.swap(encode_isa_override(InstructionSet::Avx2), Ordering::SeqCst);
Self {
prev_override: prev,
}
}
}
impl Default for ForceAvx2Guard {
fn default() -> Self {
Self::new()
}
}
impl Drop for ForceAvx2Guard {
fn drop(&mut self) {
TEST_ISA_OVERRIDE.store(self.prev_override, Ordering::SeqCst);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::math::common::effective_instruction_set;
#[test]
fn test_force_avx2_guard_lifecycle() {
let initial_isa = effective_instruction_set();
{
let _guard = ForceAvx2Guard::new();
assert_eq!(
effective_instruction_set(),
InstructionSet::Avx2,
"effective_instruction_set() must return InstructionSet::Avx2 while guard is active"
);
}
assert_eq!(
effective_instruction_set(),
initial_isa,
"effective_instruction_set() must revert to initial state after guard drops"
);
}
}