// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/jfbuild/libsquish/squish.cpp
// Function: squish::ComputeMSE(unsigned char const*, int, int, int, unsigned char const*, int, double&, double&)
void ComputeMSE( u8 const *rgba, int width, int height, int pitch, u8 const *dxt, int flags, double &colourMSE, double &alphaMSE )
{
    // fix any bad flags
    flags = FixFlags( flags );
    colourMSE = alphaMSE = 0;

    // initialise the block input
    squish::u8 const* sourceBlock = dxt;
    int bytesPerBlock = ( ( flags & squish::kDxt1 ) != 0 ) ? 8 : 16;

    // loop over blocks
    for( int y = 0; y < height; y += 4 )
    {
        for( int x = 0; x < width; x += 4 )
        {
            // decompress the block
            u8 targetRgba[4*16];
            Decompress( targetRgba, sourceBlock, flags );
            //JonoF: u8 const* sourcePixel = targetRgba;

            // copy across to a similar pixel block
            u8 originalRgba[4*16];
            u8* originalPixel = originalRgba;

            for( int py = 0; py < 4; ++py )
            {
                for( int px = 0; px < 4; ++px )
                {
                    int sx = x + px;
                    int sy = y + py;
                    if( sx < width && sy < height )
                    {
                        u8 const* targetPixel = rgba + pitch*sy + 4*sx;
                        CopyRGBA(targetPixel, originalPixel, flags);
                    }
                    //JonoF: sourcePixel += 4;
                    originalPixel += 4;
                }
            }

            // compute the weighted MSE of the block
            double blockCMSE, blockAMSE;
            ComputeBlockWMSE(originalRgba, targetRgba, std::min(4, width - x), std::min(4, height - y), blockCMSE, blockAMSE);
            colourMSE += blockCMSE;
            alphaMSE += blockAMSE;
            // advance
            sourceBlock += bytesPerBlock;
        }
    }
    colourMSE /= (width * height * 3);
    alphaMSE /= (width * height);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movl %r9d, %r14d
movq %r8, %r10
andl $0x1f, %r9d
movl %r14d, %r8d
andl $0x160, %r8d            # imm = 0x160
movd %r9d, %xmm0
pshufd $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
pcmpeqd 0x11432(%rip), %xmm0    # 0x7e6d0
movmskps %xmm0, %eax
testl %eax, %eax
movl $0x1, %eax
cmovnel %r9d, %eax
cmpl $0x100, %r8d            # imm = 0x100
movl $0x20, %ebp
cmovel %r8d, %ebp
movq 0x148(%rsp), %r9
cmpl $0x40, %r8d
cmovel %r8d, %ebp
movq 0x140(%rsp), %rbx
xorl %r8d, %r8d
movq %r8, (%r9)
movq %r8, (%rbx)
xorl %r8d, %r8d
testb $0x1, %al
sete %r9b
testl %edx, %edx
jle 0x6d63c
movl %r14d, %r11d
andl $0x280, %r11d           # imm = 0x280
orl %r11d, %ebp
orl %eax, %ebp
movl %ebp, 0x10(%rsp)
movb %r9b, %r8b
leaq 0x8(,%r8,8), %rax
movq %rax, 0x68(%rsp)
movslq %esi, %r13
movslq %ecx, %rax
movq %rdx, 0x18(%rsp)
movl %edx, %ebp
movq %rax, 0x28(%rsp)
leaq (,%rax,4), %rax
movq %rax, 0x58(%rsp)
movq $0x0, 0x8(%rsp)
movq %rdi, 0x78(%rsp)
movl %esi, 0x14(%rsp)
testl %esi, %esi
movq %rdi, 0x60(%rsp)
jle 0x6d610
movq 0x18(%rsp), %rax
movl %eax, %r12d
subl 0x8(%rsp), %r12d
cmpl $0x4, %r12d
movl $0x4, %eax
cmovgel %eax, %r12d
movq %rdi, %rax
xorl %r15d, %r15d
leaq 0xc0(%rsp), %rdi
movq %r10, 0x20(%rsp)
movq %r10, %rsi
movl 0x10(%rsp), %edx
movq %rax, %rbx
callq 0x6ccd6
movq %rbx, 0x70(%rsp)
movq %rbx, %rax
xorl %ecx, %ecx
leaq 0x80(%rsp), %rdx
movq 0x28(%rsp), %r8
movq %rcx, %rsi
orq 0x8(%rsp), %rsi
movq %rsi, %rdi
imulq %r8, %rdi
addq 0x78(%rsp), %rdi
movq %rax, %r8
xorl %r9d, %r9d
cmpq %rbp, %rsi
setb %r11b
movq %r9, %r10
orq %r15, %r10
cmpq %r13, %r10
setl %bl
andb %r11b, %bl
cmpb $0x1, %bl
jne 0x6d416
btl $0x9, %r14d
jb 0x6d3f4
xorl %r10d, %r10d
movb (%r8,%r10), %r11b
movb %r11b, (%rdx,%r10)
incq %r10
cmpl $0x4, %r10d
jne 0x6d3e1
jmp 0x6d416
movb 0x2(%rdi,%r10,4), %r11b
movb %r11b, (%rdx)
movb 0x1(%rdi,%r10,4), %r11b
movb %r11b, 0x1(%rdx)
movb (%rdi,%r10,4), %r11b
movb %r11b, 0x2(%rdx)
movb 0x3(%rdi,%r10,4), %r10b
movb %r10b, 0x3(%rdx)
addq $0x4, %rdx
incq %r9
addq $0x4, %r8
cmpq $0x4, %r9
jne 0x6d3bc
incq %rcx
movq 0x28(%rsp), %r8
addq %r8, %rax
cmpq $0x4, %rcx
jne 0x6d3a2
movl 0x14(%rsp), %esi
movl %esi, %eax
subl %r15d, %eax
cmpl $0x4, %eax
movl $0x4, %ecx
cmovgel %ecx, %eax
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
xorl %ecx, %ecx
xorps %xmm0, %xmm0
xorpd %xmm1, %xmm1
leaq 0x80(%rsp), %rdx
leaq 0xc0(%rsp), %rbx
xorl %edi, %edi
movq %rbx, %r8
cmpl %r12d, %ecx
jae 0x6d52b
cmpl %eax, %edi
jae 0x6d52b
xorpd %xmm2, %xmm2
xorl %r9d, %r9d
movzbl (%rdx,%r9), %r10d
xorps %xmm3, %xmm3
cvtsi2sd %r10d, %xmm3
movzbl (%r8,%r9), %r11d
xorps %xmm4, %xmm4
cvtsi2sd %r11d, %xmm4
subsd %xmm4, %xmm3
addl %r10d, 0x40(%rsp,%r9,4)
movzbl (%rdx,%r9), %r10d
imull %r10d, %r10d
addl %r10d, 0x30(%rsp,%r9,4)
mulsd %xmm3, %xmm3
addsd %xmm3, %xmm2
incq %r9
cmpq $0x3, %r9
jne 0x6d493
movzbl 0x3(%rdx), %r9d
movb 0x3(%r8), %r10b
xorpd %xmm3, %xmm3
testb %r10b, %r10b
je 0x6d4eb
movapd %xmm2, %xmm3
testl %r9d, %r9d
je 0x6d4f4
movapd %xmm2, %xmm3
xorps %xmm2, %xmm2
cvtsi2sd %r9d, %xmm2
movzbl %r10b, %r10d
xorps %xmm4, %xmm4
cvtsi2sd %r10d, %xmm4
subsd %xmm4, %xmm2
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm0
addsd %xmm3, %xmm1
addl %r9d, 0x4c(%rsp)
movzbl 0x3(%rdx), %r9d
imull %r9d, %r9d
addl %r9d, 0x3c(%rsp)
addq $0x4, %rdx
addq $0x4, %r8
incl %edi
cmpl $0x4, %edi
jne 0x6d47b
addq $0x10, %rbx
incl %ecx
cmpl $0x4, %ecx
jne 0x6d476
imull %r12d, %eax
movd %eax, %xmm2
pshufd $0x0, %xmm2, %xmm2      # xmm2 = xmm2[0,0,0,0]
movdqa 0x30(%rsp), %xmm3
pshufd $0xf5, %xmm3, %xmm4     # xmm4 = xmm3[1,1,3,3]
pmuludq %xmm2, %xmm4
pshufd $0xe8, %xmm4, %xmm4     # xmm4 = xmm4[0,2,2,3]
pmuludq %xmm2, %xmm3
pshufd $0xe8, %xmm3, %xmm2     # xmm2 = xmm3[0,2,2,3]
punpckldq %xmm4, %xmm2    # xmm2 = xmm2[0],xmm4[0],xmm2[1],xmm4[1]
movdqa 0x40(%rsp), %xmm3
pshufd $0xf5, %xmm3, %xmm4     # xmm4 = xmm3[1,1,3,3]
pmuludq %xmm3, %xmm3
pshufd $0xe8, %xmm3, %xmm3     # xmm3 = xmm3[0,2,2,3]
pmuludq %xmm4, %xmm4
pshufd $0xe8, %xmm4, %xmm4     # xmm4 = xmm4[0,2,2,3]
punpckldq %xmm4, %xmm3    # xmm3 = xmm3[0],xmm4[0],xmm3[1],xmm4[1]
psubd %xmm3, %xmm2
pshufd $0xee, %xmm2, %xmm3     # xmm3 = xmm2[2,3,2,3]
paddd %xmm2, %xmm3
pshufd $0x55, %xmm3, %xmm2     # xmm2 = xmm3[1,1,1,1]
paddd %xmm3, %xmm2
movd %xmm2, %ecx
imull %eax, %eax
shll $0x2, %eax
cmpl %eax, %ecx
movq 0x140(%rsp), %rbx
jae 0x6d5d8
movsd 0x11110(%rip), %xmm2    # 0x7e6e0
mulsd %xmm2, %xmm0
mulsd %xmm2, %xmm1
movq 0x20(%rsp), %r10
addsd (%rbx), %xmm1
movsd %xmm1, (%rbx)
movq 0x148(%rsp), %rax
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
addq 0x68(%rsp), %r10
addq $0x4, %r15
movq 0x70(%rsp), %rax
addq $0x10, %rax
cmpl %esi, %r15d
jl 0x6d36f
movq 0x8(%rsp), %rcx
addq $0x4, %rcx
movq 0x60(%rsp), %rdi
addq 0x58(%rsp), %rdi
movq %rcx, 0x8(%rsp)
cmpq %rbp, %rcx
jb 0x6d342
movsd (%rbx), %xmm0
movq 0x18(%rsp), %rdx
jmp 0x6d63f
xorps %xmm0, %xmm0
imull %esi, %edx
leal (%rdx,%rdx,2), %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
movsd %xmm0, (%rbx)
movq 0x148(%rsp), %rax
movsd (%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax)
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq