#include <stdio.h>

/* Function that normalizes the filter on the max element */
void gaussNorm(float *data,int size){
	float max=0;
	for (int i=0;i<size*size;i++)
		if(data[i]>max) max = data[i];
	for (int i=0;i<size*size;i++)
		data[i]/=max;
}

/* This funtion calculates the 2D gussian filter gaussFilter[size*size]
 * It is based on MATLAB's fspecial('gaussian',size,sigma) function's algorithm.
 */
float * gaussFilter(int size, float sigma){
	/* Get the mesh's rows and columns */
	float rows = ((float)size-1)/2;
	float cols = ((float)size-1)/2;
	/* Allocate the final array  */
	float *filter = (float *)malloc(size*size*sizeof(float));
	/* In the following loops a mesh is simulated from -rows to +rows and
      	 * from -cols to cols with step 1.
	 * Every element el[i][j]  of the 2D filter is calculated with the following formula
	 * el[i][j] = exp (- (x^2 + y^2) / (2*sigma^2)) / sum (el)
	 * where x and y are the mesh's elements for the specific filter's element.
	 */
	float sum = 0;
	float x=-rows;
	for (int i = 0; i<size; i++){
		float y=-cols;
		for (int j=0; j<size; j++){
			filter[i*size + j] = exp(- (x*x + y*y)/ (2*sigma*sigma));		
			sum += filter[i*size + j];
			/* Step = 1 */
			y++;
		}
		x++;
	}
	for (int i=0;i<size*size;i++)
		filter[i] =filter[i] / sum ;		

	return filter;
}

	.file	"tmpxft_002c1eb1_00000000-6_gaussian.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2031:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2031:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z9gaussNormPfi
	.type	_Z9gaussNormPfi, @function
_Z9gaussNormPfi:
.LFB2027:
	.cfi_startproc
	endbr64
	imull	%esi, %esi
	xorl	%eax, %eax
	xorps	%xmm0, %xmm0
.L3:
	cmpl	%eax, %esi
	jle	.L9
	movss	(%rdi,%rax,4), %xmm1
	incq	%rax
	maxss	%xmm0, %xmm1
	movaps	%xmm1, %xmm0
	jmp	.L3
.L9:
	xorl	%eax, %eax
.L6:
	cmpl	%eax, %esi
	jle	.L10
	movss	(%rdi,%rax,4), %xmm1
	divss	%xmm0, %xmm1
	movss	%xmm1, (%rdi,%rax,4)
	incq	%rax
	jmp	.L6
.L10:
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z9gaussNormPfi, .-_Z9gaussNormPfi
	.globl	_Z11gaussFilterif
	.type	_Z11gaussFilterif, @function
_Z11gaussFilterif:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	xorl	%r15d, %r15d
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movslq	%edi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%r12d, %ebp
	imull	%r12d, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%r12, %rbx
	movslq	%ebp, %rdi
	subq	$56, %rsp
	.cfi_def_cfa_offset 112
	salq	$2, %rdi
	movss	%xmm0, 12(%rsp)
	call	malloc@PLT
	cvtsi2ssl	%r12d, %xmm1
	subss	.LC1(%rip), %xmm1
	xorps	%xmm4, %xmm4
	mulss	.LC2(%rip), %xmm1
	movq	%rax, %r13
	leaq	0(,%r12,4), %rax
	movss	.LC3(%rip), %xmm5
	movq	%rax, 24(%rsp)
	movq	%r13, %r14
	xorps	%xmm5, %xmm1
	movaps	%xmm1, %xmm3
.L12:
	cmpl	%ebx, %r15d
	jge	.L16
	movss	12(%rsp), %xmm0
	movaps	%xmm3, %xmm6
	movq	24(%rsp), %rax
	movaps	%xmm1, %xmm2
	mulss	%xmm3, %xmm6
	addss	%xmm0, %xmm0
	mulss	12(%rsp), %xmm0
	leaq	(%r14,%rax), %r12
	movss	%xmm6, 16(%rsp)
	movss	%xmm0, 20(%rsp)
.L13:
	movaps	%xmm2, %xmm0
	movss	%xmm4, 44(%rsp)
	addq	$4, %r14
	mulss	%xmm2, %xmm0
	movss	%xmm1, 40(%rsp)
	addss	16(%rsp), %xmm0
	movss	%xmm3, 36(%rsp)
	movss	%xmm2, 32(%rsp)
	xorps	%xmm5, %xmm0
	divss	20(%rsp), %xmm0
	call	expf@PLT
	movss	44(%rsp), %xmm4
	movss	32(%rsp), %xmm2
	movss	.LC3(%rip), %xmm5
	movss	%xmm0, -4(%r14)
	cmpq	%r12, %r14
	addss	.LC1(%rip), %xmm2
	movss	36(%rsp), %xmm3
	addss	%xmm0, %xmm4
	movss	40(%rsp), %xmm1
	jne	.L13
	addss	.LC1(%rip), %xmm3
	incl	%r15d
	jmp	.L12
.L16:
	xorl	%eax, %eax
.L14:
	cmpl	%eax, %ebp
	jle	.L19
	movss	0(%r13,%rax,4), %xmm0
	divss	%xmm4, %xmm0
	movss	%xmm0, 0(%r13,%rax,4)
	incq	%rax
	jmp	.L14
.L19:
	addq	$56, %rsp
	.cfi_def_cfa_offset 56
	movq	%r13, %rax
	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
.LFE2028:
	.size	_Z11gaussFilterif, .-_Z11gaussFilterif
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2054:
	.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
.LFE2054:
	.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
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC1:
	.long	1065353216
	.align 4
.LC2:
	.long	1056964608
	.section	.rodata.cst16,"aM",@progbits,16
	.align 16
.LC3:
	.long	-2147483648
	.long	0
	.long	0
	.long	0
	.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 <stdio.h>

/* Function that normalizes the filter on the max element */
void gaussNorm(float *data,int size){
	float max=0;
	for (int i=0;i<size*size;i++)
		if(data[i]>max) max = data[i];
	for (int i=0;i<size*size;i++)
		data[i]/=max;
}

/* This funtion calculates the 2D gussian filter gaussFilter[size*size]
 * It is based on MATLAB's fspecial('gaussian',size,sigma) function's algorithm.
 */
float * gaussFilter(int size, float sigma){
	/* Get the mesh's rows and columns */
	float rows = ((float)size-1)/2;
	float cols = ((float)size-1)/2;
	/* Allocate the final array  */
	float *filter = (float *)malloc(size*size*sizeof(float));
	/* In the following loops a mesh is simulated from -rows to +rows and
      	 * from -cols to cols with step 1.
	 * Every element el[i][j]  of the 2D filter is calculated with the following formula
	 * el[i][j] = exp (- (x^2 + y^2) / (2*sigma^2)) / sum (el)
	 * where x and y are the mesh's elements for the specific filter's element.
	 */
	float sum = 0;
	float x=-rows;
	for (int i = 0; i<size; i++){
		float y=-cols;
		for (int j=0; j<size; j++){
			filter[i*size + j] = exp(- (x*x + y*y)/ (2*sigma*sigma));		
			sum += filter[i*size + j];
			/* Step = 1 */
			y++;
		}
		x++;
	}
	for (int i=0;i<size*size;i++)
		filter[i] =filter[i] / sum ;		

	return filter;
}

	.text
	.file	"gaussian.hip"
	.globl	_Z9gaussNormPfi                 # -- Begin function _Z9gaussNormPfi
	.type	_Z9gaussNormPfi,@function
_Z9gaussNormPfi:                        # @_Z9gaussNormPfi
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	je	.LBB0_6
# %bb.1:                                # %.lr.ph.preheader
	movl	%esi, %eax
	imull	%eax, %eax
	cmpl	$1, %eax
	movl	%eax, %ecx
	adcl	$0, %ecx
	xorps	%xmm0, %xmm0
	xorl	%edx, %edx
.LBB0_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movaps	%xmm0, %xmm1
	movss	(%rdi,%rdx,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	maxss	%xmm1, %xmm0
	incq	%rdx
	cmpq	%rdx, %rcx
	jne	.LBB0_2
# %bb.3:                                # %.preheader
	testl	%esi, %esi
	je	.LBB0_6
# %bb.4:                                # %.lr.ph23.preheader
	cmpl	$1, %eax
	adcl	$0, %eax
	xorl	%ecx, %ecx
.LBB0_5:                                # %.lr.ph23
                                        # =>This Inner Loop Header: Depth=1
	movss	(%rdi,%rcx,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	divss	%xmm0, %xmm1
	movss	%xmm1, (%rdi,%rcx,4)
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB0_5
.LBB0_6:                                # %._crit_edge
	retq
.Lfunc_end0:
	.size	_Z9gaussNormPfi, .Lfunc_end0-_Z9gaussNormPfi
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z11gaussFilterif
.LCPI1_0:
	.long	0xbf800000                      # float -1
.LCPI1_1:
	.long	0xbf000000                      # float -0.5
.LCPI1_3:
	.long	0x3f800000                      # float 1
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0
.LCPI1_2:
	.long	0x80000000                      # float -0
	.long	0x80000000                      # float -0
	.long	0x80000000                      # float -0
	.long	0x80000000                      # float -0
	.text
	.globl	_Z11gaussFilterif
	.type	_Z11gaussFilterif,@function
_Z11gaussFilterif:                      # @_Z11gaussFilterif
	.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	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movss	%xmm0, (%rsp)                   # 4-byte Spill
	movl	%edi, %ebx
	movl	%edi, %eax
	imull	%eax, %eax
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	leaq	(,%rax,4), %rdi
	callq	malloc
	testl	%ebx, %ebx
	jle	.LBB1_1
# %bb.6:                                # %.preheader44.lr.ph
	xorps	%xmm0, %xmm0
	cvtsi2ss	%ebx, %xmm0
	addss	.LCPI1_0(%rip), %xmm0
	mulss	.LCPI1_1(%rip), %xmm0
	movss	(%rsp), %xmm1                   # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	movaps	%xmm1, %xmm2
	addss	%xmm1, %xmm2
	mulss	%xmm1, %xmm2
	movl	%ebx, %r15d
	xorps	%xmm1, %xmm1
	xorl	%r12d, %r12d
	movaps	.LCPI1_2(%rip), %xmm3           # xmm3 = [-0.0E+0,-0.0E+0,-0.0E+0,-0.0E+0]
	xorl	%r13d, %r13d
	movss	%xmm0, 4(%rsp)                  # 4-byte Spill
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	movss	%xmm2, 12(%rsp)                 # 4-byte Spill
.LBB1_7:                                # %.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_8 Depth 2
	movl	%r12d, %ecx
	leaq	(%rax,%rcx,4), %r14
	movss	%xmm0, 8(%rsp)                  # 4-byte Spill
	mulss	%xmm0, %xmm0
	movss	%xmm0, 16(%rsp)                 # 4-byte Spill
	xorl	%ebp, %ebp
	movss	4(%rsp), %xmm0                  # 4-byte Reload
                                        # xmm0 = mem[0],zero,zero,zero
.LBB1_8:                                #   Parent Loop BB1_7 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	%xmm0, (%rsp)                   # 4-byte Spill
	movss	%xmm1, 20(%rsp)                 # 4-byte Spill
	movss	(%rsp), %xmm0                   # 4-byte Reload
                                        # xmm0 = mem[0],zero,zero,zero
	mulss	%xmm0, %xmm0
	addss	16(%rsp), %xmm0                 # 4-byte Folded Reload
	xorps	%xmm3, %xmm0
	divss	%xmm2, %xmm0
	cvtss2sd	%xmm0, %xmm0
	callq	exp
	movss	.LCPI1_3(%rip), %xmm4           # xmm4 = mem[0],zero,zero,zero
	movaps	.LCPI1_2(%rip), %xmm3           # xmm3 = [-0.0E+0,-0.0E+0,-0.0E+0,-0.0E+0]
	movss	12(%rsp), %xmm2                 # 4-byte Reload
                                        # xmm2 = mem[0],zero,zero,zero
	movss	20(%rsp), %xmm1                 # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, (%r14,%rbp,4)
	addss	%xmm0, %xmm1
	movss	(%rsp), %xmm0                   # 4-byte Reload
                                        # xmm0 = mem[0],zero,zero,zero
	addss	%xmm4, %xmm0
	incq	%rbp
	cmpq	%rbp, %r15
	jne	.LBB1_8
# %bb.9:                                # %._crit_edge
                                        #   in Loop: Header=BB1_7 Depth=1
	movss	8(%rsp), %xmm0                  # 4-byte Reload
                                        # xmm0 = mem[0],zero,zero,zero
	addss	%xmm4, %xmm0
	incq	%r13
	addl	%ebx, %r12d
	cmpq	%r15, %r13
	movq	32(%rsp), %rax                  # 8-byte Reload
	jne	.LBB1_7
	jmp	.LBB1_2
.LBB1_1:
	xorps	%xmm1, %xmm1
.LBB1_2:                                # %.preheader
	testl	%ebx, %ebx
	movq	24(%rsp), %rcx                  # 8-byte Reload
	je	.LBB1_5
# %bb.3:                                # %.lr.ph53.preheader
	cmpl	$1, %ecx
	adcl	$0, %ecx
	xorl	%edx, %edx
.LBB1_4:                                # %.lr.ph53
                                        # =>This Inner Loop Header: Depth=1
	movss	(%rax,%rdx,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	divss	%xmm1, %xmm0
	movss	%xmm0, (%rax,%rdx,4)
	incq	%rdx
	cmpq	%rdx, %rcx
	jne	.LBB1_4
.LBB1_5:                                # %._crit_edge54
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.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_end1:
	.size	_Z11gaussFilterif, .Lfunc_end1-_Z11gaussFilterif
	.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
