#include "MarkovChain.cuh"

/**
  *  Characters (26)
  *  Start of word (1)
  *  End of word (1)
  */

#define CHARACTERS 27
#define BUFFERSIZE 20
#define START 'S'
#define ENDINDEX 0

int getCharacterIndex(char character) {
	switch (character) {
		//The start character
		case START:
			return CHARACTERS;
		//The end character
		case '\0':
			return ENDINDEX;
		case '\n':
			return ENDINDEX;
		//Any other letter
		default:
			return (int(character) - int('a')+1);
	}

}

void normalizeRow(double** markovMatrix, int row) {
	int sum = 0;
	//Gets the total number of character transitions in the row
	for (int col = 0; col < CHARACTERS; col++) {
		sum+=markovMatrix[row][col];
	}

	//Gets the total number of 
	for (int col = 0; col < CHARACTERS; col++) {
		markovMatrix[row][col]/=sum;
	}
}

double** markovFromFile(char* filename) {
	//Creates a matrix for all characters
	double** markovMatrix=(double**)(calloc(CHARACTERS+1, sizeof(double*)));
	for (int i = 0; i < CHARACTERS+1; i++) {
		markovMatrix[i] = (double*)(calloc(CHARACTERS, sizeof(double)));
	}

	//Opens the file stream
	FILE* f = fopen(filename, "r");

	char* buffer=(char*)(calloc(BUFFERSIZE, sizeof(char)));

	//The current char
	char current = START;

	//Loops through the file to get the next name
	while (fgets(buffer, BUFFERSIZE-1, f)!=NULL) {


		//loops through the buffer
		for (int c = 0; c < BUFFERSIZE-1; c++) {

			//Gets the next character
			char next = buffer[c];
			
			//Changes the next letter to lowercase if it isn't already
			if (next>=int('A') && next<=int('Z')) {
				next = (next-int('A'))+int('a');
			}

			//Adds one to the markov matrix corresponding to the place pointing from the current character to the next character
			markovMatrix[getCharacterIndex(current)][getCharacterIndex(next)]++;

			//Switches the current character
			if (next=='\n' || next== '\0') {
				current = START;
				break;
			}
			else {
				current = next;
			}
		}
	}

	//Loops through the markov matrix and normalizes each row
	for (int r = 0; r < CHARACTERS + 1; r++) {
		normalizeRow(markovMatrix, r);
	}

	return markovMatrix;
}

void freeMarkov(double** markovMatrix) {
	for (int row = 0; row < CHARACTERS + 1; row++) {
		free(markovMatrix[row]);
	}
	free(markovMatrix);
}

char getChar(char currentChar, double** markovMatrix) {
	//Generates random number between 0 and 1
	double randNum = rand()/double(RAND_MAX);

	double sum=0;
	//Loops through the characters to see what range the random number falls in
	for (int i = 0; i < CHARACTERS; i++) {

		//Sums the frequency
		sum += markovMatrix[getCharacterIndex(currentChar)][i];

		//Should return the current character
		if (sum>=randNum) {

			//Terminate the name
			if (currentChar==ENDINDEX) {
				return '\0';
			}

			//Add to the name
			else {
				return char(i + int('a'));
			}
		}
	}
	return '\0';
}

void generateEmpireName(double** markovMatrix, empire* emp) {
	//Creates a name buffer of max length 20
	emp->name = (char*)(calloc(BUFFERSIZE, sizeof(char)));

	char currentChar = START;
	//Loops until the character is eof or until the buffer has no more space
	for (int i = 0; i<BUFFERSIZE-1 && currentChar != '\0'; i++) {
		//Gets the next character
		currentChar = getChar(currentChar, markovMatrix);

		//Adds this to the name
		emp->name[i] = currentChar;
	}
	printf("Empire name=%s\n", emp->name);
}
	.file	"tmpxft_002acbfc_00000000-6_MarkovChain.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2035:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2035:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z17getCharacterIndexc
	.type	_Z17getCharacterIndexc, @function
_Z17getCharacterIndexc:
.LFB2027:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
	cmpb	$10, %dil
	je	.L2
	movl	$27, %eax
	cmpb	$83, %dil
	je	.L2
	xorl	%eax, %eax
	testb	%dil, %dil
	je	.L2
	movsbl	%dil, %edi
	leal	-96(%rdi), %eax
.L2:
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z17getCharacterIndexc, .-_Z17getCharacterIndexc
	.globl	_Z12normalizeRowPPdi
	.type	_Z12normalizeRowPPdi, @function
_Z12normalizeRowPPdi:
.LFB2028:
	.cfi_startproc
	endbr64
	movslq	%esi, %rsi
	xorl	%eax, %eax
	xorl	%ecx, %ecx
	movq	(%rdi,%rsi,8), %rdx
.L9:
	cvtsi2sdl	%ecx, %xmm0
	addsd	(%rdx,%rax,8), %xmm0
	incq	%rax
	cvttsd2sil	%xmm0, %ecx
	cmpq	$27, %rax
	jne	.L9
	cvtsi2sdl	%ecx, %xmm1
	xorl	%eax, %eax
.L10:
	movsd	(%rdx,%rax,8), %xmm0
	divsd	%xmm1, %xmm0
	movsd	%xmm0, (%rdx,%rax,8)
	incq	%rax
	cmpq	$27, %rax
	jne	.L10
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z12normalizeRowPPdi, .-_Z12normalizeRowPPdi
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"r"
	.text
	.globl	_Z14markovFromFilePc
	.type	_Z14markovFromFilePc, @function
_Z14markovFromFilePc:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movl	$8, %esi
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movq	%rdi, %r12
	movl	$28, %edi
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	xorl	%ebx, %ebx
	pushq	%rcx
	.cfi_def_cfa_offset 48
	call	calloc@PLT
	movq	%rax, %rbp
.L14:
	movl	$8, %esi
	movl	$27, %edi
	call	calloc@PLT
	movq	%rax, 0(%rbp,%rbx,8)
	incq	%rbx
	cmpq	$28, %rbx
	jne	.L14
	movq	%r12, %rdi
	leaq	.LC0(%rip), %rsi
	call	fopen@PLT
	movl	$1, %esi
	movl	$20, %edi
	movq	%rax, %r13
	call	calloc@PLT
	movq	%rax, %r12
.L24:
	movb	$83, %bl
.L18:
	movq	%r13, %rcx
	movl	$19, %edx
	movl	$20, %esi
	movq	%r12, %rdi
	call	__fgets_chk@PLT
	testq	%rax, %rax
	je	.L26
	movsbl	%bl, %edi
	xorl	%edx, %edx
.L17:
	movb	(%r12,%rdx), %bl
	leal	-65(%rbx), %eax
	cmpb	$25, %al
	ja	.L16
	addl	$32, %ebx
.L16:
	call	_Z17getCharacterIndexc
	movsbl	%bl, %edi
	cltq
	movq	0(%rbp,%rax,8), %rcx
	call	_Z17getCharacterIndexc
	movsd	.LC1(%rip), %xmm0
	cltq
	leaq	(%rcx,%rax,8), %rax
	addsd	(%rax), %xmm0
	movsd	%xmm0, (%rax)
	cmpb	$10, %bl
	je	.L24
	testb	%bl, %bl
	je	.L24
	incq	%rdx
	cmpq	$19, %rdx
	je	.L18
	movsbl	%bl, %edi
	jmp	.L17
.L26:
	xorl	%r8d, %r8d
.L19:
	movl	%r8d, %esi
	movq	%rbp, %rdi
	incl	%r8d
	call	_Z12normalizeRowPPdi
	cmpl	$28, %r8d
	jne	.L19
	popq	%rdx
	.cfi_def_cfa_offset 40
	movq	%rbp, %rax
	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
.LFE2029:
	.size	_Z14markovFromFilePc, .-_Z14markovFromFilePc
	.globl	_Z10freeMarkovPPd
	.type	_Z10freeMarkovPPd, @function
_Z10freeMarkovPPd:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movq	%rdi, %rbx
	pushq	%rdx
	.cfi_def_cfa_offset 32
.L28:
	movq	(%rbx,%rbp,8), %rdi
	incq	%rbp
	call	free@PLT
	cmpq	$28, %rbp
	jne	.L28
	popq	%rax
	.cfi_def_cfa_offset 24
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	free@PLT
	.cfi_endproc
.LFE2030:
	.size	_Z10freeMarkovPPd, .-_Z10freeMarkovPPd
	.globl	_Z7getCharcPPd
	.type	_Z7getCharcPPd, @function
_Z7getCharcPPd:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movq	%rsi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movl	%edi, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movl	%edi, %ebx
	call	rand@PLT
	movsbl	%bl, %edi
	xorps	%xmm1, %xmm1
	cvtsi2sdl	%eax, %xmm0
	divsd	.LC3(%rip), %xmm0
	call	_Z17getCharacterIndexc
	cltq
	movq	(%r12,%rax,8), %rdx
	xorl	%eax, %eax
.L35:
	addsd	(%rdx,%rax,8), %xmm1
	comisd	%xmm0, %xmm1
	jb	.L38
	testb	%bl, %bl
	je	.L34
	leal	97(%rax), %ebp
	jmp	.L34
.L38:
	incq	%rax
	cmpq	$27, %rax
	jne	.L35
	xorl	%ebp, %ebp
.L34:
	movl	%ebp, %eax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.size	_Z7getCharcPPd, .-_Z7getCharcPPd
	.section	.rodata.str1.1
.LC4:
	.string	"Empire name=%s\n"
	.text
	.globl	_Z18generateEmpireNamePPdP3emp
	.type	_Z18generateEmpireNamePPdP3emp, @function
_Z18generateEmpireNamePPdP3emp:
.LFB2032:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movq	%rdi, %rbp
	movl	$20, %edi
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movq	%rsi, %rbx
	movl	$1, %esi
	call	calloc@PLT
	movq	%rax, (%rbx)
	movb	$83, %al
.L41:
	movsbl	%al, %edi
	movq	%rbp, %rsi
	call	_Z7getCharcPPd
	movq	(%rbx), %rdx
	movb	%al, (%rdx,%r12)
	leal	1(%r12), %edx
	cmpl	$18, %edx
	setle	%cl
	testb	%al, %al
	setne	%dl
	incq	%r12
	testb	%dl, %cl
	jne	.L41
	movq	(%rbx), %rdx
	leaq	.LC4(%rip), %rsi
	popq	%rbx
	.cfi_def_cfa_offset 24
	movl	$2, %edi
	popq	%rbp
	.cfi_def_cfa_offset 16
	xorl	%eax, %eax
	popq	%r12
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2032:
	.size	_Z18generateEmpireNamePPdP3emp, .-_Z18generateEmpireNamePPdP3emp
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2058:
	.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
.LFE2058:
	.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.cst8,"aM",@progbits,8
	.align 8
.LC1:
	.long	0
	.long	1072693248
	.align 8
.LC3:
	.long	-4194304
	.long	1105199103
	.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:

#ifndef MARKOVCHAIN_CUH
#define MARKOVCHAIN_CUH

#include "Empire.cuh"

#include <stdlib.h>
#include <stdio.h>

double** markovFromFile(char* filename);

void freeMarkov(double** markovMatrix);

void generateEmpireName(double** markovMatrix, empire* emp);

#endif
	.text
	.file	"MarkovChain.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
