@@ -74,40 +74,38 @@ public static (bool Difference, Rectangle Bounds) DeDuplicatePixels<TPixel>(
7474 uint x = 0 ;
7575 int length = current . Length ;
7676 int remaining = current . Length ;
77- if ( Avx2 . IsSupported && remaining >= 2 )
77+
78+ while ( Avx2 . IsSupported && remaining >= 2 )
7879 {
79- while ( remaining >= 2 )
80+ Vector256 < float > p = Unsafe . Add ( ref previousBase , x ) ;
81+ Vector256 < float > c = Unsafe . Add ( ref currentBase , x ) ;
82+
83+ // Compare the previous and current pixels
84+ Vector256 < int > mask = Avx2 . CompareEqual ( p . AsInt32 ( ) , c . AsInt32 ( ) ) ;
85+ mask = Avx2 . CompareEqual ( mask . AsInt64 ( ) , Vector256 < long > . AllBitsSet ) . AsInt32 ( ) ;
86+ mask = Avx2 . And ( mask , Avx2 . Shuffle ( mask , 0b_01_00_11_10 ) ) . AsInt32 ( ) ;
87+
88+ Vector256 < int > neq = Avx2 . Xor ( mask . AsInt64 ( ) , Vector256 < long > . AllBitsSet ) . AsInt32 ( ) ;
89+ int m = Avx2 . MoveMask ( neq . AsByte ( ) ) ;
90+ if ( m != 0 )
8091 {
81- Vector256 < float > p = Unsafe . Add ( ref previousBase , x ) ;
82- Vector256 < float > c = Unsafe . Add ( ref currentBase , x ) ;
83-
84- // Compare the previous and current pixels
85- Vector256 < int > mask = Avx2 . CompareEqual ( p . AsInt32 ( ) , c . AsInt32 ( ) ) ;
86- mask = Avx2 . CompareEqual ( mask . AsInt64 ( ) , Vector256 < long > . AllBitsSet ) . AsInt32 ( ) ;
87- mask = Avx2 . And ( mask , Avx2 . Shuffle ( mask , 0b_01_00_11_10 ) ) . AsInt32 ( ) ;
88-
89- Vector256 < int > neq = Avx2 . Xor ( mask . AsInt64 ( ) , Vector256 < long > . AllBitsSet ) . AsInt32 ( ) ;
90- int m = Avx2 . MoveMask ( neq . AsByte ( ) ) ;
91- if ( m != 0 )
92- {
93- // If is diff is found, the left side is marked by the min of previously found left side and the start position.
94- // The right is the max of the previously found right side and the end position.
95- int start = i + ( BitOperations . TrailingZeroCount ( m ) / size ) ;
96- int end = i + ( 2 - ( BitOperations . LeadingZeroCount ( ( uint ) m ) / size ) ) ;
97- left = Math . Min ( left , start ) ;
98- right = Math . Max ( right , end ) ;
99- hasRowDiff = true ;
100- hasDiff = true ;
101- }
102-
103- // Replace the pixel value with the replacement if the full pixel is matched.
104- Vector256 < float > r = Avx . BlendVariable ( c , replacement256 , mask . AsSingle ( ) ) ;
105- Unsafe . Add ( ref resultBase , x ) = r;
106-
107- x ++ ;
108- i += 2 ;
109- remaining -= 2 ;
92+ // If is diff is found, the left side is marked by the min of previously found left side and the start position.
93+ // The right is the max of the previously found right side and the end position.
94+ int start = i + ( BitOperations . TrailingZeroCount ( m ) / size ) ;
95+ int end = i + ( 2 - ( BitOperations . LeadingZeroCount ( ( uint ) m ) / size ) ) ;
96+ left = Math . Min ( left , start ) ;
97+ right = Math . Max ( right , end ) ;
98+ hasRowDiff = true ;
99+ hasDiff = true ;
110100 }
101+
102+ // Replace the pixel value with the replacement if the full pixel is matched.
103+ Vector256 < float > r = Avx . BlendVariable ( c , replacement256 , mask . AsSingle ( ) ) ;
104+ Unsafe . Add ( ref resultBase , x ) = r;
105+
106+ x ++ ;
107+ i += 2 ;
108+ remaining -= 2 ;
111109 }
112110
113111 for ( i = remaining ; i > 0 ; i -- )
0 commit comments