BugInTheSYS

SSE_MixColors.cpp

May 12th, 2012
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. typedef int Color;
  2. #define GetRValue( x ) ( unsigned char )( x & 0x000000FF );
  3. #define GetGValue( x ) ( unsigned char )( x & 0x0000FF00 >> 8 );
  4. #define GetBValue( x ) ( unsigned char )( x & 0x00FF0000 >> 16 );
  5.  
  6. Color AverageColor( Color *Input, int length ) {
  7.   int   i;
  8.   int   ColorBytes[4];
  9.   int   SuperBytes[4];
  10.   int   Check[4];
  11.   float LengthSngs[4];
  12.  
  13.   ColorBytes[0] = ColorBytes[1] = ColorBytes[2] = ColorBytes[3] = 0;
  14.   SuperBytes[0] = SuperBytes[1] = SuperBytes[2] = SuperBytes[3] = 0;
  15.   __asm {
  16.     MOVUPS XMM1, ColorBytes ;ColorBytes in den MMX-Speicher schieben
  17.     MOVUPS Check, XMM1
  18.   }
  19.   for( i = 0; i < length; i++ ) {
  20.     SuperBytes[0] = GetRValue( Input[i] );
  21.     SuperBytes[1] = GetGValue( Input[i] );
  22.     SuperBytes[2] = GetBValue( Input[i] );
  23.     __asm {
  24.       MOVUPS  XMM2, SuperBytes  ;SuperBytes in das zweite MMX-Register schreiben
  25.       PADDD   XMM1, XMM2        ;Parallel addieren... Das Ergebnis steht jetzt in XMM1!
  26.                                 ;Also erstmal nichts weiter machen...
  27.       MOVUPS Check, XMM1
  28.     }
  29.   }
  30.  
  31.   LengthSngs[0] = ( float )length;
  32.   LengthSngs[1] = ( float )length;
  33.   LengthSngs[2] = ( float )length;
  34.   LengthSngs[3] = 0.0f;
  35.   __asm {
  36.     MOVAPS    XMM2, XMM1        ;fertiges zeug in xmm2 laden, damits zurückgerechnet werden kann
  37.     CVTDQ2PS  XMM1, XMM2        ;Alles in Floats umwandeln, damits ans dividieren gehen kann
  38.     MOVUPS    XMM2, LengthSngs
  39.     DIVPS     XMM1, XMM2
  40.     MOVAPS    XMM2, XMM1        ;fertiges zeug in xmm2 laden, damits zurückgerechnet werden kann
  41.     CVTPS2DQ  XMM1, XMM2        ;Die Umwandlung zurück in integer machen
  42.     MOVUPS    ColorBytes, XMM1  ;Die umgewandelten Daten zurückladen
  43.   }
  44.   return (ColorBytes[2] << 16) | (ColorBytes[1] << 8) | (ColorBytes[0]);
  45. }
  46.  
  47. int _tmain(int argc, _TCHAR* argv[]) {
  48.   int CLRS[2];
  49.   std::cin >> CLRS[0];
  50.   std::cin >> CLRS[1];
  51.   std::cout << AverageColor( CLRS, 2 );
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment