// File: /workspace/github_cpp_makefile_star2_O1/YiweiMao[P]tessDX/src/tessellator.cpp
// Function: CHWTessellator::ComputeTessFactorContext(unsigned int, CHWTessellator::TESS_FACTOR_CONTEXT&)
void CHWTessellator::ComputeTessFactorContext( FXP fxpTessFactor, TESS_FACTOR_CONTEXT& TessFactorCtx )
{
    FXP fxpHalfTessFactor = (fxpTessFactor+1/*round*/)/2;
    if( Odd() || (fxpHalfTessFactor == FXP_ONE_HALF)) // fxpHalfTessFactor == 1/2 if TessFactor is 1, but we're pretending we are even.
    {
        fxpHalfTessFactor += FXP_ONE_HALF;
    }
    FXP fxpFloorHalfTessFactor = fxpFloor(fxpHalfTessFactor);
    FXP fxpCeilHalfTessFactor = fxpCeil(fxpHalfTessFactor);
    TessFactorCtx.fxpHalfTessFactorFraction = fxpHalfTessFactor - fxpFloorHalfTessFactor;
    //CleanseParameter(TessFactorCtx.fxpHalfTessFactorFraction);
    TessFactorCtx.numHalfTessFactorPoints = (fxpCeilHalfTessFactor>>FXP_FRACTION_BITS); // for EVEN, we don't include the point always fixed at the midpoint of the TessFactor
    if( fxpCeilHalfTessFactor == fxpFloorHalfTessFactor )
    {
        TessFactorCtx.splitPointOnFloorHalfTessFactor =  /*pick value to cause this to be ignored*/ TessFactorCtx.numHalfTessFactorPoints+1;
    }
    else if( Odd() )
    {
        if( fxpFloorHalfTessFactor == FXP_ONE )
        {
            TessFactorCtx.splitPointOnFloorHalfTessFactor = 0;
        }
        else
        {
#ifdef ALLOW_XBOX_360_COMPARISON
            if( m_bXBox360Mode )
                TessFactorCtx.splitPointOnFloorHalfTessFactor = TessFactorCtx.numHalfTessFactorPoints-2;
            else
#endif
				TessFactorCtx.splitPointOnFloorHalfTessFactor = (RemoveMSB((fxpFloorHalfTessFactor>>FXP_FRACTION_BITS)-1)<<1) + 1;
        }
    }
    else
    {
#ifdef ALLOW_XBOX_360_COMPARISON
        if( m_bXBox360Mode )
            TessFactorCtx.splitPointOnFloorHalfTessFactor = TessFactorCtx.numHalfTessFactorPoints-1;
        else
#endif
			TessFactorCtx.splitPointOnFloorHalfTessFactor = (RemoveMSB(fxpFloorHalfTessFactor>>FXP_FRACTION_BITS)<<1) + 1;
    }
    int numFloorSegments = (fxpFloorHalfTessFactor * 2)>>FXP_FRACTION_BITS;
    int numCeilSegments = (fxpCeilHalfTessFactor * 2)>>FXP_FRACTION_BITS;
    if( Odd() )
    {
        numFloorSegments -= 1;
        numCeilSegments -= 1;
    }
    TessFactorCtx.fxpInvNumSegmentsOnFloorTessFactor = s_fixedReciprocal[numFloorSegments];
    TessFactorCtx.fxpInvNumSegmentsOnCeilTessFactor = s_fixedReciprocal[numCeilSegments];
}

// Assembly:
incl %esi
shrl %esi
movl 0x4(%rdi), %ecx
movl $0x8000, %eax           # imm = 0x8000
leal (%rsi,%rax), %r8d
cmpl $0x1, %ecx
movl %esi, %r9d
cmovel %r8d, %r9d
cmpl %eax, %esi
cmovel %r8d, %r9d
movl %r9d, %eax
andl $0x7fff0000, %eax       # imm = 0x7FFF0000
leal 0x10000(%rax), %esi
testw %r9w, %r9w
cmovel %r9d, %esi
andl $0x8000ffff, %r9d       # imm = 0x8000FFFF
movl %r9d, 0x8(%rdx)
movl %esi, %r8d
shrl $0x10, %r8d
movl %r8d, 0xc(%rdx)
cmpl %eax, %esi
jne 0x4030e8
incl %r8d
movl %r8d, %r9d
xorl %edi, %edi
cmpl $0x1, %ecx
sete %dil
movl %r9d, 0x10(%rdx)
shrl $0xf, %eax
shrl $0xf, %esi
movzwl %si, %ecx
subl %edi, %eax
subl %edi, %ecx
cltq
leaq 0xc2fe7(%rip), %rsi     # 0x4c60c0
movl (%rsi,%rax,4), %eax
movl %eax, (%rdx)
movslq %ecx, %rax
movl (%rsi,%rax,4), %eax
movl %eax, 0x4(%rdx)
retq
cmpl $0x1, %ecx
jne 0x403103
xorl %r9d, %r9d
cmpl $0x10000, %eax          # imm = 0x10000
je 0x4030b6
cmpb $0x1, 0x30(%rdi)
jne 0x40313d
addl $-0x2, %r8d
jmp 0x4030b3
cmpb $0x1, 0x30(%rdi)
jne 0x40310e
decl %r8d
jmp 0x4030b3
movl %eax, %edi
shrl $0x10, %edi
cmpl $0x1000000, %eax        # imm = 0x1000000
movl $0x80, %r9d
movl $0x8000, %r8d           # imm = 0x8000
cmovbl %r9d, %r8d
movl $0x8, %r9d
testl %edi, %r8d
jne 0x403177
shrl %r8d
decl %r9d
jne 0x40312e
jmp 0x40316c
movl %eax, %edi
shrl $0x10, %edi
decl %edi
cmpl $0x1010000, %eax        # imm = 0x1010000
movl $0x80, %r9d
movl $0x8000, %r8d           # imm = 0x8000
cmovbl %r9d, %r8d
movl $0x8, %r9d
testl %edi, %r8d
jne 0x403177
shrl %r8d
decl %r9d
jne 0x40315f
movl $0x1, %r9d
jmp 0x4030b6
notl %r8d
andl %r8d, %edi
leal 0x1(,%rdi,2), %r9d
jmp 0x4030b6