// File: /workspace/github_cpp_makefile_star2_O2/YiweiMao[P]tessDX/src/tessellator.cpp
// Function: CHWTessellator::QuadProcessTessFactors(float, float, float, float, float, float, CHWTessellator::PROCESSED_TESS_FACTORS_QUAD&)
void CHWTessellator::QuadProcessTessFactors( float tessFactor_Ueq0, float tessFactor_Veq0, float tessFactor_Ueq1, float tessFactor_Veq1, 
                      float insideTessFactor_U, float insideTessFactor_V, PROCESSED_TESS_FACTORS_QUAD& processedTessFactors )
{
    // Is the patch culled?
    if( !(tessFactor_Ueq0 > 0) || // NaN will pass
        !(tessFactor_Veq0 > 0) || 
        !(tessFactor_Ueq1 > 0) ||
        !(tessFactor_Veq1 > 0) )
    {
        processedTessFactors.bPatchCulled = true;
        return;
    }
    else
    {
        processedTessFactors.bPatchCulled = false;
    }

    // Clamp edge TessFactors
    float lowerBound, upperBound;
    switch(m_originalPartitioning)
    {
        case D3D11_TESSELLATOR_PARTITIONING_INTEGER:
        case D3D11_TESSELLATOR_PARTITIONING_POW2: // don�t care about pow2 distinction for validation, just treat as integer
            lowerBound = D3D11_TESSELLATOR_MIN_ODD_TESSELLATION_FACTOR;
            upperBound = D3D11_TESSELLATOR_MAX_EVEN_TESSELLATION_FACTOR;
            break;
         
        case D3D11_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
            lowerBound = D3D11_TESSELLATOR_MIN_EVEN_TESSELLATION_FACTOR;
            upperBound = D3D11_TESSELLATOR_MAX_EVEN_TESSELLATION_FACTOR;
            break;

        case D3D11_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
            lowerBound = D3D11_TESSELLATOR_MIN_ODD_TESSELLATION_FACTOR;
            upperBound = D3D11_TESSELLATOR_MAX_ODD_TESSELLATION_FACTOR;
            break;
    }

    tessFactor_Ueq0 = fmin( upperBound, fmax( lowerBound, tessFactor_Ueq0 ) );
    tessFactor_Veq0 = fmin( upperBound, fmax( lowerBound, tessFactor_Veq0 ) );
    tessFactor_Ueq1 = fmin( upperBound, fmax( lowerBound, tessFactor_Ueq1 ) );
    tessFactor_Veq1 = fmin( upperBound, fmax( lowerBound, tessFactor_Veq1 ) );

    if( HWIntegerPartitioning()) // pow2 or integer, round to next int (hw doesn't care about pow2 distinction)
    {
        tessFactor_Ueq0 = ceil(tessFactor_Ueq0);
        tessFactor_Veq0 = ceil(tessFactor_Veq0);
        tessFactor_Ueq1 = ceil(tessFactor_Ueq1);
        tessFactor_Veq1 = ceil(tessFactor_Veq1);
    }

    // Clamp inside TessFactors
    if(D3D11_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD == m_originalPartitioning)
    {
#define EPSILON 0.0000152587890625f // 2^(-16), min positive fixed point fraction
#define MIN_ODD_TESSFACTOR_PLUS_HALF_EPSILON (D3D11_TESSELLATOR_MIN_ODD_TESSELLATION_FACTOR + EPSILON/2)
        // If any TessFactor will end up > 1 after floatToFixed conversion later,
        // then force the inside TessFactors to be > 1 so there is a picture frame.
        if( (tessFactor_Ueq0 > MIN_ODD_TESSFACTOR_PLUS_HALF_EPSILON) ||
            (tessFactor_Veq0 > MIN_ODD_TESSFACTOR_PLUS_HALF_EPSILON) ||
            (tessFactor_Ueq1 > MIN_ODD_TESSFACTOR_PLUS_HALF_EPSILON) ||
            (tessFactor_Veq1 > MIN_ODD_TESSFACTOR_PLUS_HALF_EPSILON) ||
            (insideTessFactor_U > MIN_ODD_TESSFACTOR_PLUS_HALF_EPSILON) ||
            (insideTessFactor_V > MIN_ODD_TESSFACTOR_PLUS_HALF_EPSILON) )
        {
            // Force picture frame
            lowerBound = D3D11_TESSELLATOR_MIN_ODD_TESSELLATION_FACTOR + EPSILON;
        }
    }

    insideTessFactor_U = fmin( upperBound, fmax( lowerBound, insideTessFactor_U ) );
    insideTessFactor_V = fmin( upperBound, fmax( lowerBound, insideTessFactor_V ) );
    // Note the above clamps map NaN to lowerBound


    if( HWIntegerPartitioning()) // pow2 or integer, round to next int (hw doesn't care about pow2 distinction)
    {
        insideTessFactor_U = ceil(insideTessFactor_U);
        insideTessFactor_V = ceil(insideTessFactor_V);
    }

    // Reset our vertex and index buffers.  We have enough storage for the max tessFactor.
    m_NumPoints = 0;
    m_NumIndices = 0;

    // Process tessFactors
    float outsideTessFactor[QUAD_EDGES] = {tessFactor_Ueq0, tessFactor_Veq0, tessFactor_Ueq1, tessFactor_Veq1};
    float insideTessFactor[QUAD_AXES] = {insideTessFactor_U,insideTessFactor_V};
    int edge, axis;
    if( HWIntegerPartitioning() )
    {
        for( edge = 0; edge < QUAD_EDGES; edge++ )
        {
            int edgeEven = isEven(outsideTessFactor[edge]);
            processedTessFactors.outsideTessFactorParity[edge] = edgeEven ? TESSELLATOR_PARITY_EVEN : TESSELLATOR_PARITY_ODD;
        }
        for( axis = 0; axis < QUAD_AXES; axis++ )
        {
            processedTessFactors.insideTessFactorParity[axis] = 
                (isEven(insideTessFactor[axis]) || (FLOAT_ONE == insideTessFactor[axis]) )
                ? TESSELLATOR_PARITY_EVEN : TESSELLATOR_PARITY_ODD;
        }
    }
    else
    {
        for( edge = 0; edge < QUAD_EDGES; edge++ )
        {
            processedTessFactors.outsideTessFactorParity[edge] = m_originalParity;
        }
        processedTessFactors.insideTessFactorParity[U] = processedTessFactors.insideTessFactorParity[V] = m_originalParity;
    }
    
    // Save fixed point TessFactors 
    for( edge = 0; edge < QUAD_EDGES; edge++ )
    {
        processedTessFactors.outsideTessFactor[edge] = floatToFixed(outsideTessFactor[edge]);
    }
    for( axis = 0; axis < QUAD_AXES; axis++ )
    {
        processedTessFactors.insideTessFactor[axis] = floatToFixed(insideTessFactor[axis]);
    }

    if( HWIntegerPartitioning() || Odd() )
    {
        // Special case if all TessFactors are 1
        if( (FXP_ONE == processedTessFactors.insideTessFactor[U]) &&
            (FXP_ONE == processedTessFactors.insideTessFactor[V]) &&
            (FXP_ONE == processedTessFactors.outsideTessFactor[Ueq0]) &&
            (FXP_ONE == processedTessFactors.outsideTessFactor[Veq0]) &&
            (FXP_ONE == processedTessFactors.outsideTessFactor[Ueq1]) &&
            (FXP_ONE == processedTessFactors.outsideTessFactor[Veq1]) )
        {
            processedTessFactors.bJustDoMinimumTessFactor = true;
            return;
        }
    }
    processedTessFactors.bJustDoMinimumTessFactor = false;

    // Compute TessFactor-specific metadata
    for(int edge = 0; edge < QUAD_EDGES; edge++ )
    {
        SetTessellationParity(processedTessFactors.outsideTessFactorParity[edge]);
        ComputeTessFactorContext(processedTessFactors.outsideTessFactor[edge], processedTessFactors.outsideTessFactorCtx[edge]);
    }

    for(int axis = 0; axis < QUAD_AXES; axis++)
    {
        SetTessellationParity(processedTessFactors.insideTessFactorParity[axis]);
        ComputeTessFactorContext(processedTessFactors.insideTessFactor[axis], processedTessFactors.insideTessFactorCtx[axis]);
    }

    // Compute some initial data.

    // outside edge offsets and storage
    for(int edge = 0; edge < QUAD_EDGES; edge++ )
    {
        SetTessellationParity(processedTessFactors.outsideTessFactorParity[edge]);
        processedTessFactors.numPointsForOutsideEdge[edge] = NumPointsForTessFactor(processedTessFactors.outsideTessFactor[edge]);
        m_NumPoints += processedTessFactors.numPointsForOutsideEdge[edge];
    }
    m_NumPoints -= 4;

    // inside edge offsets
    for(int axis = 0; axis < QUAD_AXES; axis++)
    {
        SetTessellationParity(processedTessFactors.insideTessFactorParity[axis]);
        processedTessFactors.numPointsForInsideTessFactor[axis] = NumPointsForTessFactor(processedTessFactors.insideTessFactor[axis]);
        int pointCountMin = ( TESSELLATOR_PARITY_ODD == processedTessFactors.insideTessFactorParity[axis] ) ? 4 : 3;
        // max() allows degenerate transition regions when inside TessFactor == 1
        processedTessFactors.numPointsForInsideTessFactor[axis] = max(pointCountMin,processedTessFactors.numPointsForInsideTessFactor[axis]);
    }

    processedTessFactors.insideEdgePointBaseOffset = m_NumPoints;

    // inside storage, including interior edges above
    int numInteriorPoints = (processedTessFactors.numPointsForInsideTessFactor[U] - 2)*(processedTessFactors.numPointsForInsideTessFactor[V]-2);
    m_NumPoints += numInteriorPoints;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xb0, %rsp
movq %rsi, %rbx
xorps %xmm6, %xmm6
ucomiss %xmm6, %xmm0
jbe 0x4021cb
ucomiss %xmm6, %xmm1
jbe 0x4021cb
ucomiss %xmm6, %xmm2
jbe 0x4021cb
ucomiss %xmm6, %xmm3
jbe 0x4021cb
movq %rdi, %r14
movb $0x0, 0xa9(%rbx)
movl 0x8(%rdi), %r15d
movss 0xc2ea1(%rip), %xmm6    # 0x4c5044
cmpl $0x2, %r15d
movaps %xmm4, 0x20(%rsp)
movaps %xmm5, 0x40(%rsp)
je 0x4021d7
movss 0xc2e85(%rip), %xmm7    # 0x4c5040
cmpl $0x3, %r15d
jne 0x4021df
movss 0xc2e7f(%rip), %xmm6    # 0x4c5048
jmp 0x4021df
movb $0x1, 0xa9(%rbx)
jmp 0x402547
movss 0xc2e6d(%rip), %xmm7    # 0x4c504c
movaps %xmm6, 0xa0(%rsp)
movaps %xmm6, %xmm4
shufps $0x0, %xmm6, %xmm4      # xmm4 = xmm4[0,0],xmm6[0,0]
unpcklps %xmm3, %xmm2            # xmm2 = xmm2[0],xmm3[0],xmm2[1],xmm3[1]
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movlhps %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0]
movaps %xmm0, %xmm1
maxps %xmm4, %xmm0
cmpunordps %xmm4, %xmm4
andps %xmm4, %xmm1
andnps %xmm0, %xmm4
orps %xmm1, %xmm4
shufps $0x0, %xmm7, %xmm7      # xmm7 = xmm7[0,0,0,0]
movaps %xmm7, %xmm1
cmpunordps %xmm7, %xmm1
movaps %xmm4, %xmm0
andps %xmm1, %xmm0
movaps %xmm7, 0x90(%rsp)
minps %xmm7, %xmm4
movaps %xmm1, 0x30(%rsp)
andnps %xmm4, %xmm1
orps %xmm0, %xmm1
movaps %xmm1, (%rsp)
movl 0xc(%r14), %ebp
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
callq 0x4010c8
movdqa %xmm0, 0x80(%rsp)
movaps (%rsp), %xmm0
callq 0x4010c8
movaps %xmm0, 0x70(%rsp)
pshufd $0xee, (%rsp), %xmm0    # xmm0 = mem[2,3,2,3]
callq 0x4010c8
movdqa %xmm0, 0x60(%rsp)
pshufd $0xff, (%rsp), %xmm0    # xmm0 = mem[3,3,3,3]
callq 0x4010c8
cmpl $0x2, %ebp
jae 0x40229d
movdqa 0x60(%rsp), %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movdqa 0x70(%rsp), %xmm0
punpckldq 0x80(%rsp), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
punpcklqdq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0]
movdqa %xmm0, (%rsp)
movaps 0x20(%rsp), %xmm1
movaps 0x40(%rsp), %xmm2
cmpl $0x2, %r15d
movaps 0xa0(%rsp), %xmm3
movaps 0x90(%rsp), %xmm4
jne 0x4022ea
movaps 0xc2dcc(%rip), %xmm0    # 0x4c5090
cmpltps (%rsp), %xmm0
movmskps %xmm0, %eax
testl %eax, %eax
jne 0x4022e2
ucomiss 0xc2d79(%rip), %xmm1    # 0x4c5050
ja 0x4022e2
ucomiss 0xc2d70(%rip), %xmm2    # 0x4c5050
jbe 0x4022ea
movss 0xc2d6a(%rip), %xmm3    # 0x4c5054
shufps $0x0, %xmm3, %xmm3      # xmm3 = xmm3[0,0,0,0]
unpcklps %xmm2, %xmm1            # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movaps %xmm1, %xmm0
maxps %xmm3, %xmm0
cmpunordps %xmm3, %xmm3
andps %xmm3, %xmm1
andnps %xmm0, %xmm3
orps %xmm1, %xmm3
movaps %xmm3, %xmm0
movaps 0x30(%rsp), %xmm1
andps %xmm1, %xmm0
minps %xmm4, %xmm3
andnps %xmm3, %xmm1
orps %xmm0, %xmm1
movaps %xmm1, 0x30(%rsp)
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
callq 0x4010c8
movdqa %xmm0, 0x20(%rsp)
movaps 0x30(%rsp), %xmm0
callq 0x4010c8
andq $0x0, 0x28(%r14)
cmpl $0x2, %ebp
jb 0x402348
movaps 0x30(%rsp), %xmm0
jmp 0x40234d
unpcklps 0x20(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
movaps (%rsp), %xmm1
movaps %xmm1, 0x50(%rsp)
movlps %xmm0, 0x18(%rsp)
jae 0x402377
xorl %eax, %eax
cmpq $0x4, %rax
je 0x40238b
cvttss2si 0x50(%rsp,%rax,4), %ecx
andl $0x1, %ecx
movl %ecx, 0x18(%rbx,%rax,4)
incq %rax
jmp 0x40235f
xorl %eax, %eax
movl (%r14), %ecx
cmpq $0x4, %rax
je 0x4023c2
movl %ecx, 0x18(%rbx,%rax,4)
incq %rax
jmp 0x402379
xorl %eax, %eax
movss 0xc2caf(%rip), %xmm0    # 0x4c5044
cmpq $0x2, %rax
je 0x4023c8
movss 0x18(%rsp,%rax,4), %xmm1
cvttss2si %xmm1, %ecx
ucomiss %xmm0, %xmm1
setp %dl
setne %sil
orb %dl, %sil
andb %cl, %sil
movzbl %sil, %ecx
movl %ecx, 0x28(%rbx,%rax,4)
incq %rax
jmp 0x402395
movl %ecx, 0x2c(%rbx)
movl %ecx, 0x28(%rbx)
xorl %r15d, %r15d
cmpq $0x10, %r15
je 0x4023e8
leaq (%rsp,%r15), %rdi
addq $0x50, %rdi
callq 0x404f49
movl %eax, (%rbx,%r15)
addq $0x4, %r15
jmp 0x4023cb
xorl %r15d, %r15d
cmpq $0x8, %r15
je 0x402409
leaq (%rsp,%r15), %rdi
addq $0x18, %rdi
callq 0x404f49
movl %eax, 0x10(%rbx,%r15)
addq $0x4, %r15
jmp 0x4023eb
cmpl $0x2, 0xc(%r14)
jb 0x402417
cmpl $0x1, 0x4(%r14)
jne 0x40244b
cmpl $0x10000, 0x10(%rbx)    # imm = 0x10000
jne 0x40244b
cmpl $0x10000, 0x14(%rbx)    # imm = 0x10000
jne 0x40244b
movdqu (%rbx), %xmm0
pcmpeqd 0xc2c6b(%rip), %xmm0    # 0x4c50a0
movmskps %xmm0, %eax
xorl $0xf, %eax
testb %al, %al
jne 0x40244b
movb $0x1, 0xa8(%rbx)
jmp 0x402547
movb $0x0, 0xa8(%rbx)
leaq 0x30(%rbx), %r15
xorl %r12d, %r12d
cmpq $0x4, %r12
je 0x402480
movl 0x18(%rbx,%r12,4), %eax
movl %eax, 0x4(%r14)
movl (%rbx,%r12,4), %esi
movq %r14, %rdi
movq %r15, %rdx
callq 0x402dda
incq %r12
addq $0x14, %r15
jmp 0x402459
leaq 0x80(%rbx), %r15
xorl %r12d, %r12d
cmpq $0x2, %r12
je 0x4024b2
movl 0x28(%rbx,%r12,4), %eax
movl %eax, 0x4(%r14)
movl 0x10(%rbx,%r12,4), %esi
movq %r14, %rdi
movq %r15, %rdx
callq 0x402dda
incq %r12
addq $0x14, %r15
jmp 0x40248a
xorl %r15d, %r15d
cmpq $0x4, %r15
je 0x4024e1
movl 0x18(%rbx,%r15,4), %eax
movl %eax, 0x4(%r14)
movl (%rbx,%r15,4), %esi
movq %r14, %rdi
callq 0x402ebc
movl %eax, 0xac(%rbx,%r15,4)
addl %eax, 0x28(%r14)
incq %r15
jmp 0x4024b5
addl $-0x4, 0x28(%r14)
xorl %r15d, %r15d
cmpq $0x2, %r15
je 0x402522
movl 0x28(%rbx,%r15,4), %ebp
movl %ebp, 0x4(%r14)
movl 0x10(%rbx,%r15,4), %esi
movq %r14, %rdi
callq 0x402ebc
xorl %ecx, %ecx
cmpl $0x1, %ebp
sete %cl
addl $0x3, %ecx
cmpl %eax, %ecx
cmovbel %eax, %ecx
movl %ecx, 0xbc(%rbx,%r15,4)
incq %r15
jmp 0x4024e9
movl 0x28(%r14), %eax
movl %eax, 0xc4(%rbx)
movl 0xbc(%rbx), %ecx
movl 0xc0(%rbx), %edx
addl $-0x2, %ecx
addl $-0x2, %edx
imull %ecx, %edx
addl %eax, %edx
movl %edx, 0x28(%r14)
addq $0xb0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop