// File: /workspace/github_c_makefile_star2_O3/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 $0xff, %r13d
xorl %eax, %eax
xorl %r8d, %r8d
xorl %ebp, %ebp
movl $0xff, %r14d
btl %eax, %esi
jae 0x70420
movzbl 0x3(%rdi,%rax,4), %ecx
cmpl %ecx, %r13d
cmovgel %ecx, %r13d
cmpl %ecx, %r8d
cmovlel %ecx, %r8d
cmpl %ecx, %r14d
movl %ecx, %edx
cmovll %r14d, %edx
testl %ecx, %ecx
cmovnel %edx, %r14d
cmpl %ecx, %ebp
movl %ecx, %edx
cmovgl %ebp, %edx
cmpl $0xff, %ecx
cmovnel %edx, %ebp
incq %rax
cmpq $0x10, %rax
jne 0x703e9
cmpl %ebp, %r14d
cmovgel %ebp, %r14d
cmpl %r8d, %r13d
cmovgel %r8d, %r13d
movl %ebp, %eax
subl %r14d, %eax
cmpl $0x5, %eax
jge 0x70463
movl $0xfa, %eax
cmpl %eax, %r14d
cmovll %r14d, %eax
leal 0x5(%rax), %ebp
movl %ebp, %ecx
subl %r14d, %ecx
cmpl $0x4, %ecx
jg 0x70463
xorl %r14d, %r14d
testl %eax, %eax
cmovgl %eax, %r14d
movl %r8d, %eax
subl %r13d, %eax
cmpl $0x6, %eax
jg 0x70492
movl $0xf8, %eax
cmpl %eax, %r13d
cmovll %r13d, %eax
leal 0x7(%rax), %r8d
movl %r8d, %ecx
subl %r13d, %ecx
cmpl $0x6, %ecx
jg 0x70492
xorl %r13d, %r13d
testl %eax, %eax
cmovgl %eax, %r13d
movb %r14b, 0x10(%rsp)
movb %bpl, 0x11(%rsp)
movd %ebp, %xmm0
pshufd $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
movd %r14d, %xmm1
pshufd $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0]
movdqa 0xe819(%rip), %xmm2     # 0x7ecd0
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 0xe806(%rip), %xmm2     # 0x7ece0
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 0xe7ef(%rip), %xmm0     # 0x7ecf0
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 0x72aa(%rip), %xmm1     # 0x777f0
packuswb %xmm1, %xmm1
packuswb %xmm1, %xmm1
movd %xmm1, 0x12(%rsp)
movw $0xff00, 0x16(%rsp)     # imm = 0xFF00
movb %r13b, 0x8(%rsp)
movb %r8b, 0x9(%rsp)
leal (%r13,%r13,4), %ecx
leal (%r8,%r8), %eax
leal (%rcx,%r8,2), %ecx
subl %r13d, %eax
subl %r13d, %eax
leal (%r13,%r13,2), %edx
movq %r8, 0x30(%rsp)
leal (%r8,%rdx,2), %edx
xorl %r9d, %r9d
cmpq $0x5, %r9
ja 0x705d7
movslq %edx, %r8
imulq $-0x6db6db6d, %r8, %r10 # imm = 0x92492493
shrq $0x20, %r10
addl %edx, %r10d
movl %r10d, %r8d
shrl $0x1f, %r8d
shrl $0x2, %r10d
addl %r8d, %r10d
movb %r10b, 0xa(%rsp,%r9)
movslq %ecx, %r8
imulq $-0x6db6db6d, %r8, %r10 # imm = 0x92492493
shrq $0x20, %r10
addl %ecx, %r10d
movl %r10d, %r8d
shrl $0x1f, %r8d
shrl $0x2, %r10d
addl %r8d, %r10d
movb %r10b, 0xb(%rsp,%r9)
addq $0x2, %r9
addl %eax, %ecx
addl %eax, %edx
cmpq $0x6, %r9
jne 0x70589
leaq 0x10(%rsp), %rdx
leaq 0x50(%rsp), %r15
movq %r15, %rcx
movl %esi, 0x18(%rsp)
movq %rdi, 0x38(%rsp)
callq 0x707d7
movl %eax, 0x1c(%rsp)
leaq 0x8(%rsp), %rdx
leaq 0x40(%rsp), %r12
movq 0x38(%rsp), %rdi
movl 0x18(%rsp), %esi
movq %r12, %rcx
callq 0x707d7
cmpl %eax, 0x1c(%rsp)
jle 0x706b2
movq 0x30(%rsp), %rsi
cmpl %esi, %r13d
jge 0x70742
xorl %eax, %eax
movzbl 0x40(%rsp,%rax), %edx
testl %edx, %edx
je 0x7064b
cmpl $0x1, %edx
jne 0x7064f
xorl %ecx, %ecx
jmp 0x70653
movb $0x1, %cl
jmp 0x70653
movb $0x9, %cl
subb %dl, %cl
movb %cl, 0x20(%rsp,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x70639
movb %sil, (%rbx)
movb %r13b, 0x1(%rbx)
addq $0x2, %rbx
movb $0x1, %sil
leaq 0x20(%rsp), %rax
xorl %ecx, %ecx
movq %rax, %rdi
xorl %edx, %edx
movzbl (%rdi), %r8d
shll %cl, %r8d
incq %rdi
orl %r8d, %edx
addl $0x3, %ecx
cmpl $0x18, %ecx
jne 0x7067a
movb %dl, (%rbx)
movb %dh, 0x1(%rbx)
shrl $0x10, %edx
movb %dl, 0x2(%rbx)
addq $0x3, %rbx
addq $0x8, %rax
testb $0x1, %sil
movl $0x0, %esi
jne 0x70673
jmp 0x707c8
cmpl %ebp, %r14d
jle 0x70786
xorl %eax, %eax
movzbl 0x50(%rsp,%rax), %ecx
testl %ecx, %ecx
je 0x706cf
cmpl $0x1, %ecx
jne 0x706d3
xorl %ecx, %ecx
jmp 0x706e3
movb $0x1, %cl
jmp 0x706e3
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 0x706bd
movb %bpl, (%rbx)
movb %r14b, 0x1(%rbx)
addq $0x2, %rbx
movb $0x1, %sil
leaq 0x20(%rsp), %rax
xorl %ecx, %ecx
movq %rax, %rdi
xorl %edx, %edx
movzbl (%rdi), %r8d
shll %cl, %r8d
incq %rdi
orl %r8d, %edx
addl $0x3, %ecx
cmpl $0x18, %ecx
jne 0x7070a
movb %dl, (%rbx)
movb %dh, 0x1(%rbx)
shrl $0x10, %edx
movb %dl, 0x2(%rbx)
addq $0x3, %rbx
addq $0x8, %rax
testb $0x1, %sil
movl $0x0, %esi
jne 0x70703
jmp 0x707c8
movb %r13b, (%rbx)
movb %sil, 0x1(%rbx)
addq $0x2, %rbx
movb $0x1, %al
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 0x70756
movb %dl, (%rbx)
movb %dh, 0x1(%rbx)
shrl $0x10, %edx
movb %dl, 0x2(%rbx)
addq $0x3, %rbx
addq $0x8, %r12
testb $0x1, %al
movl $0x0, %eax
jne 0x7074f
jmp 0x707c8
movb %r14b, (%rbx)
movb %bpl, 0x1(%rbx)
addq $0x2, %rbx
movb $0x1, %al
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 0x7079a
movb %dl, (%rbx)
movb %dh, 0x1(%rbx)
shrl $0x10, %edx
movb %dl, 0x2(%rbx)
addq $0x3, %rbx
addq $0x8, %r15
testb $0x1, %al
movl $0x0, %eax
jne 0x70793
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq