// File: /workspace/github_cpp_makefile_star2_O2/YiweiMao[P]tessDX/src/tessellator.cpp
// Function: CHWTessellator::QuadGeneratePoints(CHWTessellator::PROCESSED_TESS_FACTORS_QUAD const&)
void CHWTessellator::QuadGeneratePoints( const PROCESSED_TESS_FACTORS_QUAD& processedTessFactors )
{
    // Generate exterior ring edge points, clockwise from top-left
    int pointOffset = 0;
    int edge;
    for(edge = 0; edge < QUAD_EDGES; edge++ )
    {
        int parity = edge&0x1;
        int startPoint = 0;
        int endPoint = processedTessFactors.numPointsForOutsideEdge[edge] - 1;
        for(int p = startPoint; p < endPoint; p++,pointOffset++) // don't include end, since next edge starts with it.
        {
            FXP fxpParam;
            int q = ((edge==1)||(edge==2)) ? p : endPoint - p; // reverse order 
            SetTessellationParity(processedTessFactors.outsideTessFactorParity[edge]);
            PlacePointIn1D(processedTessFactors.outsideTessFactorCtx[edge],q,fxpParam);
            if( parity )
            {
                DefinePoint(/*U*/fxpParam,
                            /*V*/(edge == 3) ? FXP_ONE : 0,
                            /*pointStorageOffset*/pointOffset);
            }
            else
            {
                DefinePoint(/*U*/(edge == 2) ? FXP_ONE : 0,
                            /*V*/fxpParam,
                            /*pointStorageOffset*/pointOffset);
            }
        }
    }

    // Generate interior ring points, clockwise from (U==0,V==1) (bottom-left) spiralling toward center
    static const int startRing = 1;
    int minNumPointsForTessFactor = min(processedTessFactors.numPointsForInsideTessFactor[U],processedTessFactors.numPointsForInsideTessFactor[V]);
    int numRings = (minNumPointsForTessFactor >> 1);  // note for even tess we aren't counting center point here.
    for(int ring = startRing; ring < numRings; ring++)
    {
        int startPoint = ring;
        int endPoint[QUAD_AXES] = {processedTessFactors.numPointsForInsideTessFactor[U] - 1 - startPoint,
                                   processedTessFactors.numPointsForInsideTessFactor[V] - 1 - startPoint};

        for(edge = 0; edge < QUAD_EDGES; edge++ )
        {
            int parity[QUAD_AXES] = {edge&0x1,((edge+1)&0x1)};
            int perpendicularAxisPoint = (edge < 2) ? startPoint : endPoint[parity[0]];
            FXP fxpPerpParam;
            SetTessellationParity(processedTessFactors.insideTessFactorParity[parity[0]]);
            PlacePointIn1D(processedTessFactors.insideTessFactorCtx[parity[0]],perpendicularAxisPoint,fxpPerpParam);
            SetTessellationParity(processedTessFactors.insideTessFactorParity[parity[1]]);
            for(int p = startPoint; p < endPoint[parity[1]]; p++, pointOffset++) // don't include end: next edge starts with it. 
            {
                FXP fxpParam;
                int q = ((edge == 1)||(edge==2)) ? p : endPoint[parity[1]] - (p - startPoint);
                PlacePointIn1D(processedTessFactors.insideTessFactorCtx[parity[1]],q,fxpParam);
                if( parity[1] )
                {
                    DefinePoint(/*U*/fxpPerpParam,
                                /*V*/fxpParam,
                                /*pointStorageOffset*/pointOffset);
                }
                else
                {
                    DefinePoint(/*U*/fxpParam,
                                /*V*/fxpPerpParam,
                                /*pointStorageOffset*/pointOffset);
                }
            }
        }
    }
    // For even tessellation, the inner "ring" is degenerate - a row of points
    if( (processedTessFactors.numPointsForInsideTessFactor[U] > processedTessFactors.numPointsForInsideTessFactor[V]) && 
        (TESSELLATOR_PARITY_EVEN == processedTessFactors.insideTessFactorParity[V]) )
    {
        int startPoint = numRings;
        int endPoint = processedTessFactors.numPointsForInsideTessFactor[U] - 1 - startPoint;
        SetTessellationParity(processedTessFactors.insideTessFactorParity[U]);
        for( int p = startPoint; p <= endPoint; p++, pointOffset++ )
        {
            FXP fxpParam;
            PlacePointIn1D(processedTessFactors.insideTessFactorCtx[U],p,fxpParam);
            DefinePoint(/*U*/fxpParam,
                        /*V*/FXP_ONE_HALF, // middle
                        /*pointStorageOffset*/pointOffset);
        }
    }
    else if( (processedTessFactors.numPointsForInsideTessFactor[V] >= processedTessFactors.numPointsForInsideTessFactor[U]) && 
             (TESSELLATOR_PARITY_EVEN == processedTessFactors.insideTessFactorParity[U]) )
    {
        int startPoint = numRings;
        int endPoint;
        FXP fxpParam;
        endPoint = processedTessFactors.numPointsForInsideTessFactor[V] - 1 - startPoint;
        SetTessellationParity(processedTessFactors.insideTessFactorParity[V]);
        for( int p = endPoint; p >= startPoint; p--, pointOffset++ )
        {
            PlacePointIn1D(processedTessFactors.insideTessFactorCtx[V],p,fxpParam);
            DefinePoint(/*U*/FXP_ONE_HALF, // middle
                        /*V*/fxpParam,
                        /*pointStorageOffset*/pointOffset);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r12
movq %rdi, %r15
leaq 0x30(%rsi), %rax
movq %rax, 0x30(%rsp)
xorl %ebp, %ebp
movq $0x0, 0x28(%rsp)
movq %rsi, 0x38(%rsp)
cmpq $0x4, %rbp
je 0x40274f
movl 0xac(%r12,%rbp,4), %r12d
decl %r12d
leal -0x1(%rbp), %eax
movl %eax, 0x20(%rsp)
imulq $0x14, %rbp, %rax
addq 0x30(%rsp), %rax
movq %rax, 0x40(%rsp)
xorl %eax, %eax
cmpq $0x3, %rbp
sete %al
shll $0x10, %eax
movl %eax, 0x14(%rsp)
xorl %eax, %eax
cmpq $0x2, %rbp
sete %al
shll $0x10, %eax
movl %eax, 0x10(%rsp)
xorl %ebx, %ebx
testl %r12d, %r12d
movl $0x0, %r13d
cmovgl %r12d, %r13d
movq 0x28(%rsp), %rax
leal (%rax,%rbx), %r14d
cmpl %ebx, %r13d
je 0x40273a
cmpl $0x2, 0x20(%rsp)
movl %r12d, %edx
cmovbl %ebx, %edx
movq 0x38(%rsp), %rax
movl 0x18(%rax,%rbp,4), %eax
movl %eax, 0x4(%r15)
movq %r15, %rdi
movq 0x40(%rsp), %rsi
leaq 0x18(%rsp), %rcx
callq 0x402f08
movl 0x18(%rsp), %edx
movq %r15, %rdi
testb $0x1, %bpl
jne 0x402725
movl 0x10(%rsp), %esi
jmp 0x40272b
movl %edx, %esi
movl 0x14(%rsp), %edx
movl %r14d, %ecx
callq 0x402558
incl %ebx
decl %r12d
jmp 0x4026da
incq %rbp
movl %r14d, %eax
movq %rax, 0x28(%rsp)
movq 0x38(%rsp), %r12
jmp 0x402681
movl 0xbc(%r12), %eax
movl 0xc0(%r12), %ecx
cmpl %ecx, %eax
cmovll %eax, %ecx
sarl %ecx
movl %ecx, 0xc(%rsp)
leaq 0x80(%r12), %rax
movq %rax, 0x50(%rsp)
pushq $0x1
popq %rdx
movq %r15, 0x20(%rsp)
movq 0x28(%rsp), %rbp
cmpl 0xc(%rsp), %edx
jge 0x4028b8
movl %edx, %eax
notl %eax
movq 0xbc(%r12), %xmm0
movd %eax, %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
paddd %xmm0, %xmm1
movq %xmm1, 0x18(%rsp)
xorl %r14d, %r14d
movl %edx, 0x10(%rsp)
cmpl $0x4, %r14d
je 0x4028b1
movl %r14d, %eax
andl $0x1, %eax
cmpl $0x2, %r14d
jb 0x4027d0
movl 0x18(%rsp,%rax,4), %edx
leal 0x1(%r14), %ecx
movl %ecx, 0x30(%rsp)
movl %ecx, %r13d
andl $0x1, %r13d
movl 0x28(%r12,%rax,4), %ecx
movl %ecx, 0x4(%r15)
imulq $0x14, %rax, %rsi
movq 0x50(%rsp), %rbx
addq %rbx, %rsi
movq %r15, %rdi
leaq 0x4c(%rsp), %rcx
callq 0x402f08
movl %r13d, %eax
movq %r13, 0x40(%rsp)
leal (,%r13,4), %ecx
movl 0x28(%r12,%rcx), %edx
movl %edx, 0x4(%r15)
movl 0x18(%rsp,%rcx), %ecx
decl %r14d
movq %r14, 0x28(%rsp)
imulq $0x14, %rax, %r13
addq %rbx, %r13
movl 0x4c(%rsp), %eax
movl %eax, 0x14(%rsp)
movl %ecx, %r14d
movl %ecx, %ebx
movl 0x10(%rsp), %r15d
cmpl %r14d, %r15d
jge 0x402897
movq %rbp, %r12
cmpl $0x2, 0x28(%rsp)
movl %ebx, %edx
cmovbl %r15d, %edx
movq 0x20(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
leaq 0x48(%rsp), %rcx
callq 0x402f08
movl 0x48(%rsp), %edx
cmpl $0x0, 0x40(%rsp)
jne 0x40287d
movq %rbp, %rdi
movl %edx, %esi
movl 0x14(%rsp), %edx
jmp 0x402884
movq %rbp, %rdi
movl 0x14(%rsp), %esi
movq %r12, %rbp
movl %ebp, %ecx
callq 0x402558
incl %r15d
incl %ebp
decl %ebx
jmp 0x40283f
movl 0x30(%rsp), %eax
movl %eax, %r14d
movq 0x20(%rsp), %r15
movq 0x38(%rsp), %r12
movl 0x10(%rsp), %edx
jmp 0x4027b6
incl %edx
jmp 0x402784
movq %rbp, %rsi
movl 0x2c(%r12), %eax
movl 0xbc(%r12), %ebx
movl 0xc0(%r12), %ebp
cmpl %ebp, %ebx
setg %cl
testl %eax, %eax
sete %dl
testb %dl, %cl
je 0x402956
movl 0xc(%rsp), %ebp
movl %ebp, %eax
notl %eax
addl %eax, %ebx
movl 0x28(%r12), %eax
movl %eax, 0x4(%r15)
movq 0x18(%r15), %rax
movslq %esi, %rcx
leaq (%rax,%rcx,8), %r15
addq $0x4, %r15
leaq 0x18(%rsp), %r14
cmpl %ebx, %ebp
jg 0x4029d5
movq 0x20(%rsp), %rdi
movq 0x50(%rsp), %rsi
movl %ebp, %edx
movq %r14, %rcx
callq 0x402f08
movl 0x18(%rsp), %eax
movl %eax, %ecx
shrl $0x10, %ecx
cvtsi2ss %ecx, %xmm0
movzwl %ax, %eax
cvtsi2ss %eax, %xmm1
mulss 0xc26ff(%rip), %xmm1    # 0x4c503c
addss %xmm0, %xmm1
movss %xmm1, -0x4(%r15)
movl $0x3f000000, (%r15)     # imm = 0x3F000000
incl %ebp
addq $0x8, %r15
jmp 0x402905
cmpl %ebx, %ebp
movl 0xc(%rsp), %ecx
jl 0x4029d5
cmpl $0x0, 0x28(%r12)
jne 0x4029d5
notl %ecx
addl %ecx, %ebp
movq 0x20(%rsp), %r15
movl %eax, 0x4(%r15)
addq $0x94, %r12
movq 0x18(%r15), %rax
movslq %esi, %rcx
leaq (%rax,%rcx,8), %rbx
addq $0x4, %rbx
leaq 0x18(%rsp), %r14
cmpl 0xc(%rsp), %ebp
jl 0x4029d5
movq %r15, %rdi
movq %r12, %rsi
movl %ebp, %edx
movq %r14, %rcx
callq 0x402f08
movl 0x18(%rsp), %eax
movl $0x3f000000, -0x4(%rbx) # imm = 0x3F000000
movzwl %ax, %ecx
shrl $0x10, %eax
cvtsi2ss %eax, %xmm0
cvtsi2ss %ecx, %xmm1
mulss 0xc2677(%rip), %xmm1    # 0x4c503c
addss %xmm0, %xmm1
movss %xmm1, (%rbx)
decl %ebp
addq $0x8, %rbx
jmp 0x40298e
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq