// -*- c++ -*-

unsigned char* pDest;
const unsigned char* pSrcP;
const unsigned char* pSrc;
const unsigned char* pBob;
const unsigned char* pBobP;

// long is int32 on ARCH_368, int64 on ARCH_AMD64. Declaring it this way
// saves a lot of xor's to delete 64bit garbage.

#if defined(DBL_RESIZE) || defined(USE_FOR_DSCALER)
long	    src_pitch2 = src_pitch;			// even & odd lines are not longerleaved in DScaler
#else
long	    src_pitch2 = 2 * src_pitch;		// even & odd lines are longerleaved in Avisynth
#endif

long	    dst_pitch2 = 2 * dst_pitch;
long     y;

#ifdef IS_SSE2
long     Last8 = (rowsize-16);			// ofs to last 16 bytes in row for SSE2
#else
long     Last8 = (rowsize-8);			// ofs to last 8 bytes in row
#endif

long		dst_pitchw = dst_pitch; // local stor so asm can ref
	pSrc  = pWeaveSrc;			// polongs 1 weave line above
	pSrcP = pWeaveSrcP;			// " 

#ifdef DBL_RESIZE
	        
#ifdef USE_VERTICAL_FILTER
	pDest = pWeaveDest + dst_pitch2;
#else
	pDest = pWeaveDest + 3*dst_pitch;
#endif

#else

#ifdef USE_VERTICAL_FILTER
	pDest = pWeaveDest + dst_pitch;
#else
	pDest = pWeaveDest + dst_pitch2;
#endif

#endif

	if (TopFirst)
	{
		pBob = pCopySrc + src_pitch2;      // remember one weave line just copied previously
		pBobP = pCopySrcP + src_pitch2;
	}
	else
	{
		pBob =  pCopySrc;
		pBobP =  pCopySrcP;
	}

#ifndef _pBob
#define _pBob       "%0"
#define _src_pitch2 "%1"
#define _pDest      "%2"
#define _dst_pitchw "%3"
#define _Last8      "%4"
#define _pSrc       "%5"
#define _pSrcP      "%6"
#define _pBobP      "%7"
#define _olddx      "%8"
#define _UVMask     "%9"
#define _ShiftMask  "%10"
#define _FOURS      "%11"
#define _TENS       "%12"
#define _Max_Vals   "%13"
#define _Min_Vals   "%14"
#define _YMask      "%15"
#define _Max_Mov    "%16"
#define _ONES       "%17"
#define _DiffThres  "%18"
#endif

	for (y=1; y < FldHeight-1; y++)
	{
          // pretend it's indented -->>
        __asm__ __volatile__
            (
             // Loop general reg usage
             //
             // ax - pBobP, then pDest 
             // dx - pBob
             // cx - src_pitch2
             // _olddx - current offset
             // di - prev weave pixels, 1 line up
             // si - next weave pixels, 1 line up

#ifdef IS_SSE2
             
             // sse2 code deleted for now

#else
             // simple bob first 8 bytes
             MEMREG ("mov", _pBob, "dx")
             MEMREG ("mov", _src_pitch2, "cx")

#ifdef USE_VERTICAL_FILTER
             "movq "MEMREF1("dx")",       %%mm0\n\t"
             "movq "MEMREF2("dx", "cx")", %%mm1\n\t" //, qword ptr["XDX"+"XCX"]
             "movq %%mm0,                 %%mm2\n\t"
             V_PAVGB ("%%mm2", "%%mm1", "%%mm3", _ShiftMask)		// halfway between
             V_PAVGB ("%%mm0", "%%mm2", "%%mm3", _ShiftMask)		// 1/4 way
             V_PAVGB ("%%mm1", "%%mm2", "%%mm3", _ShiftMask)		// 3/4 way
             MEMREG ("mov", _pDest, "di")
             MEMREG ("mov", _dst_pitchw, "ax")
             V_MOVNTQ (MEMREF1("di"), "%%mm0")
             V_MOVNTQ (MEMREF2("di","ax"), "%%mm1") // qword ptr["XDI"+"XAX"], mm1

             // simple bob last 8 bytes
             MEMREG ("mov", _Last8, "si")
             REGMEM ("mov", "si", _olddx)
             REG2 ("add", "dx", "si") // ["XDX"+"_olddx"]
             "movq "MEMREF1("si")",       %%mm0\n\t"
             "movq "MEMREF2("si", "cx")", %%mm1\n\t"    // qword ptr["XSI"+"XCX"]
             "movq %%mm0,                 %%mm2\n\t"
             V_PAVGB ("%%mm2", "%%mm1", "%%mm3", _ShiftMask)		// halfway between
             V_PAVGB ("%%mm0", "%%mm2", "%%mm3", _ShiftMask)		// 1/4 way
             V_PAVGB ("%%mm1", "%%mm2", "%%mm3", _ShiftMask)		// 3/4 way
             MEMREG ("add", _olddx, "di") // last 8 bytes of dest
             V_MOVNTQ (MEMREF1("di"),       "%%mm0")
             V_MOVNTQ (MEMREF2("di", "ax"), "%%mm1") // qword ptr["XDI"+"XAX"], mm1)

#else
             "movq "MEMREF1("dx")", %%mm0\n\t"
             //		pavgb	mm0, qword ptr["XDX"+"XCX"]
             V_PAVGB ("%%mm0", MEMREF2("dx","cx"), "%%mm2", _ShiftMask) // qword ptr["XDX"+"XCX"], mm2, ShiftMask)
             MEMREG ("mov", _pDest, "di")
             V_MOVNTQ (MEMREF1("di"), "%%mm0")

             // simple bob last 8 bytes
             MEMREG ("mov", _Last8, "si")
             REGMEM ("mov", "si", _olddx)
             REG2 ("add", "dx", "si") //"XSI", ["XDX"+"_olddx"]
             "movq "MEMREF1("si")", %%mm0\n\t"
             //		pavgb	mm0, qword ptr["XSI"+"XCX"]
             V_PAVGB ("%%mm0", MEMREF2("si","cx"), "%%mm2", _ShiftMask) // qword ptr["XSI"+"XCX"], mm2, ShiftMask)
             MEMREG ("add", _olddx, "di")
             V_MOVNTQ (MEMREF1("di"), "%%mm0") // qword ptr["XDI"+"_olddx"], mm0)
#endif
             // now loop and get the middle qwords
             MEMREG ("mov", _pSrc, "si")
             MEMREG ("mov", _pSrcP, "di")
             CONSTMEM ("mov", "8", _olddx)  // curr offset longo all lines

             "1:\n\t"	
             MEMREG ("mov", _pBobP, "ax")
             BUMPPTR ("8", "di", "di")
             BUMPPTR ("8", "si", "si")
             BUMPPTR ("8", "dx", "dx")
             MEMREG ("add", _olddx, "ax")

#ifdef USE_STRANGE_BOB
#include "StrangeBob.inc"
#else
#include "WierdBob.inc"
#endif

             // For non-SSE2:
             // through out most of the rest of this loop we will malongain
             //	mm4		our min bob value
             //	mm5		best weave pixels so far
             // mm6		our max Bob value 
             //	mm7		best weighted pixel ratings so far
             
             // We will keep a slight bias to using the weave pixels
             // from the current location, by rating them by the min distance
             // from the Bob value instead of the avg distance from that value.
             // our best and only rating so far
             "pcmpeqb	%%mm7, %%mm7\n\t"			// ffff, say we didn't find anything good yet

#endif
