//******************************************************
// Assignment #1
// Names: Anthony Enem and Cavaughn Browne
// Parallel Programming Date: 10/10/16
//******************************************************
// This program implements the cooley tukey fft algorithm
// and computes the values fro X_k from 0 to N. The program
// should be compiled using "gcc -o cooley_fft Browne_Enem_A1.c"
// Then it should be run with "./cooley_fft N" where N is the
// maximum value N in the fft formula
//******************************************************

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

double PI = acos(-1);
#define N 16384


//define struct for complex numbers
typedef struct complex{
    double real;
    double imaginary;
} complex;

//create complex pointer for input
complex* x_input = NULL;

//******************************************************
// Parameters: two complex structs a and b
// This function multiplies 2 complex structs and returns
// the result as a complex struct
//******************************************************
complex multiplyComplex(complex a, complex b)
{
    complex result;
    result.real = a.real*b.real - a.imaginary*b.imaginary;
    result.imaginary = a.real*b.imaginary + b.real*a.imaginary;

    return result;
}

//******************************************************
// Parameters: two complex structs a and b
// This function adds 2 complex structs and returns
// the result as a complex struct
//******************************************************
complex addComplex(complex a, complex b)
{
    complex result = {a.real+b.real, a.imaginary+b.imaginary};
    return result;
}

//******************************************************
// Parameters: two complex structs a and b
// This function subtracts 2 complex structs and returns
// the result as a complex struct
//******************************************************
complex subtractComplex(complex a, complex b)
{
    complex result = {a.real-b.real, a.imaginary-b.imaginary};
    return result;
}

//******************************************************
// Parameters: integer N, integer k, pointers to complex 
//		structs E_k and O_k
// This function computes the even and odd portions of the
// fft formula and returns them by reference in the complex
// pointers
//******************************************************
void compute_Even_Odd(int k, complex* E_k, complex* O_k)
{
    if(k >= N/2){
        k -= N/2;
    }

    E_k->real = 0;
    E_k->imaginary = 0;
    O_k->real = 0;
    O_k->imaginary = 0;

    int N_half = N/2;
    complex e_part;
    double exp;

    for(int m = 0; m <= N/2 - 1; m++)
    {
        //compute exponent
        exp = -2.0 * PI * m * k / N_half;

        //compute part with e^(i * exponent)
        e_part.real = cos(exp);
        e_part.imaginary = sin(exp);
        
        //add even part
        *E_k = addComplex(
            *E_k,
            multiplyComplex(x_input[2*m], e_part)
        );

        //add odd part
        *O_k = addComplex(
            *O_k,
            multiplyComplex(x_input[2*m+1], e_part)
        );

    }

}

//******************************************************
// Parameters: integer N and integer k
// This function computes the twiddle factor for given
// N and k values
//******************************************************
complex compute_twiddle(int k)
{
    double exp = -2.0 * PI * k / N;
    complex twiddle = { cos(exp), sin(exp) };

    return twiddle;        
}


//Main function
int main(int argc, char** argv)
{
	//make sure user enters a value for N
	/*if (argc != 2)
	{
		printf("Wrong usage!\n");
		printf("Must specify value for N!\n");

		return 1;
	}*/
	//get N from command line argument

	//allocate memory for N complex inputs
	x_input = (complex *)malloc(N * sizeof(complex));
	complex* result = (complex*)malloc(N *sizeof(complex));
	
	//set defined values from 0 to 7 for x_input
	x_input[0].real = 3.6;
	x_input[0].imaginary = 2.6;
	x_input[1].real = 2.9;
	x_input[1].imaginary = 6.3;
	x_input[2].real = 5.6;
	x_input[2].imaginary = 4;
	x_input[3].real = 4.8;
	x_input[3].imaginary = 9.1;
	x_input[4].real = 3.3;
	x_input[4].imaginary = 0.4;
	x_input[5].real = 5.9;
	x_input[5].imaginary = 4.8;
	x_input[6].real = 5;
	x_input[6].imaginary = 2.6;
	x_input[7].real = 4.3;
	x_input[7].imaginary = 4.1;

	//set defined values for x[8] to x[N-1] as 0
	for (int i = 8; i < N; i++)
	{
		x_input[i].real = 0;
		x_input[i].imaginary = 0;
	}

    complex twiddle_factor;

    //allocate memory for E_k and O_k
    complex* E_k = (complex *)malloc(sizeof(complex));
    complex* O_k = (complex *)malloc(sizeof(complex));  	
	
	clock_t st = clock();
	
	cudaEvent_t start, stop;
	float elapsedTime;

	cudaEventCreate(&start);
	cudaEventRecord(start,0);
	

    for(int k = 0; k < N/2; k++)
    {
        //get twiddle factor
        twiddle_factor = compute_twiddle(k);

        //compute even and odd portions
        compute_Even_Odd(k, E_k, O_k);

        //add up results
        result[k] = addComplex(*E_k, multiplyComplex(twiddle_factor, *O_k));
		result[k + N/2] = subtractComplex(*E_k, multiplyComplex(twiddle_factor, *O_k));
    }
	
	clock_t end = clock();
	cudaEventCreate(&stop);
	cudaEventRecord(stop,0);
	cudaEventSynchronize(stop);

	cudaEventElapsedTime(&elapsedTime, start,stop);
	
	//PRINT FIRST 8 RESULTS
	
	printf("SEQUENTIAL VERSION\n\n");
	
	printf("TOTAL PROCESSED SAMPLES: %d\n", N);	
	for(int i = 0; i < 8; i++)
	{	
		printf("==================\n");
		printf("XR[%d]: %f\n", i, result[i].real);
		printf("XI[%d]: %f\n", i, result[i].imaginary);
	}

	
	printf("==================\n");
	
	printf("Cuda time : %f secs\n", elapsedTime/1000);
	
	printf("\nC clock Time: %f\n", (double)(end - st) / CLOCKS_PER_SEC);

    return 0;

}
	.file	"tmpxft_002bfb78_00000000-6_FFT_Sequential.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2036:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2036:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z15multiplyComplex7complexS_
	.type	_Z15multiplyComplex7complexS_, @function
_Z15multiplyComplex7complexS_:
.LFB2028:
	.cfi_startproc
	endbr64
	movaps	%xmm0, %xmm5
	movaps	%xmm1, %xmm4
	mulsd	%xmm2, %xmm0
	mulsd	%xmm3, %xmm1
	mulsd	%xmm3, %xmm5
	mulsd	%xmm2, %xmm4
	subsd	%xmm1, %xmm0
	movaps	%xmm5, %xmm1
	addsd	%xmm4, %xmm1
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z15multiplyComplex7complexS_, .-_Z15multiplyComplex7complexS_
	.globl	_Z10addComplex7complexS_
	.type	_Z10addComplex7complexS_, @function
_Z10addComplex7complexS_:
.LFB2029:
	.cfi_startproc
	endbr64
	addsd	%xmm3, %xmm1
	addsd	%xmm2, %xmm0
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z10addComplex7complexS_, .-_Z10addComplex7complexS_
	.globl	_Z15subtractComplex7complexS_
	.type	_Z15subtractComplex7complexS_, @function
_Z15subtractComplex7complexS_:
.LFB2030:
	.cfi_startproc
	endbr64
	subsd	%xmm3, %xmm1
	subsd	%xmm2, %xmm0
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z15subtractComplex7complexS_, .-_Z15subtractComplex7complexS_
	.globl	_Z16compute_Even_OddiP7complexS0_
	.type	_Z16compute_Even_OddiP7complexS0_, @function
_Z16compute_Even_OddiP7complexS0_:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movl	%edi, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movq	%rsi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movq	%rdx, %rbx
	subq	$24, %rsp
	.cfi_def_cfa_offset 64
	cmpl	$8191, %edi
	jle	.L6
	subl	$8192, %r12d
.L6:
	movq	$0x000000000, 0(%rbp)
	xorl	%r13d, %r13d
	movq	$0x000000000, 8(%rbp)
	movq	$0x000000000, (%rbx)
	movq	$0x000000000, 8(%rbx)
.L7:
	movsd	.LC1(%rip), %xmm0
	cvtsi2sdl	%r13d, %xmm1
	leaq	8(%rsp), %rdi
	movq	%rsp, %rsi
	mulsd	PI(%rip), %xmm0
	mulsd	%xmm1, %xmm0
	cvtsi2sdl	%r12d, %xmm1
	mulsd	%xmm1, %xmm0
	mulsd	.LC2(%rip), %xmm0
	call	sincos@PLT
	movq	x_input(%rip), %rdx
	movq	%r13, %rax
	movsd	(%rsp), %xmm2
	salq	$5, %rax
	movsd	8(%rsp), %xmm3
	incq	%r13
	movsd	(%rdx,%rax), %xmm0
	movsd	8(%rdx,%rax), %xmm1
	call	_Z15multiplyComplex7complexS_
	addsd	8(%rbp), %xmm1
	addsd	0(%rbp), %xmm0
	movsd	%xmm1, 8(%rbp)
	movsd	%xmm0, 0(%rbp)
	movsd	16(%rdx,%rax), %xmm0
	movsd	24(%rdx,%rax), %xmm1
	call	_Z15multiplyComplex7complexS_
	addsd	8(%rbx), %xmm1
	addsd	(%rbx), %xmm0
	movsd	%xmm1, 8(%rbx)
	movsd	%xmm0, (%rbx)
	cmpq	$8192, %r13
	jne	.L7
	addq	$24, %rsp
	.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
.LFE2031:
	.size	_Z16compute_Even_OddiP7complexS0_, .-_Z16compute_Even_OddiP7complexS0_
	.globl	_Z15compute_twiddlei
	.type	_Z15compute_twiddlei, @function
_Z15compute_twiddlei:
.LFB2032:
	.cfi_startproc
	endbr64
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	%edi, %eax
	movsd	.LC1(%rip), %xmm0
	mulsd	PI(%rip), %xmm0
	cvtsi2sdl	%eax, %xmm1
	leaq	8(%rsp), %rdi
	movq	%rsp, %rsi
	mulsd	%xmm1, %xmm0
	mulsd	.LC3(%rip), %xmm0
	call	sincos@PLT
	movsd	(%rsp), %xmm0
	movsd	8(%rsp), %xmm1
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2032:
	.size	_Z15compute_twiddlei, .-_Z15compute_twiddlei
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC18:
	.string	"SEQUENTIAL VERSION\n\n"
.LC19:
	.string	"TOTAL PROCESSED SAMPLES: %d\n"
.LC20:
	.string	"==================\n"
.LC21:
	.string	"XR[%d]: %f\n"
.LC22:
	.string	"XI[%d]: %f\n"
.LC24:
	.string	"Cuda time : %f secs\n"
.LC26:
	.string	"\nC clock Time: %f\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2033:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	$262144, %edi
	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
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$56, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	movl	$262144, %edi
	movq	%rax, %rbp
	movq	%rax, x_input(%rip)
	call	malloc@PLT
	movsd	.LC5(%rip), %xmm0
	movsd	.LC10(%rip), %xmm1
	movq	%rax, %rbx
	movq	.LC4(%rip), %rax
	movsd	%xmm0, 8(%rbp)
	movq	%rax, 0(%rbp)
	movq	.LC6(%rip), %rax
	movsd	%xmm1, 48(%rbp)
	movq	%rax, 16(%rbp)
	movq	.LC7(%rip), %rax
	movsd	%xmm1, 88(%rbp)
	movq	%rax, 24(%rbp)
	movq	.LC8(%rip), %rax
	movsd	%xmm0, 104(%rbp)
	movq	%rax, 32(%rbp)
	movq	.LC9(%rip), %rax
	movq	%rax, 40(%rbp)
	movq	.LC11(%rip), %rax
	movq	%rax, 56(%rbp)
	movq	.LC12(%rip), %rax
	movq	%rax, 64(%rbp)
	movq	.LC13(%rip), %rax
	movq	%rax, 72(%rbp)
	movq	.LC14(%rip), %rax
	movq	%rax, 80(%rbp)
	movq	.LC15(%rip), %rax
	movq	%rax, 96(%rbp)
	movq	.LC16(%rip), %rax
	movq	%rax, 112(%rbp)
	movq	.LC17(%rip), %rax
	movq	%rax, 120(%rbp)
	xorl	%eax, %eax
.L14:
	movq	$0x000000000, 128(%rbp,%rax)
	movq	$0x000000000, 136(%rbp,%rax)
	addq	$16, %rax
	cmpq	$262016, %rax
	jne	.L14
	movl	$16, %edi
	movq	%rbx, %r15
	xorl	%ebp, %ebp
	call	malloc@PLT
	movl	$16, %edi
	movq	%rax, %r12
	call	malloc@PLT
	movq	%rax, %r13
	call	clock@PLT
	leaq	24(%rsp), %rdi
	movq	%rax, %r14
	call	cudaEventCreate@PLT
	movq	24(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
.L15:
	movl	%ebp, %edi
	addq	$16, %r15
	call	_Z15compute_twiddlei
	movl	%ebp, %edi
	movq	%r13, %rdx
	movq	%r12, %rsi
	movsd	%xmm0, (%rsp)
	incl	%ebp
	movsd	%xmm1, 8(%rsp)
	call	_Z16compute_Even_OddiP7complexS0_
	movsd	0(%r13), %xmm2
	movsd	(%rsp), %xmm0
	movsd	8(%r13), %xmm3
	movsd	8(%rsp), %xmm1
	call	_Z15multiplyComplex7complexS_
	movsd	(%r12), %xmm7
	movsd	8(%r12), %xmm6
	addsd	%xmm7, %xmm0
	addsd	%xmm6, %xmm1
	movsd	%xmm0, -16(%r15)
	movsd	(%rsp), %xmm0
	movsd	%xmm1, -8(%r15)
	movsd	8(%rsp), %xmm1
	call	_Z15multiplyComplex7complexS_
	subsd	%xmm1, %xmm6
	subsd	%xmm0, %xmm7
	movsd	%xmm6, 131064(%r15)
	movsd	%xmm7, 131056(%r15)
	cmpl	$8192, %ebp
	jne	.L15
	call	clock@PLT
	leaq	32(%rsp), %rdi
	xorl	%r12d, %r12d
	leaq	.LC20(%rip), %r13
	movq	%rax, %rbp
	call	cudaEventCreate@PLT
	movq	32(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	32(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	32(%rsp), %rdx
	movq	24(%rsp), %rsi
	leaq	20(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC18(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$16384, %edx
	leaq	.LC19(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
.L16:
	movq	%r13, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	addq	$16, %rbx
	call	__printf_chk@PLT
	movsd	-16(%rbx), %xmm0
	movl	%r12d, %edx
	movb	$1, %al
	leaq	.LC21(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movsd	-8(%rbx), %xmm0
	movl	%r12d, %edx
	movb	$1, %al
	leaq	.LC22(%rip), %rsi
	movl	$2, %edi
	incl	%r12d
	call	__printf_chk@PLT
	cmpl	$8, %r12d
	jne	.L16
	movq	%r13, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	subq	%r14, %rbp
	call	__printf_chk@PLT
	leaq	.LC24(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	movss	20(%rsp), %xmm0
	divss	.LC23(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	leaq	.LC26(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtsi2sdq	%rbp, %xmm0
	divsd	.LC25(%rip), %xmm0
	call	__printf_chk@PLT
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L17
	call	__stack_chk_fail@PLT
.L17:
	addq	$56, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	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
.LFE2033:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2059:
	.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
.LFE2059:
	.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
	.globl	x_input
	.bss
	.align 8
	.type	x_input, @object
	.size	x_input, 8
x_input:
	.zero	8
	.globl	PI
	.data
	.align 8
	.type	PI, @object
	.size	PI, 8
PI:
	.long	1413754136
	.long	1074340347
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC1:
	.long	0
	.long	-1073741824
	.align 8
.LC2:
	.long	0
	.long	1059061760
	.align 8
.LC3:
	.long	0
	.long	1058013184
	.align 8
.LC4:
	.long	-858993459
	.long	1074580684
	.align 8
.LC5:
	.long	-858993459
	.long	1074056396
	.align 8
.LC6:
	.long	858993459
	.long	1074213683
	.align 8
.LC7:
	.long	858993459
	.long	1075393331
	.align 8
.LC8:
	.long	1717986918
	.long	1075209830
	.align 8
.LC9:
	.long	0
	.long	1074790400
	.align 8
.LC10:
	.long	858993459
	.long	1075000115
	.align 8
.LC11:
	.long	858993459
	.long	1075983155
	.align 8
.LC12:
	.long	1717986918
	.long	1074423398
	.align 8
.LC13:
	.long	-1717986918
	.long	1071225241
	.align 8
.LC14:
	.long	-1717986918
	.long	1075288473
	.align 8
.LC15:
	.long	0
	.long	1075052544
	.align 8
.LC16:
	.long	858993459
	.long	1074869043
	.align 8
.LC17:
	.long	1717986918
	.long	1074816614
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC23:
	.long	1148846080
	.section	.rodata.cst8
	.align 8
.LC25:
	.long	0
	.long	1093567616
	.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:

//******************************************************
// Assignment #1
// Names: Anthony Enem and Cavaughn Browne
// Parallel Programming Date: 10/10/16
//******************************************************
// This program implements the cooley tukey fft algorithm
// and computes the values fro X_k from 0 to N. The program
// should be compiled using "gcc -o cooley_fft Browne_Enem_A1.c"
// Then it should be run with "./cooley_fft N" where N is the
// maximum value N in the fft formula
//******************************************************


#include <hip/hip_runtime.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>

double PI = acos(-1);
#define N 16384


//define struct for complex numbers
typedef struct complex{
    double real;
    double imaginary;
} complex;

//create complex pointer for input
complex* x_input = NULL;

//******************************************************
// Parameters: two complex structs a and b
// This function multiplies 2 complex structs and returns
// the result as a complex struct
//******************************************************
complex multiplyComplex(complex a, complex b)
{
    complex result;
    result.real = a.real*b.real - a.imaginary*b.imaginary;
    result.imaginary = a.real*b.imaginary + b.real*a.imaginary;

    return result;
}

//******************************************************
// Parameters: two complex structs a and b
// This function adds 2 complex structs and returns
// the result as a complex struct
//******************************************************
complex addComplex(complex a, complex b)
{
    complex result = {a.real+b.real, a.imaginary+b.imaginary};
    return result;
}

//******************************************************
// Parameters: two complex structs a and b
// This function subtracts 2 complex structs and returns
// the result as a complex struct
//******************************************************
complex subtractComplex(complex a, complex b)
{
    complex result = {a.real-b.real, a.imaginary-b.imaginary};
    return result;
}

//******************************************************
// Parameters: integer N, integer k, pointers to complex 
//		structs E_k and O_k
// This function computes the even and odd portions of the
// fft formula and returns them by reference in the complex
// pointers
//******************************************************
void compute_Even_Odd(int k, complex* E_k, complex* O_k)
{
    if(k >= N/2){
        k -= N/2;
    }

    E_k->real = 0;
    E_k->imaginary = 0;
    O_k->real = 0;
    O_k->imaginary = 0;

    int N_half = N/2;
    complex e_part;
    double exp;

    for(int m = 0; m <= N/2 - 1; m++)
    {
        //compute exponent
        exp = -2.0 * PI * m * k / N_half;

        //compute part with e^(i * exponent)
        e_part.real = cos(exp);
        e_part.imaginary = sin(exp);
        
        //add even part
        *E_k = addComplex(
            *E_k,
            multiplyComplex(x_input[2*m], e_part)
        );

        //add odd part
        *O_k = addComplex(
            *O_k,
            multiplyComplex(x_input[2*m+1], e_part)
        );

    }

}

//******************************************************
// Parameters: integer N and integer k
// This function computes the twiddle factor for given
// N and k values
//******************************************************
complex compute_twiddle(int k)
{
    double exp = -2.0 * PI * k / N;
    complex twiddle = { cos(exp), sin(exp) };

    return twiddle;        
}


//Main function
int main(int argc, char** argv)
{
	//make sure user enters a value for N
	/*if (argc != 2)
	{
		printf("Wrong usage!\n");
		printf("Must specify value for N!\n");

		return 1;
	}*/
	//get N from command line argument

	//allocate memory for N complex inputs
	x_input = (complex *)malloc(N * sizeof(complex));
	complex* result = (complex*)malloc(N *sizeof(complex));
	
	//set defined values from 0 to 7 for x_input
	x_input[0].real = 3.6;
	x_input[0].imaginary = 2.6;
	x_input[1].real = 2.9;
	x_input[1].imaginary = 6.3;
	x_input[2].real = 5.6;
	x_input[2].imaginary = 4;
	x_input[3].real = 4.8;
	x_input[3].imaginary = 9.1;
	x_input[4].real = 3.3;
	x_input[4].imaginary = 0.4;
	x_input[5].real = 5.9;
	x_input[5].imaginary = 4.8;
	x_input[6].real = 5;
	x_input[6].imaginary = 2.6;
	x_input[7].real = 4.3;
	x_input[7].imaginary = 4.1;

	//set defined values for x[8] to x[N-1] as 0
	for (int i = 8; i < N; i++)
	{
		x_input[i].real = 0;
		x_input[i].imaginary = 0;
	}

    complex twiddle_factor;

    //allocate memory for E_k and O_k
    complex* E_k = (complex *)malloc(sizeof(complex));
    complex* O_k = (complex *)malloc(sizeof(complex));  	
	
	clock_t st = clock();
	
	hipEvent_t start, stop;
	float elapsedTime;

	hipEventCreate(&start);
	hipEventRecord(start,0);
	

    for(int k = 0; k < N/2; k++)
    {
        //get twiddle factor
        twiddle_factor = compute_twiddle(k);

        //compute even and odd portions
        compute_Even_Odd(k, E_k, O_k);

        //add up results
        result[k] = addComplex(*E_k, multiplyComplex(twiddle_factor, *O_k));
		result[k + N/2] = subtractComplex(*E_k, multiplyComplex(twiddle_factor, *O_k));
    }
	
	clock_t end = clock();
	hipEventCreate(&stop);
	hipEventRecord(stop,0);
	hipEventSynchronize(stop);

	hipEventElapsedTime(&elapsedTime, start,stop);
	
	//PRINT FIRST 8 RESULTS
	
	printf("SEQUENTIAL VERSION\n\n");
	
	printf("TOTAL PROCESSED SAMPLES: %d\n", N);	
	for(int i = 0; i < 8; i++)
	{	
		printf("==================\n");
		printf("XR[%d]: %f\n", i, result[i].real);
		printf("XI[%d]: %f\n", i, result[i].imaginary);
	}

	
	printf("==================\n");
	
	printf("Cuda time : %f secs\n", elapsedTime/1000);
	
	printf("\nC clock Time: %f\n", (double)(end - st) / CLOCKS_PER_SEC);

    return 0;

}
	.text
	.file	"FFT_Sequential.hip"
	.globl	_Z15multiplyComplex7complexS_   # -- Begin function _Z15multiplyComplex7complexS_
	.type	_Z15multiplyComplex7complexS_,@function
_Z15multiplyComplex7complexS_:          # @_Z15multiplyComplex7complexS_
	.cfi_startproc
# %bb.0:
	movapd	%xmm0, %xmm4
	mulsd	%xmm2, %xmm4
	movapd	%xmm1, %xmm5
	mulsd	%xmm3, %xmm5
	subsd	%xmm5, %xmm4
	mulsd	%xmm3, %xmm0
	mulsd	%xmm2, %xmm1
	addsd	%xmm0, %xmm1
	movapd	%xmm4, %xmm0
	retq
.Lfunc_end0:
	.size	_Z15multiplyComplex7complexS_, .Lfunc_end0-_Z15multiplyComplex7complexS_
	.cfi_endproc
                                        # -- End function
	.globl	_Z10addComplex7complexS_        # -- Begin function _Z10addComplex7complexS_
	.type	_Z10addComplex7complexS_,@function
_Z10addComplex7complexS_:               # @_Z10addComplex7complexS_
	.cfi_startproc
# %bb.0:
	addsd	%xmm2, %xmm0
	addsd	%xmm3, %xmm1
	retq
.Lfunc_end1:
	.size	_Z10addComplex7complexS_, .Lfunc_end1-_Z10addComplex7complexS_
	.cfi_endproc
                                        # -- End function
	.globl	_Z15subtractComplex7complexS_   # -- Begin function _Z15subtractComplex7complexS_
	.type	_Z15subtractComplex7complexS_,@function
_Z15subtractComplex7complexS_:          # @_Z15subtractComplex7complexS_
	.cfi_startproc
# %bb.0:
	subsd	%xmm2, %xmm0
	subsd	%xmm3, %xmm1
	retq
.Lfunc_end2:
	.size	_Z15subtractComplex7complexS_, .Lfunc_end2-_Z15subtractComplex7complexS_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z16compute_Even_OddiP7complexS0_
.LCPI3_0:
	.quad	0xc000000000000000              # double -2
.LCPI3_1:
	.quad	0x3f20000000000000              # double 1.220703125E-4
	.text
	.globl	_Z16compute_Even_OddiP7complexS0_
	.type	_Z16compute_Even_OddiP7complexS0_,@function
_Z16compute_Even_OddiP7complexS0_:      # @_Z16compute_Even_OddiP7complexS0_
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r12
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	subq	$24, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdx, %rbx
	movq	%rsi, %r14
                                        # kill: def $edi killed $edi def $rdi
	leal	-8192(%rdi), %eax
	cmpl	$8192, %edi                     # imm = 0x2000
	cmovll	%edi, %eax
	xorpd	%xmm0, %xmm0
	movupd	%xmm0, (%rsi)
	cvtsi2sd	%eax, %xmm1
	movsd	%xmm1, (%rsp)                   # 8-byte Spill
	movupd	%xmm0, (%rdx)
	xorl	%r15d, %r15d
	xorl	%r12d, %r12d
.LBB3_1:                                # =>This Inner Loop Header: Depth=1
	movsd	PI(%rip), %xmm1                 # xmm1 = mem[0],zero
	xorps	%xmm0, %xmm0
	cvtsi2sd	%r12d, %xmm0
	mulsd	.LCPI3_0(%rip), %xmm1
	mulsd	%xmm1, %xmm0
	mulsd	(%rsp), %xmm0                   # 8-byte Folded Reload
	mulsd	.LCPI3_1(%rip), %xmm0
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	callq	cos
	movsd	%xmm0, 16(%rsp)                 # 8-byte Spill
	movsd	8(%rsp), %xmm0                  # 8-byte Reload
                                        # xmm0 = mem[0],zero
	callq	sin
	movq	x_input(%rip), %rax
	movsd	(%rax,%r15), %xmm1              # xmm1 = mem[0],zero
	movsd	8(%rax,%r15), %xmm2             # xmm2 = mem[0],zero
	movsd	16(%rsp), %xmm5                 # 8-byte Reload
                                        # xmm5 = mem[0],zero
	movapd	%xmm5, %xmm3
	mulsd	%xmm1, %xmm3
	movapd	%xmm0, %xmm4
	mulsd	%xmm2, %xmm4
	subsd	%xmm4, %xmm3
	mulsd	%xmm0, %xmm1
	mulsd	%xmm5, %xmm2
	addsd	%xmm1, %xmm2
	addsd	(%r14), %xmm3
	addsd	8(%r14), %xmm2
	movsd	%xmm3, (%r14)
	movsd	%xmm2, 8(%r14)
	movq	x_input(%rip), %rax
	movsd	16(%rax,%r15), %xmm1            # xmm1 = mem[0],zero
	movsd	24(%rax,%r15), %xmm2            # xmm2 = mem[0],zero
	movapd	%xmm5, %xmm3
	mulsd	%xmm1, %xmm3
	movapd	%xmm0, %xmm4
	mulsd	%xmm2, %xmm4
	subsd	%xmm4, %xmm3
	mulsd	%xmm1, %xmm0
	movapd	%xmm5, %xmm1
	mulsd	%xmm2, %xmm1
	addsd	(%rbx), %xmm3
	addsd	%xmm0, %xmm1
	addsd	8(%rbx), %xmm1
	movsd	%xmm3, (%rbx)
	movsd	%xmm1, 8(%rbx)
	incq	%r12
	addq	$32, %r15
	cmpq	$8192, %r12                     # imm = 0x2000
	jne	.LBB3_1
# %bb.2:
	addq	$24, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	_Z16compute_Even_OddiP7complexS0_, .Lfunc_end3-_Z16compute_Even_OddiP7complexS0_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z15compute_twiddlei
.LCPI4_0:
	.quad	0xc000000000000000              # double -2
.LCPI4_1:
	.quad	0x3f10000000000000              # double 6.103515625E-5
	.text
	.globl	_Z15compute_twiddlei
	.type	_Z15compute_twiddlei,@function
_Z15compute_twiddlei:                   # @_Z15compute_twiddlei
	.cfi_startproc
# %bb.0:
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movsd	PI(%rip), %xmm1                 # xmm1 = mem[0],zero
	mulsd	.LCPI4_0(%rip), %xmm1
	cvtsi2sd	%edi, %xmm0
	mulsd	%xmm1, %xmm0
	mulsd	.LCPI4_1(%rip), %xmm0
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	callq	cos
	movsd	%xmm0, 16(%rsp)                 # 8-byte Spill
	movsd	8(%rsp), %xmm0                  # 8-byte Reload
                                        # xmm0 = mem[0],zero
	callq	sin
	movaps	%xmm0, %xmm1
	movsd	16(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end4:
	.size	_Z15compute_twiddlei, .Lfunc_end4-_Z15compute_twiddlei
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI5_0:
	.quad	0xc000000000000000              # double -2
.LCPI5_1:
	.quad	0x3f10000000000000              # double 6.103515625E-5
.LCPI5_3:
	.quad	0x412e848000000000              # double 1.0E+6
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0
.LCPI5_2:
	.long	0x447a0000                      # float 1000
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.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
	movl	$262144, %edi                   # imm = 0x40000
	callq	malloc
	movq	%rax, %r14
	movq	%rax, x_input(%rip)
	movl	$262144, %edi                   # imm = 0x40000
	callq	malloc
	movq	%rax, %rbx
	movabsq	$4615288898129284301, %rax      # imm = 0x400CCCCCCCCCCCCD
	movq	%rax, (%r14)
	movabsq	$4613037098315599053, %rax      # imm = 0x4004CCCCCCCCCCCD
	movq	%rax, 8(%r14)
	movabsq	$4613712638259704627, %rcx      # imm = 0x4007333333333333
	movq	%rcx, 16(%r14)
	movabsq	$4618779187840496435, %rcx      # imm = 0x4019333333333333
	movq	%rcx, 24(%r14)
	movabsq	$4617991057905706598, %rcx      # imm = 0x4016666666666666
	movq	%rcx, 32(%r14)
	movabsq	$4616189618054758400, %rcx      # imm = 0x4010000000000000
	movq	%rcx, 40(%r14)
	movabsq	$4617090337980232499, %rcx      # imm = 0x4013333333333333
	movq	%rcx, 48(%r14)
	movabsq	$4621312462630892339, %rdx      # imm = 0x4022333333333333
	movq	%rdx, 56(%r14)
	movabsq	$4614613358185178726, %rdx      # imm = 0x400A666666666666
	movq	%rdx, 64(%r14)
	movabsq	$4600877379321698714, %rdx      # imm = 0x3FD999999999999A
	movq	%rdx, 72(%r14)
	movabsq	$4618328827877759386, %rdx      # imm = 0x401799999999999A
	movq	%rdx, 80(%r14)
	movq	%rcx, 88(%r14)
	movabsq	$4617315517961601024, %rcx      # imm = 0x4014000000000000
	movq	%rcx, 96(%r14)
	movq	%rax, 104(%r14)
	movabsq	$4616527388026811187, %rax      # imm = 0x4011333333333333
	movq	%rax, 112(%r14)
	movabsq	$4616302208045442662, %rax      # imm = 0x4010666666666666
	movq	%rax, 120(%r14)
	subq	$-128, %r14
	xorl	%r15d, %r15d
	movl	$262016, %edx                   # imm = 0x3FF80
	movq	%r14, %rdi
	xorl	%esi, %esi
	callq	memset@PLT
	movl	$16, %edi
	callq	malloc
	movq	%rax, %r12
	movl	$16, %edi
	callq	malloc
	movq	%rax, %r13
	callq	clock
	movq	%rax, %r14
	leaq	24(%rsp), %rbp
	movq	%rbp, %rdi
	callq	hipEventCreate
	movq	(%rbp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	leaq	131080(%rbx), %rbp
.LBB5_1:                                # =>This Inner Loop Header: Depth=1
	movsd	PI(%rip), %xmm1                 # xmm1 = mem[0],zero
	mulsd	.LCPI5_0(%rip), %xmm1
	xorps	%xmm0, %xmm0
	cvtsi2sd	%r15d, %xmm0
	mulsd	%xmm1, %xmm0
	mulsd	.LCPI5_1(%rip), %xmm0
	movsd	%xmm0, (%rsp)                   # 8-byte Spill
	callq	cos
	movsd	%xmm0, 16(%rsp)                 # 8-byte Spill
	movsd	(%rsp), %xmm0                   # 8-byte Reload
                                        # xmm0 = mem[0],zero
	callq	sin
	movsd	%xmm0, (%rsp)                   # 8-byte Spill
	movl	%r15d, %edi
	movq	%r12, %rsi
	movq	%r13, %rdx
	callq	_Z16compute_Even_OddiP7complexS0_
	movsd	(%r12), %xmm0                   # xmm0 = mem[0],zero
	movsd	8(%r12), %xmm1                  # xmm1 = mem[0],zero
	movsd	(%r13), %xmm2                   # xmm2 = mem[0],zero
	movsd	8(%r13), %xmm3                  # xmm3 = mem[0],zero
	movsd	16(%rsp), %xmm7                 # 8-byte Reload
                                        # xmm7 = mem[0],zero
	movapd	%xmm7, %xmm4
	mulsd	%xmm2, %xmm4
	movsd	(%rsp), %xmm6                   # 8-byte Reload
                                        # xmm6 = mem[0],zero
	movapd	%xmm6, %xmm5
	mulsd	%xmm3, %xmm5
	subsd	%xmm5, %xmm4
	mulsd	%xmm3, %xmm7
	mulsd	%xmm2, %xmm6
	addsd	%xmm7, %xmm6
	movapd	%xmm0, %xmm2
	addsd	%xmm4, %xmm2
	movapd	%xmm1, %xmm3
	addsd	%xmm6, %xmm3
	movsd	%xmm2, -131080(%rbp)
	movsd	%xmm3, -131072(%rbp)
	subsd	%xmm4, %xmm0
	subsd	%xmm6, %xmm1
	movsd	%xmm0, -8(%rbp)
	movsd	%xmm1, (%rbp)
	incq	%r15
	addq	$16, %rbp
	cmpq	$8192, %r15                     # imm = 0x2000
	jne	.LBB5_1
# %bb.2:
	callq	clock
	movq	%rax, %r15
	leaq	32(%rsp), %r13
	movq	%r13, %rdi
	callq	hipEventCreate
	movq	(%r13), %rdi
	xorl	%r12d, %r12d
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%r13), %rdi
	callq	hipEventSynchronize
	movq	24(%rsp), %rsi
	movq	(%r13), %rdx
	leaq	12(%rsp), %rdi
	callq	hipEventElapsedTime
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$.L.str.1, %edi
	movl	$16384, %esi                    # imm = 0x4000
	xorl	%eax, %eax
	callq	printf
	addq	$8, %rbx
.LBB5_3:                                # =>This Inner Loop Header: Depth=1
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	movsd	-8(%rbx), %xmm0                 # xmm0 = mem[0],zero
	movl	$.L.str.3, %edi
	movl	%r12d, %esi
	movb	$1, %al
	callq	printf
	movsd	(%rbx), %xmm0                   # xmm0 = mem[0],zero
	movl	$.L.str.4, %edi
	movl	%r12d, %esi
	movb	$1, %al
	callq	printf
	incq	%r12
	addq	$16, %rbx
	cmpq	$8, %r12
	jne	.LBB5_3
# %bb.4:
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	movss	12(%rsp), %xmm0                 # xmm0 = mem[0],zero,zero,zero
	divss	.LCPI5_2(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.5, %edi
	movb	$1, %al
	callq	printf
	subq	%r14, %r15
	xorps	%xmm0, %xmm0
	cvtsi2sd	%r15, %xmm0
	divsd	.LCPI5_3(%rip), %xmm0
	movl	$.L.str.6, %edi
	movb	$1, %al
	callq	printf
	xorl	%eax, %eax
	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_end5:
	.size	main, .Lfunc_end5-main
	.cfi_endproc
                                        # -- End function
	.type	PI,@object                      # @PI
	.data
	.globl	PI
	.p2align	3, 0x0
PI:
	.quad	0x400921fb54442d18              # double 3.1415926535897931
	.size	PI, 8

	.type	x_input,@object                 # @x_input
	.bss
	.globl	x_input
	.p2align	3, 0x0
x_input:
	.quad	0
	.size	x_input, 8

	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"TOTAL PROCESSED SAMPLES: %d\n"
	.size	.L.str.1, 29

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"XR[%d]: %f\n"
	.size	.L.str.3, 12

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"XI[%d]: %f\n"
	.size	.L.str.4, 12

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Cuda time : %f secs\n"
	.size	.L.str.5, 21

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"\nC clock Time: %f\n"
	.size	.L.str.6, 19

	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"SEQUENTIAL VERSION\n"
	.size	.Lstr, 20

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"=================="
	.size	.Lstr.2, 19

	.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
