fast-ta 0.2.1

High-performance technical analysis indicators with batch, prepared, and streaming APIs
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//! WebAssembly SIMD128 kernels.
//!
//! These functions may only be called when the final module is compiled with
//! `target_feature=+simd128`. Dispatch keeps them unreachable in scalar WASM
//! builds and preserves the portable path for `no_std`.

use crate::simd::scalar;
use crate::types::Float;
use crate::Result;

/// SIMD128 SIMD array sum
#[cfg(not(feature = "f32"))]
#[inline(never)]
#[target_feature(enable = "simd128")]
pub unsafe fn sum(data: &[Float]) -> Float {
    use core::arch::wasm32::{f64x2_add, f64x2_extract_lane, f64x2_splat, v128, v128_load};

    const LANES: usize = 2;
    let chunks = data.chunks_exact(LANES);
    let remainder = chunks.remainder();
    let mut sum_vec = f64x2_splat(0.0);

    for chunk in chunks {
        sum_vec = f64x2_add(sum_vec, v128_load(chunk.as_ptr().cast::<v128>()));
    }

    let mut sum = f64x2_extract_lane::<0>(sum_vec) + f64x2_extract_lane::<1>(sum_vec);
    for &value in remainder {
        sum += value;
    }
    sum
}

/// SIMD128 SIMD array sum
#[cfg(feature = "f32")]
#[inline(never)]
#[target_feature(enable = "simd128")]
pub unsafe fn sum(data: &[Float]) -> Float {
    use core::arch::wasm32::{f32x4_add, f32x4_extract_lane, f32x4_splat, v128, v128_load};

    const LANES: usize = 4;
    let chunks = data.chunks_exact(LANES);
    let remainder = chunks.remainder();
    let mut sum_vec = f32x4_splat(0.0);

    for chunk in chunks {
        sum_vec = f32x4_add(sum_vec, v128_load(chunk.as_ptr().cast::<v128>()));
    }

    let mut sum = f32x4_extract_lane::<0>(sum_vec) + f32x4_extract_lane::<1>(sum_vec);
    sum += f32x4_extract_lane::<2>(sum_vec) + f32x4_extract_lane::<3>(sum_vec);
    for &value in remainder {
        sum += value;
    }
    sum
}

/// SIMD128 SIMD dot product calculation
#[cfg(not(feature = "f32"))]
#[inline(never)]
#[target_feature(enable = "simd128")]
pub unsafe fn dot_product(a: &[Float], b: &[Float]) -> Result<Float> {
    use core::arch::wasm32::{
        f64x2_add, f64x2_extract_lane, f64x2_mul, f64x2_splat, v128, v128_load,
    };

    if a.len() != b.len() {
        return Err(crate::TalibError::InvalidInput {
            message: "Dot product requires vectors of equal length".into(),
        });
    }

    const LANES: usize = 2;
    let chunks = a.chunks_exact(LANES).zip(b.chunks_exact(LANES));
    let remainder_a = a.chunks_exact(LANES).remainder();
    let remainder_b = b.chunks_exact(LANES).remainder();
    let mut sum_vec = f64x2_splat(0.0);

    for (chunk_a, chunk_b) in chunks {
        let a_values = v128_load(chunk_a.as_ptr().cast::<v128>());
        let b_values = v128_load(chunk_b.as_ptr().cast::<v128>());
        sum_vec = f64x2_add(sum_vec, f64x2_mul(a_values, b_values));
    }

    let mut sum = f64x2_extract_lane::<0>(sum_vec) + f64x2_extract_lane::<1>(sum_vec);
    for (&x, &y) in remainder_a.iter().zip(remainder_b) {
        sum += x * y;
    }
    Ok(sum)
}

/// SIMD128 SIMD dot product calculation
#[cfg(feature = "f32")]
#[inline(never)]
#[target_feature(enable = "simd128")]
pub unsafe fn dot_product(a: &[Float], b: &[Float]) -> Result<Float> {
    use core::arch::wasm32::{
        f32x4_add, f32x4_extract_lane, f32x4_mul, f32x4_splat, v128, v128_load,
    };

    if a.len() != b.len() {
        return Err(crate::TalibError::InvalidInput {
            message: "Dot product requires vectors of equal length".into(),
        });
    }

    const LANES: usize = 4;
    let chunks = a.chunks_exact(LANES).zip(b.chunks_exact(LANES));
    let remainder_a = a.chunks_exact(LANES).remainder();
    let remainder_b = b.chunks_exact(LANES).remainder();
    let mut sum_vec = f32x4_splat(0.0);

    for (chunk_a, chunk_b) in chunks {
        let a_values = v128_load(chunk_a.as_ptr().cast::<v128>());
        let b_values = v128_load(chunk_b.as_ptr().cast::<v128>());
        sum_vec = f32x4_add(sum_vec, f32x4_mul(a_values, b_values));
    }

    let mut sum = f32x4_extract_lane::<0>(sum_vec) + f32x4_extract_lane::<1>(sum_vec);
    sum += f32x4_extract_lane::<2>(sum_vec) + f32x4_extract_lane::<3>(sum_vec);
    for (&x, &y) in remainder_a.iter().zip(remainder_b) {
        sum += x * y;
    }
    Ok(sum)
}

/// Returns the first non-finite lane using SIMD128, with exact scalar recovery.
#[cfg(not(feature = "f32"))]
#[inline(never)]
#[target_feature(enable = "simd128")]
pub unsafe fn first_non_finite(values: &[Float]) -> Option<usize> {
    use core::arch::wasm32::{
        i64x2_eq, i64x2_splat, i8x16_bitmask, v128, v128_and, v128_load, v128_or,
    };

    const LANES: usize = 2;
    const UNROLLED_LANES: usize = LANES * 4;
    const EXPONENT_MASK: i64 = 0x7ff0_0000_0000_0000;

    #[inline]
    #[target_feature(enable = "simd128")]
    unsafe fn invalid_lanes(values: *const f64, exponent_mask: v128) -> v128 {
        let bits = v128_load(values.cast::<v128>());
        i64x2_eq(v128_and(bits, exponent_mask), exponent_mask)
    }

    let exponent_mask = i64x2_splat(EXPONENT_MASK);
    let mut index = 0;
    while index + UNROLLED_LANES <= values.len() {
        let ptr = values.as_ptr().add(index);
        let invalid = v128_or(
            v128_or(
                invalid_lanes(ptr, exponent_mask),
                invalid_lanes(ptr.add(LANES), exponent_mask),
            ),
            v128_or(
                invalid_lanes(ptr.add(LANES * 2), exponent_mask),
                invalid_lanes(ptr.add(LANES * 3), exponent_mask),
            ),
        );
        if i8x16_bitmask(invalid) != 0 {
            return scalar::first_non_finite(&values[index..index + UNROLLED_LANES])
                .map(|offset| index + offset);
        }
        index += UNROLLED_LANES;
    }

    while index + LANES <= values.len() {
        if i8x16_bitmask(invalid_lanes(values.as_ptr().add(index), exponent_mask)) != 0 {
            return scalar::first_non_finite(&values[index..index + LANES])
                .map(|offset| index + offset);
        }
        index += LANES;
    }

    scalar::first_non_finite(&values[index..]).map(|offset| index + offset)
}

/// Returns the first non-finite lane using SIMD128, with exact scalar recovery.
#[cfg(feature = "f32")]
#[inline(never)]
#[target_feature(enable = "simd128")]
pub unsafe fn first_non_finite(values: &[Float]) -> Option<usize> {
    use core::arch::wasm32::{
        i32x4_eq, i32x4_splat, i8x16_bitmask, v128, v128_and, v128_load, v128_or,
    };

    const LANES: usize = 4;
    const UNROLLED_LANES: usize = LANES * 4;
    const EXPONENT_MASK: i32 = 0x7f80_0000;

    #[inline]
    #[target_feature(enable = "simd128")]
    unsafe fn invalid_lanes(values: *const f32, exponent_mask: v128) -> v128 {
        let bits = v128_load(values.cast::<v128>());
        i32x4_eq(v128_and(bits, exponent_mask), exponent_mask)
    }

    let exponent_mask = i32x4_splat(EXPONENT_MASK);
    let mut index = 0;
    while index + UNROLLED_LANES <= values.len() {
        let ptr = values.as_ptr().add(index);
        let invalid = v128_or(
            v128_or(
                invalid_lanes(ptr, exponent_mask),
                invalid_lanes(ptr.add(LANES), exponent_mask),
            ),
            v128_or(
                invalid_lanes(ptr.add(LANES * 2), exponent_mask),
                invalid_lanes(ptr.add(LANES * 3), exponent_mask),
            ),
        );
        if i8x16_bitmask(invalid) != 0 {
            return scalar::first_non_finite(&values[index..index + UNROLLED_LANES])
                .map(|offset| index + offset);
        }
        index += UNROLLED_LANES;
    }

    while index + LANES <= values.len() {
        if i8x16_bitmask(invalid_lanes(values.as_ptr().add(index), exponent_mask)) != 0 {
            return scalar::first_non_finite(&values[index..index + LANES])
                .map(|offset| index + offset);
        }
        index += LANES;
    }

    scalar::first_non_finite(&values[index..]).map(|offset| index + offset)
}

/// Computes Typical Price using two `f64` lanes and an exact scalar tail.
#[cfg(not(feature = "f32"))]
#[inline(never)]
#[target_feature(enable = "simd128")]
pub unsafe fn typical_price(high: &[Float], low: &[Float], close: &[Float], output: &mut [Float]) {
    use core::arch::wasm32::{f64x2_add, f64x2_div, f64x2_splat, v128, v128_load, v128_store};

    const LANES: usize = 2;
    let denominator = f64x2_splat(3.0);
    let mut index = 0;
    while index + LANES <= high.len() {
        let high_values = v128_load(high.as_ptr().add(index).cast::<v128>());
        let low_values = v128_load(low.as_ptr().add(index).cast::<v128>());
        let close_values = v128_load(close.as_ptr().add(index).cast::<v128>());
        let values = f64x2_div(
            f64x2_add(f64x2_add(high_values, low_values), close_values),
            denominator,
        );
        v128_store(output.as_mut_ptr().add(index).cast::<v128>(), values);
        index += LANES;
    }

    scalar::typical_price(
        &high[index..],
        &low[index..],
        &close[index..],
        &mut output[index..],
    );
}

/// Computes Typical Price using four `f32` lanes and an exact scalar tail.
#[cfg(feature = "f32")]
#[inline(never)]
#[target_feature(enable = "simd128")]
pub unsafe fn typical_price(high: &[Float], low: &[Float], close: &[Float], output: &mut [Float]) {
    use core::arch::wasm32::{f32x4_add, f32x4_div, f32x4_splat, v128, v128_load, v128_store};

    const LANES: usize = 4;
    let denominator = f32x4_splat(3.0);
    let mut index = 0;
    while index + LANES <= high.len() {
        let high_values = v128_load(high.as_ptr().add(index).cast::<v128>());
        let low_values = v128_load(low.as_ptr().add(index).cast::<v128>());
        let close_values = v128_load(close.as_ptr().add(index).cast::<v128>());
        let values = f32x4_div(
            f32x4_add(f32x4_add(high_values, low_values), close_values),
            denominator,
        );
        v128_store(output.as_mut_ptr().add(index).cast::<v128>(), values);
        index += LANES;
    }

    scalar::typical_price(
        &high[index..],
        &low[index..],
        &close[index..],
        &mut output[index..],
    );
}

#[cfg(test)]
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
mod tests {
    use super::*;

    #[test]
    fn test_sum() {
        let data: Vec<Float> = (0..1000).map(|i| i as Float).collect();
        unsafe {
            let result = sum(&data);
            let expected: Float = data.iter().sum();
            assert!((result - expected).abs() < 1e-10);
        }
    }

    #[test]
    fn test_sum_empty() {
        let data: Vec<Float> = vec![];
        unsafe {
            let result = sum(&data);
            assert_eq!(result, 0.0);
        }
    }

    #[test]
    fn test_sum_single() {
        let data: Vec<Float> = vec![42.0];
        unsafe {
            let result = sum(&data);
            assert_eq!(result, 42.0);
        }
    }

    #[test]
    fn test_dot_product() {
        let a: Vec<Float> = (0..1000).map(|i| i as Float).collect();
        let b: Vec<Float> = (0..1000).map(|i| (i * 2) as Float).collect();
        unsafe {
            let result = dot_product(&a, &b).unwrap();
            let expected: Float = a.iter().zip(b.iter()).map(|(&x, &y)| x * y).sum();
            assert!((result - expected).abs() < 1e-10);
        }
    }

    #[test]
    fn test_dot_product_mismatched_lengths() {
        let a: Vec<Float> = vec![1.0, 2.0, 3.0];
        let b: Vec<Float> = vec![1.0, 2.0];
        unsafe {
            assert!(dot_product(&a, &b).is_err());
        }
    }

    #[test]
    fn first_non_finite_matches_scalar_for_lanes_tails_and_exact_recovery() {
        let lane_width = if cfg!(feature = "f32") { 4 } else { 2 };
        let lengths = [
            0,
            1,
            lane_width - 1,
            lane_width,
            lane_width + 1,
            lane_width * 4 - 1,
            lane_width * 4,
            lane_width * 4 + 1,
            65_537,
        ];

        for len in lengths {
            let values = vec![1.0 as Float; len];
            assert_eq!(
                unsafe { first_non_finite(&values) },
                scalar::first_non_finite(&values),
                "all-finite length {len}"
            );
        }

        let len = lane_width * 8 + 3;
        for (index, value) in [
            (0, Float::NAN),
            (lane_width - 1, Float::INFINITY),
            (lane_width, Float::NEG_INFINITY),
            (lane_width * 4 - 1, Float::NAN),
            (lane_width * 4, Float::INFINITY),
            (len - 1, Float::NEG_INFINITY),
        ] {
            let mut values = vec![1.0 as Float; len];
            values[index] = value;
            assert_eq!(unsafe { first_non_finite(&values) }, Some(index));
            assert_eq!(
                unsafe { first_non_finite(&values) },
                scalar::first_non_finite(&values)
            );
        }

        let mut values = vec![1.0 as Float; len];
        values[lane_width + 1] = Float::NAN;
        values[0] = Float::INFINITY;
        assert_eq!(unsafe { first_non_finite(&values) }, Some(0));
    }

    #[test]
    fn typical_price_matches_scalar_for_lanes_tails_and_large_input() {
        let lane_width = if cfg!(feature = "f32") { 4 } else { 2 };
        for len in [
            0,
            1,
            lane_width - 1,
            lane_width,
            lane_width + 1,
            lane_width * 2 + 1,
            65_537,
        ] {
            let high: Vec<Float> = (0..len)
                .map(|index| index as Float * 0.5 as Float + 3.25 as Float)
                .collect();
            let low: Vec<Float> = (0..len)
                .map(|index| index as Float * 0.25 as Float - 1.5 as Float)
                .collect();
            let close: Vec<Float> = (0..len)
                .map(|index| index as Float * 0.125 as Float + 0.75 as Float)
                .collect();
            let mut expected = vec![-12_345.0 as Float; len + 1];
            let mut actual = vec![-12_345.0 as Float; len + 1];

            scalar::typical_price(&high, &low, &close, &mut expected);
            unsafe { typical_price(&high, &low, &close, &mut actual) };

            assert_eq!(actual, expected, "length {len}");
        }
    }
}