Skip to content

Commit 1c1e608

Browse files
committed
Improve: Flushing denormals in Rust
1 parent d17c9a9 commit 1c1e608

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

rust/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
//!
2727
//! // Compute squared Euclidean distance
2828
//! let l2sq_dist = i8::l2sq(a, b);
29+
//!
30+
//! // Optimize performance by flushing denormals
31+
//! simsimd::capabilities::flush_denormals();
2932
//! ```
3033
//!
3134
//! ## Traits
@@ -130,6 +133,9 @@ extern "C" {
130133
fn simsimd_uses_sapphire() -> i32;
131134
fn simsimd_uses_turin() -> i32;
132135
fn simsimd_uses_sierra() -> i32;
136+
137+
fn simsimd_flush_denormals() -> i32;
138+
fn simsimd_uses_dynamic_dispatch() -> i32;
133139
}
134140

135141
/// A half-precision floating point number.
@@ -207,6 +213,29 @@ pub mod capabilities {
207213
pub fn uses_sierra() -> bool {
208214
unsafe { crate::simsimd_uses_sierra() != 0 }
209215
}
216+
217+
/// Flushes denormalized numbers to zero on the current CPU architecture.
218+
///
219+
/// This function should be called on each thread before any SIMD operations
220+
/// to avoid performance penalties. When facing denormalized values,
221+
/// Fused-Multiply-Add (FMA) operations can be up to 30x slower.
222+
///
223+
/// # Returns
224+
///
225+
/// Returns `true` if the operation was successful, `false` otherwise.
226+
pub fn flush_denormals() -> bool {
227+
unsafe { crate::simsimd_flush_denormals() != 0 }
228+
}
229+
230+
/// Checks if the library is using dynamic dispatch for function selection.
231+
///
232+
/// # Returns
233+
///
234+
/// Returns `true` if dynamic dispatch is enabled, `false` otherwise.
235+
/// Currently always returns `false` as dynamic dispatch is not implemented.
236+
pub fn uses_dynamic_dispatch() -> bool {
237+
unsafe { crate::simsimd_uses_dynamic_dispatch() != 0 }
238+
}
210239
}
211240

212241
/// `SpatialSimilarity` provides a set of trait methods for computing similarity

0 commit comments

Comments
 (0)