floating_distance/
simd.rs

1use cfg_if::cfg_if;
2
3/// Get the correct simd lane count for the specified scalar type
4/// 
5/// # Examples
6/// ```rust
7/// use floating_distance::*;
8/// use std::mem::size_of;
9/// 
10/// const LANES: usize = auto_lane_count!(f32);
11/// assert_eq!(LANES, 16);
12/// assert!(LANES / size_of::<f32>() >= 1);
13/// assert!(LANES % size_of::<f32>() == 0);
14/// ```
15/// 
16/// # Details
17/// It relies on `#[cfg(target_feature)]`.
18/// The result is evaluated in compile time.
19#[macro_export]
20macro_rules! auto_lane_count {
21  ($scalar: ty) => {{
22    cfg_if::cfg_if! {
23    if #[cfg(feature = "simd")] {
24      512 / 8 / std::mem::size_of::<$scalar>()
25    } else {
26      1
27    }
28    }
29  }}
30}
31
32cfg_if! {
33if #[cfg(feature = "simd")] {
34  pub (crate) use std::simd::{
35    LaneCount,
36    Simd,
37    SimdElement,
38    SimdFloat,
39    SupportedLaneCount,
40  };
41
42  /// [`std::simd::LaneCount`]
43  pub type AutoLaneCount<S> = LaneCount<{ auto_lane_count!(S) }>;
44  /// [`std::simd::Simd`]
45  pub type AutoSimd<S> = Simd<S, { auto_lane_count!(S) }>;
46  /// [`std::simd::SimdElement`]
47  pub trait AutoSimdElement: SimdElement {}
48  /// [`std::simd::SimdFloat`]
49  pub trait AutoSimdFloat: SimdFloat {}
50  /// [`std::simd::SupportedLaneCount`]
51  pub trait AutoSupportedLaneCount: SupportedLaneCount {}
52
53  impl<S: SimdElement> AutoSimdElement for S {}
54  impl<S: SimdFloat> AutoSimdFloat for S {}
55  impl<S: SupportedLaneCount> AutoSupportedLaneCount for S {}
56
57} else {
58  /// Stub for [`std::simd::LaneCount`]
59  pub type AutoLaneCount<S> = S;
60  /// Stub for [`std::simd::Simd`]
61  pub type AutoSimd<S> = S;
62  /// Stub for [`std::simd::SimdElement`]
63  pub trait AutoSimdElement {}
64  /// Stub for [`std::simd::SimdFloat`]
65  pub trait AutoSimdFloat {
66    /// Stub for [`std::simd::SimdFloat::Scalar`]
67    type Scalar;
68  }
69  /// Stub for [`std::simd::SupportedLaneCount`]
70  pub trait AutoSupportedLaneCount {}
71
72  impl<S> AutoSimdElement for S {}
73  impl<P> AutoSimdFloat for P { type Scalar = P; }
74  impl<S> AutoSupportedLaneCount for AutoLaneCount<S> {}
75}
76}