1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#[cfg(feature = "simd_assets")]
pub use crate::indicators::simd_indicators::by_asset::wilders::indicator_by_assets;
use crate::indicators::simd_indicators::simd_types::F64Constants;
#[cfg(feature = "simd_options")]
pub use crate::indicators::simd_indicators::by_option::wilders::indicator_by_options;
use std::simd::{Simd, StdFloat};
/// Initialises the Wilder's Smoothing SIMD state from raw input slices.
///
/// Computes the simple average of the first `period` values for each lane
/// as the seed for subsequent exponential smoothing.
///
/// # Arguments
///
/// * `inputs` - Per-lane input slices; must each contain at least `period` values.
/// * `period` - Number of bars to average for the initial smoothed value.
///
/// # Returns
///
/// SIMD vector containing the initial Wilder's smoothed value for each lane.
pub fn init_state<'a, const N: usize>(inputs: &[&'a [f64]; N], period: usize) -> Simd<f64, N> {
let input_ptrs: [*const f64; N] = std::array::from_fn(|i| inputs[i].as_ptr());
let mut wilders = Simd::splat(0.0);
for i in 0..period {
let values = Simd::from_array(std::array::from_fn(|j| unsafe { *input_ptrs[j].add(i) }));
wilders += values;
}
wilders /= Simd::splat(period as f64);
wilders
}
/// Computes one bar of Wilder's Smoothing for `N` assets simultaneously
/// using SIMD parallelism.
///
/// Applies `prev_wilders * multiplier + value * (1 - multiplier)` for each lane.
///
/// # Arguments
///
/// * `prev_wilders` - Previous smoothed values for each lane.
/// * `value` - New input values for this bar.
/// * `multiplier` - Per-lane decay factor `(period - 1) / period`.
///
/// # Returns
///
/// Updated Wilder's smoothed values for all `N` lanes.
#[inline(always)]
pub fn calc_simd<const N: usize>(
prev_wilders: Simd<f64, N>,
value: Simd<f64, N>,
multiplier: Simd<f64, N>,
) -> Simd<f64, N> {
prev_wilders.mul_add(multiplier, value * (F64Constants::ONE - multiplier))
}
/// Computes a partial Wilder's Smoothing step without subtracting the decay residual.
///
/// Applies `prev_wilders * multiplier + value` for each lane, omitting the
/// `(1 - multiplier)` weight on `value`. Used internally for already-scaled inputs.
///
/// # Arguments
///
/// * `prev_wilders` - Previous smoothed values for each lane.
/// * `value` - Pre-scaled new input values for this bar.
/// * `multiplier` - Per-lane decay factor `(period - 1) / period`.
///
/// # Returns
///
/// Partially updated smoothed values for all `N` lanes.
#[inline(always)]
pub fn partial_calc_simd<const N: usize>(
prev_wilders: Simd<f64, N>,
value: Simd<f64, N>,
multiplier: Simd<f64, N>,
) -> Simd<f64, N> {
prev_wilders.mul_add(multiplier, value)
}