// File: /workspace/github_c_makefile_star2_O2/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
movq %r8, %r13
movl %ecx, %ebx
movl %edx, %r15d
movl %esi, %ebp
movq %rdi, 0x68(%rsp)
movq 0x148(%rsp), %r14
movq 0x140(%rsp), %r12
movl %r9d, %edi
callq 0x617da
movq %r13, %rsi
andq $0x0, (%r14)
andq $0x0, (%r12)
xorl %ecx, %ecx
movl %eax, 0x8(%rsp)
testb $0x1, %al
sete %cl
leaq 0x8(,%rcx,8), %rax
movq %rax, 0x50(%rsp)
movslq %ebx, %rax
movq %rax, 0x60(%rsp)
movq %r15, 0x10(%rsp)
movslq %r15d, %rax
movq %rax, 0x28(%rsp)
movl %ebp, 0x4(%rsp)
movslq %ebp, %rax
movq %rax, 0x58(%rsp)
xorl %r14d, %r14d
leaq 0xc0(%rsp), %rdi
cmpq 0x28(%rsp), %r14
jge 0x61f4c
movq 0x10(%rsp), %rax
movl %eax, %ebp
subl %r14d, %ebp
cmpl $0x4, %ebp
pushq $0x4
popq %rax
cmovgel %eax, %ebp
movq $0x0, 0x20(%rsp)
xorl %r15d, %r15d
movq %r14, 0x70(%rsp)
movl %ebp, 0xc(%rsp)
cmpq 0x58(%rsp), %r15
jge 0x61f43
movq %rsi, 0x18(%rsp)
movl 0x8(%rsp), %edx
callq 0x61820
xorl %r12d, %r12d
leaq 0x80(%rsp), %rbx
cmpq $0x4, %r12
je 0x61db4
movq %r12, 0x78(%rsp)
orq %r14, %r12
movq %r12, %rbp
imulq 0x60(%rsp), %rbp
addq 0x68(%rsp), %rbp
movq 0x20(%rsp), %r14
xorl %r13d, %r13d
cmpq $0x4, %r13
je 0x61da1
cmpq 0x28(%rsp), %r12
setl %al
leal (%r15,%r13), %ecx
cmpl 0x4(%rsp), %ecx
setl %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x61d94
movl %r14d, %edi
andl $-0x4, %edi
addq %rbp, %rdi
movq %rbx, %rsi
movl 0x8(%rsp), %edx
callq 0x618b8
addq $0x4, %rbx
incq %r13
addq $0x4, %r14
jmp 0x61d5f
movq 0x78(%rsp), %r12
incq %r12
movq 0x70(%rsp), %r14
movl 0xc(%rsp), %ebp
jmp 0x61d3b
movl 0x4(%rsp), %eax
subl %r15d, %eax
cmpl $0x4, %eax
pushq $0x4
popq %rcx
cmovgel %ecx, %eax
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
movapd %xmm0, 0x30(%rsp)
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
leaq 0xc0(%rsp), %rdi
movq %rdi, %rdx
leaq 0x80(%rsp), %rsi
cmpl $0x4, %ecx
je 0x61ec7
xorl %ebx, %ebx
movq %rdx, %r8
cmpl $0x4, %ebx
je 0x61ebc
cmpl %ebp, %ecx
jae 0x61ead
cmpl %eax, %ebx
jae 0x61ead
xorpd %xmm2, %xmm2
xorl %r9d, %r9d
cmpq $0x3, %r9
je 0x61e5d
movzbl (%rsi,%r9), %r10d
cvtsi2sd %r10d, %xmm3
movzbl (%r8,%r9), %r11d
cvtsi2sd %r11d, %xmm4
subsd %xmm4, %xmm3
addl %r10d, 0x40(%rsp,%r9,4)
mulsd %xmm3, %xmm3
addsd %xmm3, %xmm2
movzbl (%rsi,%r9), %r10d
imull %r10d, %r10d
addl %r10d, 0x30(%rsp,%r9,4)
incq %r9
jmp 0x61e1f
movzbl 0x3(%rsi), %r9d
movb 0x3(%r8), %r10b
xorpd %xmm3, %xmm3
testb %r10b, %r10b
je 0x61e73
movapd %xmm2, %xmm3
testl %r9d, %r9d
je 0x61e7c
movapd %xmm2, %xmm3
cvtsi2sd %r9d, %xmm2
movzbl %r10b, %r10d
cvtsi2sd %r10d, %xmm4
subsd %xmm4, %xmm2
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm0
addsd %xmm3, %xmm1
addl %r9d, 0x4c(%rsp)
movzbl 0x3(%rsi), %r9d
imull %r9d, %r9d
addl %r9d, 0x3c(%rsp)
addq $0x4, %rsi
addq $0x4, %r8
incl %ebx
jmp 0x61dff
addq $0x10, %rdx
incl %ecx
jmp 0x61df1
imull %ebp, %eax
xorl %ecx, %ecx
xorl %edx, %edx
cmpq $0x4, %rcx
je 0x61ef0
movl 0x30(%rsp,%rcx,4), %esi
imull %eax, %esi
movl 0x40(%rsp,%rcx,4), %r8d
imull %r8d, %r8d
addl %edx, %esi
subl %r8d, %esi
incq %rcx
movl %esi, %edx
jmp 0x61ece
imull %eax, %eax
shll $0x2, %eax
cmpl %eax, %edx
jae 0x61f0a
movsd 0x1163e(%rip), %xmm2    # 0x73540
mulsd %xmm2, %xmm0
mulsd %xmm2, %xmm1
movq 0x18(%rsp), %rsi
movq 0x140(%rsp), %rax
addsd (%rax), %xmm1
movsd %xmm1, (%rax)
movq 0x148(%rsp), %rax
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
addq 0x50(%rsp), %rsi
addq $0x4, %r15
addq $0x10, 0x20(%rsp)
jmp 0x61d17
addq $0x4, %r14
jmp 0x61ce4
movq 0x10(%rsp), %rcx
imull 0x4(%rsp), %ecx
leal (%rcx,%rcx,2), %eax
cvtsi2sd %eax, %xmm0
movq 0x140(%rsp), %rax
movsd (%rax), %xmm1
divsd %xmm0, %xmm1
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