// File: /workspace/github_c_makefile_star2_O1/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, %ebp
movq %r8, %rbx
movl %esi, 0x4(%rsp)
movq %rdi, 0x20(%rsp)
movl %r9d, %edi
andl $0x1f, %edi
movl %r9d, %esi
andl $0x160, %esi            # imm = 0x160
movd %edi, %xmm0
pshufd $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
pcmpeqd 0x11cd5(%rip), %xmm0    # 0x7d230
movmskps %xmm0, %eax
testl %eax, %eax
movl $0x1, %eax
cmovnel %edi, %eax
cmpl $0x100, %esi            # imm = 0x100
movl $0x20, %r9d
cmovel %esi, %r9d
movq 0x148(%rsp), %rdi
cmpl $0x40, %esi
cmovel %esi, %r9d
movq 0x140(%rsp), %r8
xorl %esi, %esi
movq %rsi, (%rdi)
movq %rsi, (%r8)
xorl %r8d, %r8d
testb $0x1, %al
sete %sil
movq %rdx, 0x10(%rsp)
testl %edx, %edx
jle 0x6b8e5
movl %ebp, %edi
andl $0x280, %edi            # imm = 0x280
orl %edi, %r9d
orl %eax, %r9d
movl %r9d, 0x1c(%rsp)
movb %sil, %r8b
leaq 0x8(,%r8,8), %rax
movq %rax, 0x68(%rsp)
movslq 0x4(%rsp), %r13
movslq %ecx, %rax
movl 0x10(%rsp), %r15d
movq %rax, 0x28(%rsp)
leaq (,%rax,4), %rax
movq %rax, 0x58(%rsp)
movq $0x0, 0x8(%rsp)
movq 0x20(%rsp), %rdx
cmpl $0x0, 0x4(%rsp)
movq %rdx, 0x60(%rsp)
jle 0x6b8c4
movq 0x10(%rsp), %rax
movl %eax, %r12d
subl 0x8(%rsp), %r12d
cmpl $0x4, %r12d
movl $0x4, %eax
cmovgel %eax, %r12d
movq %rdx, %rax
xorl %r14d, %r14d
leaq 0xc0(%rsp), %rdi
movq %rbx, 0x78(%rsp)
movq %rbx, %rsi
movl 0x1c(%rsp), %edx
movq %rax, %rbx
callq 0x6af8a
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 0x20(%rsp), %rdi
movq %rax, %r8
xorl %r9d, %r9d
cmpq %r15, %rsi
setb %r11b
movq %r9, %r10
orq %r14, %r10
cmpq %r13, %r10
setl %bl
andb %r11b, %bl
cmpb $0x1, %bl
jne 0x6b6d3
btl $0x9, %ebp
jb 0x6b6b1
xorl %r10d, %r10d
movb (%r8,%r10), %r11b
movb %r11b, (%rdx,%r10)
incq %r10
cmpl $0x4, %r10d
jne 0x6b69e
jmp 0x6b6d3
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 0x6b67a
incq %rcx
movq 0x28(%rsp), %r8
addq %r8, %rax
cmpq $0x4, %rcx
jne 0x6b660
movl 0x4(%rsp), %eax
subl %r14d, %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), %rsi
movq 0x78(%rsp), %rbx
xorl %edi, %edi
movq %rsi, %r8
cmpl %r12d, %ecx
jae 0x6b7e2
cmpl %eax, %edi
jae 0x6b7e2
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 0x6b753
movzbl 0x3(%rdx), %r9d
testl %r9d, %r9d
jne 0x6b7aa
cmpb $0x0, 0x3(%r8)
jne 0x6b7aa
xorpd %xmm2, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %r9d, %xmm3
movzbl 0x3(%r8), %r10d
xorps %xmm4, %xmm4
cvtsi2sd %r10d, %xmm4
subsd %xmm4, %xmm3
mulsd %xmm3, %xmm3
addsd %xmm3, %xmm0
addsd %xmm2, %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 0x6b73b
addq $0x10, %rsi
incl %ecx
cmpl $0x4, %ecx
jne 0x6b736
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
jae 0x6b887
movsd 0x119c1(%rip), %xmm2    # 0x7d240
mulsd %xmm2, %xmm0
mulsd %xmm2, %xmm1
movq 0x140(%rsp), %rax
addsd (%rax), %xmm1
movsd %xmm1, (%rax)
movq 0x148(%rsp), %rax
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
addq 0x68(%rsp), %rbx
addq $0x4, %r14
movq 0x70(%rsp), %rax
addq $0x10, %rax
cmpl 0x4(%rsp), %r14d
jl 0x6b62d
movq 0x8(%rsp), %rcx
addq $0x4, %rcx
movq 0x60(%rsp), %rdx
addq 0x58(%rsp), %rdx
movq %rcx, 0x8(%rsp)
cmpq %r15, %rcx
jb 0x6b5fd
movq 0x10(%rsp), %rcx
imull 0x4(%rsp), %ecx
leal (%rcx,%rcx,2), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq 0x140(%rsp), %rax
movsd (%rax), %xmm1
divsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd %xmm1, (%rax)
movq 0x148(%rsp), %rax
movsd (%rax), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rax)
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq