@@ -691,11 +691,14 @@ public void Clear()
691691
692692 for ( int i = 0 ; i < height ; i ++ )
693693 {
694- ref T r0 = ref DangerousGetReferenceAt ( i , 0 ) ;
694+ ref T rStart = ref DangerousGetReferenceAt ( i , 0 ) ;
695+ ref T rEnd = ref Unsafe . Add ( ref rStart , width ) ;
695696
696- for ( nint j = 0 ; j < width ; j += 1 )
697+ while ( Unsafe . IsAddressLessThan ( ref rStart , ref rEnd ) )
697698 {
698- Unsafe . Add ( ref r0 , j ) = default ! ;
699+ rStart = default ! ;
700+
701+ rStart = ref Unsafe . Add ( ref rStart , 1 ) ;
699702 }
700703 }
701704#endif
@@ -738,15 +741,18 @@ public void CopyTo(Span<T> destination)
738741 nint width = ( nint ) ( uint ) this . width ;
739742
740743 ref T destinationRef = ref MemoryMarshal . GetReference ( destination ) ;
741- nint offset = 0 ;
742744
743745 for ( int i = 0 ; i < height ; i ++ )
744746 {
745- ref T sourceRef = ref DangerousGetReferenceAt ( i , 0 ) ;
747+ ref T sourceStart = ref DangerousGetReferenceAt ( i , 0 ) ;
748+ ref T sourceEnd = ref Unsafe . Add ( ref sourceStart , width ) ;
746749
747- for ( nint j = 0 ; j < width ; j += 1 , offset += 1 )
750+ while ( Unsafe . IsAddressLessThan ( ref sourceStart , ref sourceEnd ) )
748751 {
749- Unsafe . Add ( ref destinationRef , offset ) = Unsafe. Add ( ref sourceRef , j ) ;
752+ destinationRef = sourceStart ;
753+
754+ sourceStart = ref Unsafe . Add ( ref sourceStart , 1 ) ;
755+ destinationRef = ref Unsafe . Add ( ref destinationRef , 1 ) ;
750756 }
751757 }
752758#endif
@@ -792,12 +798,16 @@ public void CopyTo(Span2D<T> destination)
792798
793799 for ( int i = 0 ; i < height ; i ++ )
794800 {
795- ref T sourceRef = ref DangerousGetReferenceAt ( i , 0 ) ;
801+ ref T sourceStart = ref DangerousGetReferenceAt ( i , 0 ) ;
802+ ref T sourceEnd = ref Unsafe . Add ( ref sourceStart , width ) ;
796803 ref T destinationRef = ref destination . DangerousGetReferenceAt ( i , 0 ) ;
797804
798- for ( nint j = 0 ; j < width ; j += 1 )
805+ while ( Unsafe . IsAddressLessThan ( ref sourceStart , ref sourceEnd ) )
799806 {
800- Unsafe . Add ( ref destinationRef , j ) = Unsafe. Add ( ref sourceRef , j ) ;
807+ destinationRef = sourceStart ;
808+
809+ sourceStart = ref Unsafe . Add ( ref sourceStart , 1 ) ;
810+ destinationRef = ref Unsafe . Add ( ref destinationRef , 1 ) ;
801811 }
802812 }
803813#endif
@@ -868,11 +878,14 @@ public void Fill(T value)
868878
869879 for ( int i = 0 ; i < height ; i ++ )
870880 {
871- ref T r0 = ref DangerousGetReferenceAt ( i , 0 ) ;
881+ ref T rStart = ref DangerousGetReferenceAt ( i , 0 ) ;
882+ ref T rEnd = ref Unsafe . Add ( ref rStart , width ) ;
872883
873- for ( nint j = 0 ; j < width ; j += 1 )
884+ while ( Unsafe . IsAddressLessThan ( ref rStart , ref rEnd ) )
874885 {
875- Unsafe . Add ( ref r0 , j ) = value;
886+ rStart = value ;
887+
888+ rStart = ref Unsafe . Add ( ref rStart , 1 ) ;
876889 }
877890 }
878891#endif
@@ -1078,15 +1091,18 @@ public bool TryGetSpan(out Span<T> span)
10781091 nint width = ( nint ) ( uint ) this . width ;
10791092
10801093 ref T destinationRef = ref array . DangerousGetReference ( ) ;
1081- nint offset = 0 ;
10821094
10831095 for ( int i = 0 ; i < height ; i ++ )
10841096 {
1085- ref T sourceRef = ref DangerousGetReferenceAt ( i , 0 ) ;
1097+ ref T sourceStart = ref DangerousGetReferenceAt ( i , 0 ) ;
1098+ ref T sourceEnd = ref Unsafe . Add ( ref sourceStart , width ) ;
10861099
1087- for ( nint j = 0 ; j < width ; j += 1 , offset += 1 )
1100+ while ( Unsafe . IsAddressLessThan ( ref sourceStart , ref sourceEnd ) )
10881101 {
1089- Unsafe . Add ( ref destinationRef , offset ) = Unsafe. Add ( ref sourceRef , j ) ;
1102+ destinationRef = sourceStart ;
1103+
1104+ sourceStart = ref Unsafe . Add ( ref sourceStart , 1 ) ;
1105+ destinationRef = ref Unsafe . Add ( ref destinationRef , 1 ) ;
10901106 }
10911107 }
10921108 }
0 commit comments