Skip to main content

Module fpu_check

Module fpu_check 

Source
Expand description

FPU state checking and correction for numerical stability

This module detects dangerous FPU settings (particularly Flush-to-Zero and Denormals-Are-Zero flags) that can cause incorrect SVD results when called from Intel Fortran programs compiled with -O3 without -fp-model precise.

§Background

Intel Fortran’s -O3 optimization may set the MXCSR register’s FZ and DAZ bits at program startup for performance. However, this causes problems for SVD computations that rely on proper handling of denormalized (subnormal) numbers.

§Usage

The FpuGuard RAII guard automatically saves, corrects, and restores FPU state:

let _guard = FpuGuard::new_protect_computation();
// Computation here - FZ/DAZ are disabled
// FPU state is automatically restored when _guard is dropped

§Performance

The stmxcsr/ldmxcsr instructions are very lightweight (a few CPU cycles), so the overhead of checking and restoring FPU state is negligible compared to actual matrix computations.

Structs§

FpuGuard
RAII guard that protects a computation from dangerous FPU settings
FpuState
Result of FPU state check

Functions§

get_fpu_state
Get current FPU state (x86/x86_64 only)
init_fpu_check
Initialize FPU check (call this early to trigger warning if needed)