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
#![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::NonZeroUsize;
use pastey::paste;
macro_rules! create_tests {
($module:ident) => {
paste! {
#[test]
fn [<test_reduce_triangle_u8_2x2_ $module>]() {
// Test basic 2x2 -> 1x1 reduction
let src = vec![
10u8, 20, // first row
30, 40, // second row
];
// Destination buffer needs to be large enough for intermediate results (width*2
// wide)
let mut dest = vec![0u8; 2]; // width=1, but intermediate needs width*2=2
let src_pitch = NonZeroUsize::new(2).unwrap();
let dest_pitch = NonZeroUsize::new(2).unwrap(); // Pitch must accommodate intermediate width
let dest_width = NonZeroUsize::new(1).unwrap();
let dest_height = NonZeroUsize::new(1).unwrap();
verify_asm!($module, reduce_triangle(
&mut dest,
&src,
dest_pitch,
src_pitch,
dest_width,
dest_height,
));
// Triangle filter is separable:
// 1. Vertical: intermediate results at width*2 = 2 First column: (10 +
// 30).div_ceil(2) = 20 Second column: (20 + 40).div_ceil(2) = 30 So
// intermediate = [20, 30]
// 2. Horizontal: reduce from width*2 to width Result at [0]: (20 +
// 30).div_ceil(2) = 25
assert_eq!(dest[0], 25);
}
#[test]
fn [<test_reduce_triangle_u8_4x2_ $module>]() {
// Test 4x2 -> 2x1 reduction
let src = vec![
10u8, 20, 30, 40, // first row
50, 60, 70, 80, // second row
];
// Destination buffer needs to accommodate intermediate width of 4
// (2*dest_width)
let mut dest = vec![0u8; 4];
let src_pitch = NonZeroUsize::new(4).unwrap();
let dest_pitch = NonZeroUsize::new(4).unwrap(); // Must accommodate intermediate width
let dest_width = NonZeroUsize::new(2).unwrap();
let dest_height = NonZeroUsize::new(1).unwrap();
verify_asm!($module, reduce_triangle(
&mut dest,
&src,
dest_pitch,
src_pitch,
dest_width,
dest_height,
));
// Triangle filter is separable:
// 1. Vertical: intermediate results at width*2 = 4 [0]: (10 + 50).div_ceil(2) =
// 30 [1]: (20 + 60).div_ceil(2) = 40 [2]: (30 + 70).div_ceil(2) = 50 [3]:
// (40 + 80).div_ceil(2) = 60 So intermediate = [30, 40, 50, 60]
// 2. Horizontal: reduce from width*2=4 to width=2 dest[0]: (30 +
// 40).div_ceil(2) = 35 (note: first element is computed last) dest[1]: (40 +
// 50*2 + 60 + 2) / 4 = 202/4 = 50
assert_eq!(dest[0], 35);
assert_eq!(dest[1], 50);
}
#[test]
fn [<test_reduce_triangle_u8_4x4_ $module>]() {
// Test 4x4 -> 2x2 reduction
let src = vec![
10u8, 20, 30, 40, // first row
50, 60, 70, 80, // second row
90, 100, 110, 120, // third row
130, 140, 150, 160, // fourth row
];
// Destination buffer needs to accommodate intermediate width of 4
// (2*dest_width) and height of 2
let mut dest = vec![0u8; 8]; // 4 width * 2 height
let src_pitch = NonZeroUsize::new(4).unwrap();
let dest_pitch = NonZeroUsize::new(4).unwrap(); // Must accommodate intermediate width
let dest_width = NonZeroUsize::new(2).unwrap();
let dest_height = NonZeroUsize::new(2).unwrap();
verify_asm!($module, reduce_triangle(
&mut dest,
&src,
dest_pitch,
src_pitch,
dest_width,
dest_height,
));
// Triangle filter is separable:
// 1. Vertical reduction with averaging for first row, then 1/4, 1/2, 1/4
// filter: Row 0: [0]: (10 + 50).div_ceil(2) = 30, [1]: (20 + 60).div_ceil(2)
// = 40, [2]: (30 + 70).div_ceil(2) = 50, [3]: (40 + 80).div_ceil(2) = 60 Row
// 1: [0]: (50 + 90*2 + 130 + 2) / 4 = 362/4 = 90, [1]: (60 + 100*2 + 140 +
// 2) / 4 = 402/4 = 100, [2]: (70 + 110*2 + 150 + 2) / 4 = 442/4 = 110, [3]:
// (80 + 120*2 + 160 + 2) / 4 = 482/4 = 120 So intermediate after vertical =
// [[30, 40, 50, 60], [90, 100, 110, 120]]
// 2. Horizontal reduction: Row 0: dest[0]: (30 + 40).div_ceil(2) = 35, dest[1]:
// (40 + 50*2 + 60 + 2) / 4 = 202/4 = 50 Row 1: dest[4]: (90 +
// 100).div_ceil(2) = 95, dest[5]: (100 + 110*2 + 120 + 2) / 4 = 442/4 = 110
assert_eq!(dest[0], 35); // Top-left
assert_eq!(dest[1], 50); // Top-right
assert_eq!(dest[4], 95); // Bottom-left
assert_eq!(dest[5], 110); // Bottom-right
}
#[test]
fn [<test_reduce_triangle_u8_6x4_ $module>]() {
// Test 6x4 -> 3x2 reduction with more complex filtering
let src = vec![
10u8, 20, 30, 40, 50, 60, // first row
70, 80, 90, 100, 110, 120, // second row
130, 140, 150, 160, 170, 180, // third row
190, 200, 210, 220, 230, 240, // fourth row
];
// Destination buffer needs to accommodate intermediate width of 6
// (2*dest_width=3) and height of 2
let mut dest = vec![0u8; 12]; // 6 width * 2 height
let src_pitch = NonZeroUsize::new(6).unwrap();
let dest_pitch = NonZeroUsize::new(6).unwrap(); // Must accommodate intermediate width
let dest_width = NonZeroUsize::new(3).unwrap();
let dest_height = NonZeroUsize::new(2).unwrap();
verify_asm!($module, reduce_triangle(
&mut dest,
&src,
dest_pitch,
src_pitch,
dest_width,
dest_height,
));
// Triangle filter first processes vertically, then horizontally
// The algorithm should handle the multi-tap filtering correctly
// Verifying that we get reasonable smoothed values
assert!(dest[0] > 10 && dest[0] < 200); // Should be smoothed values
assert!(dest[1] > 10 && dest[1] < 200);
assert!(dest[2] > 10 && dest[2] < 200);
assert!(dest[6] > 10 && dest[6] < 240); // Second row
assert!(dest[7] > 10 && dest[7] < 240);
assert!(dest[8] > 10 && dest[8] < 240);
}
#[test]
fn [<test_reduce_triangle_u8_with_padding_ $module>]() {
// Test with source pitch > width (includes padding)
let src = vec![
10u8, 20, 255, 255, // first row (last 2 are padding)
30, 40, 255, 255, // second row (last 2 are padding)
];
// Destination buffer needs to accommodate intermediate width of 2
// (2*dest_width=1)
let mut dest = vec![0u8; 4]; // 2 intermediate width, plus padding for dest_pitch=4
let src_pitch = NonZeroUsize::new(4).unwrap();
let dest_pitch = NonZeroUsize::new(4).unwrap(); // Must accommodate intermediate width + padding
let dest_width = NonZeroUsize::new(1).unwrap();
let dest_height = NonZeroUsize::new(1).unwrap();
verify_asm!($module, reduce_triangle(
&mut dest,
&src,
dest_pitch,
src_pitch,
dest_width,
dest_height,
));
// Should only process the first 2x2 block, ignoring padding
// Vertical: (10 + 30).div_ceil(2) = 20, (20 + 40).div_ceil(2) = 30
// Horizontal: (20 + 30).div_ceil(2) = 25
assert_eq!(dest[0], 25);
// Note: Other elements in dest are not guaranteed to remain 0 due to
// intermediate processing
}
#[test]
fn [<test_reduce_triangle_u16_basic_ $module>]() {
// Test with u16 values
let src = vec![
1000u16, 2000, // first row
3000, 4000, // second row
];
// Destination buffer needs to accommodate intermediate width of 2
// (2*dest_width=1)
let mut dest = vec![0u16; 2];
let src_pitch = NonZeroUsize::new(2).unwrap();
let dest_pitch = NonZeroUsize::new(2).unwrap(); // Must accommodate intermediate width
let dest_width = NonZeroUsize::new(1).unwrap();
let dest_height = NonZeroUsize::new(1).unwrap();
verify_asm!($module, reduce_triangle(
&mut dest,
&src,
dest_pitch,
src_pitch,
dest_width,
dest_height,
));
// Triangle filter:
// Vertical: (1000 + 3000).div_ceil(2) = 2000, (2000 + 4000).div_ceil(2) = 3000
// Horizontal: (2000 + 3000).div_ceil(2) = 2500
assert_eq!(dest[0], 2500);
}
#[test]
fn [<test_reduce_triangle_u16_large_values_ $module>]() {
// Test with larger u16 values near the upper range
let src = vec![
60000u16, 61000, // first row
62000, 63000, // second row
];
// Destination buffer needs to accommodate intermediate width of 2
// (2*dest_width=1)
let mut dest = vec![0u16; 2];
let src_pitch = NonZeroUsize::new(2).unwrap();
let dest_pitch = NonZeroUsize::new(2).unwrap(); // Must accommodate intermediate width
let dest_width = NonZeroUsize::new(1).unwrap();
let dest_height = NonZeroUsize::new(1).unwrap();
verify_asm!($module, reduce_triangle(
&mut dest,
&src,
dest_pitch,
src_pitch,
dest_width,
dest_height,
));
// Triangle filter:
// Vertical: (60000 + 62000).div_ceil(2) = 61000, (61000 + 63000).div_ceil(2) =
// 62000 Horizontal: (61000 + 62000).div_ceil(2) = 61500
assert_eq!(dest[0], 61500);
}
#[test]
fn [<test_reduce_triangle_u16_4x4_ $module>]() {
// Test 4x4 -> 2x2 reduction with u16
let src = vec![
1000u16, 2000, 3000, 4000, // first row
5000, 6000, 7000, 8000, // second row
9000, 10000, 11000, 12000, // third row
13000, 14000, 15000, 16000, // fourth row
];
// Destination buffer needs to accommodate intermediate width of 4
// (2*dest_width=2) and height of 2
let mut dest = vec![0u16; 8]; // 4 width * 2 height
let src_pitch = NonZeroUsize::new(4).unwrap();
let dest_pitch = NonZeroUsize::new(4).unwrap(); // Must accommodate intermediate width
let dest_width = NonZeroUsize::new(2).unwrap();
let dest_height = NonZeroUsize::new(2).unwrap();
verify_asm!($module, reduce_triangle(
&mut dest,
&src,
dest_pitch,
src_pitch,
dest_width,
dest_height,
));
// Similar to the u8 case but with larger values
// The triangle filter should produce smoothed results
assert!(dest[0] > 1000 && dest[0] < 16000);
assert!(dest[1] > 1000 && dest[1] < 16000);
assert!(dest[4] > 1000 && dest[4] < 16000);
assert!(dest[5] > 1000 && dest[5] < 16000);
// Values should increase from top-left to bottom-right due to input pattern
assert!(dest[0] < dest[1]);
assert!(dest[0] < dest[4]);
assert!(dest[4] < dest[5]);
}
#[test]
fn [<test_reduce_triangle_edge_case_2x2_ $module>]() {
// Test minimal 2x2 reduction (was incorrectly 1x2)
let src = vec![
100u8, 150, // first row
200u8, 250, // second row
];
// Destination buffer needs to accommodate intermediate width of 2
// (2*dest_width=1)
let mut dest = vec![0u8; 2];
let src_pitch = NonZeroUsize::new(2).unwrap();
let dest_pitch = NonZeroUsize::new(2).unwrap(); // Must accommodate intermediate width
let dest_width = NonZeroUsize::new(1).unwrap();
let dest_height = NonZeroUsize::new(1).unwrap();
verify_asm!($module, reduce_triangle(
&mut dest,
&src,
dest_pitch,
src_pitch,
dest_width,
dest_height,
));
// Vertical: (100 + 200).div_ceil(2) = 150, (150 + 250).div_ceil(2) = 200
// Horizontal: (150 + 200).div_ceil(2) = 175
assert_eq!(dest[0], 175);
}
#[test]
fn [<test_reduce_triangle_uniform_values_ $module>]() {
// Test with uniform input - should preserve the value
let src = vec![
128u8, 128, 128, 128, // first row
128, 128, 128, 128, // second row
128, 128, 128, 128, // third row
128, 128, 128, 128, // fourth row
];
// Destination buffer needs to accommodate intermediate width of 4
// (2*dest_width=2) and height of 2
let mut dest = vec![0u8; 8]; // 4 width * 2 height
let src_pitch = NonZeroUsize::new(4).unwrap();
let dest_pitch = NonZeroUsize::new(4).unwrap(); // Must accommodate intermediate width
let dest_width = NonZeroUsize::new(2).unwrap();
let dest_height = NonZeroUsize::new(2).unwrap();
verify_asm!($module, reduce_triangle(
&mut dest,
&src,
dest_pitch,
src_pitch,
dest_width,
dest_height,
));
// Uniform values should remain uniform after filtering
assert_eq!(dest[0], 128);
assert_eq!(dest[1], 128);
assert_eq!(dest[4], 128);
assert_eq!(dest[5], 128);
}
#[test]
fn [<test_reduce_triangle_large_height_ $module>]() {
// Test 4x6 -> 2x3 reduction to exercise larger heights
let src = vec![
10u8, 20, 30, 40, // row 0
50, 60, 70, 80, // row 1
90, 100, 110, 120, // row 2
130, 140, 150, 160, // row 3
170, 180, 190, 200, // row 4
210, 220, 230, 240, // row 5
];
// Destination buffer needs to accommodate intermediate width of 4
// (dest_width*2) and height of 3
let mut dest = vec![0u8; 12]; // 4 width * 3 height
let src_pitch = NonZeroUsize::new(4).unwrap();
let dest_pitch = NonZeroUsize::new(4).unwrap(); // Must accommodate intermediate width
let dest_width = NonZeroUsize::new(2).unwrap();
let dest_height = NonZeroUsize::new(3).unwrap();
verify_asm!($module, reduce_triangle(
&mut dest,
&src,
dest_pitch,
src_pitch,
dest_width,
dest_height,
));
// This test ensures triangle filter works correctly with larger heights
assert_ne!(dest[0], 0); // Should have been modified
assert_ne!(dest[4], 0); // Second row should have been modified
assert_ne!(dest[8], 0); // Third row should have been modified
}
#[test]
fn [<test_reduce_triangle_u8_large_simd_first_row_ $module>]() {
// Test large enough to trigger SIMD processing for u8 first row (64x2 -> 32x1)
// This ensures we cover the u8 SIMD loop at lines 91-113: while x + 32 <= width_usize
let mut src = Vec::new();
// First row: 64 pixels with values 0-63
for i in 0..64u8 {
src.push(i);
}
// Second row: 64 pixels with values 64-127
for i in 64..128u8 {
src.push(i);
}
// Destination buffer needs intermediate width of 64 (dest_width*2)
let mut dest = vec![0u8; 64];
let src_pitch = NonZeroUsize::new(64).unwrap();
let dest_pitch = NonZeroUsize::new(64).unwrap(); // Must accommodate intermediate width
let dest_width = NonZeroUsize::new(32).unwrap();
let dest_height = NonZeroUsize::new(1).unwrap();
verify_asm!($module, reduce_triangle(
&mut dest,
&src,
dest_pitch,
src_pitch,
dest_width,
dest_height,
));
// Verify the SIMD processing results for u8 first row
// The SIMD loop should process x=0..31 in one iteration, then x=32..63 in another iteration
// since 0 + 32 <= 64 and 32 + 32 <= 64
// First row uses simple averaging: (a + b + 1) / 2
for i in 0..64usize {
// After vertical reduction, we get intermediate result for horizontal processing
// Since we only have height=1, this goes directly to horizontal processing
// The exact final result depends on the horizontal processing algorithm
assert!(dest[i] < 255,
"SIMD processing should produce valid u8 values at position {}: {}", i, dest[i]);
}
// Verify the SIMD code path was exercised by checking consistent results
// Note: The triangle filter uses overlapping horizontal processing, so strict ordering isn't guaranteed
// Instead, we verify that all values are reasonable for our input pattern
for i in 0..64usize {
assert!(dest[i] >= 10 && dest[i] <= 200,
"Triangle filter should produce reasonable values at position {}: {}", i, dest[i]);
}
// Verify specific known values from the simple averaging pattern
// For position 0: (0 + 64 + 1) / 2 = 32 (after vertical), then horizontal processing
// For position 31: (31 + 95 + 1) / 2 = 63 (after vertical), then horizontal processing
// The horizontal processing will modify these values, but they should be reasonable
assert!(dest[0] > 10 && dest[0] < 100, "First value should be reasonable: {}", dest[0]);
assert!(dest[31] > 40 && dest[31] < 150, "Middle value should be reasonable: {}", dest[31]);
assert!(dest[63] > 70 && dest[63] < 200, "Last value should be reasonable: {}", dest[63]);
// Verify that the SIMD processing covered the full width
// All positions should have been processed (non-zero for our input pattern)
for i in 0..64usize {
assert_ne!(dest[i], 0, "SIMD should have processed all positions including {}", i);
}
}
#[test]
fn [<test_reduce_triangle_u8_large_simd_middle_rows_ $module>]() {
// Test large enough to trigger SIMD processing for u8 middle rows (64x6 -> 32x3)
// This ensures we cover the u8 SIMD loop at lines 129-158: while x + 32 <= width_usize (for y >= 1)
// With dest_height=3, this gives y=1,2 (two iterations of the middle rows loop)
let mut src = Vec::new();
// Create 6 rows of 64 pixels each with a controlled pattern
// Keep values moderate to ensure triangle filter calculations don't overflow
for row in 0..6u8 {
for col in 0..64u8 {
src.push((row * 30 + col / 2) % 200); // Values 0-199
}
}
// Destination buffer needs intermediate width of 64 (dest_width*2) and height of 3
let mut dest = vec![0u8; 192]; // 64 width * 3 height
let src_pitch = NonZeroUsize::new(64).unwrap();
let dest_pitch = NonZeroUsize::new(64).unwrap(); // Must accommodate intermediate width
let dest_width = NonZeroUsize::new(32).unwrap();
let dest_height = NonZeroUsize::new(3).unwrap();
verify_asm!($module, reduce_triangle(
&mut dest,
&src,
dest_pitch,
src_pitch,
dest_width,
dest_height,
));
// Verify the triangle filter processing for middle rows
// Row 0 uses simple averaging: (a + b + 1) / 2
// Rows 1,2 use full triangle filter: (a + b * 2 + c + 2) / 4
// where a, b, c are from consecutive source rows
// The SIMD loop should process x=0..31 in the first iteration, then x=32..63 in the second iteration
// since 0 + 32 <= 64 and 32 + 32 <= 64
// Verify that all rows have been processed
for y in 0..3usize {
for x in 0..64usize {
let dest_idx = y * 64 + x; // Using dest_pitch=64 for intermediate buffer
// Verify the output is reasonable (SIMD should have processed this)
assert!(dest[dest_idx] < 255,
"SIMD processing should produce valid u8 values at row {} position {}: {}",
y, x, dest[dest_idx]);
}
}
// Specifically verify the middle rows (y=1,2) which use the full triangle filter
for y in 1..3usize {
let row_start = y * 64; // y * dest_pitch=64
// Test SIMD-processed pixels across the full width
for &x in &[0, 16, 32, 48, 63] {
let middle_value = dest[row_start + x];
// The triangle filter should produce reasonable values
assert!(middle_value < 220,
"Middle row triangle filter should produce reasonable values at row {} position {}: {}",
y, x, middle_value);
}
}
// Verify the SIMD code path was exercised by checking value consistency
// The triangle filter should produce smoothed values
let first_row_sample = dest[0]; // y=0, x=0 (simple averaging)
let middle_row1_sample = dest[64]; // y=1, x=0 (triangle filter)
let middle_row2_sample = dest[128]; // y=2, x=0 (triangle filter)
// All should be valid u8 values
assert!(first_row_sample < 255 && middle_row1_sample < 255 && middle_row2_sample < 255,
"All processed values should be valid u8");
// Test a few more samples to ensure the SIMD loop processed the full width
for &sample_x in &[0, 16, 32, 48, 63] {
for y in 1..3usize {
let sample_value = dest[y * 64 + sample_x]; // Middle rows, various positions
assert_ne!(sample_value, 0, "SIMD should have processed row {} position {}", y, sample_x);
assert!(sample_value < 255, "SIMD result should be valid u8 at row {} position {}", y, sample_x);
}
}
// Verify the triangle filter produces different results from simple averaging
// Row 0 (simple averaging) vs Row 1 (triangle filter) should potentially differ
// due to the different algorithms, though this isn't guaranteed for all inputs
let simple_avg_sample = dest[32]; // Row 0, middle position
let triangle_sample = dest[64 + 32]; // Row 1, same position
// Both should be reasonable values (the exact relationship depends on input pattern)
assert!(simple_avg_sample < 220 && triangle_sample < 220,
"Both averaging methods should produce reasonable results: {} vs {}",
simple_avg_sample, triangle_sample);
}
#[test]
fn [<test_reduce_triangle_u16_large_simd_first_row_ $module>]() {
// Test large enough to trigger SIMD processing for u16 first row (32x2 -> 16x1)
// This ensures we cover the u16 SIMD loop at lines 239-261: while x + 16 <= width_usize
let mut src = Vec::new();
// First row: 32 pixels with values 0-31 scaled to u16 range
for i in 0..32u16 {
src.push(i * 1000); // Values 0, 1000, 2000, ..., 31000
}
// Second row: 32 pixels with values 32-63 scaled to u16 range
for i in 32..64u16 {
src.push(i * 1000); // Values 32000, 33000, ..., 63000
}
// Destination buffer needs intermediate width of 32 (dest_width*2)
let mut dest = vec![0u16; 32];
let src_pitch = NonZeroUsize::new(32).unwrap();
let dest_pitch = NonZeroUsize::new(32).unwrap(); // Must accommodate intermediate width
let dest_width = NonZeroUsize::new(16).unwrap();
let dest_height = NonZeroUsize::new(1).unwrap();
verify_asm!($module, reduce_triangle(
&mut dest,
&src,
dest_pitch,
src_pitch,
dest_width,
dest_height,
));
// Verify the SIMD processing results for u16 first row
// The SIMD loop should process x=0..15 in one iteration, then x=16..31 in another iteration
// since 0 + 16 <= 32 and 16 + 16 <= 32
// First row uses simple averaging: (a + b + 1) / 2
for i in 0..32usize {
// After vertical reduction, we get intermediate result for horizontal processing
// Since we only have height=1, this goes directly to horizontal processing
// The exact final result depends on the horizontal processing algorithm
assert!(dest[i] < 65535,
"SIMD processing should produce valid u16 values at position {}: {}", i, dest[i]);
}
// Verify the SIMD code path was exercised by checking consistent results
// Note: The triangle filter uses overlapping horizontal processing, so strict ordering isn't guaranteed
// Instead, we verify that all values are reasonable for our input pattern
for i in 0..32usize {
assert!(dest[i] >= 5000 && dest[i] <= 60000,
"Triangle filter should produce reasonable values at position {}: {}", i, dest[i]);
}
// Verify specific known values from the simple averaging pattern
// For position 0: (0 + 32000 + 1) / 2 = 16000 (after vertical), then horizontal processing
// For position 15: (15000 + 47000 + 1) / 2 = 31000 (after vertical), then horizontal processing
// The horizontal processing will modify these values, but they should be reasonable
assert!(dest[0] > 8000 && dest[0] < 35000, "First value should be reasonable: {}", dest[0]);
assert!(dest[15] > 20000 && dest[15] < 50000, "Middle value should be reasonable: {}", dest[15]);
assert!(dest[31] > 35000 && dest[31] < 60000, "Last value should be reasonable: {}", dest[31]);
// Verify that the SIMD processing covered the full width
// All positions should have been processed (non-zero for our input pattern)
for i in 0..32usize {
assert_ne!(dest[i], 0, "SIMD should have processed all positions including {}", i);
}
// Verify the values show the expected general trend from our input pattern
// The first few and last few values should reflect the input gradient
assert!(dest[0] < dest[31], "Overall trend should be maintained: first={}, last={}", dest[0], dest[31]);
}
#[test]
fn [<test_reduce_triangle_u16_large_simd_middle_rows_ $module>]() {
// Test large enough to trigger SIMD processing for u16 middle rows (32x6 -> 16x3)
// This ensures we cover the u16 SIMD loop at lines 277-306: while x + 16 <= width_usize (for y >= 1)
// With dest_height=3, this gives y=1,2 (two iterations of the middle rows loop)
let mut src = Vec::new();
// Create 6 rows of 32 pixels each with a controlled pattern
// Keep values moderate to ensure triangle filter calculations don't overflow
for row in 0..6u16 {
for col in 0..32u16 {
src.push((row * 2000 + col * 100) % 40000); // Values 0-39999
}
}
// Destination buffer needs intermediate width of 32 (dest_width*2) and height of 3
let mut dest = vec![0u16; 96]; // 32 width * 3 height
let src_pitch = NonZeroUsize::new(32).unwrap();
let dest_pitch = NonZeroUsize::new(32).unwrap(); // Must accommodate intermediate width
let dest_width = NonZeroUsize::new(16).unwrap();
let dest_height = NonZeroUsize::new(3).unwrap();
verify_asm!($module, reduce_triangle(
&mut dest,
&src,
dest_pitch,
src_pitch,
dest_width,
dest_height,
));
// Verify the triangle filter processing for middle rows
// Row 0 uses simple averaging: (a + b + 1) / 2
// Rows 1,2 use full triangle filter: (a + b * 2 + c + 2) / 4
// where a, b, c are from consecutive source rows
// The SIMD loop should process x=0..15 in the first iteration, then x=16..31 in the second iteration
// since 0 + 16 <= 32 and 16 + 16 <= 32
// Verify that all rows have been processed
for y in 0..3usize {
for x in 0..32usize {
let dest_idx = y * 32 + x; // Using dest_pitch=32 for intermediate buffer
// Verify the output is reasonable (SIMD should have processed this)
assert!(dest[dest_idx] < 65535,
"SIMD processing should produce valid u16 values at row {} position {}: {}",
y, x, dest[dest_idx]);
}
}
// Specifically verify the middle rows (y=1,2) which use the full triangle filter
for y in 1..3usize {
let row_start = y * 32; // y * dest_pitch=32
// Test SIMD-processed pixels across the full width
for &x in &[0, 8, 16, 24, 31] {
let middle_value = dest[row_start + x];
// The triangle filter should produce reasonable values for u16
assert!(middle_value < 45000,
"Middle row triangle filter should produce reasonable u16 values at row {} position {}: {}",
y, x, middle_value);
}
}
// Verify the SIMD code path was exercised by checking value consistency
// The triangle filter should produce smoothed values
let first_row_sample = dest[0]; // y=0, x=0 (simple averaging)
let middle_row1_sample = dest[32]; // y=1, x=0 (triangle filter)
let middle_row2_sample = dest[64]; // y=2, x=0 (triangle filter)
// All should be valid u16 values
assert!(first_row_sample < 65535 && middle_row1_sample < 65535 && middle_row2_sample < 65535,
"All processed values should be valid u16");
// Test a few more samples to ensure the SIMD loop processed the full width
for &sample_x in &[0, 8, 16, 24, 31] {
for y in 1..3usize {
let sample_value = dest[y * 32 + sample_x]; // Middle rows, various positions
assert_ne!(sample_value, 0, "SIMD should have processed row {} position {}", y, sample_x);
assert!(sample_value < 65535, "SIMD result should be valid u16 at row {} position {}", y, sample_x);
}
}
// Verify the triangle filter produces different results from simple averaging
// Row 0 (simple averaging) vs Row 1 (triangle filter) should potentially differ
// due to the different algorithms, though this isn't guaranteed for all inputs
let simple_avg_sample = dest[16]; // Row 0, middle position
let triangle_sample = dest[32 + 16]; // Row 1, same position
// Both should be reasonable values (the exact relationship depends on input pattern)
assert!(simple_avg_sample < 45000 && triangle_sample < 45000,
"Both averaging methods should produce reasonable u16 results: {} vs {}",
simple_avg_sample, triangle_sample);
// Verify that the SIMD processing covered both iterations (x=0..15 and x=16..31)
// Check samples from both SIMD iterations
let first_simd_sample = dest[32]; // Row 1, x=0 (first SIMD iteration)
let second_simd_sample = dest[32 + 16]; // Row 1, x=16 (second SIMD iteration)
assert_ne!(first_simd_sample, 0, "First SIMD iteration should have processed data");
assert_ne!(second_simd_sample, 0, "Second SIMD iteration should have processed data");
assert!(first_simd_sample < 45000 && second_simd_sample < 45000,
"Both SIMD iterations should produce reasonable results: {} vs {}",
first_simd_sample, second_simd_sample);
}
#[test]
fn [<test_reduce_triangle_u8_horizontal_simd_scalar_tail_ $module>]() {
// Width 18 hits one 16-pixel horizontal SIMD chunk plus scalar tail.
let mut src = Vec::new();
for col in 0..36u16 {
let value = ((col * 7 + 11) % 220) + 10;
src.push(u8::try_from(value).expect("value fits in u8"));
}
for col in 0..36u16 {
let value = ((col * 3 + 71) % 200) + 20;
src.push(u8::try_from(value).expect("value fits in u8"));
}
let mut dest = vec![0u8; 36];
let src_pitch = NonZeroUsize::new(36).unwrap();
let dest_pitch = NonZeroUsize::new(36).unwrap();
let dest_width = NonZeroUsize::new(18).unwrap();
let dest_height = NonZeroUsize::new(1).unwrap();
verify_asm!($module, reduce_triangle(
&mut dest,
&src,
dest_pitch,
src_pitch,
dest_width,
dest_height,
));
let mut intermediate = vec![0u32; 36];
for x in 0..36usize {
intermediate[x] = (u32::from(src[x]) + u32::from(src[36 + x]) + 1) / 2;
}
let mut expected = vec![0u8; 18];
expected[0] = ((intermediate[0] + intermediate[1] + 1) / 2) as u8;
for x in 1..18usize {
expected[x] = ((intermediate[x * 2 - 1]
+ intermediate[x * 2] * 2
+ intermediate[x * 2 + 1]
+ 2)
/ 4) as u8;
}
assert_eq!(&dest[..18], expected.as_slice());
}
#[test]
fn [<test_reduce_triangle_u16_horizontal_simd_scalar_tail_ $module>]() {
// Width 10 hits one 8-pixel horizontal SIMD chunk plus scalar tail.
let mut src = Vec::new();
for col in 0..20u32 {
src.push((col * 151 + 4_000) as u16);
}
for col in 0..20u32 {
src.push((col * 97 + 19_000) as u16);
}
let mut dest = vec![0u16; 20];
let src_pitch = NonZeroUsize::new(20).unwrap();
let dest_pitch = NonZeroUsize::new(20).unwrap();
let dest_width = NonZeroUsize::new(10).unwrap();
let dest_height = NonZeroUsize::new(1).unwrap();
verify_asm!($module, reduce_triangle(
&mut dest,
&src,
dest_pitch,
src_pitch,
dest_width,
dest_height,
));
let mut intermediate = vec![0u32; 20];
for x in 0..20usize {
intermediate[x] = (u32::from(src[x]) + u32::from(src[20 + x]) + 1) / 2;
}
let mut expected = vec![0u16; 10];
expected[0] = ((intermediate[0] + intermediate[1] + 1) / 2) as u16;
for x in 1..10usize {
expected[x] = ((intermediate[x * 2 - 1]
+ intermediate[x * 2] * 2
+ intermediate[x * 2 + 1]
+ 2)
/ 4) as u16;
}
assert_eq!(&dest[..10], expected.as_slice());
}
}
};
}
create_tests!(rust);
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
create_tests!(avx2);