// File: /workspace/github_cpp_makefile_star2_O1/Alcaro[P]bridges/arlib/image-png-decode.cpp
// Function: image::png_defilter(unsigned char*, unsigned long, unsigned char const*, int, unsigned long, unsigned long)
bool image::png_defilter(uint8_t * out, size_t out_stride, const uint8_t * in, int bytes_per_pixel, size_t width_bytes, size_t height)
{
	for (size_t y=0;y<height;y++)
	{
		const uint8_t * defilter_in = in + y*(width_bytes+1) + 1;
		uint8_t filter_type = defilter_in[-1]; // cache this, in case input and output ends at same place
		
		uint8_t * defilter_out = out + y*out_stride;
		*(defilter_out++) = 0; // x=-1 is all zeroes (always use 4, not width_bytes, makes the math easier)
		*(defilter_out++) = 0;
		*(defilter_out++) = 0;
		*(defilter_out++) = 0;
		
		memmove(defilter_out, defilter_in, width_bytes);
		switch (filter_type)
		{
		case 0: // None
			break;
		
		case 1: // Sub
			for (size_t x=0;x<width_bytes;x++)
				defilter_out[x] += defilter_out[x-bytes_per_pixel];
			break;
		
		case 2: // Up
			for (size_t x=0;x<width_bytes;x++)
				defilter_out[x] += defilter_out[x-out_stride];
			break;
		
		case 3: // Average
			for (size_t x=0;x<width_bytes;x++)
			{
				int a = defilter_out[x-bytes_per_pixel];
				int b = defilter_out[x-out_stride];
				defilter_out[x] += (a+b)/2;
			}
			break;
		
		case 4: // Paeth
		{
#ifdef __SSE2__
#ifndef __SSSE3__
			auto _mm_abs_epi16 = [](__m128i a) -> __m128i {
				// abs(x) = (x^(x<0)) - (x<0)
				// however, that's four insns (three obvious, plus have to copy the argument)
				// instead, this one is three (or two, if first call reuses a zeroed reg from the a/b/c calculation)
				// abs(x) = max_i16(x, -x) (or min_u16(x, -x))
				return _mm_max_epi16(a, _mm_sub_epi16(_mm_setzero_si128(), a)); 
			};
#endif
			// ignore simding the other filters; Paeth is the most common, and also the most expensive per pixel
			// it can only be SIMDed to a degree of bytes_per_pixel, so ignore bytes_per_pixel=1 (and =2, gray+alpha, too rare)
			// (it's possible to interlace processing of two consecutive Paeth scanlines by having the upper one 1px ahead of the
			//    one below, but the required data structures would be quite annoying, and combining the bytes would be annoying too)
			// (it'd also be possible to assume the previous pixel was predicted Up aka B and discard the calculation if wrong,
			//    but it's 84.6% Left/A, 13% Up/B, 2.4% Diag/C, so such a thing would be wrong most of the time and not accomplish much)
			// macro instead of function is 7% faster
#define SIMD_PAETH(bytes_per_pixel) do { \
					uint32_t a32 = *(uint32_t*)(defilter_out+x           -bytes_per_pixel);          \
					uint32_t b32 = *(uint32_t*)(defilter_out+x-out_stride                );          \
					uint32_t c32 = *(uint32_t*)(defilter_out+x-out_stride-bytes_per_pixel);          \
					__m128i a = _mm_unpacklo_epi8(_mm_cvtsi32_si128(a32), _mm_setzero_si128());      \
					__m128i b = _mm_unpacklo_epi8(_mm_cvtsi32_si128(b32), _mm_setzero_si128());      \
					__m128i c = _mm_unpacklo_epi8(_mm_cvtsi32_si128(c32), _mm_setzero_si128());      \
					                                                                                 \
					__m128i p = _mm_sub_epi16(_mm_add_epi16(a, b), c);                               \
					__m128i pa = _mm_abs_epi16(_mm_sub_epi16(p, a));                                 \
					__m128i pb = _mm_abs_epi16(_mm_sub_epi16(p, b));                                 \
					__m128i pc = _mm_abs_epi16(_mm_sub_epi16(p, c));                                 \
					                                                                                 \
					/* 0 to use pa, -1 for something else */                                         \
					__m128i pa_skip = _mm_cmplt_epi16(_mm_min_epi16(pb, pc), pa);                    \
					/* -1 to use pc, 0 for something else */                                         \
					__m128i pc_use = _mm_and_si128(pa_skip, _mm_cmplt_epi16(pc, pb));                \
					__m128i pb_use = _mm_andnot_si128(pc_use, pa_skip);                              \
					                                                                                 \
					__m128i prediction = _mm_or_si128(_mm_andnot_si128(pa_skip, a),                  \
					                     _mm_or_si128(_mm_and_si128(   pb_use, b),                   \
					                                  _mm_and_si128(   pc_use, c)));                 \
					                                                                                 \
					uint32_t* outp = (uint32_t*)(defilter_out+x);                                    \
					__m128i out = _mm_add_epi8(_mm_cvtsi32_si128(*outp),                             \
					                           _mm_packus_epi16(prediction, _mm_undefined_si128())); \
					uint32_t out32 = _mm_cvtsi128_si32(out);                                         \
					if (bytes_per_pixel == 3)                                                        \
						out32 = (out32&0xFFFFFF00) | (*outp&0x000000FF);                             \
					*outp = out32;                                                                   \
				} while(0)
			if (bytes_per_pixel == 3)
			{
				//one pixel at the time, no need for a scalar tail loop
				//shift one byte to the left, so we don't overflow the malloc at the last scanline (x=-1 is safe, it's all zero)
				//we don't actually change that byte, but it's UB anyways
				defilter_out--;
				for (size_t x=0;x<width_bytes;x+=3)
				{
					SIMD_PAETH(3);
				}
				break;
			}
			if (bytes_per_pixel == 4)
			{
				for (size_t x=0;x<width_bytes;x+=4)
				{
					SIMD_PAETH(4);
				}
				break;
			}
			
			// not SIMD_LOOP_TAIL, the below runs for bytes_per_pixel=1 aka palette
#endif
			for (size_t x=0;x<width_bytes;x++)
			{
				int a = defilter_out[x-bytes_per_pixel];
				int b = defilter_out[x-out_stride];
				int c = defilter_out[x-out_stride-bytes_per_pixel];
				
				int p = a+b-c;
				int pa = abs(p-a);
				int pb = abs(p-b);
				int pc = abs(p-c);
				
				int prediction;
				if (pa <= pb && pa <= pc) prediction = a;
				else if (pb <= pc) prediction = b;
				else prediction = c;
				
				defilter_out[x] += prediction;
			}
		}
		break;
		
		default:
			return false;
		}
	}
	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, 0x60(%rsp)
movq %r9, 0x58(%rsp)
testq %r9, %r9
sete 0xf(%rsp)
je 0xb7f9
movq %r8, %r14
movq %rsi, %r11
movq %rdi, %rbx
leaq 0x1(%r8), %rax
movq %rax, 0x50(%rsp)
movq %rsi, %rbp
negq %rbp
movl %ecx, 0x1c(%rsp)
movslq %ecx, %r13
leaq 0x4(%rdi), %rax
movq %rax, 0x20(%rsp)
leaq 0x3(%rdi), %rax
movq %rax, 0x10(%rsp)
movl $0x4, %eax
subq %rsi, %rax
leaq (%rdi,%rax), %rcx
movq %rcx, 0x30(%rsp)
subq %r13, %rax
addq %rdi, %rax
movq %rax, 0x38(%rsp)
movq %rdi, %rax
subq %r13, %rax
addq $0x4, %rax
movq %rax, 0x28(%rsp)
negq %r13
xorl %r15d, %r15d
movq %r13, 0x48(%rsp)
movq %r15, 0x40(%rsp)
imulq %r11, %r15
movq 0x40(%rsp), %rax
imulq 0x50(%rsp), %rax
movq 0x60(%rsp), %rcx
leaq (%rcx,%rax), %rsi
incq %rsi
movzbl -0x1(%rsi), %r12d
leaq 0x4(%rbx,%r15), %rdi
movl $0x0, -0x4(%rdi)
movq %r14, %rdx
movq %r14, %r13
movq %rbp, %r14
movq %rbx, %rbp
movq %r11, %rbx
callq 0x3600
pxor %xmm7, %xmm7
movq %rbx, %r11
movq %rbp, %rbx
movq %r14, %rbp
movq %r13, %r14
movq 0x48(%rsp), %r13
cmpq $0x4, %r12
ja 0xb7f9
leaq 0x243aa(%rip), %rcx     # 0x2f8c0
movslq (%rcx,%r12,4), %rax
addq %rcx, %rax
movq 0x20(%rsp), %r12
jmpq *%rax
testq %r14, %r14
je 0xb7c5
leaq (%r12,%r13), %rax
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
addb %dl, (%r12,%rcx)
incq %rcx
cmpq %rcx, %r14
jne 0xb533
jmp 0xb7c5
movl 0x1c(%rsp), %eax
cmpl $0x3, %eax
je 0xb678
cmpl $0x4, %eax
jne 0xb751
testq %r14, %r14
je 0xb7c5
movl (%rbx,%r15), %edx
leaq (%r12,%rbp), %rax
xorl %ecx, %ecx
movd %edx, %xmm0
punpcklbw %xmm7, %xmm0    # xmm0 = xmm0[0],xmm7[0],xmm0[1],xmm7[1],xmm0[2],xmm7[2],xmm0[3],xmm7[3],xmm0[4],xmm7[4],xmm0[5],xmm7[5],xmm0[6],xmm7[6],xmm0[7],xmm7[7]
movd (%rax,%rcx), %xmm2
punpcklbw %xmm7, %xmm2    # xmm2 = xmm2[0],xmm7[0],xmm2[1],xmm7[1],xmm2[2],xmm7[2],xmm2[3],xmm7[3],xmm2[4],xmm7[4],xmm2[5],xmm7[5],xmm2[6],xmm7[6],xmm2[7],xmm7[7]
movd -0x4(%rax,%rcx), %xmm1
punpcklbw %xmm7, %xmm1    # xmm1 = xmm1[0],xmm7[0],xmm1[1],xmm7[1],xmm1[2],xmm7[2],xmm1[3],xmm7[3],xmm1[4],xmm7[4],xmm1[5],xmm7[5],xmm1[6],xmm7[6],xmm1[7],xmm7[7]
movdqa %xmm2, %xmm5
paddw %xmm0, %xmm5
psubw %xmm1, %xmm5
movdqa %xmm5, %xmm3
psubw %xmm0, %xmm3
pxor %xmm4, %xmm4
psubw %xmm3, %xmm4
pmaxsw %xmm3, %xmm4
movdqa %xmm5, %xmm6
psubw %xmm2, %xmm6
pxor %xmm3, %xmm3
psubw %xmm6, %xmm3
pmaxsw %xmm6, %xmm3
psubw %xmm1, %xmm5
pxor %xmm6, %xmm6
psubw %xmm5, %xmm6
pmaxsw %xmm5, %xmm6
movdqa %xmm3, %xmm5
pminsw %xmm6, %xmm5
pcmpgtw %xmm5, %xmm4
pcmpgtw %xmm6, %xmm3
pand %xmm4, %xmm3
movdqa %xmm3, %xmm5
pandn %xmm2, %xmm5
pand %xmm4, %xmm5
pandn %xmm0, %xmm4
pand %xmm1, %xmm3
por %xmm4, %xmm3
por %xmm5, %xmm3
packuswb %xmm3, %xmm3
movd (%r12,%rcx), %xmm0
paddb %xmm3, %xmm0
movd %xmm0, %edx
movd %xmm0, (%r12,%rcx)
addq $0x4, %rcx
cmpq %r14, %rcx
jb 0xb570
jmp 0xb7c5
testq %r14, %r14
je 0xb7c5
leaq (%r12,%rbp), %rax
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
addb %dl, (%r12,%rcx)
incq %rcx
cmpq %rcx, %r14
jne 0xb638
jmp 0xb7c5
movq %r12, %rax
movq %r14, %rcx
testq %r14, %r14
je 0xb7c5
movzbl (%rax,%r13), %edx
movzbl (%rax,%rbp), %esi
addl %edx, %esi
shrl %esi
addb %sil, (%rax)
incq %rax
decq %rcx
jne 0xb65b
jmp 0xb7c5
testq %r14, %r14
je 0xb7c5
movq 0x10(%rsp), %rax
addq %rbp, %rax
xorl %ecx, %ecx
movq 0x10(%rsp), %rdi
movd -0x3(%rdi,%rcx), %xmm0
punpcklbw %xmm7, %xmm0    # xmm0 = xmm0[0],xmm7[0],xmm0[1],xmm7[1],xmm0[2],xmm7[2],xmm0[3],xmm7[3],xmm0[4],xmm7[4],xmm0[5],xmm7[5],xmm0[6],xmm7[6],xmm0[7],xmm7[7]
movd (%rax,%rcx), %xmm2
punpcklbw %xmm7, %xmm2    # xmm2 = xmm2[0],xmm7[0],xmm2[1],xmm7[1],xmm2[2],xmm7[2],xmm2[3],xmm7[3],xmm2[4],xmm7[4],xmm2[5],xmm7[5],xmm2[6],xmm7[6],xmm2[7],xmm7[7]
movd -0x3(%rax,%rcx), %xmm1
punpcklbw %xmm7, %xmm1    # xmm1 = xmm1[0],xmm7[0],xmm1[1],xmm7[1],xmm1[2],xmm7[2],xmm1[3],xmm7[3],xmm1[4],xmm7[4],xmm1[5],xmm7[5],xmm1[6],xmm7[6],xmm1[7],xmm7[7]
movdqa %xmm2, %xmm5
paddw %xmm0, %xmm5
psubw %xmm1, %xmm5
movdqa %xmm5, %xmm3
psubw %xmm0, %xmm3
pxor %xmm4, %xmm4
psubw %xmm3, %xmm4
pmaxsw %xmm3, %xmm4
movdqa %xmm5, %xmm6
psubw %xmm2, %xmm6
pxor %xmm3, %xmm3
psubw %xmm6, %xmm3
pmaxsw %xmm6, %xmm3
psubw %xmm1, %xmm5
pxor %xmm6, %xmm6
psubw %xmm5, %xmm6
pmaxsw %xmm5, %xmm6
movdqa %xmm3, %xmm5
pminsw %xmm6, %xmm5
pcmpgtw %xmm5, %xmm4
pcmpgtw %xmm6, %xmm3
pand %xmm4, %xmm3
movdqa %xmm3, %xmm5
pandn %xmm2, %xmm5
pand %xmm4, %xmm5
pandn %xmm0, %xmm4
pand %xmm1, %xmm3
por %xmm4, %xmm3
por %xmm5, %xmm3
packuswb %xmm3, %xmm3
movl (%rdi,%rcx), %edx
movd %edx, %xmm0
paddb %xmm3, %xmm0
movd %xmm0, %esi
andl $0xffffff00, %esi       # imm = 0xFFFFFF00
movzbl %dl, %edx
orl %esi, %edx
movl %edx, (%rdi,%rcx)
addq $0x3, %rcx
cmpq %r14, %rcx
jb 0xb68b
jmp 0xb7c5
testq %r14, %r14
je 0xb7c5
xorl %eax, %eax
movq 0x28(%rsp), %rcx
movzbl (%rcx,%rax), %ecx
movq 0x30(%rsp), %rdx
movzbl (%rdx,%rax), %esi
movq 0x38(%rsp), %rdx
movzbl (%rdx,%rax), %edx
leal (%rsi,%rcx), %r10d
subl %edx, %r10d
movl %r10d, %r8d
subl %ecx, %r8d
movl %r8d, %edi
negl %edi
cmovsl %r8d, %edi
movl %r10d, %r9d
subl %esi, %r9d
movl %r9d, %r8d
negl %r8d
cmovsl %r9d, %r8d
subl %edx, %r10d
movl %r10d, %r9d
negl %r9d
cmovsl %r10d, %r9d
cmpl %r9d, %r8d
ja 0xb7ad
movl %esi, %edx
cmpl %r9d, %edi
cmoval %edx, %ecx
cmpl %r8d, %edi
cmoval %edx, %ecx
addb %cl, (%r12,%rax)
incq %rax
cmpq %rax, %r14
jne 0xb758
movq 0x40(%rsp), %r15
incq %r15
addq %r11, %r12
movq %r12, 0x20(%rsp)
addq %r11, 0x10(%rsp)
addq %r11, 0x38(%rsp)
addq %r11, 0x30(%rsp)
addq %r11, 0x28(%rsp)
cmpq 0x58(%rsp), %r15
setae 0xf(%rsp)
jne 0xb4ab
movb 0xf(%rsp), %al
andb $0x1, %al
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq