#include "cufft.h"
#include "cuda_runtime_api.h"

typedef float2 Complex;

void
PerformCUDAFFT(float *inputData, float *outputData, unsigned int numSamples) {
    cufftHandle plan;
    cufftComplex *inputDataG, *outputDataG;
    int i;

    float *inputDataC, *outputDataC;
    outputDataC = (float*) malloc(sizeof(float) * numSamples * 2);
    inputDataC  = (float*) malloc(sizeof(float) * numSamples * 2);

    for (i = 0; i < numSamples; i++) {
      inputDataC[i*2]     = inputData[i];
      inputDataC[i*2 + 1] = 0.0f;
    }

    cudaMalloc((void**)&inputDataG,  sizeof(cufftComplex)*numSamples);
    cudaMalloc((void**)&outputDataG, sizeof(cufftComplex)*numSamples);

    cudaMemcpy(inputDataG, inputDataC, sizeof(cufftComplex)*numSamples, cudaMemcpyHostToDevice);
    cufftPlan1d(&plan, numSamples, CUFFT_C2C, 1);
    cufftExecC2C(plan, inputDataG, outputDataG, CUFFT_FORWARD);
    cufftDestroy(plan);
    cudaMemcpy(outputDataC, outputDataG, sizeof(cufftComplex)*numSamples, cudaMemcpyDeviceToHost);
    cudaFree(inputDataG);
    cudaFree(outputDataG);
    
    for (i = 0; i < numSamples; i++) {
        outputData[i] = outputDataC[i * 2];
    }
    free(outputDataC);
    free(inputDataC);
}

	.file	"tmpxft_002711c5_00000000-6_fftcuda.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2034:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2034:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z14PerformCUDAFFTPfS_j
	.type	_Z14PerformCUDAFFTPfS_j, @function
_Z14PerformCUDAFFTPfS_j:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rdi, %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%rsi, %r14
	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
	movl	%edx, %ebx
	movq	%rbx, %r12
	salq	$3, %rbx
	movq	%rbx, %rdi
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	movq	%rbx, %rdi
	movq	%rax, %r13
	call	malloc@PLT
	movq	%rax, %rbp
	xorl	%eax, %eax
.L3:
	cmpl	%r12d, %eax
	jnb	.L11
	movss	(%r15,%rax,4), %xmm0
	movl	$0x00000000, 4(%rbp,%rax,8)
	movss	%xmm0, 0(%rbp,%rax,8)
	incq	%rax
	jmp	.L3
.L11:
	leaq	8(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	leaq	16(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movq	8(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%rbp, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	leaq	4(%rsp), %rdi
	movl	$1, %ecx
	movl	%r12d, %esi
	movl	$41, %edx
	call	cufftPlan1d@PLT
	movq	16(%rsp), %rdx
	movq	8(%rsp), %rsi
	orl	$-1, %ecx
	movl	4(%rsp), %edi
	call	cufftExecC2C@PLT
	movl	4(%rsp), %edi
	call	cufftDestroy@PLT
	movq	16(%rsp), %rsi
	movq	%rbx, %rdx
	movq	%r13, %rdi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	xorl	%eax, %eax
.L5:
	cmpl	%r12d, %eax
	jnb	.L12
	movss	0(%r13,%rax,8), %xmm0
	movss	%xmm0, (%r14,%rax,4)
	incq	%rax
	jmp	.L5
.L12:
	movq	%r13, %rdi
	call	free@PLT
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L7
	call	__stack_chk_fail@PLT
.L7:
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	movq	%rbp, %rdi
	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
	jmp	free@PLT
	.cfi_endproc
.LFE2031:
	.size	_Z14PerformCUDAFFTPfS_j, .-_Z14PerformCUDAFFTPfS_j
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2057:
	.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
.LFE2057:
	.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 "hipfft/hipfft.h"
#include "hip/hip_runtime_api.h"

typedef float2 Complex;

void
PerformCUDAFFT(float *inputData, float *outputData, unsigned int numSamples) {
    hipfftHandle plan;
    hipfftComplex *inputDataG, *outputDataG;
    int i;

    float *inputDataC, *outputDataC;
    outputDataC = (float*) malloc(sizeof(float) * numSamples * 2);
    inputDataC  = (float*) malloc(sizeof(float) * numSamples * 2);

    for (i = 0; i < numSamples; i++) {
      inputDataC[i*2]     = inputData[i];
      inputDataC[i*2 + 1] = 0.0f;
    }

    hipMalloc((void**)&inputDataG,  sizeof(hipfftComplex)*numSamples);
    hipMalloc((void**)&outputDataG, sizeof(hipfftComplex)*numSamples);

    hipMemcpy(inputDataG, inputDataC, sizeof(hipfftComplex)*numSamples, hipMemcpyHostToDevice);
    hipfftPlan1d(&plan, numSamples, HIPFFT_C2C, 1);
    hipfftExecC2C(plan, inputDataG, outputDataG, HIPFFT_FORWARD);
    hipfftDestroy(plan);
    hipMemcpy(outputDataC, outputDataG, sizeof(hipfftComplex)*numSamples, hipMemcpyDeviceToHost);
    hipFree(inputDataG);
    hipFree(outputDataG);
    
    for (i = 0; i < numSamples; i++) {
        outputData[i] = outputDataC[i * 2];
    }
    free(outputDataC);
    free(inputDataC);
}

	.text
	.file	"fftcuda.hip"
	.globl	_Z14PerformCUDAFFTPfS_j         # -- Begin function _Z14PerformCUDAFFTPfS_j
	.type	_Z14PerformCUDAFFTPfS_j,@function
_Z14PerformCUDAFFTPfS_j:                # @_Z14PerformCUDAFFTPfS_j
	.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	%edx, %r12d
	movq	%rsi, %rbx
	movq	%rdi, %r13
	movl	%edx, %ebp
	leaq	(,%rbp,8), %r15
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, %r14
	movq	%r15, 16(%rsp)                  # 8-byte Spill
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, %r15
	movl	%r12d, 4(%rsp)                  # 4-byte Spill
	testl	%r12d, %r12d
	je	.LBB0_3
# %bb.1:                                # %.lr.ph.preheader
	xorl	%eax, %eax
.LBB0_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movss	(%r13,%rax,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, (%r15,%rax,8)
	movl	$0, 4(%r15,%rax,8)
	incq	%rax
	cmpq	%rax, %rbp
	jne	.LBB0_2
.LBB0_3:                                # %._crit_edge
	leaq	8(%rsp), %rdi
	movq	%rdi, %r13
	movq	16(%rsp), %r12                  # 8-byte Reload
	movq	%r12, %rsi
	callq	hipMalloc
	leaq	24(%rsp), %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	movq	(%r13), %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	32(%rsp), %r13
	movq	%r13, %rdi
	movl	4(%rsp), %esi                   # 4-byte Reload
	movl	$41, %edx
	movl	$1, %ecx
	callq	hipfftPlan1d
	movq	(%r13), %rdi
	leaq	8(%rsp), %rax
	movq	(%rax), %rsi
	leaq	24(%rsp), %r13
	movq	(%r13), %rdx
	movl	$-1, %ecx
	callq	hipfftExecC2C
	leaq	32(%rsp), %rax
	movq	(%rax), %rdi
	callq	hipfftDestroy
	movq	(%r13), %rsi
	movq	%r14, %rdi
	movq	%r12, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	leaq	8(%rsp), %rax
	movq	(%rax), %rdi
	callq	hipFree
	movq	(%r13), %rdi
	callq	hipFree
	cmpl	$0, 4(%rsp)                     # 4-byte Folded Reload
	je	.LBB0_6
# %bb.4:                                # %.lr.ph30.preheader
	xorl	%eax, %eax
.LBB0_5:                                # %.lr.ph30
                                        # =>This Inner Loop Header: Depth=1
	movss	(%r14,%rax,8), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, (%rbx,%rax,4)
	incq	%rax
	cmpq	%rax, %rbp
	jne	.LBB0_5
.LBB0_6:                                # %._crit_edge31
	movq	%r14, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	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_end0:
	.size	_Z14PerformCUDAFFTPfS_j, .Lfunc_end0-_Z14PerformCUDAFFTPfS_j
	.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
