#include "deinterleave.cuh"

#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include <iostream>
#include <stdio.h>
#include "summed_area_table.cuh"

__global__ void deinterleave_kernel(unsigned char* image, unsigned int NxN, float* r, float* g, float* b) 
{
	int tid = threadIdx.x + blockIdx.x * blockDim.x;
	while (tid < NxN) {
		
		unsigned char* pixelOffset = image + tid * 3;
		r[tid] = pixelOffset[0];
		g[tid] = pixelOffset[1];
		b[tid] = pixelOffset[2];

		tid += blockDim.x * gridDim.x;
	}
}

void deinterleave(unsigned char* image, unsigned int NxN, float** r, float** g, float** b)
{
	
	//input: img
	//output: r, g, b

	unsigned char* dev_img;
	cudaMalloc((void**)&dev_img, 3 * NxN * sizeof(char));

	float* dev_r, *dev_g, *dev_b;
	cudaMalloc((void**)&dev_r, NxN * sizeof(float));
	cudaMalloc((void**)&dev_g, NxN * sizeof(float));
	cudaMalloc((void**)&dev_b, NxN * sizeof(float));
	
	cudaMemcpy(dev_img, image, 3 * NxN * sizeof(char), cudaMemcpyHostToDevice);

	deinterleave_kernel << <512, 512>> > (dev_img, NxN, dev_r, dev_g, dev_b);
	
	cudaMemcpy(*r, dev_r, NxN * sizeof(float), cudaMemcpyDeviceToHost);
	cudaMemcpy(*g, dev_g, NxN * sizeof(float), cudaMemcpyDeviceToHost);
	cudaMemcpy(*b, dev_b, NxN * sizeof(float), cudaMemcpyDeviceToHost);
	
	/*for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			std::cout << *r[i * 512 + j] << " ";
		}
		std::cout << std::endl;
	}
	std::cout << std::endl;*/

	cudaFree(dev_r);
	cudaFree(dev_g);
	cudaFree(dev_b);
}


//int main()
//{
//	//input: img
//	//output: r, g, b
//
//	unsigned int N = 4; //N x N image
//
//	unsigned char* img = (unsigned char*)malloc(3 * N * N * sizeof(char));
//	for (int i = 0; i < 3 * N * N; i++)
//		img[i] = 1;
//	unsigned int* r = (unsigned int*)malloc(N * N * sizeof(int));
//	unsigned int* g = (unsigned int*)malloc(N * N * sizeof(int));
//	unsigned int* b = (unsigned int*)malloc(N * N *sizeof(int));
//
//	deinterleave(img, N * N, &r, &g, &b);
//
//	/*for (int i = 0; i < N; i++)
//	{
//		std::cout << r[i] << " " << g[i] << " " << b[i] << " ";
//	}*/
//
//	summed_area_table(g, N);
//
//	for (int i = 0; i < N; i++)
//	{
//		for (int j = 0; j < N; j++)
//		{
//			std::cout << g[i * N + j] << " ";
//		}
//		std::cout << std::endl;
//	}
//
//
//	return 0;
//}

	.file	"tmpxft_002464d3_00000000-6_deinterleave.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3638:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3638:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z48__device_stub__Z19deinterleave_kernelPhjPfS0_S0_PhjPfS0_S0_
	.type	_Z48__device_stub__Z19deinterleave_kernelPhjPfS0_S0_PhjPfS0_S0_, @function
_Z48__device_stub__Z19deinterleave_kernelPhjPfS0_S0_PhjPfS0_S0_:
.LFB3660:
	.cfi_startproc
	endbr64
	subq	$168, %rsp
	.cfi_def_cfa_offset 176
	movq	%rdi, 40(%rsp)
	leaq	64(%rsp), %rdi
	movl	%esi, 36(%rsp)
	leaq	76(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	48(%rsp), %rdx
	movq	%rcx, 16(%rsp)
	leaq	56(%rsp), %rcx
	movq	%r8, 8(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 152(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 72(%rsp)
	movq	%rax, 112(%rsp)
	leaq	36(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 144(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 64(%rsp)
	movq	%rax, 76(%rsp)
	movl	$1, 84(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	56(%rsp)
	.cfi_def_cfa_offset 184
	leaq	_Z19deinterleave_kernelPhjPfS0_S0_(%rip), %rdi
	pushq	56(%rsp)
	.cfi_def_cfa_offset 192
	movq	92(%rsp), %rcx
	movl	100(%rsp), %r8d
	movq	80(%rsp), %rsi
	movl	88(%rsp), %edx
	leaq	128(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 184
	popq	%rdx
	.cfi_def_cfa_offset 176
.L2:
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$168, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3660:
	.size	_Z48__device_stub__Z19deinterleave_kernelPhjPfS0_S0_PhjPfS0_S0_, .-_Z48__device_stub__Z19deinterleave_kernelPhjPfS0_S0_PhjPfS0_S0_
	.globl	_Z19deinterleave_kernelPhjPfS0_S0_
	.type	_Z19deinterleave_kernelPhjPfS0_S0_, @function
_Z19deinterleave_kernelPhjPfS0_S0_:
.LFB3661:
	.cfi_startproc
	endbr64
	jmp	_Z48__device_stub__Z19deinterleave_kernelPhjPfS0_S0_PhjPfS0_S0_
	.cfi_endproc
.LFE3661:
	.size	_Z19deinterleave_kernelPhjPfS0_S0_, .-_Z19deinterleave_kernelPhjPfS0_S0_
	.globl	_Z12deinterleavePhjPPfS1_S1_
	.type	_Z12deinterleavePhjPPfS1_S1_, @function
_Z12deinterleavePhjPPfS1_S1_:
.LFB3635:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rdi, %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%rdx, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rcx, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%r8, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%esi, %ebx
	imull	$3, %ebx, %edx
	movq	%rbx, %rbp
	salq	$2, %rbx
	subq	$88, %rsp
	.cfi_def_cfa_offset 144
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	movq	%rdx, %rsi
	leaq	16(%rsp), %rdi
	movq	%rdx, 8(%rsp)
	call	cudaMalloc@PLT
	leaq	24(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	leaq	32(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	leaq	40(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movq	8(%rsp), %rdx
	movq	16(%rsp), %rdi
	movq	%r15, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	$8388609, %edi
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	salq	$9, %rdi
	movl	$1, %ecx
	movl	$1, %esi
	movq	%rdi, %rdx
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L9
	movq	40(%rsp), %r8
	movq	32(%rsp), %rcx
	movl	%ebp, %esi
	movq	24(%rsp), %rdx
	movq	16(%rsp), %rdi
	call	_Z48__device_stub__Z19deinterleave_kernelPhjPfS0_S0_PhjPfS0_S0_
.L9:
	movq	24(%rsp), %rsi
	movq	(%r14), %rdi
	movl	$2, %ecx
	movq	%rbx, %rdx
	call	cudaMemcpy@PLT
	movq	32(%rsp), %rsi
	movq	0(%r13), %rdi
	movq	%rbx, %rdx
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movq	40(%rsp), %rsi
	movq	(%r12), %rdi
	movq	%rbx, %rdx
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L10
	call	__stack_chk_fail@PLT
.L10:
	addq	$88, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3635:
	.size	_Z12deinterleavePhjPPfS1_S1_, .-_Z12deinterleavePhjPPfS1_S1_
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"_Z19deinterleave_kernelPhjPfS0_S0_"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3663:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC0(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z19deinterleave_kernelPhjPfS0_S0_(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE3663:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
void deinterleave(unsigned char* in_image, unsigned int NxN, float** out_r, float** out_g, float** out_b);
	.text
	.file	"deinterleave.hip"
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
