// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/libsquish/alpha.cpp
// Function: squish::CompressAlphaDxt5(unsigned char const*, int, void*)
void CompressAlphaDxt5( u8 const* rgba, int mask, void* block )
{
    // get the range for 5-alpha and 7-alpha interpolation
    int min5 = 255;
    int max5 = 0;
    int min7 = 255;
    int max7 = 0;
    for( int i = 0; i < 16; ++i )
    {
        // check this pixel is valid
        int bit = 1 << i;
        if( ( mask & bit ) == 0 )
            continue;

        // incorporate into the min/max
        int value = rgba[4*i + 3];
        if( value < min7 )
            min7 = value;
        if( value > max7 )
            max7 = value;
        if( value != 0 && value < min5 )
            min5 = value;
        if( value != 255 && value > max5 )
            max5 = value;
    }

    // handle the case that no valid range was found
    if( min5 > max5 )
        min5 = max5;
    if( min7 > max7 )
        min7 = max7;

    // fix the range to be the minimum in each case
    FixRange( min5, max5, 5 );
    FixRange( min7, max7, 7 );

    // set up the 5-alpha code book
    u8 codes5[8];
    codes5[0] = ( u8 )min5;
    codes5[1] = ( u8 )max5;
    for( int i = 1; i < 5; ++i )
        codes5[1 + i] = ( u8 )( ( ( 5 - i )*min5 + i*max5 )/5 );
    codes5[6] = 0;
    codes5[7] = 255;

    // set up the 7-alpha code book
    u8 codes7[8];
    codes7[0] = ( u8 )min7;
    codes7[1] = ( u8 )max7;
    for( int i = 1; i < 7; ++i )
        codes7[1 + i] = ( u8 )( ( ( 7 - i )*min7 + i*max7 )/7 );

    // fit the data to both code books
    u8 indices5[16];
    u8 indices7[16];
    int err5 = FitCodes( rgba, mask, codes5, indices5 );
    int err7 = FitCodes( rgba, mask, codes7, indices7 );

    // save the block with least error
    if( err5 <= err7 )
        WriteAlphaBlock5( min5, max5, indices5, block );
    else
        WriteAlphaBlock7( min7, max7, indices7, block );
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, 0x10(%rsp)
movl %esi, %ebp
movq %rdi, %r14
movl $0xff, %ecx
xorl %edi, %edi
xorl %edx, %edx
xorl %eax, %eax
movl $0xff, %r10d
cmpq $0x10, %rdi
je 0x64ac2
btl %edi, %ebp
jae 0x64abd
movzbl 0x3(%r14,%rdi,4), %r8d
cmpl %r8d, %r10d
cmovgel %r8d, %r10d
cmpl %r8d, %eax
cmovlel %r8d, %eax
cmpl %r8d, %ecx
movl %r8d, %r9d
cmovll %ecx, %r9d
testl %r8d, %r8d
cmovnel %r9d, %ecx
cmpl %r8d, %edx
movl %r8d, %r9d
cmovgl %edx, %r9d
cmpl $0xff, %r8d
cmovnel %r9d, %edx
incq %rdi
jmp 0x64a78
leaq 0xc(%rsp), %r8
leaq 0x24(%rsp), %rdi
movl %edx, (%rdi)
movq %rdi, %rsi
cmpl %edx, %ecx
cmovll %ecx, %edx
movl %eax, (%r8)
leaq 0x4c(%rsp), %r12
movl %edx, (%r12)
cmpl %eax, %r10d
cmovll %r10d, %eax
leaq 0x20(%rsp), %r13
movl %eax, (%r13)
pushq $0x5
popq %rbx
movq %r12, %rdi
movl %ebx, %edx
callq 0x64c73
pushq $0x7
popq %r15
movq %r13, %rdi
leaq 0xc(%rsp), %rsi
movl %r15d, %edx
callq 0x64c73
movl (%r12), %eax
movb %al, 0x18(%rsp)
leaq 0x24(%rsp), %rcx
movl (%rcx), %r12d
movb %r12b, 0x19(%rsp)
leal (%r12,%rax,4), %ecx
movl %r12d, %esi
movq %rax, 0x40(%rsp)
subl %eax, %esi
pushq $0x2
popq %rdi
cmpq $0x6, %rdi
je 0x64b4f
movl %ecx, %eax
cltd
idivl %ebx
movb %al, 0x18(%rsp,%rdi)
addl %esi, %ecx
incq %rdi
jmp 0x64b39
movw $0xff00, 0x1e(%rsp)     # imm = 0xFF00
movl 0x20(%rsp), %r13d
movb %r13b, 0x28(%rsp)
movl 0xc(%rsp), %ebx
movb %bl, 0x29(%rsp)
imull $0x6, %r13d, %ecx
addl %ebx, %ecx
movl %ebx, %esi
subl %r13d, %esi
pushq $0x2
popq %rdi
cmpq $0x8, %rdi
je 0x64b8d
movl %ecx, %eax
cltd
idivl %r15d
movb %al, 0x28(%rsp,%rdi)
addl %esi, %ecx
incq %rdi
jmp 0x64b76
leaq 0x18(%rsp), %rdx
leaq 0x60(%rsp), %rcx
movq %r14, %rdi
movl %ebp, %esi
callq 0x64ca9
movl %eax, %r15d
leaq 0x28(%rsp), %rdx
leaq 0x50(%rsp), %rcx
movq %r14, %rdi
movl %ebp, %esi
callq 0x64ca9
cmpl %eax, %r15d
jle 0x64bed
cmpl %r13d, %ebx
jle 0x64c2e
xorl %eax, %eax
cmpq $0x10, %rax
je 0x64c3a
movzbl 0x50(%rsp,%rax), %edx
testl %edx, %edx
je 0x64bdc
cmpl $0x1, %edx
jne 0x64be0
xorl %ecx, %ecx
jmp 0x64be4
movb $0x1, %cl
jmp 0x64be4
movb $0x9, %cl
subb %dl, %cl
movb %cl, 0x30(%rsp,%rax)
incq %rax
jmp 0x64bc4
movq 0x40(%rsp), %rsi
cmpl %esi, %r12d
jge 0x64c46
xorl %eax, %eax
cmpq $0x10, %rax
je 0x64c52
movzbl 0x60(%rsp,%rax), %ecx
testl %ecx, %ecx
je 0x64c11
cmpl $0x1, %ecx
jne 0x64c15
xorl %ecx, %ecx
jmp 0x64c25
movb $0x1, %cl
jmp 0x64c25
movb $0x7, %dl
subb %cl, %dl
cmpb $0x6, %cl
movzbl %dl, %edx
movzbl %cl, %ecx
cmovbl %edx, %ecx
movb %cl, 0x30(%rsp,%rax)
incq %rax
jmp 0x64bf9
leaq 0x50(%rsp), %rdx
movl %r13d, %edi
movl %ebx, %esi
jmp 0x64c5a
leaq 0x30(%rsp), %rdx
movl %ebx, %edi
movl %r13d, %esi
jmp 0x64c5a
leaq 0x60(%rsp), %rdx
movl %esi, %edi
movl %r12d, %esi
jmp 0x64c5a
leaq 0x30(%rsp), %rdx
movl %r12d, %edi
movq 0x10(%rsp), %rcx
callq 0x64df4
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq