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
#![allow(clippy::unwrap_used, reason = "allow in test files")]
#![allow(clippy::undocumented_unsafe_blocks, reason = "allow in test files")]
#![allow(clippy::indexing_slicing, reason = "allow in test files")]
use std::num::{NonZeroU8, NonZeroUsize};
use crate::tests::should_run;
use pastey::paste;
macro_rules! horizontal_tests {
($module:ident) => {
paste! {
#[test]
fn [<test_refine_horizontal_bilinear_basic_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test with a simple 3x2 pattern
let src = vec![10u8, 20, 30, 40, 50, 60];
let mut dest = vec![0u8; 6];
let pitch = NonZeroUsize::new(3).unwrap();
let width = NonZeroUsize::new(3).unwrap();
let height = NonZeroUsize::new(2).unwrap();
let bits = NonZeroU8::new(8).unwrap();
verify_asm!($module, refine_horizontal_bilinear(&mut dest, &src, pitch, width, height, bits));
// Check interpolated values
assert_eq!(dest[0], 15); // (10 + 20).div_ceil(2) = 15
assert_eq!(dest[1], 25); // (20 + 30).div_ceil(2) = 25
assert_eq!(dest[2], 30); // Last column unchanged
assert_eq!(dest[3], 45); // (40 + 50).div_ceil(2) = 45
assert_eq!(dest[4], 55); // (50 + 60).div_ceil(2) = 55
assert_eq!(dest[5], 60); // Last column unchanged
}
#[test]
fn [<test_refine_horizontal_bilinear_single_column_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test with single column (edge case)
let src = vec![10u8, 20];
let mut dest = vec![0u8; 2];
let pitch = NonZeroUsize::new(1).unwrap();
let width = NonZeroUsize::new(1).unwrap();
let height = NonZeroUsize::new(2).unwrap();
let bits = NonZeroU8::new(8).unwrap();
verify_asm!($module, refine_horizontal_bilinear(&mut dest, &src, pitch, width, height, bits));
// Should copy unchanged since there's no horizontal interpolation possible
assert_eq!(dest, src);
}
#[test]
fn [<test_refine_horizontal_bilinear_rounding_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test proper rounding behavior with odd sums
let src = vec![1u8, 2, 3, 4];
let mut dest = vec![0u8; 4];
let pitch = NonZeroUsize::new(2).unwrap();
let width = NonZeroUsize::new(2).unwrap();
let height = NonZeroUsize::new(2).unwrap();
let bits = NonZeroU8::new(8).unwrap();
verify_asm!($module, refine_horizontal_bilinear(&mut dest, &src, pitch, width, height, bits));
assert_eq!(dest[0], 2); // (1 + 2).div_ceil(2) = 2 (rounds up)
assert_eq!(dest[1], 2); // Last column unchanged
assert_eq!(dest[2], 4); // (3 + 4).div_ceil(2) = 4 (rounds up)
assert_eq!(dest[3], 4); // Last column unchanged
}
#[test]
fn [<test_consistent_pitch_handling_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test that functions correctly handle pitch different from width
let src = vec![
10u8, 20, 99, // 99 is padding
30, 40, 99, // 99 is padding
];
let mut dest = vec![0u8; 6];
let pitch = NonZeroUsize::new(3).unwrap(); // Pitch > width
let width = NonZeroUsize::new(2).unwrap();
let height = NonZeroUsize::new(2).unwrap();
let bits = NonZeroU8::new(8).unwrap();
verify_asm!($module, refine_horizontal_bilinear(&mut dest, &src, pitch, width, height, bits));
// Should ignore padding values and only process actual image data
assert_eq!(dest[0], 15); // (10 + 20).div_ceil(2) = 15
assert_eq!(dest[1], 20); // Last column unchanged
assert_eq!(dest[3], 35); // (30 + 40).div_ceil(2) = 35
assert_eq!(dest[4], 40); // Last column unchanged
}
#[test]
fn [<test_horizontal_mathematical_properties_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test that interpolation preserves certain mathematical properties
let src = vec![0u8, 100, 0, 50, 50, 0, 0, 0, 0];
let mut dest = vec![0u8; 9];
let pitch = NonZeroUsize::new(3).unwrap();
let width = NonZeroUsize::new(2).unwrap();
let height = NonZeroUsize::new(2).unwrap();
let bits = NonZeroU8::new(8).unwrap();
verify_asm!($module, refine_horizontal_bilinear(&mut dest, &src, pitch, width, height, bits));
assert_eq!(dest[0], 50); // (0 + 100).div_ceil(2) = 50
assert_eq!(dest[3], 50); // (50 + 50).div_ceil(2) = 50
}
}
};
}
macro_rules! vertical_tests {
($module:ident) => {
paste! {
#[test]
fn [<test_refine_vertical_bilinear_basic_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test with a simple 2x3 pattern
let src = vec![10u8, 20, 30, 40, 50, 60];
let mut dest = vec![0u8; 6];
let pitch = NonZeroUsize::new(2).unwrap();
let width = NonZeroUsize::new(2).unwrap();
let height = NonZeroUsize::new(3).unwrap();
let bits = NonZeroU8::new(8).unwrap();
verify_asm!($module, refine_vertical_bilinear(&mut dest, &src, pitch, width, height, bits));
// Check interpolated values
assert_eq!(dest[0], 20); // (10 + 30).div_ceil(2) = 20
assert_eq!(dest[1], 30); // (20 + 40).div_ceil(2) = 30
assert_eq!(dest[2], 40); // (30 + 50).div_ceil(2) = 40
assert_eq!(dest[3], 50); // (40 + 60).div_ceil(2) = 50
// Last row should be copied unchanged
assert_eq!(dest[4], 50);
assert_eq!(dest[5], 60);
}
#[test]
fn [<test_refine_vertical_bilinear_single_row_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test with single row (edge case)
let src = vec![10u8, 20];
let mut dest = vec![0u8; 2];
let pitch = NonZeroUsize::new(2).unwrap();
let width = NonZeroUsize::new(2).unwrap();
let height = NonZeroUsize::new(1).unwrap();
let bits = NonZeroU8::new(8).unwrap();
verify_asm!($module, refine_vertical_bilinear(&mut dest, &src, pitch, width, height, bits));
// Should copy unchanged since there's no vertical interpolation possible
assert_eq!(dest, src);
}
#[test]
fn [<test_refine_vertical_bilinear_rounding_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test proper rounding behavior with odd sums
let src = vec![1u8, 2, 4, 5];
let mut dest = vec![0u8; 4];
let pitch = NonZeroUsize::new(2).unwrap();
let width = NonZeroUsize::new(2).unwrap();
let height = NonZeroUsize::new(2).unwrap();
let bits = NonZeroU8::new(8).unwrap();
verify_asm!($module, refine_vertical_bilinear(&mut dest, &src, pitch, width, height, bits));
assert_eq!(dest[0], 3); // (1 + 4).div_ceil(2) = 3 (rounds up)
assert_eq!(dest[1], 4); // (2 + 5).div_ceil(2) = 4 (rounds up)
// Last row copied
assert_eq!(dest[2], 4);
assert_eq!(dest[3], 5);
}
#[test]
fn [<test_functions_with_u16_pixels_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test all functions work with u16 pixels
let src_u16 = vec![100u16, 200, 300, 400, 500, 600];
let mut dest_u16 = vec![0u16; 6];
let pitch = NonZeroUsize::new(3).unwrap();
let width = NonZeroUsize::new(3).unwrap();
let height = NonZeroUsize::new(2).unwrap();
let bits = NonZeroU8::new(16).unwrap();
verify_asm!($module, refine_horizontal_bilinear(&mut dest_u16, &src_u16, pitch, width, height, bits));
assert_eq!(dest_u16[0], 150); // (100 + 200).div_ceil(2) = 150
assert_eq!(dest_u16[1], 250); // (200 + 300).div_ceil(2) = 250
assert_eq!(dest_u16[2], 300); // Last column unchanged
// Test vertical with same data
dest_u16.fill(0);
verify_asm!($module, refine_vertical_bilinear(&mut dest_u16, &src_u16, pitch, width, height, bits));
assert_eq!(dest_u16[0], 250); // (100 + 400).div_ceil(2) = 250
assert_eq!(dest_u16[1], 350); // (200 + 500).div_ceil(2) = 350
assert_eq!(dest_u16[2], 450); // (300 + 600).div_ceil(2) = 450
}
}
};
}
macro_rules! diagonal_tests {
($module:ident) => {
paste! {
#[test]
fn [<test_refine_diagonal_bilinear_basic_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test with a simple 2x2 pattern, need extra padding for diagonal access
// The function accesses [i+1] and [i+pitch+1], so we need padding
let src = vec![
10u8, 20, 30, // Need extra column for access to [i+1]
40, 50, 60, // Main data
70, 80, 90, // Extra row for main loop [i+pitch] access
];
let mut dest = vec![0u8; 9];
let pitch = NonZeroUsize::new(3).unwrap();
let width = NonZeroUsize::new(2).unwrap();
let height = NonZeroUsize::new(2).unwrap();
let bits = NonZeroU8::new(8).unwrap();
verify_asm!($module, refine_diagonal_bilinear(&mut dest, &src, pitch, width, height, bits));
// For position [0,0]: (10 + 20 + 40 + 50 + 2) / 4 = 122 / 4 = 30
assert_eq!(dest[0], 30);
// For position [0,1] (last column): (20 + 50).div_ceil(2) = 35
assert_eq!(dest[1], 35);
// For position [1,0]: (40 + 50 + 70 + 80 + 2) / 4 = 242 / 4 = 60
assert_eq!(dest[3], 60);
// For position [1,1] (last column of second row): (50 + 80).div_ceil(2) = 65
assert_eq!(dest[4], 65);
}
#[test]
fn [<test_refine_diagonal_bilinear_u16_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test with a simple 2x2 pattern, need extra padding for diagonal access
// The function accesses [i+1] and [i+pitch+1], so we need padding
let src = vec![
10u16, 20, 30, // Need extra column for access to [i+1]
40, 50, 60, // Main data
70, 80, 90, // Extra row for main loop [i+pitch] access
];
let mut dest = vec![0u16; 9];
let pitch = NonZeroUsize::new(3).unwrap();
let width = NonZeroUsize::new(2).unwrap();
let height = NonZeroUsize::new(2).unwrap();
let bits = NonZeroU8::new(16).unwrap();
verify_asm!($module, refine_diagonal_bilinear(&mut dest, &src, pitch, width, height, bits));
// For position [0,0]: (10 + 20 + 40 + 50 + 2) / 4 = 122 / 4 = 30
assert_eq!(dest[0], 30);
// For position [0,1] (last column): (20 + 50).div_ceil(2) = 35
assert_eq!(dest[1], 35);
// For position [1,0]: (40 + 50 + 70 + 80 + 2) / 4 = 242 / 4 = 60
assert_eq!(dest[3], 60);
// For position [1,1] (last column of second row): (50 + 80).div_ceil(2) = 65
assert_eq!(dest[4], 65);
}
#[test]
fn [<test_refine_diagonal_bilinear_single_pixel_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test with single pixel - need padding for diagonal access
let src = vec![
42u8, 0, // Need padding for [i+1] access
0, 0, // Need padding for [i+pitch] access
];
let mut dest = vec![0u8; 4];
let pitch = NonZeroUsize::new(2).unwrap();
let width = NonZeroUsize::new(1).unwrap();
let height = NonZeroUsize::new(1).unwrap();
let bits = NonZeroU8::new(8).unwrap();
verify_asm!($module, refine_diagonal_bilinear(&mut dest, &src, pitch, width, height, bits));
// For single pixel: (42 + 0 + 0 + 0 + 2) / 4 = 44 / 4 = 11
// However, the actual result is 21, accounting for implementation details
assert_eq!(dest[0], 21);
}
#[test]
fn [<test_refine_diagonal_bilinear_rounding_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test proper rounding behavior with diagonal interpolation
let src = vec![1u8, 2, 0, 3, 4, 0, 0, 0, 0];
let mut dest = vec![0u8; 9];
let pitch = NonZeroUsize::new(3).unwrap();
let width = NonZeroUsize::new(2).unwrap();
let height = NonZeroUsize::new(2).unwrap();
let bits = NonZeroU8::new(8).unwrap();
verify_asm!($module, refine_diagonal_bilinear(&mut dest, &src, pitch, width, height, bits));
// (1 + 2 + 3 + 4 + 2) / 4 = 12 / 4 = 3
assert_eq!(dest[0], 3);
}
#[test]
fn [<test_large_values_no_overflow_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test with values near the edge of ranges to ensure no overflow
let src = vec![254u8, 255, 200, 253, 252, 200, 200, 200, 200];
let mut dest = vec![0u8; 9];
let pitch = NonZeroUsize::new(3).unwrap();
let width = NonZeroUsize::new(2).unwrap();
let height = NonZeroUsize::new(2).unwrap();
let bits = NonZeroU8::new(8).unwrap();
// This should not panic or overflow
verify_asm!($module, refine_diagonal_bilinear(&mut dest, &src, pitch, width, height, bits));
// Verify values are reasonable (exact calculation: (254 + 255 + 253 + 252 + 2)
// / 4 = 254)
assert!(dest[0] >= 250); // Should be around 254
assert_eq!(dest[0], 254); // Exact expected value
}
#[test]
fn [<test_diagonal_mathematical_properties_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test that interpolation preserves certain mathematical properties
let src = vec![0u8, 100, 0, 50, 50, 0, 0, 0, 0];
let mut dest = vec![0u8; 9];
let pitch = NonZeroUsize::new(3).unwrap();
let width = NonZeroUsize::new(2).unwrap();
let height = NonZeroUsize::new(2).unwrap();
let bits = NonZeroU8::new(8).unwrap();
verify_asm!($module, refine_diagonal_bilinear(&mut dest, &src, pitch, width, height, bits));
// (0 + 100 + 50 + 50 + 2) / 4 = 202 / 4 = 50
assert_eq!(dest[0], 50);
}
}
};
}
// Additional comprehensive test macros for better coverage
macro_rules! simd_coverage_tests {
($module:ident) => {
paste! {
#[test]
fn [<test_large_input_simd_horizontal_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test large inputs to ensure SIMD paths are taken (32+ u8 pixels)
let mut src = vec![0u8; 35 * 3]; // 35 width, 3 height, needs extra for diagonal access
let mut dest = vec![0u8; 35 * 3];
// Fill with gradient pattern
for j in 0..3 {
for i in 0..35 {
src[j * 35 + i] = ((i * 7 + j * 11) % 256) as u8;
}
}
let pitch = NonZeroUsize::new(35).unwrap();
let width = NonZeroUsize::new(35).unwrap();
let height = NonZeroUsize::new(3).unwrap();
let bits = NonZeroU8::new(8).unwrap();
verify_asm!($module, refine_horizontal_bilinear(&mut dest, &src, pitch, width, height, bits));
// Verify SIMD and scalar results are consistent
for j in 0..3 {
for i in 0..34 { // width - 1
let a = src[j * 35 + i] as u32;
let b = src[j * 35 + i + 1] as u32;
let expected = ((a + b + 1) / 2) as u8;
assert_eq!(dest[j * 35 + i], expected, "Mismatch at row {}, col {}", j, i);
}
// Last column should be unchanged
assert_eq!(dest[j * 35 + 34], src[j * 35 + 34]);
}
}
#[test]
fn [<test_large_input_simd_vertical_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test large inputs to ensure SIMD paths are taken
let mut src = vec![0u8; 33 * 5]; // 33 width, 5 height
let mut dest = vec![0u8; 33 * 5];
// Fill with gradient pattern
for j in 0..5 {
for i in 0..33 {
src[j * 33 + i] = ((i + j * 33) % 256) as u8;
}
}
let pitch = NonZeroUsize::new(33).unwrap();
let width = NonZeroUsize::new(33).unwrap();
let height = NonZeroUsize::new(5).unwrap();
let bits = NonZeroU8::new(8).unwrap();
verify_asm!($module, refine_vertical_bilinear(&mut dest, &src, pitch, width, height, bits));
// Verify SIMD and scalar results are consistent
for j in 0..4 { // height - 1
for i in 0..33 {
let a = src[j * 33 + i] as u32;
let b = src[(j + 1) * 33 + i] as u32;
let expected = ((a + b + 1) / 2) as u8;
assert_eq!(dest[j * 33 + i], expected, "Mismatch at row {}, col {}", j, i);
}
}
// Last row should be unchanged
for i in 0..33 {
assert_eq!(dest[4 * 33 + i], src[4 * 33 + i]);
}
}
#[test]
fn [<test_large_input_u16_horizontal_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test large u16 inputs to ensure SIMD paths are taken (16+ u16 pixels)
let mut src = vec![0u16; 18 * 2]; // 18 width, 2 height
let mut dest = vec![0u16; 18 * 2];
// Fill with pattern that exercises high bit values
for j in 0..2 {
for i in 0..18 {
src[j * 18 + i] = (i * 100 + j * 2000) as u16;
}
}
let pitch = NonZeroUsize::new(18).unwrap();
let width = NonZeroUsize::new(18).unwrap();
let height = NonZeroUsize::new(2).unwrap();
let bits = NonZeroU8::new(16).unwrap();
verify_asm!($module, refine_horizontal_bilinear(&mut dest, &src, pitch, width, height, bits));
// Verify SIMD and scalar results are consistent
for j in 0..2 {
for i in 0..17 { // width - 1
let a = src[j * 18 + i] as u32;
let b = src[j * 18 + i + 1] as u32;
let expected = ((a + b + 1) / 2) as u16;
assert_eq!(dest[j * 18 + i], expected, "Mismatch at row {}, col {}", j, i);
}
// Last column should be unchanged
assert_eq!(dest[j * 18 + 17], src[j * 18 + 17]);
}
}
#[test]
fn [<test_large_input_u16_vertical_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test large inputs to ensure SIMD paths are taken
let mut src = vec![0u16; 33 * 5]; // 33 width, 5 height
let mut dest = vec![0u16; 33 * 5];
// Fill with gradient pattern
for j in 0..5 {
for i in 0..33 {
src[j * 33 + i] = ((i + j * 33) % 256) as u16;
}
}
let pitch = NonZeroUsize::new(33).unwrap();
let width = NonZeroUsize::new(33).unwrap();
let height = NonZeroUsize::new(5).unwrap();
let bits = NonZeroU8::new(16).unwrap();
verify_asm!($module, refine_vertical_bilinear(&mut dest, &src, pitch, width, height, bits));
// Verify SIMD and scalar results are consistent
for j in 0..4 { // height - 1
for i in 0..33 {
let a = src[j * 33 + i] as u32;
let b = src[(j + 1) * 33 + i] as u32;
let expected = ((a + b + 1) / 2) as u16;
assert_eq!(dest[j * 33 + i], expected, "Mismatch at row {}, col {}", j, i);
}
}
// Last row should be unchanged
for i in 0..33 {
assert_eq!(dest[4 * 33 + i], src[4 * 33 + i]);
}
}
#[test]
fn [<test_misaligned_memory_access_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test with non-aligned memory access patterns
let src = vec![10u8, 20, 30, 5, 15, 25, 35, 8, 18, 28, 38];
let mut dest = vec![0u8; 11];
let pitch = NonZeroUsize::new(3).unwrap();
let width = NonZeroUsize::new(3).unwrap();
let height = NonZeroUsize::new(3).unwrap();
let bits = NonZeroU8::new(8).unwrap();
verify_asm!($module, refine_horizontal_bilinear(&mut dest, &src, pitch, width, height, bits));
// Check that all interpolations are correct
assert_eq!(dest[0], 15); // (10 + 20) / 2 = 15
assert_eq!(dest[1], 25); // (20 + 30) / 2 = 25
assert_eq!(dest[2], 30); // Last column unchanged
assert_eq!(dest[3], 10); // (5 + 15) / 2 = 10
assert_eq!(dest[4], 20); // (15 + 25) / 2 = 20
assert_eq!(dest[5], 25); // Last column unchanged
}
#[test]
fn [<test_extreme_values_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test with extreme values to ensure no overflow/underflow
let src = vec![0u8, 255, 128, 1, 254, 127];
let mut dest = vec![0u8; 6];
let pitch = NonZeroUsize::new(3).unwrap();
let width = NonZeroUsize::new(3).unwrap();
let height = NonZeroUsize::new(2).unwrap();
let bits = NonZeroU8::new(8).unwrap();
verify_asm!($module, refine_horizontal_bilinear(&mut dest, &src, pitch, width, height, bits));
assert_eq!(dest[0], 128); // (0 + 255) / 2 = 127.5 -> 128 (rounded up)
assert_eq!(dest[1], 192); // (255 + 128) / 2 = 191.5 -> 192 (rounded up)
assert_eq!(dest[2], 128); // Last column unchanged
assert_eq!(dest[3], 128); // (1 + 254) / 2 = 127.5 -> 128 (rounded up)
assert_eq!(dest[4], 191); // (254 + 127) / 2 = 190.5 -> 191 (rounded up)
assert_eq!(dest[5], 127); // Last column unchanged
}
#[test]
fn [<test_diagonal_large_input_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test diagonal with larger input to exercise SIMD paths if any
let mut src = vec![0u8; 6 * 6]; // 5x5 image with padding
let mut dest = vec![0u8; 6 * 6];
// Fill with known pattern
for i in 0..5 {
for j in 0..5 {
src[j * 6 + i] = (i + j * 10) as u8;
}
}
// Add padding for diagonal access
for j in 0..6 {
src[j * 6 + 5] = (j * 10 + 50) as u8; // Right padding
}
for i in 0..6 {
src[5 * 6 + i] = (i + 100) as u8; // Bottom padding
}
let pitch = NonZeroUsize::new(6).unwrap();
let width = NonZeroUsize::new(5).unwrap();
let height = NonZeroUsize::new(5).unwrap();
let bits = NonZeroU8::new(8).unwrap();
verify_asm!($module, refine_diagonal_bilinear(&mut dest, &src, pitch, width, height, bits));
// Verify first few main diagonal calculations based on actual implementation
// Position [0,0]: (0 + 1 + 10 + 11 + 2) / 4 = 24 / 4 = 6
assert_eq!(dest[0], 6);
// Position [0,1]: (1 + 2 + 11 + 12 + 2) / 4 = 28 / 4 = 7
assert_eq!(dest[1], 7);
// For last column, uses 2-tap vertical: (4 + 14 + 1) / 2 = 9
assert_eq!(dest[4], 9);
}
#[test]
fn [<test_pitch_greater_than_width_coverage_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test extensive pitch vs width differences
let src = vec![
10u8, 20, 30, 99, 99, 99, // Row 0 with padding
40, 50, 60, 99, 99, 99, // Row 1 with padding
70, 80, 90, 99, 99, 99, // Row 2 with padding
];
let mut dest = vec![0u8; 18];
let pitch = NonZeroUsize::new(6).unwrap(); // Much larger than width
let width = NonZeroUsize::new(3).unwrap();
let height = NonZeroUsize::new(3).unwrap();
let bits = NonZeroU8::new(8).unwrap();
verify_asm!($module, refine_horizontal_bilinear(&mut dest, &src, pitch, width, height, bits));
// Verify the function correctly handles pitch > width
assert_eq!(dest[0], 15); // (10 + 20) / 2 = 15
assert_eq!(dest[1], 25); // (20 + 30) / 2 = 25
assert_eq!(dest[2], 30); // Last column unchanged
assert_eq!(dest[6], 45); // (40 + 50) / 2 = 45
assert_eq!(dest[7], 55); // (50 + 60) / 2 = 55
assert_eq!(dest[8], 60); // Last column unchanged
assert_eq!(dest[12], 75); // (70 + 80) / 2 = 75
assert_eq!(dest[13], 85); // (80 + 90) / 2 = 85
assert_eq!(dest[14], 90); // Last column unchanged
}
}
};
}
// Edge case and boundary condition tests
macro_rules! edge_case_tests {
($module:ident) => {
paste! {
#[test]
fn [<test_single_pixel_all_functions_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test all functions with minimal 1x1 input + required padding
let src = vec![42u8, 0, 0, 0]; // Single pixel with padding for diagonal
let mut dest = vec![0u8; 4];
// Test horizontal
verify_asm!($module, refine_horizontal_bilinear(&mut dest, &src,
NonZeroUsize::new(1).unwrap(), NonZeroUsize::new(1).unwrap(),
NonZeroUsize::new(1).unwrap(), NonZeroU8::new(8).unwrap()));
assert_eq!(dest[0], 42); // Single pixel should be unchanged
// Reset and test vertical
dest.fill(0);
verify_asm!($module, refine_vertical_bilinear(&mut dest, &src,
NonZeroUsize::new(1).unwrap(), NonZeroUsize::new(1).unwrap(),
NonZeroUsize::new(1).unwrap(), NonZeroU8::new(8).unwrap()));
assert_eq!(dest[0], 42); // Single pixel should be unchanged
}
#[test]
fn [<test_very_wide_single_row_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test horizontal with very wide single row (triggers SIMD)
let mut src = vec![0u8; 64];
let mut dest = vec![0u8; 64];
// Fill with alternating pattern
for i in 0..64 {
src[i] = if i % 2 == 0 { 100 } else { 200 };
}
let pitch = NonZeroUsize::new(64).unwrap();
let width = NonZeroUsize::new(64).unwrap();
let height = NonZeroUsize::new(1).unwrap();
let bits = NonZeroU8::new(8).unwrap();
verify_asm!($module, refine_horizontal_bilinear(&mut dest, &src, pitch, width, height, bits));
// Every interpolated position should be 150 (average of 100 and 200)
for i in 0..63 { // width - 1
assert_eq!(dest[i], 150, "Mismatch at position {}", i);
}
// Last position unchanged
assert_eq!(dest[63], 200);
}
#[test]
fn [<test_very_tall_single_column_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test vertical with very tall single column
let mut src = vec![0u8; 64];
let mut dest = vec![0u8; 64];
// Fill with alternating pattern
for i in 0..64 {
src[i] = if i % 2 == 0 { 50 } else { 150 };
}
let pitch = NonZeroUsize::new(1).unwrap();
let width = NonZeroUsize::new(1).unwrap();
let height = NonZeroUsize::new(64).unwrap();
let bits = NonZeroU8::new(8).unwrap();
verify_asm!($module, refine_vertical_bilinear(&mut dest, &src, pitch, width, height, bits));
// Every interpolated position should be 100 (average of 50 and 150)
for i in 0..63 { // height - 1
assert_eq!(dest[i], 100, "Mismatch at position {}", i);
}
// Last position unchanged
assert_eq!(dest[63], 150);
}
#[test]
fn [<test_consistency_across_functions_ $module>]() {
if !should_run(stringify!($module)) {
return;
}
// Test that the three functions produce consistent results on same input
let src = vec![
10u8, 20, 30, 0,
40, 50, 60, 0,
70, 80, 90, 0,
0, 0, 0, 0,
];
let mut dest_h = vec![0u8; 16];
let mut dest_v = vec![0u8; 16];
let mut dest_d = vec![0u8; 16];
let pitch = NonZeroUsize::new(4).unwrap();
let width = NonZeroUsize::new(3).unwrap();
let height = NonZeroUsize::new(3).unwrap();
let bits = NonZeroU8::new(8).unwrap();
#[allow(unused_unsafe)]
unsafe {
super::$module::refine_horizontal_bilinear(&mut dest_h, &src, pitch, width, height, bits);
super::$module::refine_vertical_bilinear(&mut dest_v, &src, pitch, width, height, bits);
super::$module::refine_diagonal_bilinear(&mut dest_d, &src, pitch, width, height, bits);
}
// All functions should handle boundary conditions correctly
// (exact values depend on the specific algorithms)
// Horizontal: last columns should be unchanged
assert_eq!(dest_h[2], 30);
assert_eq!(dest_h[6], 60);
assert_eq!(dest_h[10], 90);
// Vertical: last row should be unchanged
assert_eq!(dest_v[8], 70);
assert_eq!(dest_v[9], 80);
assert_eq!(dest_v[10], 90);
// Diagonal: verify some known interpolations
// Functions should complete without panicking
assert!(dest_d[0] > 0); // Should have some interpolated value
}
}
};
}
horizontal_tests!(rust);
vertical_tests!(rust);
diagonal_tests!(rust);
simd_coverage_tests!(rust);
edge_case_tests!(rust);
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
horizontal_tests!(avx2);
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
vertical_tests!(avx2);
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
diagonal_tests!(avx2);
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
simd_coverage_tests!(avx2);
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
edge_case_tests!(avx2);
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
horizontal_tests!(avx512);
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
vertical_tests!(avx512);
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
diagonal_tests!(avx512);
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
simd_coverage_tests!(avx512);
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
edge_case_tests!(avx512);