title: How to optimize this Delphi function with SSE2?
body: <p>I need a hint, how to implement this Delphi function using SSE2 assembly (32 Bit). Other optimizations are welcome too. Maybe one can tell me, what kind of instructions could be used, so I have a starting point for further reading.</p>
  
  <p>Actual:</p>
  
  <pre><code>const Precision = 10000;
  
  // This function adds all Pixels into one. The pixels are weighted before adding. 
  // A weight can range from 0 to "Precision". "Size" is typically 10 to 50.
  
  function TFilter.Combine(Pixels: PByte; Weights: PCardinal; const Size: Cardinal): Cardinal;
  var
    i, R, G, B, A: Cardinal;
  begin
    B := Pixels^ * Weights^; Inc(Pixels);
    G := Pixels^ * Weights^; Inc(Pixels);
    R := Pixels^ * Weights^; Inc(Pixels);
    A := Pixels^ * Weights^; Inc(Pixels);
    Inc(Weights); // goto next weight
    for i := 1 to Size - 1 do
    begin
      Inc(B, Pixels^ * Weights^); Inc(Pixels);
      Inc(G, Pixels^ * Weights^); Inc(Pixels);
      Inc(R, Pixels^ * Weights^); Inc(Pixels);
      Inc(A, Pixels^ * Weights^); Inc(Pixels);
      Inc(Weights); // goto next weight
    end;
    B := B div Precision;
    G := G div Precision;
    R := R div Precision;
    A := A div Precision;
  
    Result := A shl 24 or R shl 16 or G shl 8 or B;
  end;
  </code></pre>
  
  <p>Expected:</p>
  
  <pre><code>function TFilter.Combine(Pixels: PByte; Weights: PCardinal; const Size: Cardinal): Cardinal;
  asm
    // Insert fast SSE2-Code here ;-)
  end;
  </code></pre>