ta_lib_dispatch/lib.rs
1//! Runtime CPU-feature dispatch for the `ta-lib` crate (issue #156).
2//!
3//! Support crate: it exists so the library crate can stay `#![forbid(unsafe_code)]`
4//! while selecting hardware-FMA clones at runtime — the single `unsafe` in the
5//! ta-lib workspace lives here, adjacent to the CPU-feature check that justifies
6//! it. This is the Rust analogue of the C library's
7//! `__attribute__((target_clones("default","fma")))` ifunc dispatch.
8//! Internal contract; no semver promises outside the ta-lib workspace.
9
10/// Dispatch one indicator call to its hardware-FMA clone when the CPU supports
11/// FMA, else to the portable implementation.
12///
13/// Both paths compute IEEE-754 correctly-rounded fused multiply-adds (`vfmadd`
14/// vs libm `fma()`), so which one runs can change speed, never bits.
15///
16/// # Contract (enforced by ta_codegen, not checkable by a macro)
17///
18/// `$fma` must name a method whose only `#[target_feature]` requirement is
19/// `fma`; the generator emits the clone and this dispatch call as a pair.
20#[macro_export]
21macro_rules! dispatch_fma {
22 ($core:expr, $fma:ident, $plain:ident, ( $($arg:expr),* $(,)? )) => {
23 if std::arch::is_x86_feature_detected!("fma") {
24 // SAFETY: $fma's only target_feature requirement is "fma", proven
25 // present on this CPU by the guard above.
26 unsafe { $core.$fma($($arg),*) }
27 } else {
28 $core.$plain($($arg),*)
29 }
30 };
31}