@@ -749,6 +749,7 @@ public static Vector256<float> Lerp(
749749        public static  float  Lerp( float  value1 ,  float  value2 ,  float  amount ) 
750750            =>  ( ( value2  -  value1 )  *  amount )  +  value1 ; 
751751
752+ #if SUPPORTS_RUNTIME_INTRINSICS 
752753        [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ] 
753754        public  static  void Accumulate( ref  Vector < uint >  accumulator ,  Vector < byte >  values ) 
754755        { 
@@ -762,5 +763,50 @@ public static void Accumulate(ref Vector<uint> accumulator, Vector<byte> values)
762763            accumulator +=  intLow; 
763764            accumulator +=  intHigh; 
764765        } 
766+ 
767+         /// <summary> 
768+         /// Reduces elements of the vector into one sum. 
769+         /// </summary> 
770+         /// <param name="accumulator">The accumulator to reduce.</param> 
771+         /// <returns>The sum of all elements.</returns> 
772+         [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ] 
773+         public static  int  ReduceSum( Vector128 < int >  accumulator ) 
774+         { 
775+             if  ( Ssse3 . IsSupported ) 
776+             { 
777+                 Vector128< int >  hadd  =  Ssse3 . HorizontalAdd ( accumulator ,  accumulator ) ; 
778+                 Vector128 < int >  swapped =  Sse2 . Shuffle ( hadd ,  0x1 ) ; 
779+                 Vector128 < int >  tmp =  Sse2 . Add ( hadd ,  swapped ) ; 
780+ 
781+                 // Vector128<int>.ToScalar() isn't optimized pre-net5.0 https://github.com/dotnet/runtime/pull/37882 
782+                 return  Sse2. ConvertToInt32 ( tmp ) ; 
783+             } 
784+             else 
785+             { 
786+                 int  sum =  0 ; 
787+                 for  ( int  i  =  0 ;  i <  Vector128 < int > . Count ;  i++ ) 
788+                 { 
789+                     sum  +=  accumulator . GetElement ( i ) ; 
790+                 } 
791+ 
792+                 return  sum; 
793+             } 
794+         } 
795+ 
796+         /// <summary> 
797+         /// Reduces even elements of the vector into one sum. 
798+         /// </summary> 
799+         /// <param name="accumulator">The accumulator to reduce.</param> 
800+         /// <returns>The sum of even elements.</returns> 
801+         [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ] 
802+         public  static  int EvenReduceSum( Vector256 < int >  accumulator ) 
803+         { 
804+             Vector128 < int >  vsum  =  Sse2. Add( accumulator . GetLower ( ) ,  accumulator . GetUpper ( ) ) ;  // add upper lane to lower lane 
805+             vsum =  Sse2. Add ( vsum ,  Sse2 . Shuffle ( vsum ,  0b_11_10_11_10 ) ) ;                       // add high to low 
806+ 
807+             // Vector128<int>.ToScalar() isn't optimized pre-net5.0 https://github.com/dotnet/runtime/pull/37882 
808+             return  Sse2. ConvertToInt32 ( vsum ) ; 
809+         } 
810+ #endif
765811    } 
766812} 
0 commit comments