// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x88, %rsp
movq %rdi, 0x80(%rsp)
movl %esi, 0x7c(%rsp)
movq %rdx, 0x70(%rsp)
movl $0xff, 0x6c(%rsp)
movl $0x0, 0x68(%rsp)
movl $0xff, 0x64(%rsp)
movl $0x0, 0x60(%rsp)
movl $0x0, 0x5c(%rsp)
cmpl $0x10, 0x5c(%rsp)
jge 0xa8851
movl 0x5c(%rsp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, 0x58(%rsp)
movl 0x7c(%rsp), %eax
andl 0x58(%rsp), %eax
cmpl $0x0, %eax
jne 0xa87c9
jmp 0xa8841
movq 0x80(%rsp), %rax
movl 0x5c(%rsp), %ecx
shll $0x2, %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl %eax, 0x54(%rsp)
movl 0x54(%rsp), %eax
cmpl 0x64(%rsp), %eax
jge 0xa87f8
movl 0x54(%rsp), %eax
movl %eax, 0x64(%rsp)
movl 0x54(%rsp), %eax
cmpl 0x60(%rsp), %eax
jle 0xa880a
movl 0x54(%rsp), %eax
movl %eax, 0x60(%rsp)
cmpl $0x0, 0x54(%rsp)
je 0xa8823
movl 0x54(%rsp), %eax
cmpl 0x6c(%rsp), %eax
jge 0xa8823
movl 0x54(%rsp), %eax
movl %eax, 0x6c(%rsp)
cmpl $0xff, 0x54(%rsp)
je 0xa883f
movl 0x54(%rsp), %eax
cmpl 0x68(%rsp), %eax
jle 0xa883f
movl 0x54(%rsp), %eax
movl %eax, 0x68(%rsp)
jmp 0xa8841
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0xa87a0
movl 0x6c(%rsp), %eax
cmpl 0x68(%rsp), %eax
jle 0xa8863
movl 0x68(%rsp), %eax
movl %eax, 0x6c(%rsp)
movl 0x64(%rsp), %eax
cmpl 0x60(%rsp), %eax
jle 0xa8875
movl 0x60(%rsp), %eax
movl %eax, 0x64(%rsp)
leaq 0x6c(%rsp), %rdi
leaq 0x68(%rsp), %rsi
movl $0x5, %edx
callq 0xa89e0
leaq 0x64(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movl $0x7, %edx
callq 0xa89e0
movl 0x6c(%rsp), %eax
movb %al, 0x4c(%rsp)
movl 0x68(%rsp), %eax
movb %al, 0x4d(%rsp)
movl $0x1, 0x48(%rsp)
cmpl $0x5, 0x48(%rsp)
jge 0xa88f9
movl $0x5, %eax
subl 0x48(%rsp), %eax
imull 0x6c(%rsp), %eax
movl 0x48(%rsp), %ecx
imull 0x68(%rsp), %ecx
addl %ecx, %eax
movl $0x5, %ecx
cltd
idivl %ecx
movb %al, %cl
movl 0x48(%rsp), %eax
addl $0x1, %eax
cltq
movb %cl, 0x4c(%rsp,%rax)
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0xa88b5
movb $0x0, 0x52(%rsp)
movb $-0x1, 0x53(%rsp)
movl 0x64(%rsp), %eax
movb %al, 0x40(%rsp)
movl 0x60(%rsp), %eax
movb %al, 0x41(%rsp)
movl $0x1, 0x3c(%rsp)
cmpl $0x7, 0x3c(%rsp)
jge 0xa895f
movl $0x7, %eax
subl 0x3c(%rsp), %eax
imull 0x64(%rsp), %eax
movl 0x3c(%rsp), %ecx
imull 0x60(%rsp), %ecx
addl %ecx, %eax
movl $0x7, %ecx
cltd
idivl %ecx
movb %al, %cl
movl 0x3c(%rsp), %eax
addl $0x1, %eax
cltq
movb %cl, 0x40(%rsp,%rax)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0xa891b
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %esi
leaq 0x4c(%rsp), %rdx
leaq 0x20(%rsp), %rcx
callq 0xa8a80
movl %eax, 0xc(%rsp)
movq 0x80(%rsp), %rdi
movl 0x7c(%rsp), %esi
leaq 0x40(%rsp), %rdx
leaq 0x10(%rsp), %rcx
callq 0xa8a80
movl %eax, 0x8(%rsp)
movl 0xc(%rsp), %eax
cmpl 0x8(%rsp), %eax
jg 0xa89c0
movl 0x6c(%rsp), %edi
movl 0x68(%rsp), %esi
leaq 0x20(%rsp), %rdx
movq 0x70(%rsp), %rcx
callq 0xa8ba0
jmp 0xa89d7
movl 0x64(%rsp), %edi
movl 0x60(%rsp), %esi
leaq 0x10(%rsp), %rdx
movq 0x70(%rsp), %rcx
callq 0xa8c90
addq $0x88, %rsp
retq
nop