#include <iostream>
#include "cuda_runtime.h"
#include <cufft.h>
#include <stdio.h>

const double PI = 3.141592653589793238460;

int main() {
	
	int N = 10;
	size_t memorySize = N * sizeof(cufftComplex);

	cufftComplex* A = (cufftComplex*) malloc(memorySize);
	cufftComplex* B = (cufftComplex*)malloc(memorySize);

	for (int i = 0; i < N; i++) {
		A[i].x = (float) sin(2 * PI * i / N);
		A[i].y = 0.0;
	}

	cufftComplex* d_A;
	cufftComplex* d_B;

	cudaMalloc(&d_A, memorySize);
	cudaMalloc(&d_B, memorySize);

	cudaMemcpy(d_A, A, memorySize, cudaMemcpyHostToDevice);

	cufftHandle plan;
	cufftPlan1d(&plan, N, CUFFT_C2C, 1);
	cufftExecC2C(plan, d_A, d_B, CUFFT_FORWARD);

	cudaMemcpy(B, d_B, memorySize, cudaMemcpyDeviceToHost);

	for (int i = 0; i < N; i++) {
		printf("%f %f %f %f\n", A[i].x, A[i].y, B[i].x, B[i].y);
	}

	return 0;
}
	.file	"tmpxft_0035ca2d_00000000-6_FFTSineWave.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3661:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3661:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC3:
	.string	"%f %f %f %f\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3658:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movl	$80, %edi
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	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
	subq	$40, %rsp
	.cfi_def_cfa_offset 80
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	movl	$80, %edi
	movq	%rax, %rbp
	call	malloc@PLT
	movq	%rax, %r12
.L3:
	cvtsi2sdl	%ebx, %xmm0
	mulsd	.LC0(%rip), %xmm0
	divsd	.LC1(%rip), %xmm0
	call	sin@PLT
	movl	$0x00000000, 4(%rbp,%rbx,8)
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, 0(%rbp,%rbx,8)
	incq	%rbx
	cmpq	$10, %rbx
	jne	.L3
	leaq	8(%rsp), %rdi
	movl	$80, %esi
	xorl	%ebx, %ebx
	call	cudaMalloc@PLT
	leaq	16(%rsp), %rdi
	movl	$80, %esi
	leaq	.LC3(%rip), %r13
	call	cudaMalloc@PLT
	movq	8(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbp, %rsi
	movl	$80, %edx
	call	cudaMemcpy@PLT
	leaq	4(%rsp), %rdi
	movl	$1, %ecx
	movl	$41, %edx
	movl	$10, %esi
	call	cufftPlan1d@PLT
	movq	16(%rsp), %rdx
	movq	8(%rsp), %rsi
	orl	$-1, %ecx
	movl	4(%rsp), %edi
	call	cufftExecC2C@PLT
	movq	16(%rsp), %rsi
	movl	$2, %ecx
	movq	%r12, %rdi
	movl	$80, %edx
	call	cudaMemcpy@PLT
.L4:
	cvtss2sd	0(%rbp,%rbx), %xmm0
	movq	%r13, %rsi
	movl	$2, %edi
	movb	$4, %al
	cvtss2sd	4(%r12,%rbx), %xmm3
	cvtss2sd	(%r12,%rbx), %xmm2
	cvtss2sd	4(%rbp,%rbx), %xmm1
	addq	$8, %rbx
	call	__printf_chk@PLT
	cmpq	$80, %rbx
	jne	.L4
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L5
	call	__stack_chk_fail@PLT
.L5:
	addq	$40, %rsp
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	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
.LFE3658:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3684:
	.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
.LFE3684:
	.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
.LC0:
	.long	1413754136
	.long	1075388923
	.align 8
.LC1:
	.long	0
	.long	1076101120
	.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 <iostream>
#include "hip/hip_runtime.h"
#include <hipfft/hipfft.h>
#include <stdio.h>

const double PI = 3.141592653589793238460;

int main() {
	
	int N = 10;
	size_t memorySize = N * sizeof(hipfftComplex);

	hipfftComplex* A = (hipfftComplex*) malloc(memorySize);
	hipfftComplex* B = (hipfftComplex*)malloc(memorySize);

	for (int i = 0; i < N; i++) {
		A[i].x = (float) sin(2 * PI * i / N);
		A[i].y = 0.0;
	}

	hipfftComplex* d_A;
	hipfftComplex* d_B;

	hipMalloc(&d_A, memorySize);
	hipMalloc(&d_B, memorySize);

	hipMemcpy(d_A, A, memorySize, hipMemcpyHostToDevice);

	hipfftHandle plan;
	hipfftPlan1d(&plan, N, HIPFFT_C2C, 1);
	hipfftExecC2C(plan, d_A, d_B, HIPFFT_FORWARD);

	hipMemcpy(B, d_B, memorySize, hipMemcpyDeviceToHost);

	for (int i = 0; i < N; i++) {
		printf("%f %f %f %f\n", A[i].x, A[i].y, B[i].x, B[i].y);
	}

	return 0;
}
	.text
	.file	"FFTSineWave.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI0_0:
	.quad	0x401921fb54442d18              # double 6.2831853071795862
.LCPI0_1:
	.quad	0x4024000000000000              # double 10
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$32, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$80, %edi
	callq	malloc
	movq	%rax, %rbx
	movl	$80, %edi
	callq	malloc
	movq	%rax, %r14
	xorl	%r15d, %r15d
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
	xorps	%xmm0, %xmm0
	cvtsi2sd	%r15d, %xmm0
	mulsd	.LCPI0_0(%rip), %xmm0
	divsd	.LCPI0_1(%rip), %xmm0
	callq	sin
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, (%rbx,%r15,8)
	movl	$0, 4(%rbx,%r15,8)
	incq	%r15
	cmpq	$10, %r15
	jne	.LBB0_1
# %bb.2:
	leaq	24(%rsp), %r12
	movl	$80, %esi
	movq	%r12, %rdi
	callq	hipMalloc
	leaq	16(%rsp), %r15
	movl	$80, %esi
	movq	%r15, %rdi
	callq	hipMalloc
	movq	(%r12), %rdi
	movl	$80, %edx
	movq	%rbx, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	8(%rsp), %r13
	movq	%r13, %rdi
	movl	$10, %esi
	movl	$41, %edx
	movl	$1, %ecx
	callq	hipfftPlan1d
	movq	(%r13), %rdi
	movq	(%r12), %rsi
	movq	(%r15), %rdx
	movl	$-1, %ecx
	callq	hipfftExecC2C
	movq	(%r15), %rsi
	movl	$80, %edx
	movq	%r14, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	xorl	%r15d, %r15d
.LBB0_3:                                # =>This Inner Loop Header: Depth=1
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rbx,%r15,8), %xmm0
	xorps	%xmm1, %xmm1
	cvtss2sd	4(%rbx,%r15,8), %xmm1
	xorps	%xmm2, %xmm2
	cvtss2sd	(%r14,%r15,8), %xmm2
	xorps	%xmm3, %xmm3
	cvtss2sd	4(%r14,%r15,8), %xmm3
	movl	$.L.str, %edi
	movb	$4, %al
	callq	printf
	incq	%r15
	cmpq	$10, %r15
	jne	.LBB0_3
# %bb.4:
	xorl	%eax, %eax
	addq	$32, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.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
	retq
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"%f %f %f %f\n"
	.size	.L.str, 13

	.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
