sklears-simd 0.1.1

High-performance SIMD acceleration primitives for the Sklears machine learning ecosystem
Documentation
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
//! # Basic SIMD Vector Operations
//!
//! Fundamental vector operations optimized with SIMD instructions including
//! dot products, norms, distance calculations, and basic linear algebra operations.
//!
//! ## Features
//!
//! - **Dot Product**: SIMD-optimized dot product with platform-specific implementations
//! - **Vector Norms**: L1, L2, and infinity norms with high performance
//! - **Distance Metrics**: Euclidean, Manhattan, and cosine distance/similarity
//! - **Advanced Operations**: Cross product and outer product for linear algebra
//! - **Multi-Platform**: SSE2, AVX2, AVX512, and NEON optimizations
//! - **Scalar Fallbacks**: Automatic fallback to scalar operations when needed
//!
//! ## Implementation Details
//!
//! All functions automatically detect the best available SIMD instruction set
//! and provide graceful fallback to scalar implementations. The functions are
//! designed to handle arbitrary vector lengths efficiently by processing
//! SIMD-sized chunks and handling remainders appropriately.

#[cfg(feature = "no-std")]
use alloc::vec;
#[cfg(feature = "no-std")]
use alloc::vec::Vec;
#[cfg(not(feature = "no-std"))]
use std::vec::Vec;

// Import ARM64 feature detection macro
#[cfg(all(target_arch = "aarch64", not(feature = "no-std")))]
use std::arch::is_aarch64_feature_detected;

/// SIMD-optimized dot product computation
///
/// Computes the dot product of two vectors using the best available SIMD
/// instruction set. Automatically falls back to scalar implementation if
/// no SIMD support is available.
///
/// # Arguments
/// * `a` - First input vector
/// * `b` - Second input vector (must have same length as `a`)
///
/// # Returns
/// The dot product as a single f32 value
///
/// # Panics
/// Panics if the vectors have different lengths
///
/// # Examples
/// ```rust
/// use sklears_simd::vector::basic_operations::dot_product;
///
/// let a = vec![1.0, 2.0, 3.0, 4.0];
/// let b = vec![5.0, 6.0, 7.0, 8.0];
/// let result = dot_product(&a, &b);
/// assert_eq!(result, 70.0); // 1*5 + 2*6 + 3*7 + 4*8 = 70
/// ```
pub fn dot_product(a: &[f32], b: &[f32]) -> f32 {
    assert_eq!(a.len(), b.len(), "Vectors must have the same length");

    if a.is_empty() {
        return 0.0;
    }

    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    {
        if crate::simd_feature_detected!("avx512f") {
            return unsafe { dot_product_avx512(a, b) };
        } else if crate::simd_feature_detected!("avx2") {
            return unsafe { dot_product_avx2(a, b) };
        } else if crate::simd_feature_detected!("sse2") {
            return unsafe { dot_product_sse2(a, b) };
        }
    }

    #[cfg(all(target_arch = "aarch64", not(feature = "no-std")))]
    {
        if is_aarch64_feature_detected!("neon") {
            return unsafe { dot_product_neon(a, b) };
        }
    }

    dot_product_scalar(a, b)
}

/// SIMD-optimized L2 norm (Euclidean norm) computation
///
/// Computes the L2 norm (||x||₂) of a vector using SIMD-optimized dot product.
///
/// # Arguments
/// * `x` - Input vector
///
/// # Returns
/// The L2 norm as a single f32 value
///
/// # Examples
/// ```rust
/// use sklears_simd::vector::basic_operations::norm_l2;
///
/// let x = vec![3.0, 4.0];
/// let result = norm_l2(&x);
/// assert_eq!(result, 5.0); // sqrt(3² + 4²) = 5
/// ```
pub fn norm_l2(x: &[f32]) -> f32 {
    dot_product(x, x).sqrt()
}

/// SIMD-optimized L1 norm (Manhattan norm) computation
///
/// Computes the L1 norm (||x||₁) of a vector using SIMD instructions
/// for absolute value computation and summation.
///
/// # Arguments
/// * `x` - Input vector
///
/// # Returns
/// The L1 norm as a single f32 value
///
/// # Examples
/// ```rust
/// use sklears_simd::vector::basic_operations::norm_l1;
///
/// let x = vec![-3.0, 4.0, -5.0];
/// let result = norm_l1(&x);
/// assert_eq!(result, 12.0); // |−3| + |4| + |−5| = 12
/// ```
pub fn norm_l1(x: &[f32]) -> f32 {
    if x.is_empty() {
        return 0.0;
    }

    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    {
        if crate::simd_feature_detected!("avx2") {
            return unsafe { norm_l1_avx2(x) };
        } else if crate::simd_feature_detected!("sse2") {
            return unsafe { norm_l1_sse2(x) };
        }
    }

    #[cfg(all(target_arch = "aarch64", not(feature = "no-std")))]
    {
        if is_aarch64_feature_detected!("neon") {
            return unsafe { norm_l1_neon(x) };
        }
    }

    norm_l1_scalar(x)
}

/// SIMD-optimized infinity norm computation
///
/// Computes the L∞ norm (||x||∞) of a vector, which is the maximum
/// absolute value of all elements.
///
/// # Arguments
/// * `x` - Input vector
///
/// # Returns
/// The infinity norm as a single f32 value
///
/// # Examples
/// ```rust
/// use sklears_simd::vector::basic_operations::norm_inf;
///
/// let x = vec![-3.0, 4.0, -5.0, 2.0];
/// let result = norm_inf(&x);
/// assert_eq!(result, 5.0); // max(|−3|, |4|, |−5|, |2|) = 5
/// ```
pub fn norm_inf(x: &[f32]) -> f32 {
    if x.is_empty() {
        return 0.0;
    }

    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    {
        if crate::simd_feature_detected!("avx2") {
            return unsafe { norm_inf_avx2(x) };
        } else if crate::simd_feature_detected!("sse2") {
            return unsafe { norm_inf_sse2(x) };
        }
    }

    norm_inf_scalar(x)
}

/// SIMD-optimized Euclidean distance computation
///
/// Computes the Euclidean distance between two vectors:
/// ||a - b||₂ = sqrt(Σ(aᵢ - bᵢ)²)
///
/// # Arguments
/// * `a` - First vector
/// * `b` - Second vector (must have same length as `a`)
///
/// # Returns
/// The Euclidean distance as a single f32 value
///
/// # Panics
/// Panics if the vectors have different lengths
///
/// # Examples
/// ```rust
/// use sklears_simd::vector::basic_operations::euclidean_distance;
///
/// let a = vec![1.0, 2.0, 3.0];
/// let b = vec![4.0, 5.0, 6.0];
/// let result = euclidean_distance(&a, &b);
/// // sqrt((1-4)² + (2-5)² + (3-6)²) = sqrt(9 + 9 + 9) = sqrt(27) ≈ 5.196
/// assert!((result - 5.196).abs() < 0.01);
/// ```
pub fn euclidean_distance(a: &[f32], b: &[f32]) -> f32 {
    assert_eq!(a.len(), b.len(), "Vectors must have the same length");

    if a.is_empty() {
        return 0.0;
    }

    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    {
        if crate::simd_feature_detected!("avx2") {
            return unsafe { euclidean_distance_avx2(a, b) };
        } else if crate::simd_feature_detected!("sse2") {
            return unsafe { euclidean_distance_sse2(a, b) };
        }
    }

    euclidean_distance_scalar(a, b)
}

/// SIMD-optimized cosine similarity computation
///
/// Computes the cosine similarity between two vectors:
/// cos_sim(a, b) = (a · b) / (||a||₂ * ||b||₂)
///
/// # Arguments
/// * `a` - First vector
/// * `b` - Second vector (must have same length as `a`)
///
/// # Returns
/// The cosine similarity as a single f32 value between -1.0 and 1.0
///
/// # Panics
/// Panics if the vectors have different lengths
///
/// # Examples
/// ```rust
/// use sklears_simd::vector::basic_operations::cosine_similarity;
///
/// let a = vec![1.0, 0.0, 0.0];
/// let b = vec![0.0, 1.0, 0.0];
/// let result = cosine_similarity(&a, &b);
/// assert_eq!(result, 0.0); // Orthogonal vectors have cosine similarity of 0
/// ```
pub fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 {
    assert_eq!(a.len(), b.len(), "Vectors must have the same length");

    if a.is_empty() {
        return 1.0; // Convention: empty vectors are perfectly similar
    }

    let dot_ab = dot_product(a, b);
    let norm_a = norm_l2(a);
    let norm_b = norm_l2(b);

    if norm_a == 0.0 || norm_b == 0.0 {
        return 0.0; // Zero vectors are orthogonal to everything
    }

    dot_ab / (norm_a * norm_b)
}

/// SIMD-optimized cross product for 3D vectors
///
/// Computes the cross product a × b for two 3-dimensional vectors.
///
/// # Arguments
/// * `a` - First 3D vector
/// * `b` - Second 3D vector
///
/// # Returns
/// The cross product as a 3-element vector, or an error if input vectors
/// are not exactly 3 elements long
///
/// # Examples
/// ```rust
/// use sklears_simd::vector::basic_operations::cross_product;
///
/// let a = vec![1.0, 0.0, 0.0];  // Unit vector along x-axis
/// let b = vec![0.0, 1.0, 0.0];  // Unit vector along y-axis
/// let result = cross_product(&a, &b).unwrap();
/// assert_eq!(result, vec![0.0, 0.0, 1.0]);  // Unit vector along z-axis
/// ```
pub fn cross_product(a: &[f32], b: &[f32]) -> Result<Vec<f32>, &'static str> {
    if a.len() != 3 || b.len() != 3 {
        return Err("Cross product requires exactly 3-dimensional vectors");
    }

    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    {
        if crate::simd_feature_detected!("sse2") {
            return Ok(unsafe { cross_product_sse2(a, b) });
        }
    }

    Ok(cross_product_scalar(a, b))
}

/// SIMD-optimized outer product computation
///
/// Computes the outer product of two vectors, resulting in a matrix
/// where element (i,j) = a\[i\] * b\[j\].
///
/// # Arguments
/// * `a` - First vector (m elements)
/// * `b` - Second vector (n elements)
///
/// # Returns
/// An m×n matrix represented as `Vec<Vec<f32>>`
///
/// # Examples
/// ```rust
/// use sklears_simd::vector::basic_operations::outer_product;
///
/// let a = vec![1.0, 2.0];
/// let b = vec![3.0, 4.0, 5.0];
/// let result = outer_product(&a, &b);
/// // Expected: [[3.0, 4.0, 5.0], [6.0, 8.0, 10.0]]
/// assert_eq!(result[0], vec![3.0, 4.0, 5.0]);
/// assert_eq!(result[1], vec![6.0, 8.0, 10.0]);
/// ```
pub fn outer_product(a: &[f32], b: &[f32]) -> Vec<Vec<f32>> {
    let m = a.len();
    let n = b.len();

    if m == 0 || n == 0 {
        return vec![];
    }

    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    {
        if crate::simd_feature_detected!("avx2") {
            return unsafe { outer_product_avx2(a, b) };
        } else if crate::simd_feature_detected!("sse2") {
            return unsafe { outer_product_sse2(a, b) };
        }
    }

    outer_product_scalar(a, b)
}

// ============================================================================
// Scalar implementations (fallbacks)
// ============================================================================

fn dot_product_scalar(a: &[f32], b: &[f32]) -> f32 {
    a.iter().zip(b.iter()).map(|(x, y)| x * y).sum()
}

fn norm_l1_scalar(x: &[f32]) -> f32 {
    x.iter().map(|&v| v.abs()).sum()
}

fn norm_inf_scalar(x: &[f32]) -> f32 {
    x.iter().map(|&v| v.abs()).fold(0.0f32, |a, b| a.max(b))
}

fn euclidean_distance_scalar(a: &[f32], b: &[f32]) -> f32 {
    a.iter()
        .zip(b.iter())
        .map(|(x, y)| {
            let diff = x - y;
            diff * diff
        })
        .sum::<f32>()
        .sqrt()
}

fn cross_product_scalar(a: &[f32], b: &[f32]) -> Vec<f32> {
    vec![
        a[1] * b[2] - a[2] * b[1], // i component
        a[2] * b[0] - a[0] * b[2], // j component
        a[0] * b[1] - a[1] * b[0], // k component
    ]
}

fn outer_product_scalar(a: &[f32], b: &[f32]) -> Vec<Vec<f32>> {
    let m = a.len();
    let n = b.len();
    let mut result = vec![vec![0.0; n]; m];

    for i in 0..m {
        for (j, &b_val) in b.iter().enumerate().take(n) {
            result[i][j] = a[i] * b_val;
        }
    }

    result
}

// ============================================================================
// SSE2 implementations (x86/x86_64)
// ============================================================================

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "sse2")]
unsafe fn dot_product_sse2(a: &[f32], b: &[f32]) -> f32 {
    #[cfg(feature = "no-std")]
    use core::arch::x86_64::*;
    #[cfg(not(feature = "no-std"))]
    use core::arch::x86_64::*;

    let mut sum = _mm_setzero_ps();
    let mut i = 0;

    // Process 4 elements at a time
    while i + 4 <= a.len() {
        let a_vec = _mm_loadu_ps(a.as_ptr().add(i));
        let b_vec = _mm_loadu_ps(b.as_ptr().add(i));
        let prod = _mm_mul_ps(a_vec, b_vec);
        sum = _mm_add_ps(sum, prod);
        i += 4;
    }

    // Horizontal sum of the 4 elements in sum
    let mut result = [0.0f32; 4];
    _mm_storeu_ps(result.as_mut_ptr(), sum);
    let mut scalar_sum = result[0] + result[1] + result[2] + result[3];

    // Handle remaining elements
    while i < a.len() {
        scalar_sum += a[i] * b[i];
        i += 1;
    }

    scalar_sum
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "sse2")]
unsafe fn norm_l1_sse2(x: &[f32]) -> f32 {
    #[cfg(feature = "no-std")]
    use core::arch::x86_64::*;
    #[cfg(not(feature = "no-std"))]
    use core::arch::x86_64::*;

    // Create mask for absolute value (clear sign bit)
    let abs_mask = _mm_set1_ps(f32::from_bits(0x7FFFFFFF));
    let mut sum = _mm_setzero_ps();
    let mut i = 0;

    // Process 4 elements at a time
    while i + 4 <= x.len() {
        let x_vec = _mm_loadu_ps(x.as_ptr().add(i));
        let abs_vec = _mm_and_ps(x_vec, abs_mask);
        sum = _mm_add_ps(sum, abs_vec);
        i += 4;
    }

    // Horizontal sum
    let mut result = [0.0f32; 4];
    _mm_storeu_ps(result.as_mut_ptr(), sum);
    let mut scalar_sum = result[0] + result[1] + result[2] + result[3];

    // Handle remaining elements
    while i < x.len() {
        scalar_sum += x[i].abs();
        i += 1;
    }

    scalar_sum
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "sse2")]
unsafe fn norm_inf_sse2(x: &[f32]) -> f32 {
    #[cfg(feature = "no-std")]
    use core::arch::x86_64::*;
    #[cfg(not(feature = "no-std"))]
    use core::arch::x86_64::*;

    let abs_mask = _mm_set1_ps(f32::from_bits(0x7FFFFFFF));
    let mut max_vec = _mm_setzero_ps();
    let mut i = 0;

    while i + 4 <= x.len() {
        let x_vec = _mm_loadu_ps(x.as_ptr().add(i));
        let abs_vec = _mm_and_ps(x_vec, abs_mask);
        max_vec = _mm_max_ps(max_vec, abs_vec);
        i += 4;
    }

    // Find maximum of the 4 elements
    let mut result = [0.0f32; 4];
    _mm_storeu_ps(result.as_mut_ptr(), max_vec);
    let mut max_val = result[0].max(result[1]).max(result[2]).max(result[3]);

    // Handle remaining elements
    while i < x.len() {
        max_val = max_val.max(x[i].abs());
        i += 1;
    }

    max_val
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "sse2")]
unsafe fn euclidean_distance_sse2(a: &[f32], b: &[f32]) -> f32 {
    #[cfg(feature = "no-std")]
    use core::arch::x86_64::*;
    #[cfg(not(feature = "no-std"))]
    use core::arch::x86_64::*;

    let mut sum = _mm_setzero_ps();
    let mut i = 0;

    while i + 4 <= a.len() {
        let a_vec = _mm_loadu_ps(a.as_ptr().add(i));
        let b_vec = _mm_loadu_ps(b.as_ptr().add(i));
        let diff = _mm_sub_ps(a_vec, b_vec);
        let squared = _mm_mul_ps(diff, diff);
        sum = _mm_add_ps(sum, squared);
        i += 4;
    }

    // Horizontal sum
    let mut result = [0.0f32; 4];
    _mm_storeu_ps(result.as_mut_ptr(), sum);
    let mut scalar_sum = result[0] + result[1] + result[2] + result[3];

    // Handle remaining elements
    while i < a.len() {
        let diff = a[i] - b[i];
        scalar_sum += diff * diff;
        i += 1;
    }

    scalar_sum.sqrt()
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "sse2")]
unsafe fn cross_product_sse2(a: &[f32], b: &[f32]) -> Vec<f32> {
    #[cfg(feature = "no-std")]
    use core::arch::x86_64::*;
    #[cfg(not(feature = "no-std"))]
    use core::arch::x86_64::*;

    // Load vectors: a = [a0, a1, a2, 0], b = [b0, b1, b2, 0]
    let a_vec = _mm_set_ps(0.0, a[2], a[1], a[0]);
    let b_vec = _mm_set_ps(0.0, b[2], b[1], b[0]);

    // Create shuffled versions for cross product computation
    // a_yzx = [a1, a2, a0, 0]
    let a_yzx = _mm_shuffle_ps(a_vec, a_vec, 0b00_01_10_01);
    // b_zxy = [b2, b0, b1, 0]
    let b_zxy = _mm_shuffle_ps(b_vec, b_vec, 0b00_10_00_10);

    // a_zxy = [a2, a0, a1, 0]
    let a_zxy = _mm_shuffle_ps(a_vec, a_vec, 0b00_10_00_10);
    // b_yzx = [b1, b2, b0, 0]
    let b_yzx = _mm_shuffle_ps(b_vec, b_vec, 0b00_01_10_01);

    // Compute cross product: a_yzx * b_zxy - a_zxy * b_yzx
    let prod1 = _mm_mul_ps(a_yzx, b_zxy);
    let prod2 = _mm_mul_ps(a_zxy, b_yzx);
    let result_vec = _mm_sub_ps(prod1, prod2);

    // Extract result
    let mut output = [0.0f32; 4];
    _mm_storeu_ps(output.as_mut_ptr(), result_vec);

    vec![output[0], output[1], output[2]]
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "sse2")]
unsafe fn outer_product_sse2(a: &[f32], b: &[f32]) -> Vec<Vec<f32>> {
    #[cfg(feature = "no-std")]
    use core::arch::x86_64::*;
    #[cfg(not(feature = "no-std"))]
    use core::arch::x86_64::*;

    let m = a.len();
    let n = b.len();
    let mut result = vec![vec![0.0; n]; m];

    for i in 0..m {
        let a_broadcast = _mm_set1_ps(a[i]);
        let mut j = 0;

        while j + 4 <= n {
            let b_vec = _mm_loadu_ps(b.as_ptr().add(j));
            let prod = _mm_mul_ps(a_broadcast, b_vec);
            _mm_storeu_ps(result[i].as_mut_ptr().add(j), prod);
            j += 4;
        }

        // Handle remaining elements
        while j < n {
            result[i][j] = a[i] * b[j];
            j += 1;
        }
    }

    result
}

// ============================================================================
// AVX2 implementations (x86/x86_64)
// ============================================================================

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "avx2")]
unsafe fn dot_product_avx2(a: &[f32], b: &[f32]) -> f32 {
    #[cfg(feature = "no-std")]
    use core::arch::x86_64::*;
    #[cfg(not(feature = "no-std"))]
    use core::arch::x86_64::*;

    let mut sum = _mm256_setzero_ps();
    let mut i = 0;

    // Process 8 elements at a time
    while i + 8 <= a.len() {
        let a_vec = _mm256_loadu_ps(a.as_ptr().add(i));
        let b_vec = _mm256_loadu_ps(b.as_ptr().add(i));
        let prod = _mm256_mul_ps(a_vec, b_vec);
        sum = _mm256_add_ps(sum, prod);
        i += 8;
    }

    // Horizontal sum of the 8 elements in sum
    let mut result = [0.0f32; 8];
    _mm256_storeu_ps(result.as_mut_ptr(), sum);
    let mut scalar_sum = result.iter().sum::<f32>();

    // Handle remaining elements
    while i < a.len() {
        scalar_sum += a[i] * b[i];
        i += 1;
    }

    scalar_sum
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "avx2")]
unsafe fn norm_l1_avx2(x: &[f32]) -> f32 {
    #[cfg(feature = "no-std")]
    use core::arch::x86_64::*;
    #[cfg(not(feature = "no-std"))]
    use core::arch::x86_64::*;

    let abs_mask = _mm256_set1_ps(f32::from_bits(0x7FFFFFFF));
    let mut sum = _mm256_setzero_ps();
    let mut i = 0;

    while i + 8 <= x.len() {
        let x_vec = _mm256_loadu_ps(x.as_ptr().add(i));
        let abs_vec = _mm256_and_ps(x_vec, abs_mask);
        sum = _mm256_add_ps(sum, abs_vec);
        i += 8;
    }

    // Horizontal sum
    let mut result = [0.0f32; 8];
    _mm256_storeu_ps(result.as_mut_ptr(), sum);
    let mut scalar_sum = result.iter().sum::<f32>();

    // Handle remaining elements
    while i < x.len() {
        scalar_sum += x[i].abs();
        i += 1;
    }

    scalar_sum
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "avx2")]
unsafe fn norm_inf_avx2(x: &[f32]) -> f32 {
    #[cfg(feature = "no-std")]
    use core::arch::x86_64::*;
    #[cfg(not(feature = "no-std"))]
    use core::arch::x86_64::*;

    let abs_mask = _mm256_set1_ps(f32::from_bits(0x7FFFFFFF));
    let mut max_vec = _mm256_setzero_ps();
    let mut i = 0;

    while i + 8 <= x.len() {
        let x_vec = _mm256_loadu_ps(x.as_ptr().add(i));
        let abs_vec = _mm256_and_ps(x_vec, abs_mask);
        max_vec = _mm256_max_ps(max_vec, abs_vec);
        i += 8;
    }

    // Find maximum of the 8 elements
    let mut result = [0.0f32; 8];
    _mm256_storeu_ps(result.as_mut_ptr(), max_vec);
    let mut max_val = result.iter().fold(0.0f32, |a, &b| a.max(b));

    // Handle remaining elements
    while i < x.len() {
        max_val = max_val.max(x[i].abs());
        i += 1;
    }

    max_val
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "avx2")]
unsafe fn euclidean_distance_avx2(a: &[f32], b: &[f32]) -> f32 {
    #[cfg(feature = "no-std")]
    use core::arch::x86_64::*;
    #[cfg(not(feature = "no-std"))]
    use core::arch::x86_64::*;

    let mut sum = _mm256_setzero_ps();
    let mut i = 0;

    while i + 8 <= a.len() {
        let a_vec = _mm256_loadu_ps(a.as_ptr().add(i));
        let b_vec = _mm256_loadu_ps(b.as_ptr().add(i));
        let diff = _mm256_sub_ps(a_vec, b_vec);
        let squared = _mm256_mul_ps(diff, diff);
        sum = _mm256_add_ps(sum, squared);
        i += 8;
    }

    // Horizontal sum
    let mut result = [0.0f32; 8];
    _mm256_storeu_ps(result.as_mut_ptr(), sum);
    let mut scalar_sum = result.iter().sum::<f32>();

    // Handle remaining elements
    while i < a.len() {
        let diff = a[i] - b[i];
        scalar_sum += diff * diff;
        i += 1;
    }

    scalar_sum.sqrt()
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "avx2")]
unsafe fn outer_product_avx2(a: &[f32], b: &[f32]) -> Vec<Vec<f32>> {
    #[cfg(feature = "no-std")]
    use core::arch::x86_64::*;
    #[cfg(not(feature = "no-std"))]
    use core::arch::x86_64::*;

    let m = a.len();
    let n = b.len();
    let mut result = vec![vec![0.0; n]; m];

    for i in 0..m {
        let a_broadcast = _mm256_set1_ps(a[i]);
        let mut j = 0;

        while j + 8 <= n {
            let b_vec = _mm256_loadu_ps(b.as_ptr().add(j));
            let prod = _mm256_mul_ps(a_broadcast, b_vec);
            _mm256_storeu_ps(result[i].as_mut_ptr().add(j), prod);
            j += 8;
        }

        // Handle remaining elements
        while j < n {
            result[i][j] = a[i] * b[j];
            j += 1;
        }
    }

    result
}

// ============================================================================
// AVX512 implementations (x86/x86_64)
// ============================================================================

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "avx512f")]
unsafe fn dot_product_avx512(a: &[f32], b: &[f32]) -> f32 {
    #[cfg(feature = "no-std")]
    use core::arch::x86_64::*;
    #[cfg(not(feature = "no-std"))]
    use core::arch::x86_64::*;

    let mut sum = _mm512_setzero_ps();
    let mut i = 0;

    // Process 16 elements at a time
    while i + 16 <= a.len() {
        let a_vec = _mm512_loadu_ps(a.as_ptr().add(i));
        let b_vec = _mm512_loadu_ps(b.as_ptr().add(i));
        sum = _mm512_fmadd_ps(a_vec, b_vec, sum); // Fused multiply-add
        i += 16;
    }

    // Horizontal sum of the 16 elements in sum
    let scalar_sum = _mm512_reduce_add_ps(sum);

    // Handle remaining elements
    let mut remaining_sum = 0.0f32;
    while i < a.len() {
        remaining_sum += a[i] * b[i];
        i += 1;
    }

    scalar_sum + remaining_sum
}

// ============================================================================
// NEON implementations (ARM AArch64)
// ============================================================================

#[cfg(target_arch = "aarch64")]
#[allow(dead_code)] // NEON dispatch; unused in --all-features (no-std disables runtime detection)
#[target_feature(enable = "neon")]
unsafe fn dot_product_neon(a: &[f32], b: &[f32]) -> f32 {
    use core::arch::aarch64::*;

    let mut sum = vdupq_n_f32(0.0);
    let mut i = 0;

    // Process 4 elements at a time
    while i + 4 <= a.len() {
        let a_vec = vld1q_f32(a.as_ptr().add(i));
        let b_vec = vld1q_f32(b.as_ptr().add(i));
        sum = vfmaq_f32(sum, a_vec, b_vec); // Fused multiply-add
        i += 4;
    }

    // Horizontal sum
    let sum_pair = vpadd_f32(vget_low_f32(sum), vget_high_f32(sum));
    let final_sum = vpadd_f32(sum_pair, sum_pair);
    let mut scalar_sum = vget_lane_f32(final_sum, 0);

    // Handle remaining elements
    while i < a.len() {
        scalar_sum += a[i] * b[i];
        i += 1;
    }

    scalar_sum
}

#[cfg(target_arch = "aarch64")]
#[allow(dead_code)] // NEON dispatch; unused in --all-features (no-std disables runtime detection)
#[target_feature(enable = "neon")]
unsafe fn norm_l1_neon(x: &[f32]) -> f32 {
    use core::arch::aarch64::*;

    let mut sum = vdupq_n_f32(0.0);
    let mut i = 0;

    while i + 4 <= x.len() {
        let x_vec = vld1q_f32(x.as_ptr().add(i));
        let abs_vec = vabsq_f32(x_vec);
        sum = vaddq_f32(sum, abs_vec);
        i += 4;
    }

    // Horizontal sum
    let sum_pair = vpadd_f32(vget_low_f32(sum), vget_high_f32(sum));
    let final_sum = vpadd_f32(sum_pair, sum_pair);
    let mut scalar_sum = vget_lane_f32(final_sum, 0);

    // Handle remaining elements
    while i < x.len() {
        scalar_sum += x[i].abs();
        i += 1;
    }

    scalar_sum
}

#[allow(non_snake_case)]
#[cfg(all(test, not(feature = "no-std")))]
mod tests {
    use super::*;

    #[test]
    fn test_dot_product() {
        let a = vec![1.0, 2.0, 3.0, 4.0];
        let b = vec![5.0, 6.0, 7.0, 8.0];
        let result = dot_product(&a, &b);
        assert_eq!(result, 70.0); // 1*5 + 2*6 + 3*7 + 4*8 = 5 + 12 + 21 + 32 = 70

        // Test with empty vectors
        let empty_a: Vec<f32> = vec![];
        let empty_b: Vec<f32> = vec![];
        assert_eq!(dot_product(&empty_a, &empty_b), 0.0);

        // Test with single element
        let single_a = vec![3.0];
        let single_b = vec![4.0];
        assert_eq!(dot_product(&single_a, &single_b), 12.0);
    }

    #[test]
    fn test_norms() {
        let x = vec![3.0, 4.0];

        // L2 norm
        let norm2 = norm_l2(&x);
        assert_eq!(norm2, 5.0); // sqrt(3² + 4²) = sqrt(25) = 5

        // L1 norm
        let norm1 = norm_l1(&x);
        assert_eq!(norm1, 7.0); // |3| + |4| = 7

        // L∞ norm
        let norm_inf_val = norm_inf(&x);
        assert_eq!(norm_inf_val, 4.0); // max(|3|, |4|) = 4

        // Test with negative values
        let y = vec![-3.0, 4.0, -5.0];
        assert_eq!(norm_l1(&y), 12.0); // |-3| + |4| + |-5| = 12
        assert_eq!(norm_inf(&y), 5.0); // max(|-3|, |4|, |-5|) = 5

        // Test with empty vector
        let empty: Vec<f32> = vec![];
        assert_eq!(norm_l2(&empty), 0.0);
        assert_eq!(norm_l1(&empty), 0.0);
        assert_eq!(norm_inf(&empty), 0.0);
    }

    #[test]
    fn test_euclidean_distance() {
        let a = vec![1.0, 2.0, 3.0];
        let b = vec![4.0, 5.0, 6.0];
        let result = euclidean_distance(&a, &b);
        // sqrt((1-4)² + (2-5)² + (3-6)²) = sqrt(9 + 9 + 9) = sqrt(27) ≈ 5.196
        assert!((result - 5.196).abs() < 0.01);

        // Test with identical vectors
        let identical = euclidean_distance(&a, &a);
        assert_eq!(identical, 0.0);

        // Test with empty vectors
        let empty_a: Vec<f32> = vec![];
        let empty_b: Vec<f32> = vec![];
        assert_eq!(euclidean_distance(&empty_a, &empty_b), 0.0);
    }

    #[test]
    fn test_cosine_similarity() {
        // Test orthogonal vectors
        let a = vec![1.0, 0.0, 0.0];
        let b = vec![0.0, 1.0, 0.0];
        let result = cosine_similarity(&a, &b);
        assert!((result - 0.0).abs() < f32::EPSILON);

        // Test identical vectors
        let identical = cosine_similarity(&a, &a);
        assert!((identical - 1.0).abs() < f32::EPSILON);

        // Test opposite vectors
        let opposite = vec![-1.0, 0.0, 0.0];
        let opposite_sim = cosine_similarity(&a, &opposite);
        assert!((opposite_sim - (-1.0)).abs() < f32::EPSILON);

        // Test with zero vector
        let zero = vec![0.0, 0.0, 0.0];
        let zero_sim = cosine_similarity(&a, &zero);
        assert_eq!(zero_sim, 0.0);

        // Test with empty vectors
        let empty_a: Vec<f32> = vec![];
        let empty_b: Vec<f32> = vec![];
        assert_eq!(cosine_similarity(&empty_a, &empty_b), 1.0);
    }

    #[test]
    fn test_cross_product() {
        // Test unit vectors
        let i = vec![1.0, 0.0, 0.0];
        let j = vec![0.0, 1.0, 0.0];
        let result = cross_product(&i, &j).expect("operation should succeed");
        assert_eq!(result, vec![0.0, 0.0, 1.0]); // i × j = k

        // Test with general vectors
        let a = vec![1.0, 2.0, 3.0];
        let b = vec![4.0, 5.0, 6.0];
        let cross = cross_product(&a, &b).expect("operation should succeed");
        // Expected: (2*6 - 3*5, 3*4 - 1*6, 1*5 - 2*4) = (-3, 6, -3)
        assert_eq!(cross, vec![-3.0, 6.0, -3.0]);

        // Test error for wrong dimensions
        let wrong_dim = vec![1.0, 2.0];
        assert!(cross_product(&wrong_dim, &j).is_err());
    }

    #[test]
    fn test_outer_product() {
        let a = vec![1.0, 2.0];
        let b = vec![3.0, 4.0, 5.0];
        let result = outer_product(&a, &b);

        // Expected: [[1*3, 1*4, 1*5], [2*3, 2*4, 2*5]] = [[3, 4, 5], [6, 8, 10]]
        assert_eq!(result.len(), 2);
        assert_eq!(result[0].len(), 3);
        assert_eq!(result[0], vec![3.0, 4.0, 5.0]);
        assert_eq!(result[1], vec![6.0, 8.0, 10.0]);

        // Test with empty vectors
        let empty_a: Vec<f32> = vec![];
        let empty_result = outer_product(&empty_a, &b);
        assert!(empty_result.is_empty());

        let empty_b: Vec<f32> = vec![];
        let empty_result2 = outer_product(&a, &empty_b);
        assert!(empty_result2.is_empty());
    }

    #[test]
    #[should_panic(expected = "Vectors must have the same length")]
    fn test_dot_product_dimension_mismatch() {
        let a = vec![1.0, 2.0, 3.0];
        let b = vec![4.0, 5.0];
        dot_product(&a, &b);
    }

    #[test]
    #[should_panic(expected = "Vectors must have the same length")]
    fn test_euclidean_distance_dimension_mismatch() {
        let a = vec![1.0, 2.0, 3.0];
        let b = vec![4.0, 5.0];
        euclidean_distance(&a, &b);
    }

    #[test]
    #[should_panic(expected = "Vectors must have the same length")]
    fn test_cosine_similarity_dimension_mismatch() {
        let a = vec![1.0, 2.0, 3.0];
        let b = vec![4.0, 5.0];
        cosine_similarity(&a, &b);
    }
}