Skip to main content

qualia_core_db/solvers/calculus/
grid.rs

1//! Continuous-grid numerical integration — Simpson / trapezoidal rules (Kahan-
2//! compensated, SIMD-accelerated chunking) over zero-copy mmap-backed f64 grids, plus
3//! the DMA-alignment / state-suspension helpers. Relocated here from
4//! `modalities::calculus` as STEM math; the VM opcode surface that *dispatches* these
5//! stays in `modalities::calculus` (the modality), the numbers live in the solver.
6
7use crate::NQuin;
8
9#[cfg(target_arch = "x86_64")]
10use std::arch::x86_64::_MM_HINT_T0;
11
12// ─── DMA Alignment Helpers ─────────────────────────────────────────────────────
13
14/// Translates a starting float boundary into a strictly 4096-aligned byte offset
15/// assuming the underlying grid is an array of contiguous 64-bit floats (8 bytes).
16///
17/// This function rounds DOWN to the nearest 4KB boundary to prevent `IoError::MisalignedOffset`
18/// when the VM dispatches the Quin to host hardware (io_uring, DirectStorage, GPUDirect).
19///
20/// # Returns
21/// - `page_aligned_offset`: The 4096-byte aligned byte offset
22/// - `remainder`: The 12-bit remainder (0-4095) indicating the offset within the first page
23#[inline(always)]
24pub fn resolve_aligned_byte_offset(start_index: usize) -> (u64, u16) {
25    let exact_byte_offset = (start_index * 8) as u64;
26
27    // 4096 is 2^12. The bitwise NOT of 4095 (0xFFF) gives a mask of ...1111000000000000
28    // Performing an AND operation strictly rounds DOWN to the nearest 4KB boundary.
29    let page_aligned_offset = exact_byte_offset & !0xFFF;
30
31    // Calculate the remainder (the difference between exact and aligned offset)
32    // This is at most 4095 (12 bits), which fits in a u16
33    let remainder = (exact_byte_offset - page_aligned_offset) as u16;
34
35    (page_aligned_offset, remainder)
36}
37
38/// Bit-packs two f32 values into a single 64-bit context field
39/// Used for packing step_size and Kahan compensation into the Quin context field
40#[inline(always)]
41pub fn pack_f32_pair(step: f32, comp: f32) -> u64 {
42    let step_bits = step.to_bits() as u64;
43    let comp_bits = comp.to_bits() as u64;
44    (step_bits << 32) | comp_bits
45}
46
47/// Unpacks a 64-bit context field back into two f32 values
48#[inline(always)]
49pub fn unpack_f32_pair(packed: u64) -> (f32, f32) {
50    let step_bits = (packed >> 32) as u32;
51    let comp_bits = (packed & 0xFFFFFFFF) as u32;
52    (f32::from_bits(step_bits), f32::from_bits(comp_bits))
53}
54
55// ─── Errors ─────────────────────────────────────────────────────────────────────
56
57#[derive(Debug, Clone, Copy, PartialEq, Eq)]
58pub enum CalculusError {
59    AlignmentError(AlignmentError),
60    InvalidOffset,
61    InsufficientData,
62    InvalidStepSize,
63    NonFiniteInput,
64    SimpsonRequiresEvenPanels,
65}
66
67#[derive(Debug, Clone, Copy, PartialEq, Eq)]
68pub enum AlignmentError {
69    MisalignedPointer,
70    MisalignedLength,
71    MisalignedOffset,
72}
73
74// ─── Continuous Grid ───────────────────────────────────────────────────────────
75
76/// Zero-copy continuous data grid view.
77///
78/// Takes a raw byte slice from the Host OS (mmap or io_uring buffer) and
79/// provides a safe, aligned view as an f64 slice for numerical processing.
80pub struct ContinuousGrid<'a> {
81    data: &'a [f64],
82}
83
84impl<'a> ContinuousGrid<'a> {
85    /// Creates a new continuous grid from a raw byte slice.
86    ///
87    /// # Safety
88    ///
89    /// This function validates that the raw slice is properly aligned to 8-byte
90    /// boundaries before casting to f64. It returns an error if alignment is invalid.
91    pub fn new(raw_slice: &'a [u8], points: usize) -> Result<Self, AlignmentError> {
92        let byte_len = points
93            .checked_mul(core::mem::size_of::<f64>())
94            .ok_or(AlignmentError::MisalignedLength)?;
95
96        if raw_slice.len() < byte_len {
97            return Err(AlignmentError::MisalignedLength);
98        }
99
100        // Validate pointer alignment
101        if raw_slice.as_ptr() as usize % 8 != 0 {
102            return Err(AlignmentError::MisalignedPointer);
103        }
104
105        // Validate length alignment
106        if byte_len % 8 != 0 {
107            return Err(AlignmentError::MisalignedLength);
108        }
109
110        // Safe to cast now - alignment is validated
111        let float_slice =
112            unsafe { core::slice::from_raw_parts(raw_slice.as_ptr() as *const f64, points) };
113
114        Ok(Self { data: float_slice })
115    }
116
117    /// Resumes integration from a suspended Quin state.
118    ///
119    /// Extracts the byte offset from the Quin's object field and validates
120    /// that it is 8-byte aligned before creating the grid view.
121    pub fn resume_from_quin(
122        raw_slice: &'a [u8],
123        quin: &NQuin,
124    ) -> Result<(Self, usize), CalculusError> {
125        let offset = quin.object as usize;
126
127        // CRITICAL: Validate offset is 8-byte aligned
128        if offset % 8 != 0 {
129            return Err(CalculusError::AlignmentError(
130                AlignmentError::MisalignedOffset,
131            ));
132        }
133
134        if offset >= raw_slice.len() {
135            return Err(CalculusError::InvalidOffset);
136        }
137
138        let grid = Self::new(&raw_slice[offset..], (raw_slice.len() - offset) / 8)
139            .map_err(CalculusError::AlignmentError)?;
140
141        Ok((grid, offset))
142    }
143
144    /// Returns the number of f64 values in the grid.
145    pub fn len(&self) -> usize {
146        self.data.len()
147    }
148
149    /// Returns true if the grid is empty.
150    pub fn is_empty(&self) -> bool {
151        self.data.is_empty()
152    }
153
154    /// Returns the underlying f64 slice.
155    pub fn as_slice(&self) -> &[f64] {
156        self.data
157    }
158}
159
160// ─── SIMD Width Detection ───────────────────────────────────────────────────────
161
162#[derive(Debug, Clone, Copy, PartialEq, Eq)]
163pub enum SimdWidth {
164    Scalar,
165    Neon2, // ARM NEON: 2 f64 per instruction
166    Avx2,  // x86 AVX2: 4 f64 per instruction
167}
168
169/// Detected SIMD width.
170pub fn detect_simd_width() -> SimdWidth {
171    #[cfg(target_arch = "x86_64")]
172    {
173        if std::is_x86_feature_detected!("avx2") {
174            SimdWidth::Avx2
175        } else {
176            SimdWidth::Scalar
177        }
178    }
179
180    #[cfg(target_arch = "aarch64")]
181    {
182        SimdWidth::Neon2
183    }
184
185    #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
186    {
187        SimdWidth::Scalar
188    }
189}
190
191/// Detected cache-line size.
192pub fn detect_cache_line_size() -> usize {
193    #[cfg(target_arch = "x86_64")]
194    {
195        #[cfg(target_arch = "x86_64")]
196        {
197            // Default to 64 bytes for most modern x86_64 CPUs
198            64
199        }
200    }
201
202    #[cfg(target_arch = "aarch64")]
203    {
204        64 // ARM64 typically 64-byte cache lines
205    }
206
207    #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
208    {
209        64 // Conservative default
210    }
211}
212
213// ─── Integration Functions ─────────────────────────────────────────────────────
214
215/// Simpson's rule integration with Kahan summation for precision.
216///
217/// Processes the grid in chunks to maintain cache locality and enable SIMD
218/// acceleration. The grid must contain an odd number of samples (an even
219/// number of panels). Returns the integrated value and Kahan compensation.
220pub fn integrate_simpsons_kahan(
221    grid: &ContinuousGrid,
222    step_size: f32,
223) -> Result<(f64, f32), CalculusError> {
224    validate_simpson_inputs(grid.data, step_size as f64)?;
225
226    let mut sum = 0.0f64;
227    let mut compensation = 0.0f64;
228    let chunk_size = calculate_optimal_chunk_size();
229
230    for (chunk_index, chunk) in grid.data.chunks(chunk_size).enumerate() {
231        let start = chunk_index * chunk_size;
232        let chunk_sum = process_simpson_weighted_chunk(chunk, start, grid.data.len());
233
234        // Kahan summation
235        let y = chunk_sum - compensation;
236        let t = sum + y;
237        compensation = (t - sum) - y;
238        sum = t;
239    }
240
241    let scale = step_size as f64 / 3.0;
242    Ok((sum * scale, (compensation * scale) as f32))
243}
244
245/// Simpson's rule integration (standard, without Kahan compensation).
246///
247/// Use this for smaller grids where precision loss is acceptable.
248pub fn integrate_simpsons_chunked(
249    grid: &ContinuousGrid,
250    step_size: f64,
251) -> Result<f64, CalculusError> {
252    validate_simpson_inputs(grid.data, step_size)?;
253
254    let mut accumulator = 0.0f64;
255    let chunk_size = calculate_optimal_chunk_size();
256    let prefetch_distance = chunk_size * 2;
257
258    let chunks = grid.data.chunks(chunk_size);
259    for (i, chunk) in chunks.enumerate() {
260        // Prefetch next chunk into L1 cache
261        if let Some(future_data) = grid.data.get(i * chunk_size + prefetch_distance) {
262            issue_prefetch(future_data);
263        }
264
265        accumulator += process_simpson_weighted_chunk(chunk, i * chunk_size, grid.data.len());
266    }
267
268    Ok(accumulator * (step_size / 3.0))
269}
270
271/// Trapezoidal rule integration (fallback for simpler integrands).
272pub fn integrate_trapezoidal_chunked(
273    grid: &ContinuousGrid,
274    step_size: f64,
275) -> Result<f64, CalculusError> {
276    validate_common_inputs(grid.data, step_size, 2)?;
277
278    let mut accumulator = 0.0f64;
279    let chunk_size = calculate_optimal_chunk_size();
280
281    for (chunk_index, chunk) in grid.data.chunks(chunk_size).enumerate() {
282        accumulator += process_trapezoidal_chunk(chunk, chunk_index * chunk_size, grid.data.len());
283    }
284
285    Ok(accumulator * (step_size / 2.0))
286}
287
288// ─── Chunk Processing ───────────────────────────────────────────────────────────
289
290fn calculate_optimal_chunk_size() -> usize {
291    let simd_width = detect_simd_width();
292    let cache_line_size = detect_cache_line_size();
293
294    // Base chunk: multiple of SIMD width
295    let base = match simd_width {
296        SimdWidth::Scalar => 1,
297        SimdWidth::Neon2 => 2,
298        SimdWidth::Avx2 => 4,
299    };
300
301    // Scale to fill cache line (64 bytes = 8 f64)
302    let f64_per_cache_line = cache_line_size / 8;
303
304    // Target: 2-4 cache lines per chunk for prefetch effectiveness
305    let target = f64_per_cache_line * 2;
306
307    // Round up to nearest multiple of SIMD width
308    ((target + base - 1) / base) * base
309}
310
311#[inline]
312fn simpson_weight(index: usize, total_len: usize) -> f64 {
313    if index == 0 || index + 1 == total_len {
314        1.0
315    } else if index & 1 == 1 {
316        4.0
317    } else {
318        2.0
319    }
320}
321
322fn validate_common_inputs(
323    data: &[f64],
324    step_size: f64,
325    minimum_len: usize,
326) -> Result<(), CalculusError> {
327    if data.len() < minimum_len {
328        return Err(CalculusError::InsufficientData);
329    }
330    if !step_size.is_finite() || step_size == 0.0 {
331        return Err(CalculusError::InvalidStepSize);
332    }
333    if data.iter().any(|value| !value.is_finite()) {
334        return Err(CalculusError::NonFiniteInput);
335    }
336    Ok(())
337}
338
339fn validate_simpson_inputs(data: &[f64], step_size: f64) -> Result<(), CalculusError> {
340    validate_common_inputs(data, step_size, 3)?;
341    if data.len() & 1 == 0 {
342        return Err(CalculusError::SimpsonRequiresEvenPanels);
343    }
344    Ok(())
345}
346
347/// Produces the unscaled globally weighted Simpson sum for one cache chunk.
348///
349/// `global_start` is deliberately explicit: restarting parity or endpoint
350/// weights at a chunk boundary changes the mathematical rule.
351fn process_simpson_weighted_chunk(chunk: &[f64], global_start: usize, total_len: usize) -> f64 {
352    #[cfg(target_arch = "x86_64")]
353    {
354        if std::is_x86_feature_detected!("avx2") {
355            // SAFETY: the runtime feature probe dominates this call and the
356            // function itself is the only AVX2 compilation boundary.
357            return unsafe { process_simpson_weighted_chunk_avx2(chunk, global_start, total_len) };
358        }
359    }
360
361    #[cfg(target_arch = "aarch64")]
362    {
363        return process_simpson_weighted_chunk_neon(chunk, global_start, total_len);
364    }
365
366    process_simpson_weighted_chunk_scalar(chunk, global_start, total_len)
367}
368
369fn process_simpson_weighted_chunk_scalar(
370    chunk: &[f64],
371    global_start: usize,
372    total_len: usize,
373) -> f64 {
374    chunk
375        .iter()
376        .enumerate()
377        .map(|(offset, value)| simpson_weight(global_start + offset, total_len) * value)
378        .sum()
379}
380
381/// Processes a globally indexed chunk using AVX2 intrinsics.
382#[cfg(target_arch = "x86_64")]
383#[target_feature(enable = "avx2")]
384unsafe fn process_simpson_weighted_chunk_avx2(
385    chunk: &[f64],
386    global_start: usize,
387    total_len: usize,
388) -> f64 {
389    use core::arch::x86_64::*;
390
391    let mut sum = 0.0f64;
392    let len = chunk.len();
393
394    let simd_chunks = len / 4;
395    for i in 0..simd_chunks {
396        let idx = i * 4;
397        let global = global_start + idx;
398        let vals = _mm256_loadu_pd(chunk.as_ptr().add(idx));
399        let weights = _mm256_set_pd(
400            simpson_weight(global + 3, total_len),
401            simpson_weight(global + 2, total_len),
402            simpson_weight(global + 1, total_len),
403            simpson_weight(global, total_len),
404        );
405        let weighted = _mm256_mul_pd(vals, weights);
406        let mut lanes = [0.0_f64; 4];
407        _mm256_storeu_pd(lanes.as_mut_ptr(), weighted);
408        sum += lanes[0] + lanes[1] + lanes[2] + lanes[3];
409    }
410
411    for i in (simd_chunks * 4)..len {
412        sum += simpson_weight(global_start + i, total_len) * chunk[i];
413    }
414
415    sum
416}
417
418/// Processes a chunk using NEON intrinsics.
419#[cfg(target_arch = "aarch64")]
420fn process_simpson_weighted_chunk_neon(
421    chunk: &[f64],
422    global_start: usize,
423    total_len: usize,
424) -> f64 {
425    use core::arch::aarch64::*;
426
427    let mut sum = 0.0f64;
428    let len = chunk.len();
429
430    // Process 2 doubles at a time (NEON)
431    let simd_chunks = len / 2;
432    for i in 0..simd_chunks {
433        let idx = i * 2;
434        unsafe {
435            let vals = vld1q_f64(chunk.as_ptr().add(idx));
436            let global = global_start + idx;
437            let weights = [
438                simpson_weight(global, total_len),
439                simpson_weight(global + 1, total_len),
440            ];
441            let weighted = vmulq_f64(vals, vld1q_f64(weights.as_ptr()));
442            sum += vgetq_lane_f64::<0>(weighted) + vgetq_lane_f64::<1>(weighted);
443        }
444    }
445
446    // Process remaining elements
447    for i in (simd_chunks * 2)..len {
448        sum += simpson_weight(global_start + i, total_len) * chunk[i];
449    }
450
451    sum
452}
453
454/// Processes a chunk using trapezoidal rule.
455fn process_trapezoidal_chunk(chunk: &[f64], global_start: usize, total_len: usize) -> f64 {
456    chunk
457        .iter()
458        .enumerate()
459        .map(|(offset, value)| {
460            let index = global_start + offset;
461            let weight = if index == 0 || index + 1 == total_len {
462                1.0
463            } else {
464                2.0
465            };
466            weight * value
467        })
468        .sum()
469}
470
471/// Issues a hardware prefetch instruction for the given data.
472fn issue_prefetch(data: &f64) {
473    #[cfg(target_arch = "x86_64")]
474    {
475        use core::arch::x86_64::_mm_prefetch;
476        unsafe {
477            _mm_prefetch(data as *const f64 as *const i8, _MM_HINT_T0);
478        }
479    }
480
481    #[cfg(target_arch = "aarch64")]
482    {
483        let _ = data; // prefetch is a no-op hint; aarch64 has no stable Rust intrinsic
484    }
485
486    #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
487    {
488        // Preserve a scheduling barrier on targets without a stable prefetch intrinsic.
489        core::hint::black_box(data);
490    }
491}
492
493// ─── Tests ─────────────────────────────────────────────────────────────────────
494
495#[cfg(test)]
496mod tests {
497    use super::*;
498
499    #[test]
500    fn test_simpsons_integration() {
501        #[repr(C, align(4096))]
502        struct TestBuffer {
503            data: [f64; 101],
504        }
505
506        let buffer = TestBuffer {
507            data: [1.0f64; 101],
508        };
509
510        let raw_bytes: &[u8] = unsafe {
511            core::slice::from_raw_parts(buffer.data.as_ptr() as *const u8, buffer.data.len() * 8)
512        };
513
514        let grid = ContinuousGrid::new(raw_bytes, 101).unwrap();
515        let result = integrate_simpsons_chunked(&grid, 0.02).unwrap();
516        assert_eq!(result, 2.0);
517    }
518
519    #[test]
520    fn test_alignment_safety() {
521        // Test misaligned pointer rejection
522        // Use 4096-byte aligned buffer, then pass misaligned slice
523        #[repr(C, align(4096))]
524        struct TestBuffer {
525            data: [u8; 8192], // 2 OS pages
526        }
527
528        let buffer = TestBuffer { data: [0u8; 8192] };
529        let result = ContinuousGrid::new(&buffer.data[1..], 2);
530        assert!(matches!(result, Err(AlignmentError::MisalignedPointer)));
531    }
532
533    #[test]
534    fn test_resolve_aligned_byte_offset() {
535        // Test that the alignment resolver rounds down to 4KB boundaries
536        let (aligned, remainder) = resolve_aligned_byte_offset(0);
537        assert_eq!(aligned, 0);
538        assert_eq!(remainder, 0);
539
540        // Index 512 = 4096 bytes exactly (512 * 8)
541        let (aligned, remainder) = resolve_aligned_byte_offset(512);
542        assert_eq!(aligned, 4096);
543        assert_eq!(remainder, 0);
544
545        // Index 513 = 4104 bytes (4096 + 8)
546        let (aligned, remainder) = resolve_aligned_byte_offset(513);
547        assert_eq!(aligned, 4096);
548        assert_eq!(remainder, 8);
549
550        // Index 1023 = 8184 bytes (8192 - 8)
551        let (aligned, remainder) = resolve_aligned_byte_offset(1023);
552        assert_eq!(aligned, 4096);
553        assert_eq!(remainder, 4088);
554
555        // Index 1024 = 8192 bytes exactly (2 * 4096)
556        let (aligned, remainder) = resolve_aligned_byte_offset(1024);
557        assert_eq!(aligned, 8192);
558        assert_eq!(remainder, 0);
559    }
560
561    #[test]
562    fn test_pack_unpack_f32_pair() {
563        let step = 0.001f32;
564        let comp = 0.0f32;
565        let packed = pack_f32_pair(step, comp);
566        let (unpacked_step, unpacked_comp) = unpack_f32_pair(packed);
567        assert_eq!(step, unpacked_step);
568        assert_eq!(comp, unpacked_comp);
569    }
570
571    #[test]
572    fn test_state_suspension() {
573        // Test that integration state can be packed into Quin
574        let mut quin = NQuin::default();
575        quin.object = 1024; // Byte offset
576        quin.metadata = f64::to_bits(42.5); // Accumulator
577
578        let offset = quin.object;
579        let accumulator = f64::from_bits(quin.metadata);
580
581        assert_eq!(offset, 1024);
582        assert_eq!(accumulator, 42.5);
583    }
584
585    #[test]
586    fn test_resume_from_quin() {
587        let mut data = [0.0f64; 100];
588        for i in 0..100 {
589            data[i] = i as f64;
590        }
591
592        let raw_bytes: &[u8] =
593            unsafe { core::slice::from_raw_parts(data.as_ptr() as *const u8, data.len() * 8) };
594
595        let mut quin = NQuin::default();
596        quin.object = 64; // Aligned offset (8 * 8 = 64)
597
598        let (grid, offset) = ContinuousGrid::resume_from_quin(raw_bytes, &quin).unwrap();
599        assert_eq!(offset, 64);
600        assert_eq!(grid.len(), 92); // (800 - 64) / 8 = 92
601    }
602
603    #[test]
604    fn test_resume_from_quin_misaligned() {
605        let data = [0u8; 100];
606        let mut quin = NQuin::default();
607        quin.object = 63; // Misaligned offset
608
609        let result = ContinuousGrid::resume_from_quin(&data, &quin);
610        assert!(matches!(
611            result,
612            Err(CalculusError::AlignmentError(
613                AlignmentError::MisalignedOffset
614            ))
615        ));
616    }
617
618    #[test]
619    fn test_simd_width_detection() {
620        let width = detect_simd_width();
621        // Should return a valid width based on target architecture
622        match width {
623            SimdWidth::Scalar | SimdWidth::Neon2 | SimdWidth::Avx2 => {}
624        }
625    }
626
627    #[test]
628    fn test_cache_line_size_detection() {
629        let size = detect_cache_line_size();
630        // Should return a reasonable cache line size (typically 64)
631        assert!(size == 32 || size == 64 || size == 128);
632    }
633
634    #[test]
635    fn test_kahan_summation() {
636        // Test Kahan summation with values that cause precision loss
637        let mut data = [0.0f64; 1001];
638        for i in 0..1001 {
639            data[i] = 1e-10; // Very small values
640        }
641
642        let raw_bytes: &[u8] =
643            unsafe { core::slice::from_raw_parts(data.as_ptr() as *const u8, data.len() * 8) };
644
645        let grid = ContinuousGrid::new(raw_bytes, 1001).unwrap();
646        let (sum, _compensation) = integrate_simpsons_kahan(&grid, 0.001).unwrap();
647
648        // Kahan should preserve precision better than naive summation
649        assert!(sum > 0.0);
650    }
651
652    #[test]
653    fn simpson_is_exact_for_cubic_across_cache_chunks() {
654        #[repr(C, align(64))]
655        struct Buffer {
656            data: [f64; 101],
657        }
658        let mut buffer = Buffer { data: [0.0; 101] };
659        for (i, value) in buffer.data.iter_mut().enumerate() {
660            let x = i as f64 / 100.0;
661            *value = x * x * x;
662        }
663        let bytes = unsafe {
664            core::slice::from_raw_parts(
665                buffer.data.as_ptr().cast::<u8>(),
666                buffer.data.len() * core::mem::size_of::<f64>(),
667            )
668        };
669        let grid = ContinuousGrid::new(bytes, buffer.data.len()).unwrap();
670        let integral = integrate_simpsons_chunked(&grid, 0.01).unwrap();
671        assert!((integral - 0.25).abs() <= 8.0 * f64::EPSILON);
672    }
673
674    #[test]
675    fn scalar_quadrature_exactness_holds_for_all_small_legal_lengths() {
676        #[repr(C, align(64))]
677        struct Buffer {
678            data: [f64; 257],
679        }
680        let mut buffer = Buffer { data: [0.0; 257] };
681        let h = 1.0 / 256.0;
682        for (i, value) in buffer.data.iter_mut().enumerate() {
683            let x = i as f64 * h;
684            *value = x * x * x;
685        }
686        let bytes = unsafe {
687            core::slice::from_raw_parts(
688                buffer.data.as_ptr().cast::<u8>(),
689                buffer.data.len() * core::mem::size_of::<f64>(),
690            )
691        };
692
693        for len in (3..=257).step_by(2) {
694            let grid = ContinuousGrid::new(bytes, len).unwrap();
695            let upper = (len - 1) as f64 * h;
696            let expected = upper.powi(4) / 4.0;
697            let actual = integrate_simpsons_chunked(&grid, h).unwrap();
698            assert!(
699                (actual - expected).abs() <= 128.0 * f64::EPSILON * expected.max(1.0),
700                "len={len}: expected {expected}, got {actual}"
701            );
702        }
703
704        for (i, value) in buffer.data.iter_mut().enumerate() {
705            *value = 3.0 * i as f64 * h - 2.0;
706        }
707        for len in 2..=257 {
708            let grid = ContinuousGrid::new(bytes, len).unwrap();
709            let upper = (len - 1) as f64 * h;
710            let expected = 1.5 * upper * upper - 2.0 * upper;
711            let actual = integrate_trapezoidal_chunked(&grid, h).unwrap();
712            assert!(
713                (actual - expected).abs() <= 128.0 * f64::EPSILON * expected.abs().max(1.0),
714                "len={len}: expected {expected}, got {actual}"
715            );
716        }
717    }
718
719    #[test]
720    fn trapezoid_is_exact_for_affine_data_across_cache_chunks() {
721        #[repr(C, align(64))]
722        struct Buffer {
723            data: [f64; 101],
724        }
725        let mut buffer = Buffer { data: [0.0; 101] };
726        for (i, value) in buffer.data.iter_mut().enumerate() {
727            *value = i as f64 / 100.0;
728        }
729        let bytes = unsafe {
730            core::slice::from_raw_parts(
731                buffer.data.as_ptr().cast::<u8>(),
732                buffer.data.len() * core::mem::size_of::<f64>(),
733            )
734        };
735        let grid = ContinuousGrid::new(bytes, buffer.data.len()).unwrap();
736        let integral = integrate_trapezoidal_chunked(&grid, 0.01).unwrap();
737        assert!((integral - 0.5).abs() <= 4.0 * f64::EPSILON);
738    }
739
740    #[test]
741    fn simpson_rejects_invalid_panel_count_and_non_finite_data() {
742        let even = [1.0_f64; 4];
743        let even_bytes = unsafe {
744            core::slice::from_raw_parts(
745                even.as_ptr().cast::<u8>(),
746                even.len() * core::mem::size_of::<f64>(),
747            )
748        };
749        let even_grid = ContinuousGrid::new(even_bytes, even.len()).unwrap();
750        assert_eq!(
751            integrate_simpsons_chunked(&even_grid, 1.0),
752            Err(CalculusError::SimpsonRequiresEvenPanels)
753        );
754
755        let non_finite = [0.0, f64::NAN, 1.0];
756        let non_finite_bytes = unsafe {
757            core::slice::from_raw_parts(
758                non_finite.as_ptr().cast::<u8>(),
759                non_finite.len() * core::mem::size_of::<f64>(),
760            )
761        };
762        let non_finite_grid = ContinuousGrid::new(non_finite_bytes, non_finite.len()).unwrap();
763        assert_eq!(
764            integrate_simpsons_chunked(&non_finite_grid, 0.5),
765            Err(CalculusError::NonFiniteInput)
766        );
767        assert_eq!(
768            integrate_simpsons_chunked(&non_finite_grid, 0.0),
769            Err(CalculusError::InvalidStepSize)
770        );
771    }
772
773    #[cfg(target_arch = "x86_64")]
774    #[test]
775    fn runtime_avx2_weighted_sum_matches_forced_scalar() {
776        if !std::is_x86_feature_detected!("avx2") {
777            return;
778        }
779        let mut data = [0.0_f64; 80];
780        for (index, value) in data.iter_mut().enumerate() {
781            *value = (index as f64 * 0.37).sin() * (1.0 + index as f64);
782        }
783        for offset in 0..8 {
784            for len in 0..=64 {
785                let slice = &data[offset..offset + len];
786                let scalar = process_simpson_weighted_chunk_scalar(slice, offset + 3, 97);
787                let avx2 = unsafe { process_simpson_weighted_chunk_avx2(slice, offset + 3, 97) };
788                let scale = scalar.abs().max(1.0);
789                assert!(
790                    (scalar - avx2).abs() <= 64.0 * f64::EPSILON * scale,
791                    "offset={offset}, len={len}, scalar={scalar}, avx2={avx2}"
792                );
793            }
794        }
795    }
796
797    #[cfg(target_arch = "aarch64")]
798    #[test]
799    fn neon_weighted_sum_matches_forced_scalar() {
800        let mut data = [0.0_f64; 80];
801        for (index, value) in data.iter_mut().enumerate() {
802            *value = (index as f64 * 0.37).sin() * (1.0 + index as f64);
803        }
804        for offset in 0..8 {
805            for len in 0..=64 {
806                let slice = &data[offset..offset + len];
807                let scalar = process_simpson_weighted_chunk_scalar(slice, offset + 3, 97);
808                let neon = process_simpson_weighted_chunk_neon(slice, offset + 3, 97);
809                let scale = scalar.abs().max(1.0);
810                assert!((scalar - neon).abs() <= 64.0 * f64::EPSILON * scale);
811            }
812        }
813    }
814}