1use crate::NQuin;
8
9#[cfg(target_arch = "x86_64")]
10use std::arch::x86_64::_MM_HINT_T0;
11
12#[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 let page_aligned_offset = exact_byte_offset & !0xFFF;
30
31 let remainder = (exact_byte_offset - page_aligned_offset) as u16;
34
35 (page_aligned_offset, remainder)
36}
37
38#[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#[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#[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
74pub struct ContinuousGrid<'a> {
81 data: &'a [f64],
82}
83
84impl<'a> ContinuousGrid<'a> {
85 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 if raw_slice.as_ptr() as usize % 8 != 0 {
102 return Err(AlignmentError::MisalignedPointer);
103 }
104
105 if byte_len % 8 != 0 {
107 return Err(AlignmentError::MisalignedLength);
108 }
109
110 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 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 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 pub fn len(&self) -> usize {
146 self.data.len()
147 }
148
149 pub fn is_empty(&self) -> bool {
151 self.data.is_empty()
152 }
153
154 pub fn as_slice(&self) -> &[f64] {
156 self.data
157 }
158}
159
160#[derive(Debug, Clone, Copy, PartialEq, Eq)]
163pub enum SimdWidth {
164 Scalar,
165 Neon2, Avx2, }
168
169pub 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
191pub fn detect_cache_line_size() -> usize {
193 #[cfg(target_arch = "x86_64")]
194 {
195 #[cfg(target_arch = "x86_64")]
196 {
197 64
199 }
200 }
201
202 #[cfg(target_arch = "aarch64")]
203 {
204 64 }
206
207 #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
208 {
209 64 }
211}
212
213pub 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 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
245pub 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 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
271pub 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
288fn calculate_optimal_chunk_size() -> usize {
291 let simd_width = detect_simd_width();
292 let cache_line_size = detect_cache_line_size();
293
294 let base = match simd_width {
296 SimdWidth::Scalar => 1,
297 SimdWidth::Neon2 => 2,
298 SimdWidth::Avx2 => 4,
299 };
300
301 let f64_per_cache_line = cache_line_size / 8;
303
304 let target = f64_per_cache_line * 2;
306
307 ((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
347fn 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 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#[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#[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 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 for i in (simd_chunks * 2)..len {
448 sum += simpson_weight(global_start + i, total_len) * chunk[i];
449 }
450
451 sum
452}
453
454fn 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
471fn 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; }
485
486 #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
487 {
488 core::hint::black_box(data);
490 }
491}
492
493#[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 #[repr(C, align(4096))]
524 struct TestBuffer {
525 data: [u8; 8192], }
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 let (aligned, remainder) = resolve_aligned_byte_offset(0);
537 assert_eq!(aligned, 0);
538 assert_eq!(remainder, 0);
539
540 let (aligned, remainder) = resolve_aligned_byte_offset(512);
542 assert_eq!(aligned, 4096);
543 assert_eq!(remainder, 0);
544
545 let (aligned, remainder) = resolve_aligned_byte_offset(513);
547 assert_eq!(aligned, 4096);
548 assert_eq!(remainder, 8);
549
550 let (aligned, remainder) = resolve_aligned_byte_offset(1023);
552 assert_eq!(aligned, 4096);
553 assert_eq!(remainder, 4088);
554
555 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 let mut quin = NQuin::default();
575 quin.object = 1024; quin.metadata = f64::to_bits(42.5); 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; let (grid, offset) = ContinuousGrid::resume_from_quin(raw_bytes, &quin).unwrap();
599 assert_eq!(offset, 64);
600 assert_eq!(grid.len(), 92); }
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; 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 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 assert!(size == 32 || size == 64 || size == 128);
632 }
633
634 #[test]
635 fn test_kahan_summation() {
636 let mut data = [0.0f64; 1001];
638 for i in 0..1001 {
639 data[i] = 1e-10; }
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 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}