// File: /workspace/github_cpp_makefile_star2_O1/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 $0x60, %rsp
movq %rsi, %rbx
xorps %xmm6, %xmm6
ucomiss %xmm6, %xmm0
jbe 0x4021cf
ucomiss %xmm6, %xmm1
jbe 0x4021cf
ucomiss %xmm6, %xmm2
jbe 0x4021cf
ucomiss %xmm6, %xmm3
jbe 0x4021cf
movq %rdi, %r14
movb $0x0, 0xa9(%rbx)
movl 0x8(%rdi), %r15d
movss 0xc3e95(%rip), %xmm9    # 0x4c6044
cmpl $0x2, %r15d
je 0x4021db
movss 0xc3e82(%rip), %xmm8    # 0x4c6040
cmpl $0x3, %r15d
jne 0x4021e4
movss 0xc3e7b(%rip), %xmm9    # 0x4c6048
jmp 0x4021e4
movb $0x1, 0xa9(%rbx)
jmp 0x402605
movss 0xc3e68(%rip), %xmm8    # 0x4c604c
movaps %xmm9, %xmm6
cmpunordss %xmm9, %xmm6
movaps %xmm6, %xmm7
andps %xmm0, %xmm7
maxss %xmm9, %xmm0
andnps %xmm0, %xmm6
ucomiss %xmm9, %xmm9
jp 0x402207
maxss %xmm9, %xmm1
orps %xmm7, %xmm6
jp 0x402211
maxss %xmm9, %xmm2
movaps %xmm8, %xmm10
cmpunordss %xmm8, %xmm10
movaps %xmm6, %xmm7
minss %xmm8, %xmm7
jp 0x40222a
maxss %xmm9, %xmm3
movaps %xmm10, %xmm0
andps %xmm6, %xmm0
movaps %xmm10, %xmm11
andnps %xmm7, %xmm11
ucomiss %xmm8, %xmm8
jp 0x402244
minss %xmm8, %xmm1
orps %xmm0, %xmm11
jp 0x40224f
minss %xmm8, %xmm2
jp 0x402256
minss %xmm8, %xmm3
movl 0xc(%r14), %ebp
cmpl $0x1, %ebp
movss %xmm2, 0x4(%rsp)
movss %xmm1, (%rsp)
ja 0x4022f4
movaps %xmm11, %xmm0
movss %xmm5, 0xc(%rsp)
movss %xmm3, 0x8(%rsp)
movaps %xmm4, 0x50(%rsp)
movss %xmm8, 0x14(%rsp)
movss %xmm9, 0x10(%rsp)
movaps %xmm10, 0x40(%rsp)
callq 0x4010c8
movaps %xmm0, 0x30(%rsp)
movss (%rsp), %xmm0
callq 0x4010c8
movss %xmm0, (%rsp)
movss 0x4(%rsp), %xmm0
callq 0x4010c8
movss %xmm0, 0x4(%rsp)
movss 0x8(%rsp), %xmm0
callq 0x4010c8
movaps 0x30(%rsp), %xmm11
movaps 0x40(%rsp), %xmm10
movss 0x10(%rsp), %xmm9
movss 0x14(%rsp), %xmm8
movaps 0x50(%rsp), %xmm4
movss 0xc(%rsp), %xmm5
movaps %xmm0, %xmm3
cmpl $0x2, %r15d
jne 0x402347
ucomiss 0xc3d4f(%rip), %xmm5    # 0x4c6050
movss 0x4(%rsp), %xmm2
movss (%rsp), %xmm6
ja 0x40233c
ucomiss 0xc3d3b(%rip), %xmm4    # 0x4c6050
ja 0x40233c
ucomiss 0xc3d32(%rip), %xmm3    # 0x4c6050
ja 0x40233c
ucomiss 0xc3d29(%rip), %xmm2    # 0x4c6050
ja 0x40233c
ucomiss 0xc3d20(%rip), %xmm6    # 0x4c6050
ja 0x40233c
ucomiss 0xc3d16(%rip), %xmm11   # 0x4c6050
jbe 0x402352
movss 0xc3d0f(%rip), %xmm9    # 0x4c6054
jmp 0x402352
movss 0x4(%rsp), %xmm2
movss (%rsp), %xmm6
ucomiss %xmm9, %xmm9
jp 0x40235d
maxss %xmm9, %xmm5
ucomiss %xmm8, %xmm8
jp 0x402368
minss %xmm8, %xmm5
movaps %xmm9, %xmm0
cmpunordss %xmm9, %xmm0
movaps %xmm0, %xmm1
andps %xmm4, %xmm1
maxss %xmm9, %xmm4
andnps %xmm4, %xmm0
orps %xmm1, %xmm0
movaps %xmm10, %xmm1
andps %xmm0, %xmm1
minss %xmm8, %xmm0
andnps %xmm0, %xmm10
orps %xmm1, %xmm10
cmpl $0x1, %ebp
ja 0x4023e7
movaps %xmm10, %xmm0
movss %xmm5, 0xc(%rsp)
movss %xmm3, 0x8(%rsp)
movaps %xmm11, 0x30(%rsp)
callq 0x4010c8
movaps %xmm0, 0x40(%rsp)
movss 0xc(%rsp), %xmm0
callq 0x4010c8
movaps 0x30(%rsp), %xmm11
movaps 0x40(%rsp), %xmm10
movss (%rsp), %xmm6
movss 0x4(%rsp), %xmm2
movss 0x8(%rsp), %xmm3
movaps %xmm0, %xmm5
movq $0x0, 0x28(%r14)
movss %xmm11, 0x20(%rsp)
movss %xmm6, 0x24(%rsp)
movss %xmm2, 0x28(%rsp)
movss %xmm3, 0x2c(%rsp)
movss %xmm10, 0x18(%rsp)
movss %xmm5, 0x1c(%rsp)
xorl %eax, %eax
cmpl $0x2, %ebp
jae 0x402469
cvttss2si 0x20(%rsp,%rax,4), %ecx
andl $0x1, %ecx
movl %ecx, 0x18(%rbx,%rax,4)
incq %rax
cmpq $0x4, %rax
jne 0x40241c
xorl %eax, %eax
movss 0xc3c08(%rip), %xmm0    # 0x4c6044
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
cmpq $0x1, %rax
je 0x40243c
jmp 0x402482
movl (%r14), %ecx
movl %ecx, 0x18(%rbx,%rax,4)
incq %rax
cmpq $0x4, %rax
jne 0x402469
movl (%r14), %eax
movl %eax, 0x2c(%rbx)
movl %eax, 0x28(%rbx)
xorl %r15d, %r15d
leaq (%rsp,%r15), %rdi
addq $0x20, %rdi
callq 0x4057c1
movl %eax, (%rbx,%r15)
addq $0x4, %r15
cmpq $0x10, %r15
jne 0x402485
xorl %r15d, %r15d
leaq (%rsp,%r15), %rdi
addq $0x18, %rdi
callq 0x4057c1
movl %eax, 0x10(%rbx,%r15)
addq $0x4, %r15
cmpq $0x4, %r15
je 0x4024a3
cmpl $0x2, 0xc(%r14)
jb 0x4024cd
cmpl $0x1, 0x4(%r14)
jne 0x40250e
cmpl $0x10000, 0x10(%rbx)    # imm = 0x10000
jne 0x40250e
cmpl $0x10000, 0x14(%rbx)    # imm = 0x10000
jne 0x40250e
cmpl $0x10000, (%rbx)        # imm = 0x10000
jne 0x40250e
cmpl $0x10000, 0x4(%rbx)     # imm = 0x10000
jne 0x40250e
cmpl $0x10000, 0x8(%rbx)     # imm = 0x10000
jne 0x40250e
cmpl $0x10000, 0xc(%rbx)     # imm = 0x10000
jne 0x40250e
movb $0x1, 0xa8(%rbx)
jmp 0x402605
movb $0x0, 0xa8(%rbx)
leaq 0x30(%rbx), %r15
xorl %r12d, %r12d
movl 0x18(%rbx,%r12,4), %eax
movl %eax, 0x4(%r14)
movl (%rbx,%r12,4), %esi
movq %r14, %rdi
movq %r15, %rdx
callq 0x403060
incq %r12
addq $0x14, %r15
cmpq $0x4, %r12
jne 0x40251c
leaq 0x80(%rbx), %r15
xorl %r12d, %r12d
movl 0x28(%rbx,%r12,4), %eax
movl %eax, 0x4(%r14)
movl 0x10(%rbx,%r12,4), %esi
movq %r14, %rdi
movq %r15, %rdx
callq 0x403060
incq %r12
addq $0x14, %r15
cmpq $0x1, %r12
je 0x40254b
xorl %r15d, %r15d
movl 0x18(%rbx,%r15,4), %eax
movl %eax, 0x4(%r14)
movl (%rbx,%r15,4), %esi
movq %r14, %rdi
callq 0x40318a
movl %eax, 0xac(%rbx,%r15,4)
addl %eax, 0x28(%r14)
incq %r15
cmpq $0x4, %r15
jne 0x402574
addl $-0x4, 0x28(%r14)
xorl %r15d, %r15d
movl 0x28(%rbx,%r15,4), %eax
movl %eax, 0x4(%r14)
movl 0x10(%rbx,%r15,4), %esi
movq %r14, %rdi
callq 0x40318a
xorl %ecx, %ecx
cmpl $0x1, 0x28(%rbx,%r15,4)
sete %cl
addl $0x3, %ecx
cmpl %eax, %ecx
cmovbel %eax, %ecx
movl %ecx, 0xbc(%rbx,%r15,4)
incq %r15
cmpq $0x1, %r15
je 0x4025a6
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 $0x60, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq