// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
incl %esi
shrl %esi
movl 0x4(%rdi), %ebp
movl $0x8000, %eax           # imm = 0x8000
leal (%rsi,%rax), %ecx
cmpl $0x1, %ebp
movl %esi, %r8d
cmovel %ecx, %r8d
movq %rdx, %rbx
cmpl %eax, %esi
cmovel %ecx, %r8d
movl %r8d, %r14d
andl $0x7fff0000, %r14d      # imm = 0x7FFF0000
leal 0x10000(%r14), %r15d
testw %r8w, %r8w
cmovel %r8d, %r15d
andl $0x8000ffff, %r8d       # imm = 0x8000FFFF
movl %r8d, 0x8(%rdx)
movl %r15d, %eax
shrl $0x10, %eax
movl %eax, 0xc(%rdx)
cmpl %r14d, %r15d
jne 0x402e3b
incl %eax
movl %eax, %ecx
jmp 0x402e7c
cmpl $0x1, %ebp
jne 0x402e56
xorl %ecx, %ecx
cmpl $0x10000, %r14d         # imm = 0x10000
je 0x402e7c
cmpb $0x1, 0x30(%rdi)
jne 0x402e68
addl $-0x2, %eax
jmp 0x402e37
cmpb $0x1, 0x30(%rdi)
jne 0x402e60
decl %eax
jmp 0x402e37
movl %r14d, %edi
shrl $0x10, %edi
jmp 0x402e70
movl %r14d, %edi
shrl $0x10, %edi
decl %edi
callq 0x4041f8
leal 0x1(,%rax,2), %ecx
xorl %eax, %eax
cmpl $0x1, %ebp
sete %al
movl %ecx, 0x10(%rbx)
shrl $0xf, %r14d
shrl $0xf, %r15d
movzwl %r15w, %ecx
subl %eax, %r14d
subl %eax, %ecx
movslq %r14d, %rax
leaq 0xc22ae(%rip), %rdx     # 0x4c5150
movl (%rdx,%rax,4), %eax
movl %eax, (%rbx)
movslq %ecx, %rax
movl (%rdx,%rax,4), %eax
movl %eax, 0x4(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nop