#include"tfidf.cuh"

#include<cmath>

//TODO: parallelize
void tfidf(int rows, int cols, float * matrix) {
	int numDocs = rows;
	int numTerms = cols;
	for (int termIdx = 0; termIdx < numTerms; termIdx++) {
		int includeDocs = 0;
		for (int docIdx = 0; docIdx < numDocs; docIdx++) {
			if (matrix[docIdx * cols + termIdx] > 0) {
				includeDocs++;
			}
		}
		float termIdf = log10(numDocs / float(includeDocs + 1));
		for (int docIdx = 0; docIdx < numDocs; docIdx++) {
			matrix[docIdx * cols + termIdx] *= termIdf;
		}
	}
}
	.file	"tmpxft_00399d2e_00000000-6_tfidf.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2011:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2011:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z5tfidfiiPf
	.type	_Z5tfidfiiPf, @function
_Z5tfidfiiPf:
.LFB2008:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	xorl	%r14d, %r14d
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movslq	%esi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movq	%r13, %r12
	salq	$2, %r13
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	movl	%edi, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	movq	%rdx, %rbx
.L3:
	cmpl	%r12d, %r14d
	jge	.L12
	movq	%rbx, %rsi
	xorl	%edx, %edx
	xorps	%xmm0, %xmm0
	xorl	%ecx, %ecx
.L8:
	leal	1(%rcx), %eax
	cmpl	%ebp, %edx
	jge	.L13
	movss	(%rsi), %xmm1
	comiss	%xmm0, %xmm1
	cmovbe	%ecx, %eax
	incl	%edx
	addq	%r13, %rsi
	movl	%eax, %ecx
	jmp	.L8
.L13:
	cvtsi2ssl	%eax, %xmm1
	cvtsi2ssl	%ebp, %xmm0
	divss	%xmm1, %xmm0
	call	log10f@PLT
	movq	%rbx, %rax
	xorl	%edx, %edx
.L6:
	cmpl	%ebp, %edx
	jge	.L14
	movss	(%rax), %xmm1
	incl	%edx
	mulss	%xmm0, %xmm1
	movss	%xmm1, (%rax)
	addq	%r13, %rax
	jmp	.L6
.L14:
	incl	%r14d
	addq	$4, %rbx
	jmp	.L3
.L12:
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2008:
	.size	_Z5tfidfiiPf, .-_Z5tfidfiiPf
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2034:
	.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
.LFE2034:
	.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"tfidf.cuh"

#include<cmath>

//TODO: parallelize
void tfidf(int rows, int cols, float * matrix) {
	int numDocs = rows;
	int numTerms = cols;
	for (int termIdx = 0; termIdx < numTerms; termIdx++) {
		int includeDocs = 0;
		for (int docIdx = 0; docIdx < numDocs; docIdx++) {
			if (matrix[docIdx * cols + termIdx] > 0) {
				includeDocs++;
			}
		}
		float termIdf = log10(numDocs / float(includeDocs + 1));
		for (int docIdx = 0; docIdx < numDocs; docIdx++) {
			matrix[docIdx * cols + termIdx] *= termIdf;
		}
	}
}
	.text
	.file	"tfidf.hip"
	.globl	_Z5tfidfiiPf                    # -- Begin function _Z5tfidfiiPf
	.type	_Z5tfidfiiPf,@function
_Z5tfidfiiPf:                           # @_Z5tfidfiiPf
	.cfi_startproc
# %bb.0:
	testl	%esi, %esi
	jle	.LBB0_10
# %bb.1:                                # %.preheader.lr.ph
	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
	pushq	%rax
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rdx, %rbx
	movl	%edi, %ebp
	cvtsi2ss	%edi, %xmm0
	movss	%xmm0, 4(%rsp)                  # 4-byte Spill
	movl	%esi, %r14d
	movl	%edi, %r15d
	leaq	(,%r14,4), %r12
	xorl	%r13d, %r13d
	xorps	%xmm1, %xmm1
.LBB0_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_4 Depth 2
                                        #     Child Loop BB0_7 Depth 2
	movl	$0, %eax
	testl	%ebp, %ebp
	jle	.LBB0_5
# %bb.3:                                # %.lr.ph.preheader
                                        #   in Loop: Header=BB0_2 Depth=1
	xorl	%eax, %eax
	movq	%r15, %rcx
	movq	%rbx, %rdx
.LBB0_4:                                # %.lr.ph
                                        #   Parent Loop BB0_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%rdx), %xmm0                   # xmm0 = mem[0],zero,zero,zero
	xorl	%esi, %esi
	ucomiss	%xmm1, %xmm0
	seta	%sil
	addl	%esi, %eax
	addq	%r12, %rdx
	decq	%rcx
	jne	.LBB0_4
.LBB0_5:                                # %._crit_edge
                                        #   in Loop: Header=BB0_2 Depth=1
	incl	%eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	movss	4(%rsp), %xmm1                  # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	divss	%xmm0, %xmm1
	xorps	%xmm0, %xmm0
	cvtss2sd	%xmm1, %xmm0
	callq	log10
	testl	%ebp, %ebp
	jle	.LBB0_8
# %bb.6:                                #   in Loop: Header=BB0_2 Depth=1
	cvtsd2ss	%xmm0, %xmm0
	movq	%r15, %rax
	movq	%rbx, %rcx
.LBB0_7:                                # %.lr.ph31
                                        #   Parent Loop BB0_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	(%rcx), %xmm1                   # xmm1 = mem[0],zero,zero,zero
	mulss	%xmm0, %xmm1
	movss	%xmm1, (%rcx)
	addq	%r12, %rcx
	decq	%rax
	jne	.LBB0_7
.LBB0_8:                                # %._crit_edge32
                                        #   in Loop: Header=BB0_2 Depth=1
	incq	%r13
	addq	$4, %rbx
	cmpq	%r14, %r13
	xorps	%xmm1, %xmm1
	jne	.LBB0_2
# %bb.9:
	addq	$8, %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
	.cfi_restore %rbx
	.cfi_restore %r12
	.cfi_restore %r13
	.cfi_restore %r14
	.cfi_restore %r15
	.cfi_restore %rbp
.LBB0_10:                               # %._crit_edge34
	retq
.Lfunc_end0:
	.size	_Z5tfidfiiPf, .Lfunc_end0-_Z5tfidfiiPf
	.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
