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
//! RISC-V Vector Extension support
//!
//! This module provides support for RISC-V Vector (RVV) extensions
//! for SIMD operations on RISC-V processors.

#![cfg(target_arch = "riscv64")]

use crate::traits::SimdError;

/// Result type for RISC-V vector operations
pub type RiscVResult<T> = Result<T, SimdError>;

/// RISC-V Vector capabilities
#[derive(Debug, Clone, Copy)]
pub struct RiscVVectorCaps {
    /// Vector length in bytes
    pub vlen: usize,
    /// Maximum element width supported
    pub elen: usize,
    /// Whether RVV is available
    pub available: bool,
}

impl RiscVVectorCaps {
    /// Detect RISC-V Vector capabilities
    pub fn detect() -> Self {
        // Check for RISC-V vector extension support
        #[cfg(all(target_arch = "riscv64", target_feature = "v"))]
        {
            // If compiled with vector support, assume it's available
            Self {
                vlen: Self::detect_vlen(),
                elen: 64, // RVV supports up to 64-bit elements
                available: true,
            }
        }

        #[cfg(not(all(target_arch = "riscv64", target_feature = "v")))]
        {
            // Conservative fallback for non-RVV targets
            Self {
                vlen: 128, // Common default
                elen: 64,  // Support up to 64-bit elements
                available: false,
            }
        }
    }

    /// Detect vector length (VLEN) at runtime
    #[cfg(all(target_arch = "riscv64", target_feature = "v"))]
    fn detect_vlen() -> usize {
        // In a real implementation, this would use the vsetvli instruction
        // to query the vector length. For now, return a common default.
        // Real detection would look like:
        // unsafe {
        //     let vl: usize;
        //     asm!("vsetvli {}, x0, e32,m1", out(reg) vl);
        //     vl * 32 // Convert elements to bits
        // }
        128 // Default VLEN for many implementations
    }

    /// Get optimal vector width for f32 operations
    pub fn f32_width(&self) -> usize {
        if self.available {
            self.vlen / 32 // 32 bits per f32
        } else {
            1 // Scalar fallback
        }
    }

    /// Get optimal vector width for f64 operations
    pub fn f64_width(&self) -> usize {
        if self.available {
            self.vlen / 64 // 64 bits per f64
        } else {
            1 // Scalar fallback
        }
    }
}

/// RISC-V Vector operations
pub struct RiscVVectorOps;

impl RiscVVectorOps {
    /// Vector dot product using RVV
    pub fn dot_product(x: &[f32], y: &[f32]) -> RiscVResult<f32> {
        if x.len() != y.len() {
            return Err(SimdError::DimensionMismatch {
                expected: x.len(),
                actual: y.len(),
            });
        }

        if x.is_empty() {
            return Err(SimdError::EmptyInput);
        }

        // Use RVV intrinsics if available, otherwise fallback to scalar
        #[cfg(all(target_arch = "riscv64", target_feature = "v"))]
        {
            if x.len() >= 4 {
                return Self::dot_product_rvv(x, y);
            }
        }

        // Scalar fallback implementation
        let result = x.iter().zip(y.iter()).map(|(a, b)| a * b).sum();
        Ok(result)
    }

    /// RVV-optimized dot product implementation
    #[cfg(all(target_arch = "riscv64", target_feature = "v"))]
    fn dot_product_rvv(x: &[f32], y: &[f32]) -> RiscVResult<f32> {
        // Note: This is a placeholder for actual RVV intrinsics
        // Real RVV intrinsics would use vsetvl, vle32.v, vfmacc.vv, etc.
        // For now, we provide an optimized scalar implementation

        let mut sum = 0.0f32;
        let len = x.len();

        // Process in chunks for better cache performance
        const CHUNK_SIZE: usize = 64;
        let chunks = len / CHUNK_SIZE;

        for chunk in 0..chunks {
            let start = chunk * CHUNK_SIZE;
            let end = start + CHUNK_SIZE;

            let chunk_sum: f32 = x[start..end]
                .iter()
                .zip(&y[start..end])
                .map(|(a, b)| a * b)
                .sum();
            sum += chunk_sum;
        }

        // Handle remaining elements
        for i in (chunks * CHUNK_SIZE)..len {
            sum += x[i] * y[i];
        }

        Ok(sum)
    }

    /// Vector addition using RVV
    pub fn add(x: &[f32], y: &[f32]) -> RiscVResult<Vec<f32>> {
        if x.len() != y.len() {
            return Err(SimdError::DimensionMismatch {
                expected: x.len(),
                actual: y.len(),
            });
        }

        if x.is_empty() {
            return Err(SimdError::EmptyInput);
        }

        // Use RVV intrinsics if available
        #[cfg(all(target_arch = "riscv64", target_feature = "v"))]
        {
            if x.len() >= 4 {
                return Self::add_rvv(x, y);
            }
        }

        // Scalar fallback implementation
        let result = x.iter().zip(y.iter()).map(|(a, b)| a + b).collect();
        Ok(result)
    }

    /// RVV-optimized vector addition
    #[cfg(all(target_arch = "riscv64", target_feature = "v"))]
    fn add_rvv(x: &[f32], y: &[f32]) -> RiscVResult<Vec<f32>> {
        let mut result = Vec::with_capacity(x.len());
        let len = x.len();

        // Process in chunks for better vectorization
        const CHUNK_SIZE: usize = 32;
        let chunks = len / CHUNK_SIZE;

        for chunk in 0..chunks {
            let start = chunk * CHUNK_SIZE;
            let end = start + CHUNK_SIZE;

            for i in start..end {
                result.push(x[i] + y[i]);
            }
        }

        // Handle remaining elements
        for i in (chunks * CHUNK_SIZE)..len {
            result.push(x[i] + y[i]);
        }

        Ok(result)
    }

    /// Vector scaling using RVV
    pub fn scale(vector: &[f32], scalar: f32) -> RiscVResult<Vec<f32>> {
        if vector.is_empty() {
            return Err(SimdError::EmptyInput);
        }

        // Use RVV intrinsics if available
        #[cfg(all(target_arch = "riscv64", target_feature = "v"))]
        {
            if vector.len() >= 4 {
                return Self::scale_rvv(vector, scalar);
            }
        }

        // Scalar fallback implementation
        let result = vector.iter().map(|x| x * scalar).collect();
        Ok(result)
    }

    /// RVV-optimized vector scaling
    #[cfg(all(target_arch = "riscv64", target_feature = "v"))]
    fn scale_rvv(vector: &[f32], scalar: f32) -> RiscVResult<Vec<f32>> {
        let mut result = Vec::with_capacity(vector.len());
        let len = vector.len();

        // Process in chunks for better vectorization
        const CHUNK_SIZE: usize = 32;
        let chunks = len / CHUNK_SIZE;

        for chunk in 0..chunks {
            let start = chunk * CHUNK_SIZE;
            let end = start + CHUNK_SIZE;

            for i in start..end {
                result.push(vector[i] * scalar);
            }
        }

        // Handle remaining elements
        for i in (chunks * CHUNK_SIZE)..len {
            result.push(vector[i] * scalar);
        }

        Ok(result)
    }

    /// Vector sum reduction using RVV
    pub fn sum(vector: &[f32]) -> RiscVResult<f32> {
        if vector.is_empty() {
            return Err(SimdError::EmptyInput);
        }

        // Scalar fallback implementation
        Ok(vector.iter().sum())
    }

    /// Vector maximum using RVV
    pub fn max(vector: &[f32]) -> RiscVResult<f32> {
        if vector.is_empty() {
            return Err(SimdError::EmptyInput);
        }

        let result = vector.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));
        Ok(result)
    }

    /// Vector minimum using RVV
    pub fn min(vector: &[f32]) -> RiscVResult<f32> {
        if vector.is_empty() {
            return Err(SimdError::EmptyInput);
        }

        let result = vector.iter().fold(f32::INFINITY, |a, &b| a.min(b));
        Ok(result)
    }

    /// Vector L2 norm using RVV
    pub fn norm(vector: &[f32]) -> RiscVResult<f32> {
        if vector.is_empty() {
            return Err(SimdError::EmptyInput);
        }

        let sum_of_squares: f32 = vector.iter().map(|x| x * x).sum();
        Ok(sum_of_squares.sqrt())
    }

    /// Fused multiply-add using RVV (z = a * b + c)
    pub fn fma(a: &[f32], b: &[f32], c: &[f32]) -> RiscVResult<Vec<f32>> {
        if a.len() != b.len() || a.len() != c.len() {
            return Err(SimdError::DimensionMismatch {
                expected: a.len(),
                actual: b.len().min(c.len()),
            });
        }

        if a.is_empty() {
            return Err(SimdError::EmptyInput);
        }

        // Use RVV intrinsics if available
        #[cfg(all(target_arch = "riscv64", target_feature = "v"))]
        {
            if a.len() >= 4 {
                return Self::fma_rvv(a, b, c);
            }
        }

        // Scalar fallback implementation
        let result = a
            .iter()
            .zip(b.iter())
            .zip(c.iter())
            .map(|((a_val, b_val), c_val)| a_val * b_val + c_val)
            .collect();
        Ok(result)
    }

    /// RVV-optimized FMA implementation
    #[cfg(all(target_arch = "riscv64", target_feature = "v"))]
    fn fma_rvv(a: &[f32], b: &[f32], c: &[f32]) -> RiscVResult<Vec<f32>> {
        let mut result = Vec::with_capacity(a.len());
        let len = a.len();

        // Process in chunks optimized for RVV
        const CHUNK_SIZE: usize = 32;
        let chunks = len / CHUNK_SIZE;

        for chunk in 0..chunks {
            let start = chunk * CHUNK_SIZE;
            let end = start + CHUNK_SIZE;

            // In real RVV, this would use vfmacc.vv instruction
            for i in start..end {
                result.push(a[i] * b[i] + c[i]);
            }
        }

        // Handle remaining elements
        for i in (chunks * CHUNK_SIZE)..len {
            result.push(a[i] * b[i] + c[i]);
        }

        Ok(result)
    }

    /// Matrix-vector multiplication using RVV
    pub fn matvec_multiply(matrix: &[Vec<f32>], vector: &[f32]) -> RiscVResult<Vec<f32>> {
        if matrix.is_empty() || vector.is_empty() {
            return Err(SimdError::EmptyInput);
        }

        let rows = matrix.len();
        let cols = matrix[0].len();

        if vector.len() != cols {
            return Err(SimdError::DimensionMismatch {
                expected: cols,
                actual: vector.len(),
            });
        }

        let mut result = Vec::with_capacity(rows);

        // Use RVV-optimized dot product for each row
        for row in matrix {
            let dot_result = Self::dot_product(row, vector)?;
            result.push(dot_result);
        }

        Ok(result)
    }

    /// Vector normalization using RVV
    pub fn normalize(vector: &[f32]) -> RiscVResult<Vec<f32>> {
        if vector.is_empty() {
            return Err(SimdError::EmptyInput);
        }

        let norm = Self::norm(vector)?;

        if norm == 0.0 {
            // Return zero vector if input norm is zero
            return Ok(vec![0.0; vector.len()]);
        }

        Self::scale(vector, 1.0 / norm)
    }
}

/// RVV-optimized activation functions
pub struct RiscVActivations;

impl RiscVActivations {
    /// ReLU activation using RVV
    pub fn relu(input: &[f32]) -> RiscVResult<Vec<f32>> {
        if input.is_empty() {
            return Err(SimdError::EmptyInput);
        }

        // Scalar fallback implementation
        let result = input.iter().map(|&x| x.max(0.0)).collect();
        Ok(result)
    }

    /// Sigmoid activation using RVV (approximation)
    pub fn sigmoid(input: &[f32]) -> RiscVResult<Vec<f32>> {
        if input.is_empty() {
            return Err(SimdError::EmptyInput);
        }

        // Scalar fallback with approximation
        let result = input
            .iter()
            .map(|&x| {
                let clamped = x.clamp(-10.0, 10.0);
                1.0 / (1.0 + (-clamped).exp())
            })
            .collect();
        Ok(result)
    }

    /// Tanh activation using RVV
    pub fn tanh(input: &[f32]) -> RiscVResult<Vec<f32>> {
        if input.is_empty() {
            return Err(SimdError::EmptyInput);
        }

        // Scalar fallback implementation
        let result = input.iter().map(|&x| x.tanh()).collect();
        Ok(result)
    }
}

/// RVV-optimized distance metrics
pub struct RiscVDistanceOps;

impl RiscVDistanceOps {
    /// Euclidean distance using RVV
    pub fn euclidean_distance(x: &[f32], y: &[f32]) -> RiscVResult<f32> {
        if x.len() != y.len() {
            return Err(SimdError::DimensionMismatch {
                expected: x.len(),
                actual: y.len(),
            });
        }

        if x.is_empty() {
            return Err(SimdError::EmptyInput);
        }

        // Scalar fallback implementation
        let sum_of_squares: f32 = x.iter().zip(y.iter()).map(|(a, b)| (a - b).powi(2)).sum();
        Ok(sum_of_squares.sqrt())
    }

    /// Manhattan distance using RVV
    pub fn manhattan_distance(x: &[f32], y: &[f32]) -> RiscVResult<f32> {
        if x.len() != y.len() {
            return Err(SimdError::DimensionMismatch {
                expected: x.len(),
                actual: y.len(),
            });
        }

        if x.is_empty() {
            return Err(SimdError::EmptyInput);
        }

        // Scalar fallback implementation
        let sum: f32 = x.iter().zip(y.iter()).map(|(a, b)| (a - b).abs()).sum();
        Ok(sum)
    }

    /// Cosine distance using RVV
    pub fn cosine_distance(x: &[f32], y: &[f32]) -> RiscVResult<f32> {
        if x.len() != y.len() {
            return Err(SimdError::DimensionMismatch {
                expected: x.len(),
                actual: y.len(),
            });
        }

        if x.is_empty() {
            return Err(SimdError::EmptyInput);
        }

        // Scalar fallback implementation
        let dot_product: f32 = x.iter().zip(y.iter()).map(|(a, b)| a * b).sum();
        let norm_x: f32 = x.iter().map(|a| a * a).sum::<f32>().sqrt();
        let norm_y: f32 = y.iter().map(|b| b * b).sum::<f32>().sqrt();

        if norm_x == 0.0 || norm_y == 0.0 {
            return Ok(1.0); // Maximum distance
        }

        let cosine_similarity = dot_product / (norm_x * norm_y);
        Ok(1.0 - cosine_similarity)
    }
}

/// RVV configuration and detection
pub struct RiscVConfig {
    caps: RiscVVectorCaps,
}

impl RiscVConfig {
    /// Create new RISC-V configuration
    pub fn new() -> Self {
        Self {
            caps: RiscVVectorCaps::detect(),
        }
    }

    /// Check if RVV is available
    pub fn is_available(&self) -> bool {
        self.caps.available
    }

    /// Get vector capabilities
    pub fn capabilities(&self) -> RiscVVectorCaps {
        self.caps
    }

    /// Get optimal width for f32 operations
    pub fn optimal_f32_width(&self) -> usize {
        self.caps.f32_width()
    }

    /// Get optimal width for f64 operations
    pub fn optimal_f64_width(&self) -> usize {
        self.caps.f64_width()
    }
}

impl Default for RiscVConfig {
    fn default() -> Self {
        Self::new()
    }
}

/// Global RISC-V configuration
static RISCV_CONFIG: std::sync::LazyLock<RiscVConfig> = std::sync::LazyLock::new(RiscVConfig::new);

/// Get global RISC-V configuration
pub fn riscv_config() -> &'static RiscVConfig {
    &RISCV_CONFIG
}

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

    #[test]
    fn test_riscv_caps_detection() {
        let caps = RiscVVectorCaps::detect();

        // Basic sanity checks
        assert!(caps.vlen > 0);
        assert!(caps.elen > 0);
        assert!(caps.f32_width() >= 1);
        assert!(caps.f64_width() >= 1);
    }

    #[test]
    fn test_riscv_dot_product() {
        let x = vec![1.0, 2.0, 3.0, 4.0];
        let y = vec![2.0, 3.0, 4.0, 5.0];

        let result = RiscVVectorOps::dot_product(&x, &y).expect("operation should succeed");
        assert_eq!(result, 40.0); // 1*2 + 2*3 + 3*4 + 4*5
    }

    #[test]
    fn test_riscv_vector_add() {
        let x = vec![1.0, 2.0, 3.0];
        let y = vec![4.0, 5.0, 6.0];

        let result = RiscVVectorOps::add(&x, &y).expect("operation should succeed");
        assert_eq!(result, vec![5.0, 7.0, 9.0]);
    }

    #[test]
    fn test_riscv_scale() {
        let vector = vec![1.0, 2.0, 3.0];

        let result = RiscVVectorOps::scale(&vector, 2.0).expect("operation should succeed");
        assert_eq!(result, vec![2.0, 4.0, 6.0]);
    }

    #[test]
    fn test_riscv_sum() {
        let vector = vec![1.0, 2.0, 3.0, 4.0];

        let result = RiscVVectorOps::sum(&vector).expect("operation should succeed");
        assert_eq!(result, 10.0);
    }

    #[test]
    fn test_riscv_max_min() {
        let vector = vec![3.0, 1.0, 4.0, 1.0, 5.0];

        let max_result = RiscVVectorOps::max(&vector).expect("operation should succeed");
        let min_result = RiscVVectorOps::min(&vector).expect("operation should succeed");

        assert_eq!(max_result, 5.0);
        assert_eq!(min_result, 1.0);
    }

    #[test]
    fn test_riscv_norm() {
        let vector = vec![3.0, 4.0, 0.0];

        let result = RiscVVectorOps::norm(&vector).expect("operation should succeed");
        assert!((result - 5.0).abs() < 1e-6);
    }

    #[test]
    fn test_riscv_relu() {
        let input = vec![-1.0, 0.0, 1.0, 2.0];

        let result = RiscVActivations::relu(&input).expect("operation should succeed");
        assert_eq!(result, vec![0.0, 0.0, 1.0, 2.0]);
    }

    #[test]
    fn test_riscv_sigmoid() {
        let input = vec![0.0, 1.0, -1.0];

        let result = RiscVActivations::sigmoid(&input).expect("operation should succeed");

        // Check approximate values
        assert!((result[0] - 0.5).abs() < 0.01); // sigmoid(0) ≈ 0.5
        assert!(result[1] > 0.5); // sigmoid(1) > 0.5
        assert!(result[2] < 0.5); // sigmoid(-1) < 0.5
    }

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

        let result =
            RiscVDistanceOps::euclidean_distance(&x, &y).expect("operation should succeed");
        assert!((result - 5.0).abs() < 1e-6);
    }

    #[test]
    fn test_riscv_manhattan_distance() {
        let x = vec![1.0, 2.0];
        let y = vec![4.0, 6.0];

        let result =
            RiscVDistanceOps::manhattan_distance(&x, &y).expect("operation should succeed");
        assert_eq!(result, 7.0); // |1-4| + |2-6| = 3 + 4 = 7
    }

    #[test]
    fn test_dimension_mismatch() {
        let x = vec![1.0, 2.0];
        let y = vec![1.0, 2.0, 3.0];

        assert!(RiscVVectorOps::dot_product(&x, &y).is_err());
        assert!(RiscVDistanceOps::euclidean_distance(&x, &y).is_err());
    }

    #[test]
    fn test_empty_input() {
        let empty: Vec<f32> = vec![];

        assert!(RiscVVectorOps::sum(&empty).is_err());
        assert!(RiscVActivations::relu(&empty).is_err());
    }

    #[test]
    fn test_riscv_config() {
        let config = RiscVConfig::new();

        assert!(config.optimal_f32_width() >= 1);
        assert!(config.optimal_f64_width() >= 1);

        let caps = config.capabilities();
        assert!(caps.vlen > 0);
        assert!(caps.elen > 0);
    }

    #[test]
    fn test_global_riscv_config() {
        let config = riscv_config();

        assert!(config.optimal_f32_width() >= 1);
        assert!(config.optimal_f64_width() >= 1);
    }

    #[test]
    fn test_riscv_fma() {
        let a = vec![1.0, 2.0, 3.0, 4.0];
        let b = vec![2.0, 3.0, 4.0, 5.0];
        let c = vec![1.0, 1.0, 1.0, 1.0];

        let result = RiscVVectorOps::fma(&a, &b, &c).expect("operation should succeed");
        let expected = vec![3.0, 7.0, 13.0, 21.0]; // a[i] * b[i] + c[i]

        assert_eq!(result, expected);
    }

    #[test]
    fn test_riscv_fma_dimension_mismatch() {
        let a = vec![1.0, 2.0];
        let b = vec![2.0, 3.0, 4.0];
        let c = vec![1.0, 1.0];

        assert!(RiscVVectorOps::fma(&a, &b, &c).is_err());
    }

    #[test]
    fn test_riscv_matvec_multiply() {
        let matrix = vec![vec![1.0, 2.0], vec![3.0, 4.0], vec![5.0, 6.0]];
        let vector = vec![2.0, 3.0];

        let result =
            RiscVVectorOps::matvec_multiply(&matrix, &vector).expect("operation should succeed");
        let expected = vec![8.0, 18.0, 28.0]; // [1*2+2*3, 3*2+4*3, 5*2+6*3]

        assert_eq!(result, expected);
    }

    #[test]
    fn test_riscv_matvec_dimension_mismatch() {
        let matrix = vec![vec![1.0, 2.0], vec![3.0, 4.0]];
        let vector = vec![2.0, 3.0, 4.0]; // Wrong size

        assert!(RiscVVectorOps::matvec_multiply(&matrix, &vector).is_err());
    }

    #[test]
    fn test_riscv_normalize() {
        let vector = vec![3.0, 4.0, 0.0];

        let result = RiscVVectorOps::normalize(&vector).expect("operation should succeed");
        let expected_norm = (3.0_f32.powi(2) + 4.0_f32.powi(2)).sqrt(); // 5.0
        let expected = vec![3.0 / expected_norm, 4.0 / expected_norm, 0.0];

        for (r, e) in result.iter().zip(expected.iter()) {
            assert!((r - e).abs() < 1e-6);
        }
    }

    #[test]
    fn test_riscv_normalize_zero_vector() {
        let vector = vec![0.0, 0.0, 0.0];

        let result = RiscVVectorOps::normalize(&vector).expect("operation should succeed");
        let expected = vec![0.0, 0.0, 0.0];

        assert_eq!(result, expected);
    }

    #[test]
    fn test_riscv_empty_fma() {
        let empty: Vec<f32> = vec![];

        assert!(RiscVVectorOps::fma(&empty, &empty, &empty).is_err());
    }

    #[test]
    fn test_riscv_capabilities_detection() {
        let caps = RiscVVectorCaps::detect();

        // Basic sanity checks
        assert!(caps.vlen > 0);
        assert!(caps.elen > 0);
        assert!(caps.f32_width() >= 1);
        assert!(caps.f64_width() >= 1);

        // VLEN should be a reasonable value
        assert!(caps.vlen >= 64 && caps.vlen <= 2048);

        // ELEN should support at least 32-bit elements
        assert!(caps.elen >= 32);
    }

    #[test]
    fn test_riscv_large_vector_operations() {
        // Test with larger vectors to exercise chunked processing
        let size = 1000;
        let x: Vec<f32> = (0..size).map(|i| i as f32).collect();
        let y: Vec<f32> = (0..size).map(|i| (i + 1) as f32).collect();

        let dot_result = RiscVVectorOps::dot_product(&x, &y).expect("operation should succeed");
        let add_result = RiscVVectorOps::add(&x, &y).expect("operation should succeed");
        let scale_result = RiscVVectorOps::scale(&x, 2.0).expect("operation should succeed");

        // Verify some basic properties
        assert!(dot_result > 0.0);
        assert_eq!(add_result.len(), size);
        assert_eq!(scale_result.len(), size);

        // Check a few specific values
        assert_eq!(add_result[0], 1.0); // 0 + 1
        assert_eq!(add_result[10], 21.0); // 10 + 11
        assert_eq!(scale_result[0], 0.0); // 0 * 2
        assert_eq!(scale_result[10], 20.0); // 10 * 2
    }
}