// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movq %rdx, %rbx
movl %esi, %ebp
movq %rdi, %r14
movl $0xff, %eax
xorl %edi, %edi
xorl %ecx, %ecx
xorl %esi, %esi
movl $0xff, %edx
btl %edi, %ebp
jae 0x6e6a2
movzbl 0x3(%r14,%rdi,4), %r8d
cmpl %r8d, %eax
cmovgel %r8d, %eax
cmpl %r8d, %ecx
cmovlel %r8d, %ecx
cmpl %r8d, %edx
movl %r8d, %r9d
cmovll %edx, %r9d
testl %r8d, %r8d
cmovnel %r9d, %edx
cmpl %r8d, %esi
movl %r8d, %r9d
cmovgl %esi, %r9d
cmpl $0xff, %r8d
cmovnel %r9d, %esi
incq %rdi
cmpq $0x10, %rdi
jne 0x6e663
cmpl %esi, %edx
cmovgel %esi, %edx
cmpl %ecx, %eax
cmovgel %ecx, %eax
movl %esi, %edi
subl %edx, %edi
movl $0xfa, %r8d
cmpl %r8d, %edx
cmovll %edx, %r8d
addl $0x5, %r8d
cmpl $0x5, %edi
cmovgel %esi, %r8d
movl %r8d, %esi
subl %edx, %esi
cmpl $0x6, %r8d
movl $0x5, %r9d
cmovgel %r8d, %r9d
addl $-0x5, %r9d
cmpl $0x5, %esi
cmovgel %edx, %r9d
movl %ecx, %edx
subl %eax, %edx
movl $0xf8, %r13d
cmpl %r13d, %eax
cmovll %eax, %r13d
addl $0x7, %r13d
cmpl $0x7, %edx
cmovgel %ecx, %r13d
movl %r13d, %ecx
subl %eax, %ecx
cmpl $0x8, %r13d
movl $0x7, %esi
cmovgel %r13d, %esi
addl $-0x7, %esi
movb %r9b, 0xc(%rsp)
movb %r8b, 0xd(%rsp)
movl %r8d, 0x18(%rsp)
movd %r8d, %xmm0
pshufd $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movd %r9d, %xmm1
pshufd $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0]
movdqa 0xf0d5(%rip), %xmm2     # 0x7d820
pshufd $0xf5, %xmm2, %xmm3     # xmm3 = xmm2[1,1,3,3]
pmuludq %xmm0, %xmm3
pshufd $0xe8, %xmm3, %xmm3     # xmm3 = xmm3[0,2,2,3]
pmuludq %xmm2, %xmm0
pshufd $0xe8, %xmm0, %xmm0     # xmm0 = xmm0[0,2,2,3]
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movdqa 0xf0c2(%rip), %xmm2     # 0x7d830
pshufd $0xf5, %xmm2, %xmm3     # xmm3 = xmm2[1,1,3,3]
pmuludq %xmm1, %xmm3
pshufd $0xe8, %xmm3, %xmm3     # xmm3 = xmm3[0,2,2,3]
pmuludq %xmm2, %xmm1
pshufd $0xe8, %xmm1, %xmm1     # xmm1 = xmm1[0,2,2,3]
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
paddd %xmm0, %xmm1
movdqa 0xf0ab(%rip), %xmm0     # 0x7d840
pshufd $0xf5, %xmm1, %xmm2     # xmm2 = xmm1[1,1,3,3]
pxor %xmm3, %xmm3
pcmpgtd %xmm1, %xmm3
pmuludq %xmm0, %xmm1
pshufd $0xed, %xmm1, %xmm1     # xmm1 = xmm1[1,3,2,3]
pmuludq %xmm0, %xmm2
pshufd $0xed, %xmm2, %xmm2     # xmm2 = xmm2[1,3,2,3]
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
pand %xmm0, %xmm3
psubd %xmm3, %xmm1
movdqa %xmm1, %xmm0
psrld $0x1f, %xmm0
psrld $0x1, %xmm1
paddd %xmm0, %xmm1
pand 0xf076(%rip), %xmm1     # 0x7d850
packuswb %xmm1, %xmm1
packuswb %xmm1, %xmm1
cmpl $0x7, %ecx
cmovgel %eax, %esi
movd %xmm1, 0xe(%rsp)
movw $0xff00, 0x12(%rsp)     # imm = 0xFF00
movb %sil, 0x4(%rsp)
movb %r13b, 0x5(%rsp)
leal (%rsi,%rsi,4), %ecx
leal (,%r13,2), %eax
leal (%rcx,%r13,2), %ecx
subl %esi, %eax
subl %esi, %eax
movq %rsi, 0x38(%rsp)
leal (%rsi,%rsi,2), %edx
leal (%r13,%rdx,2), %edx
xorl %esi, %esi
cmpq $0x5, %rsi
ja 0x6e86b
movslq %edx, %rdi
imulq $-0x6db6db6d, %rdi, %rdi # imm = 0x92492493
shrq $0x20, %rdi
addl %edx, %edi
movl %edi, %r8d
shrl $0x1f, %r8d
shrl $0x2, %edi
addl %r8d, %edi
movb %dil, 0x6(%rsp,%rsi)
movslq %ecx, %rdi
imulq $-0x6db6db6d, %rdi, %rdi # imm = 0x92492493
shrq $0x20, %rdi
addl %ecx, %edi
movl %edi, %r8d
shrl $0x1f, %r8d
shrl $0x2, %edi
addl %r8d, %edi
movb %dil, 0x7(%rsp,%rsi)
addq $0x2, %rsi
addl %eax, %ecx
addl %eax, %edx
cmpq $0x6, %rsi
jne 0x6e821
movl %r9d, 0x14(%rsp)
leaq 0xc(%rsp), %rdx
leaq 0x50(%rsp), %r15
movq %r14, %rdi
movl %ebp, %esi
movq %r15, %rcx
callq 0x6ea7d
movl %eax, 0x1c(%rsp)
leaq 0x4(%rsp), %rdx
leaq 0x40(%rsp), %r12
movq %r14, %rdi
movl %ebp, %esi
movq %r12, %rcx
callq 0x6ea7d
cmpl %eax, 0x1c(%rsp)
jle 0x6e945
movq 0x38(%rsp), %rsi
cmpl %r13d, %esi
jge 0x6e9de
xorl %eax, %eax
movzbl 0x40(%rsp,%rax), %edx
testl %edx, %edx
je 0x6e8dc
cmpl $0x1, %edx
jne 0x6e8e0
xorl %ecx, %ecx
jmp 0x6e8e4
movb $0x1, %cl
jmp 0x6e8e4
movb $0x9, %cl
subb %dl, %cl
movb %cl, 0x20(%rsp,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x6e8ca
movb %r13b, (%rbx)
movb %sil, 0x1(%rbx)
addq $0x2, %rbx
xorl %edx, %edx
leaq 0x20(%rsp), %rax
xorl %ecx, %ecx
movq %rax, %rdi
xorl %esi, %esi
movzbl (%rdi), %r8d
shll %cl, %r8d
incq %rdi
orl %r8d, %esi
addl $0x3, %ecx
cmpl $0x18, %ecx
jne 0x6e90a
xorl %ecx, %ecx
movl %esi, %edi
sarl %cl, %edi
movb %dil, (%rbx)
incq %rbx
addl $0x8, %ecx
cmpl $0x18, %ecx
jne 0x6e921
addq $0x8, %rax
leal 0x1(%rdx), %ecx
testl %edx, %edx
movl %ecx, %edx
je 0x6e903
jmp 0x6ea6e
movl 0x18(%rsp), %esi
movl 0x14(%rsp), %edi
cmpl %esi, %edi
jle 0x6ea27
xorl %eax, %eax
movzbl 0x50(%rsp,%rax), %ecx
testl %ecx, %ecx
je 0x6e969
cmpl $0x1, %ecx
jne 0x6e96d
xorl %ecx, %ecx
jmp 0x6e97d
movb $0x1, %cl
jmp 0x6e97d
movb $0x7, %dl
subb %cl, %dl
cmpb $0x6, %cl
movzbl %dl, %edx
movzbl %cl, %ecx
cmovbl %edx, %ecx
movb %cl, 0x20(%rsp,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x6e957
movb %sil, (%rbx)
movb %dil, 0x1(%rbx)
addq $0x2, %rbx
xorl %edx, %edx
leaq 0x20(%rsp), %rax
xorl %ecx, %ecx
movq %rax, %rdi
xorl %esi, %esi
movzbl (%rdi), %r8d
shll %cl, %r8d
incq %rdi
orl %r8d, %esi
addl $0x3, %ecx
cmpl $0x18, %ecx
jne 0x6e9a3
xorl %ecx, %ecx
movl %esi, %edi
sarl %cl, %edi
movb %dil, (%rbx)
incq %rbx
addl $0x8, %ecx
cmpl $0x18, %ecx
jne 0x6e9ba
addq $0x8, %rax
leal 0x1(%rdx), %ecx
testl %edx, %edx
movl %ecx, %edx
je 0x6e99c
jmp 0x6ea6e
movb %sil, (%rbx)
movb %r13b, 0x1(%rbx)
addq $0x2, %rbx
xorl %eax, %eax
xorl %ecx, %ecx
movq %r12, %rsi
xorl %edx, %edx
movzbl (%rsi), %edi
shll %cl, %edi
incq %rsi
orl %edi, %edx
addl $0x3, %ecx
cmpl $0x18, %ecx
jne 0x6e9f2
xorl %ecx, %ecx
movl %edx, %esi
sarl %cl, %esi
movb %sil, (%rbx)
incq %rbx
addl $0x8, %ecx
cmpl $0x18, %ecx
jne 0x6ea06
addq $0x8, %r12
leal 0x1(%rax), %ecx
testl %eax, %eax
movl %ecx, %eax
je 0x6e9eb
jmp 0x6ea6e
movb %dil, (%rbx)
movb %sil, 0x1(%rbx)
addq $0x2, %rbx
xorl %eax, %eax
xorl %ecx, %ecx
movq %r15, %rsi
xorl %edx, %edx
movzbl (%rsi), %edi
shll %cl, %edi
incq %rsi
orl %edi, %edx
addl $0x3, %ecx
cmpl $0x18, %ecx
jne 0x6ea3b
xorl %ecx, %ecx
movl %edx, %esi
sarl %cl, %esi
movb %sil, (%rbx)
incq %rbx
addl $0x8, %ecx
cmpl $0x18, %ecx
jne 0x6ea4f
addq $0x8, %r15
leal 0x1(%rax), %ecx
testl %eax, %eax
movl %ecx, %eax
je 0x6ea34
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq