#include "memory.h"

#define USE_CUDA_MEMALLOC 0
#define USE_WC 1

void memalloc_host(int imgWidthF, int imgHeightF, unsigned char **input, 
	               int imgWidth, int imgHeight, unsigned char **output)
{
	cudaError_t cuda_ret;

#if USE_CUDA_MEMALLOC == 0
	*input   = (unsigned char *)malloc(3*imgWidthF*imgHeightF*sizeof(unsigned char));
	*output = (unsigned char *)malloc(3*imgWidth*imgHeight*sizeof(unsigned char));
#else
	#if USE_WC == 0
		cuda_ret = cudaHostAlloc(input, 3*imgWidthF*imgHeightF*sizeof(unsigned char), cudaHostAllocDefault);
	#else
		cuda_ret = cudaHostAlloc(input, 3*imgWidthF*imgHeightF*sizeof(unsigned char), cudaHostAllocWriteCombined);
	#endif
	cuda_ret = cudaHostAlloc(output, 3*imgWidth*imgHeight*sizeof(unsigned char), cudaHostAllocDefault);

#endif
}

void memfree_host(unsigned char **input, unsigned char **output)
{
	cudaError_t cuda_ret;

#if USE_CUDA_MEMALLOC == 0
	free(*input);
	free(*output);
#else
	cuda_ret = cudaFree(input);
	cuda_ret = cudaFree(output);
#endif
}
	.file	"tmpxft_00263966_00000000-6_memalloc_host.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2012:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2012:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z13memalloc_hostiiPPhiiS0_
	.type	_Z13memalloc_hostiiPPhiiS0_, @function
_Z13memalloc_hostiiPPhiiS0_:
.LFB2008:
	.cfi_startproc
	endbr64
	imull	%esi, %edi
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movq	%rdx, %r13
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movl	%r8d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movq	%r9, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	leal	(%rdi,%rdi,2), %edi
	movl	%ecx, %ebx
	pushq	%rax
	.cfi_def_cfa_offset 48
	imull	%r12d, %ebx
	movslq	%edi, %rdi
	call	malloc@PLT
	leal	(%rbx,%rbx,2), %edi
	movq	%rax, 0(%r13)
	movslq	%edi, %rdi
	call	malloc@PLT
	movq	%rax, 0(%rbp)
	popq	%rdx
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2008:
	.size	_Z13memalloc_hostiiPPhiiS0_, .-_Z13memalloc_hostiiPPhiiS0_
	.globl	_Z12memfree_hostPPhS0_
	.type	_Z12memfree_hostPPhS0_, @function
_Z12memfree_hostPPhS0_:
.LFB2009:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	(%rdi), %rdi
	movq	%rsi, %rbx
	call	free@PLT
	movq	(%rbx), %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	free@PLT
	.cfi_endproc
.LFE2009:
	.size	_Z12memfree_hostPPhS0_, .-_Z12memfree_hostPPhS0_
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2035:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2035:
	.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>
#include "memory.h"

#define USE_CUDA_MEMALLOC 0
#define USE_WC 1

void memalloc_host(int imgWidthF, int imgHeightF, unsigned char **input, 
	               int imgWidth, int imgHeight, unsigned char **output)
{
	hipError_t cuda_ret;

#if USE_CUDA_MEMALLOC == 0
	*input   = (unsigned char *)malloc(3*imgWidthF*imgHeightF*sizeof(unsigned char));
	*output = (unsigned char *)malloc(3*imgWidth*imgHeight*sizeof(unsigned char));
#else
	#if USE_WC == 0
		cuda_ret = hipHostAlloc(input, 3*imgWidthF*imgHeightF*sizeof(unsigned char), hipHostMallocDefault);
	#else
		cuda_ret = hipHostAlloc(input, 3*imgWidthF*imgHeightF*sizeof(unsigned char), hipHostMallocWriteCombined);
	#endif
	cuda_ret = hipHostAlloc(output, 3*imgWidth*imgHeight*sizeof(unsigned char), hipHostMallocDefault);

#endif
}

void memfree_host(unsigned char **input, unsigned char **output)
{
	hipError_t cuda_ret;

#if USE_CUDA_MEMALLOC == 0
	free(*input);
	free(*output);
#else
	cuda_ret = hipFree(input);
	cuda_ret = hipFree(output);
#endif
}
	.text
	.file	"memalloc_host.hip"
	.globl	_Z13memalloc_hostiiPPhiiS0_     # -- Begin function _Z13memalloc_hostiiPPhiiS0_
	.type	_Z13memalloc_hostiiPPhiiS0_,@function
_Z13memalloc_hostiiPPhiiS0_:            # @_Z13memalloc_hostiiPPhiiS0_
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%r9, %rbx
	movl	%r8d, %ebp
	movl	%ecx, %r14d
	movq	%rdx, %r15
                                        # kill: def $edi killed $edi def $rdi
	imull	%esi, %edi
	leal	(%rdi,%rdi,2), %eax
	movslq	%eax, %rdi
	callq	malloc
	movq	%rax, (%r15)
	imull	%ebp, %r14d
	leal	(%r14,%r14,2), %eax
	movslq	%eax, %rdi
	callq	malloc
	movq	%rax, (%rbx)
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z13memalloc_hostiiPPhiiS0_, .Lfunc_end0-_Z13memalloc_hostiiPPhiiS0_
	.cfi_endproc
                                        # -- End function
	.globl	_Z12memfree_hostPPhS0_          # -- Begin function _Z12memfree_hostPPhS0_
	.type	_Z12memfree_hostPPhS0_,@function
_Z12memfree_hostPPhS0_:                 # @_Z12memfree_hostPPhS0_
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rsi, %rbx
	movq	(%rdi), %rdi
	callq	free
	movq	(%rbx), %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	free                            # TAILCALL
.Lfunc_end1:
	.size	_Z12memfree_hostPPhS0_, .Lfunc_end1-_Z12memfree_hostPPhS0_
	.cfi_endproc
                                        # -- End function
	.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
