1
0
Fork 0

Simplify ScopedFtz

This commit is contained in:
Robbert van der Helm 2022-02-13 13:51:02 +01:00
parent e7c3e19519
commit 7d3beb174e

View file

@ -94,8 +94,8 @@ pub fn process_wrapper<T, F: FnOnce() -> T>(f: F) -> T {
/// Enable the CPU's Flush To Zero flag while this object is in scope. If the flag was not already /// Enable the CPU's Flush To Zero flag while this object is in scope. If the flag was not already
/// set, it will be restored to its old value when this gets dropped. /// set, it will be restored to its old value when this gets dropped.
struct ScopedFtz { struct ScopedFtz {
/// The old FTZ mode to restore to, if FTZ was not already set. /// Whether FTZ should be disabled again, i.e. if FTZ was not enabled before.
old_ftz_mode: Option<u32>, should_disable_again: bool,
/// We can't directly implement !Send and !Sync, but this will do the same thing. This object /// We can't directly implement !Send and !Sync, but this will do the same thing. This object
/// affects the current thread's floating point registers, so it may only be dropped on the /// affects the current thread's floating point registers, so it may only be dropped on the
/// current thread. /// current thread.
@ -111,12 +111,12 @@ impl ScopedFtz {
unsafe { std::arch::x86_64::_MM_SET_FLUSH_ZERO_MODE(std::arch::x86_64::_MM_FLUSH_ZERO_ON) }; unsafe { std::arch::x86_64::_MM_SET_FLUSH_ZERO_MODE(std::arch::x86_64::_MM_FLUSH_ZERO_ON) };
Self { Self {
old_ftz_mode: Some(mode), should_disable_again: true,
send_sync_marker: PhantomData, send_sync_marker: PhantomData,
} }
} else { } else {
Self { Self {
old_ftz_mode: None, should_disable_again: false,
send_sync_marker: PhantomData, send_sync_marker: PhantomData,
} }
} }
@ -132,10 +132,10 @@ impl ScopedFtz {
impl Drop for ScopedFtz { impl Drop for ScopedFtz {
fn drop(&mut self) { fn drop(&mut self) {
if let Some(mode) = self.old_ftz_mode { if self.should_disable_again {
cfg_if::cfg_if! { cfg_if::cfg_if! {
if #[cfg(target_feature = "sse")] { if #[cfg(target_feature = "sse")] {
unsafe { std::arch::x86_64::_MM_SET_FLUSH_ZERO_MODE(mode) }; unsafe { std::arch::x86_64::_MM_SET_FLUSH_ZERO_MODE(std::arch::x86_64::_MM_FLUSH_ZERO_OFF) };
} }
}; };
} }