lambdust 0.1.1

A Scheme dialect with gradual typing and effect systems
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
//! SIMD optimization engine for high-performance numeric computations
//! 
//! This module provides comprehensive SIMD acceleration for Lambdust's numeric operations,
//! targeting 2-5x performance improvements through intelligent vectorization.
//! 
//! Key features:
//! - Cross-platform SIMD support (x86-64: AVX-512/AVX2/SSE2, ARM64: NEON)
//! - Adaptive optimization strategies based on data patterns
//! - Transparent integration with Scheme numeric tower
//! - Memory-aligned buffer management for optimal performance
//! - Comprehensive fallback mechanisms for compatibility

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use std::arch::x86_64::*;
use std::mem;
use std::ptr;
use std::slice;
use crate::eval::value::Value;
use crate::diagnostics::{Error, Result};

/// SIMD operation types for intelligent strategy selection
#[derive(Debug, Clone, PartialEq)]
pub enum SimdOperationType {
    /// Dense uniform arrays - optimal for maximum SIMD performance
    DenseUniform,
    /// Sparse arrays with many zeros - use conditional branching optimization
    Sparse,
    /// Mixed numeric types - requires type conversion optimization
    MixedTypes,
    /// Large streaming data - use chunked streaming processing
    Streaming,
    /// Small arrays - fallback to scalar processing
    Small,
}

/// Memory alignment requirements for optimal SIMD performance
pub struct AlignedBuffer<T> {
    /// Raw aligned memory pointer
    ptr: *mut T,
    /// Number of elements allocated
    capacity: usize,
    /// Current number of valid elements
    len: usize,
    /// Alignment boundary (16, 32, or 64 bytes)
    alignment: usize,
}

impl<T> AlignedBuffer<T> {
    /// Creates a new aligned buffer with specified alignment
    pub fn new(capacity: usize, alignment: usize) -> Result<Self> {
        if !alignment.is_power_of_two() || alignment < mem::align_of::<T>() {
            return Err(Box::new(Error::runtime_error(
                format!("Invalid alignment: {} (must be power of 2 and >= {})", 
                        alignment, mem::align_of::<T>()),
                None
            )));
        }

        let layout = std::alloc::Layout::from_size_align(
            capacity * mem::size_of::<T>(),
            alignment,
        ).map_err(|e| Error::runtime_error(
            format!("Failed to create layout: {}", e),
            None
        ))?;

        let ptr = unsafe { std::alloc::alloc(layout) as *mut T };
        if ptr.is_null() {
            return Err(Box::new(Error::runtime_error(
                "Failed to allocate aligned memory".to_string(),
                None
            )));
        }

        Ok(AlignedBuffer {
            ptr,
            capacity,
            len: 0,
            alignment,
        })
    }

    /// Returns a mutable slice view of the buffer
    pub fn as_mut_slice(&mut self) -> &mut [T] {
        unsafe { slice::from_raw_parts_mut(self.ptr, self.len) }
    }

    /// Returns an immutable slice view of the buffer
    pub fn as_slice(&self) -> &[T] {
        unsafe { slice::from_raw_parts(self.ptr, self.len) }
    }

    /// Resizes the buffer to the specified length
    pub fn resize(&mut self, new_len: usize) -> Result<()> {
        if new_len > self.capacity {
            return Err(Box::new(Error::runtime_error(
                format!("Buffer resize {} exceeds capacity {}", new_len, self.capacity),
                None
            )));
        }
        self.len = new_len;
        Ok(())
    }

    /// Extends the buffer with elements from a slice
    pub fn extend_from_slice(&mut self, source: &[T]) -> Result<()> 
    where 
        T: Copy 
    {
        if self.len + source.len() > self.capacity {
            return Err(Box::new(Error::runtime_error(
                "Buffer extension would exceed capacity".to_string(),
                None
            )));
        }

        unsafe {
            ptr::copy_nonoverlapping(
                source.as_ptr(),
                self.ptr.add(self.len),
                source.len()
            );
        }
        self.len += source.len();
        Ok(())
    }
}

impl<T> Drop for AlignedBuffer<T> {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            let layout = std::alloc::Layout::from_size_align(
                self.capacity * mem::size_of::<T>(),
                self.alignment,
            ).unwrap();
            unsafe {
                std::alloc::dealloc(self.ptr as *mut u8, layout);
            }
        }
    }
}

unsafe impl<T: Send> Send for AlignedBuffer<T> {}
unsafe impl<T: Sync> Sync for AlignedBuffer<T> {}

/// Core SIMD numeric operations engine
pub struct SimdNumericOps {
    /// CPU feature flags for runtime optimization
    cpu_features: CpuFeatures,
    /// Memory pools for different alignment requirements
    f64_pool_avx512: Vec<AlignedBuffer<f64>>,
    f64_pool_avx2: Vec<AlignedBuffer<f64>>,
    f64_pool_sse2: Vec<AlignedBuffer<f64>>,
    /// Performance statistics for adaptive optimization
    perf_stats: SimdPerfStats,
}

/// CPU feature detection for SIMD optimization
#[derive(Debug, Clone)]
pub struct CpuFeatures {
    pub avx512f: bool,
    pub avx2: bool,
    pub sse2: bool,
    pub fma: bool,
    pub neon: bool,  // ARM NEON support
}

/// Performance statistics for adaptive optimization
#[derive(Debug, Default)]
struct SimdPerfStats {
    /// Total operations performed
    total_ops: u64,
    /// Total time spent in SIMD operations (nanoseconds)
    total_time_ns: u64,
    /// Operations by type
    dense_ops: u64,
    sparse_ops: u64,
    mixed_ops: u64,
    streaming_ops: u64,
    small_ops: u64,
}

impl SimdNumericOps {
    /// Creates a new SIMD numeric operations engine
    pub fn new() -> Self {
        let cpu_features = Self::detect_cpu_features();
        
        SimdNumericOps {
            cpu_features,
            f64_pool_avx512: Vec::new(),
            f64_pool_avx2: Vec::new(),
            f64_pool_sse2: Vec::new(),
            perf_stats: SimdPerfStats::default(),
        }
    }

    /// Detects available CPU SIMD features
    fn detect_cpu_features() -> CpuFeatures {
        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
        {
            if is_x86_feature_detected!("avx512f") {
                CpuFeatures {
                    avx512f: true,
                    avx2: is_x86_feature_detected!("avx2"),
                    sse2: is_x86_feature_detected!("sse2"),
                    fma: is_x86_feature_detected!("fma"),
                    neon: false,
                }
            } else if is_x86_feature_detected!("avx2") {
                CpuFeatures {
                    avx512f: false,
                    avx2: true,
                    sse2: is_x86_feature_detected!("sse2"),
                    fma: is_x86_feature_detected!("fma"),
                    neon: false,
                }
            } else {
                CpuFeatures {
                    avx512f: false,
                    avx2: false,
                    sse2: is_x86_feature_detected!("sse2"),
                    fma: false,
                    neon: false,
                }
            }
        }
        #[cfg(target_arch = "aarch64")]
        {
            CpuFeatures {
                avx512f: false,
                avx2: false,
                sse2: false,
                fma: false,
                neon: true,
            }
        }
        #[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64")))]
        {
            CpuFeatures {
                avx512f: false,
                avx2: false,
                sse2: false,
                fma: false,
                neon: false,
            }
        }
    }

    /// Analyzes data pattern to determine optimal SIMD strategy
    pub fn analyze_operation_type(&self, data: &[f64]) -> SimdOperationType {
        let len = data.len();
        
        // Small arrays use scalar processing
        if len < 8 {
            return SimdOperationType::Small;
        }
        
        // Large arrays use streaming
        if len > 8192 {
            return SimdOperationType::Streaming;
        }
        
        // Analyze sparsity
        let zero_count = data.iter().filter(|&&x| x == 0.0).count();
        let sparsity_ratio = zero_count as f64 / len as f64;
        
        if sparsity_ratio > 0.7 {
            SimdOperationType::Sparse
        } else {
            SimdOperationType::DenseUniform
        }
    }

    /// High-performance SIMD addition for f64 arrays
    pub fn add_f64_arrays(&mut self, a: &[f64], b: &[f64], result: &mut [f64]) -> Result<()> {
        if a.len() != b.len() || a.len() != result.len() {
            return Err(Box::new(Error::runtime_error(
                "Array length mismatch in SIMD addition".to_string(),
                None
            )));
        }

        let start_time = std::time::Instant::now();
        let op_type = self.analyze_operation_type(a);
        
        let result_code = match op_type {
            SimdOperationType::Small => {
                self.add_f64_arrays_scalar(a, b, result)
            },
            SimdOperationType::DenseUniform => {
                if self.cpu_features.avx512f {
                    unsafe { self.add_f64_arrays_avx512(a, b, result) }
                } else if self.cpu_features.avx2 {
                    unsafe { self.add_f64_arrays_avx2(a, b, result) }
                } else if self.cpu_features.sse2 {
                    unsafe { self.add_f64_arrays_sse2(a, b, result) }
                } else {
                    self.add_f64_arrays_scalar(a, b, result)
                }
            },
            SimdOperationType::Sparse => {
                self.add_f64_arrays_sparse(a, b, result)
            },
            SimdOperationType::Streaming => {
                self.add_f64_arrays_streaming(a, b, result)
            },
            SimdOperationType::MixedTypes => {
                // For now, fallback to scalar - mixed types would need type conversion
                self.add_f64_arrays_scalar(a, b, result)
            }
        };

        // Update performance statistics
        let elapsed = start_time.elapsed();
        self.perf_stats.total_ops += 1;
        self.perf_stats.total_time_ns += elapsed.as_nanos() as u64;
        
        match op_type {
            SimdOperationType::DenseUniform => self.perf_stats.dense_ops += 1,
            SimdOperationType::Sparse => self.perf_stats.sparse_ops += 1,
            SimdOperationType::MixedTypes => self.perf_stats.mixed_ops += 1,
            SimdOperationType::Streaming => self.perf_stats.streaming_ops += 1,
            SimdOperationType::Small => self.perf_stats.small_ops += 1,
        }

        result_code
    }

    /// Scalar fallback implementation for addition
    fn add_f64_arrays_scalar(&self, a: &[f64], b: &[f64], result: &mut [f64]) -> Result<()> {
        for i in 0..a.len() {
            result[i] = a[i] + b[i];
        }
        Ok(())
    }

    /// AVX-512 optimized addition (8 f64 elements per instruction)
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "avx512f")]
    unsafe fn add_f64_arrays_avx512(&self, a: &[f64], b: &[f64], result: &mut [f64]) -> Result<()> {
        let len = a.len();
        let chunks = len / 8;
        let remainder = len % 8;

        // Process 8 elements at a time using AVX-512
        for i in 0..chunks {
            let offset = i * 8;
            let a_chunk = _mm512_loadu_pd(a.as_ptr().add(offset));
            let b_chunk = _mm512_loadu_pd(b.as_ptr().add(offset));
            let sum = _mm512_add_pd(a_chunk, b_chunk);
            _mm512_storeu_pd(result.as_mut_ptr().add(offset), sum);
        }

        // Handle remaining elements with scalar processing
        if remainder > 0 {
            let offset = chunks * 8;
            for i in 0..remainder {
                result[offset + i] = a[offset + i] + b[offset + i];
            }
        }

        Ok(())
    }

    /// AVX2 optimized addition (4 f64 elements per instruction)
    #[target_feature(enable = "avx2")]
    unsafe fn add_f64_arrays_avx2(&self, a: &[f64], b: &[f64], result: &mut [f64]) -> Result<()> {
        let len = a.len();
        let chunks = len / 4;
        let remainder = len % 4;

        // Process 4 elements at a time using AVX2
        for i in 0..chunks {
            let offset = i * 4;
            let a_chunk = _mm256_loadu_pd(a.as_ptr().add(offset));
            let b_chunk = _mm256_loadu_pd(b.as_ptr().add(offset));
            let sum = _mm256_add_pd(a_chunk, b_chunk);
            _mm256_storeu_pd(result.as_mut_ptr().add(offset), sum);
        }

        // Handle remaining elements
        if remainder > 0 {
            let offset = chunks * 4;
            for i in 0..remainder {
                result[offset + i] = a[offset + i] + b[offset + i];
            }
        }

        Ok(())
    }

    /// SSE2 optimized addition (2 f64 elements per instruction)
    #[target_feature(enable = "sse2")]
    unsafe fn add_f64_arrays_sse2(&self, a: &[f64], b: &[f64], result: &mut [f64]) -> Result<()> {
        let len = a.len();
        let chunks = len / 2;
        let remainder = len % 2;

        // Process 2 elements at a time using SSE2
        for i in 0..chunks {
            let offset = i * 2;
            let a_chunk = _mm_loadu_pd(a.as_ptr().add(offset));
            let b_chunk = _mm_loadu_pd(b.as_ptr().add(offset));
            let sum = _mm_add_pd(a_chunk, b_chunk);
            _mm_storeu_pd(result.as_mut_ptr().add(offset), sum);
        }

        // Handle remaining element
        if remainder > 0 {
            let offset = chunks * 2;
            result[offset] = a[offset] + b[offset];
        }

        Ok(())
    }

    /// Sparse-optimized addition with conditional processing
    fn add_f64_arrays_sparse(&self, a: &[f64], b: &[f64], result: &mut [f64]) -> Result<()> {
        for i in 0..a.len() {
            // Skip computation if both operands are zero
            if a[i] == 0.0 && b[i] == 0.0 {
                result[i] = 0.0;
            } else if a[i] == 0.0 {
                result[i] = b[i];
            } else if b[i] == 0.0 {
                result[i] = a[i];
            } else {
                result[i] = a[i] + b[i];
            }
        }
        Ok(())
    }

    /// Streaming addition for large arrays with cache-friendly chunks
    fn add_f64_arrays_streaming(&self, a: &[f64], b: &[f64], result: &mut [f64]) -> Result<()> {
        const CHUNK_SIZE: usize = 1024; // Cache-friendly chunk size
        
        let chunks = a.len() / CHUNK_SIZE;
        let remainder = a.len() % CHUNK_SIZE;

        // Process in cache-friendly chunks
        for chunk in 0..chunks {
            let start = chunk * CHUNK_SIZE;
            let end = start + CHUNK_SIZE;
            
            let a_chunk = &a[start..end];
            let b_chunk = &b[start..end];
            let result_chunk = &mut result[start..end];
            
            // Use the best available SIMD for this chunk
            if self.cpu_features.avx2 {
                unsafe {
                    self.add_f64_arrays_avx2(a_chunk, b_chunk, result_chunk)?;
                }
            } else if self.cpu_features.sse2 {
                unsafe {
                    self.add_f64_arrays_sse2(a_chunk, b_chunk, result_chunk)?;
                }
            } else {
                self.add_f64_arrays_scalar(a_chunk, b_chunk, result_chunk)?;
            }
        }

        // Process remainder
        if remainder > 0 {
            let start = chunks * CHUNK_SIZE;
            let a_rem = &a[start..];
            let b_rem = &b[start..];
            let result_rem = &mut result[start..];
            self.add_f64_arrays_scalar(a_rem, b_rem, result_rem)?;
        }

        Ok(())
    }

    /// High-performance SIMD multiplication for f64 arrays
    pub fn multiply_f64_arrays(&mut self, a: &[f64], b: &[f64], result: &mut [f64]) -> Result<()> {
        if a.len() != b.len() || a.len() != result.len() {
            return Err(Box::new(Error::runtime_error(
                "Array length mismatch in SIMD multiplication".to_string(),
                None
            )));
        }

        let start_time = std::time::Instant::now();
        let op_type = self.analyze_operation_type(a);
        
        let result_code = match op_type {
            SimdOperationType::Small => {
                self.multiply_f64_arrays_scalar(a, b, result)
            },
            SimdOperationType::DenseUniform => {
                if self.cpu_features.avx2 {
                    unsafe { self.multiply_f64_arrays_avx2(a, b, result) }
                } else if self.cpu_features.sse2 {
                    unsafe { self.multiply_f64_arrays_sse2(a, b, result) }
                } else {
                    self.multiply_f64_arrays_scalar(a, b, result)
                }
            },
            _ => {
                // For sparse/streaming/mixed, use scalar for now
                self.multiply_f64_arrays_scalar(a, b, result)
            }
        };

        // Update performance statistics
        let elapsed = start_time.elapsed();
        self.perf_stats.total_ops += 1;
        self.perf_stats.total_time_ns += elapsed.as_nanos() as u64;

        result_code
    }

    /// Scalar multiplication fallback
    fn multiply_f64_arrays_scalar(&self, a: &[f64], b: &[f64], result: &mut [f64]) -> Result<()> {
        for i in 0..a.len() {
            result[i] = a[i] * b[i];
        }
        Ok(())
    }

    /// AVX2 optimized multiplication
    #[target_feature(enable = "avx2")]
    unsafe fn multiply_f64_arrays_avx2(&self, a: &[f64], b: &[f64], result: &mut [f64]) -> Result<()> {
        let len = a.len();
        let chunks = len / 4;
        let remainder = len % 4;

        for i in 0..chunks {
            let offset = i * 4;
            let a_chunk = _mm256_loadu_pd(a.as_ptr().add(offset));
            let b_chunk = _mm256_loadu_pd(b.as_ptr().add(offset));
            let product = _mm256_mul_pd(a_chunk, b_chunk);
            _mm256_storeu_pd(result.as_mut_ptr().add(offset), product);
        }

        if remainder > 0 {
            let offset = chunks * 4;
            for i in 0..remainder {
                result[offset + i] = a[offset + i] * b[offset + i];
            }
        }

        Ok(())
    }

    /// SSE2 optimized multiplication
    #[target_feature(enable = "sse2")]
    unsafe fn multiply_f64_arrays_sse2(&self, a: &[f64], b: &[f64], result: &mut [f64]) -> Result<()> {
        let len = a.len();
        let chunks = len / 2;
        let remainder = len % 2;

        for i in 0..chunks {
            let offset = i * 2;
            let a_chunk = _mm_loadu_pd(a.as_ptr().add(offset));
            let b_chunk = _mm_loadu_pd(b.as_ptr().add(offset));
            let product = _mm_mul_pd(a_chunk, b_chunk);
            _mm_storeu_pd(result.as_mut_ptr().add(offset), product);
        }

        if remainder > 0 {
            let offset = chunks * 2;
            result[offset] = a[offset] * b[offset];
        }

        Ok(())
    }

    /// Computes dot product with SIMD optimization
    pub fn dot_product_f64(&mut self, a: &[f64], b: &[f64]) -> Result<f64> {
        if a.len() != b.len() {
            return Err(Box::new(Error::runtime_error(
                "Array length mismatch in dot product".to_string(),
                None
            )));
        }

        let result = if self.cpu_features.avx2 {
            unsafe { self.dot_product_f64_avx2(a, b) }
        } else if self.cpu_features.sse2 {
            unsafe { self.dot_product_f64_sse2(a, b) }
        } else {
            self.dot_product_f64_scalar(a, b)
        };

        Ok(result)
    }

    /// Scalar dot product fallback
    fn dot_product_f64_scalar(&self, a: &[f64], b: &[f64]) -> f64 {
        a.iter().zip(b.iter()).map(|(x, y)| x * y).sum()
    }

    /// AVX2 optimized dot product
    #[target_feature(enable = "avx2")]
    unsafe fn dot_product_f64_avx2(&self, a: &[f64], b: &[f64]) -> f64 {
        let len = a.len();
        let chunks = len / 4;
        let remainder = len % 4;

        let mut sum_vec = _mm256_setzero_pd();

        // Accumulate 4 elements at a time
        for i in 0..chunks {
            let offset = i * 4;
            let a_chunk = _mm256_loadu_pd(a.as_ptr().add(offset));
            let b_chunk = _mm256_loadu_pd(b.as_ptr().add(offset));
            let product = _mm256_mul_pd(a_chunk, b_chunk);
            sum_vec = _mm256_add_pd(sum_vec, product);
        }

        // Horizontal sum of the 4 elements in sum_vec
        let sum_high = _mm256_extractf128_pd(sum_vec, 1);
        let sum_low = _mm256_castpd256_pd128(sum_vec);
        let sum_combined = _mm_add_pd(sum_low, sum_high);
        let sum_final = _mm_add_pd(sum_combined, _mm_shuffle_pd(sum_combined, sum_combined, 1));
        
        let mut result = _mm_cvtsd_f64(sum_final);

        // Handle remainder elements
        if remainder > 0 {
            let offset = chunks * 4;
            for i in 0..remainder {
                result += a[offset + i] * b[offset + i];
            }
        }

        result
    }

    /// SSE2 optimized dot product
    #[target_feature(enable = "sse2")]
    unsafe fn dot_product_f64_sse2(&self, a: &[f64], b: &[f64]) -> f64 {
        let len = a.len();
        let chunks = len / 2;
        let remainder = len % 2;

        let mut sum_vec = _mm_setzero_pd();

        for i in 0..chunks {
            let offset = i * 2;
            let a_chunk = _mm_loadu_pd(a.as_ptr().add(offset));
            let b_chunk = _mm_loadu_pd(b.as_ptr().add(offset));
            let product = _mm_mul_pd(a_chunk, b_chunk);
            sum_vec = _mm_add_pd(sum_vec, product);
        }

        let sum_final = _mm_add_pd(sum_vec, _mm_shuffle_pd(sum_vec, sum_vec, 1));
        let mut result = _mm_cvtsd_f64(sum_final);

        if remainder > 0 {
            let offset = chunks * 2;
            result += a[offset] * b[offset];
        }

        result
    }

    /// Returns performance statistics for monitoring
    pub fn get_performance_stats(&self) -> &SimdPerfStats {
        &self.perf_stats
    }

    /// Resets performance statistics
    pub fn reset_performance_stats(&mut self) {
        self.perf_stats = SimdPerfStats::default();
    }

    /// Integration with Scheme numeric tower for automatic SIMD optimization
    pub fn optimize_scheme_numeric_operation(&mut self, op: &str, args: &[Value]) -> Result<Option<Value>> {
        match op {
            "+" | "add" => self.optimize_scheme_addition(args),
            "*" | "multiply" => self.optimize_scheme_multiplication(args),
            "dot-product" => self.optimize_scheme_dot_product(args),
            _ => Ok(None), // Not optimizable
        }
    }

    /// Optimizes Scheme addition with SIMD when possible
    fn optimize_scheme_addition(&mut self, args: &[Value]) -> Result<Option<Value>> {
        // Check if all arguments are numeric vectors that can be SIMD-optimized
        if args.len() != 2 {
            return Ok(None);
        }

        let (vec_a, vec_b) = match (&args[0], &args[1]) {
            (Value::Vector(a), Value::Vector(b)) if a.len() == b.len() => {
                let a_f64: Result<Vec<f64>> = a.iter()
                    .map(|v| v.as_f64().ok_or_else(|| Error::runtime_error(
                        "Vector element not convertible to f64".to_string(), None)))
                    .collect();
                let b_f64: Result<Vec<f64>> = b.iter()
                    .map(|v| v.as_f64().ok_or_else(|| Error::runtime_error(
                        "Vector element not convertible to f64".to_string(), None)))
                    .collect();
                
                match (a_f64, b_f64) {
                    (Ok(a_vals), Ok(b_vals)) => (a_vals, b_vals),
                    _ => return Ok(None), // Not all f64 convertible
                }
            },
            _ => return Ok(None), // Not vector addition
        };

        // Perform SIMD-optimized addition
        let mut result = vec![0.0; vec_a.len()];
        self.add_f64_arrays(&vec_a, &vec_b, &mut result)?;
        
        // Convert back to Scheme values
        let scheme_result: Vec<Value> = result.into_iter()
            .map(Value::real)
            .collect();
        
        Ok(Some(Value::vector(scheme_result)))
    }

    /// Optimizes Scheme multiplication with SIMD when possible
    fn optimize_scheme_multiplication(&mut self, args: &[Value]) -> Result<Option<Value>> {
        if args.len() != 2 {
            return Ok(None);
        }

        let (vec_a, vec_b) = match (&args[0], &args[1]) {
            (Value::Vector(a), Value::Vector(b)) if a.len() == b.len() => {
                let a_f64: Result<Vec<f64>> = a.iter()
                    .map(|v| v.as_f64().ok_or_else(|| Error::runtime_error(
                        "Vector element not convertible to f64".to_string(), None)))
                    .collect();
                let b_f64: Result<Vec<f64>> = b.iter()
                    .map(|v| v.as_f64().ok_or_else(|| Error::runtime_error(
                        "Vector element not convertible to f64".to_string(), None)))
                    .collect();
                
                match (a_f64, b_f64) {
                    (Ok(a_vals), Ok(b_vals)) => (a_vals, b_vals),
                    _ => return Ok(None),
                }
            },
            _ => return Ok(None),
        };

        let mut result = vec![0.0; vec_a.len()];
        self.multiply_f64_arrays(&vec_a, &vec_b, &mut result)?;
        
        let scheme_result: Vec<Value> = result.into_iter()
            .map(Value::real)
            .collect();
        
        Ok(Some(Value::vector(scheme_result)))
    }

    /// Optimizes Scheme dot product with SIMD
    fn optimize_scheme_dot_product(&mut self, args: &[Value]) -> Result<Option<Value>> {
        if args.len() != 2 {
            return Ok(None);
        }

        let (vec_a, vec_b) = match (&args[0], &args[1]) {
            (Value::Vector(a), Value::Vector(b)) if a.len() == b.len() => {
                let a_f64: Result<Vec<f64>> = a.iter()
                    .map(|v| v.as_f64().ok_or_else(|| Error::runtime_error(
                        "Vector element not convertible to f64".to_string(), None)))
                    .collect();
                let b_f64: Result<Vec<f64>> = b.iter()
                    .map(|v| v.as_f64().ok_or_else(|| Error::runtime_error(
                        "Vector element not convertible to f64".to_string(), None)))
                    .collect();
                
                match (a_f64, b_f64) {
                    (Ok(a_vals), Ok(b_vals)) => (a_vals, b_vals),
                    _ => return Ok(None),
                }
            },
            _ => return Ok(None),
        };

        let result = self.dot_product_f64(&vec_a, &vec_b)?;
        Ok(Some(Value::real(result)))
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_cpu_feature_detection() {
        let features = SimdNumericOps::detect_cpu_features();
        
        // On x86-64, at least SSE2 should be available
        if cfg!(target_arch = "x86_64") {
            assert!(features.sse2);
        }
        
        // On AArch64, NEON should be available
        if cfg!(target_arch = "aarch64") {
            assert!(features.neon);
        }
    }

    #[test]
    fn test_aligned_buffer_creation() {
        let buffer: Result<AlignedBuffer<f64>> = AlignedBuffer::new(16, 64);
        assert!(buffer.is_ok());
        
        let mut buf = buffer.unwrap();
        assert_eq!(buf.as_slice().len(), 0);
        
        buf.resize(8).unwrap();
        assert_eq!(buf.as_slice().len(), 8);
    }

    #[test]
    fn test_operation_type_analysis() {
        let simd = SimdNumericOps::new();
        
        // Small array should return Small
        let small = vec![1.0, 2.0, 3.0];
        assert_eq!(simd.analyze_operation_type(&small), SimdOperationType::Small);
        
        // Large array should return Streaming
        let large = vec![1.0; 10000];
        assert_eq!(simd.analyze_operation_type(&large), SimdOperationType::Streaming);
        
        // Sparse array should return Sparse
        let sparse = vec![0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
        assert_eq!(simd.analyze_operation_type(&sparse), SimdOperationType::Sparse);
        
        // Dense array should return DenseUniform
        let dense = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
        assert_eq!(simd.analyze_operation_type(&dense), SimdOperationType::DenseUniform);
    }

    #[test]
    fn test_scalar_addition() {
        let simd = SimdNumericOps::new();
        let a = vec![1.0, 2.0, 3.0, 4.0];
        let b = vec![5.0, 6.0, 7.0, 8.0];
        let mut result = vec![0.0; 4];
        
        simd.add_f64_arrays_scalar(&a, &b, &mut result).unwrap();
        assert_eq!(result, vec![6.0, 8.0, 10.0, 12.0]);
    }

    #[test]
    fn test_simd_addition() {
        let mut simd = SimdNumericOps::new();
        let a = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
        let b = vec![8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0];
        let mut result = vec![0.0; 8];
        
        simd.add_f64_arrays(&a, &b, &mut result).unwrap();
        assert_eq!(result, vec![9.0; 8]);
    }

    #[test]
    fn test_dot_product() {
        let mut simd = SimdNumericOps::new();
        let a = vec![1.0, 2.0, 3.0, 4.0];
        let b = vec![2.0, 3.0, 4.0, 5.0];
        
        let result = simd.dot_product_f64(&a, &b).unwrap();
        assert_eq!(result, 40.0); // 1*2 + 2*3 + 3*4 + 4*5 = 2 + 6 + 12 + 20 = 40
    }

    #[test]
    fn test_scheme_integration() {
        let mut simd = SimdNumericOps::new();
        
        // Test vector addition optimization
        let vec_a = Value::vector(vec![Value::real(1.0), Value::real(2.0), Value::real(3.0)]);
        let vec_b = Value::vector(vec![Value::real(4.0), Value::real(5.0), Value::real(6.0)]);
        let args = vec![vec_a, vec_b];
        
        let result = simd.optimize_scheme_numeric_operation("+", &args).unwrap();
        assert!(result.is_some());
        
        if let Some(Value::Vector(result_vec)) = result {
            assert_eq!(result_vec.len(), 3);
            assert_eq!(result_vec[0].as_f64().unwrap(), 5.0);
            assert_eq!(result_vec[1].as_f64().unwrap(), 7.0);
            assert_eq!(result_vec[2].as_f64().unwrap(), 9.0);
        } else {
            panic!("Expected vector result");
        }
    }

    #[test]
    fn test_performance_stats() {
        let mut simd = SimdNumericOps::new();
        let initial_stats = simd.get_performance_stats();
        assert_eq!(initial_stats.total_ops, 0);
        
        // Perform some operations
        let a = vec![1.0; 100];
        let b = vec![2.0; 100];
        let mut result = vec![0.0; 100];
        
        simd.add_f64_arrays(&a, &b, &mut result).unwrap();
        
        let updated_stats = simd.get_performance_stats();
        assert_eq!(updated_stats.total_ops, 1);
        assert!(updated_stats.total_time_ns > 0);
    }
}