#include <cufft.h>
#include <cuComplex.h>
#include <stdio.h>
#define N_SIGS 32
#define SIG_LEN 1024

int main(){

  cuFloatComplex *h_signal, *d_signal, *h_result, *d_result;

  h_signal = (cuFloatComplex *)malloc(N_SIGS*SIG_LEN*sizeof(cuFloatComplex));
  h_result = (cuFloatComplex *)malloc(N_SIGS*SIG_LEN*sizeof(cuFloatComplex));
  for (int i = 0; i < N_SIGS; i ++)
    for (int j = 0; j < SIG_LEN; j++)
      h_signal[(i*SIG_LEN) + j] = make_cuFloatComplex(sin((i+1)*6.283*j/SIG_LEN), 0); //this is how to put data into cuFloatComplex type variable
  cudaMalloc(&d_signal, N_SIGS*SIG_LEN*sizeof(cuFloatComplex));
  cudaMalloc(&d_result, N_SIGS*SIG_LEN*sizeof(cuFloatComplex));

  cudaMemcpy(d_signal, h_signal, N_SIGS*SIG_LEN*sizeof(cuFloatComplex), cudaMemcpyHostToDevice);
  cufftHandle plan;
  int n[1] = {SIG_LEN};

  cufftResult res = cufftPlanMany(&plan, 1, n,
     NULL, 1, SIG_LEN,  //advanced data layout, NULL shuts it off
     NULL, 1, SIG_LEN,  //advanced data layout, NULL shuts it off
     CUFFT_C2C, N_SIGS);
  if (res != CUFFT_SUCCESS) {printf("plan create fail\n"); return 1;}

  res = cufftExecC2C(plan, d_signal, d_result, CUFFT_FORWARD);
  if (res != CUFFT_SUCCESS) {printf("forward transform fail\n"); return 1;}
  cudaMemcpy(h_result, d_result, N_SIGS*SIG_LEN*sizeof(cuFloatComplex), cudaMemcpyDeviceToHost);

  for (int i = 0; i < N_SIGS; i++){
    for (int j = 0; j < 10; j++)
      printf("%.3f ", cuCrealf(h_result[(i*SIG_LEN)+j]));
    printf("\n"); }

  return 0;
}

	.file	"tmpxft_0039f6fc_00000000-6_example_2d_cuda.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2053:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2053:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC3:
	.string	"plan create fail\n"
.LC4:
	.string	"forward transform fail\n"
.LC5:
	.string	"%.3f "
.LC6:
	.string	"\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2050:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movl	$262144, %edi
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	subq	$64, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	movl	$262144, %edi
	movq	%rax, %r13
	call	malloc@PLT
	movq	%rax, %r12
.L3:
	leal	1(%rbp), %eax
	movq	%rbp, %rbx
	xorl	%r14d, %r14d
	cvtsi2sdl	%eax, %xmm0
	mulsd	.LC2(%rip), %xmm0
	salq	$13, %rbx
	addq	%r13, %rbx
	movsd	%xmm0, 8(%rsp)
.L4:
	cvtsi2sdl	%r14d, %xmm0
	mulsd	8(%rsp), %xmm0
	incl	%r14d
	addq	$8, %rbx
	mulsd	.LC0(%rip), %xmm0
	call	sin@PLT
	movl	$0x00000000, -4(%rbx)
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, -8(%rbx)
	cmpl	$1024, %r14d
	jne	.L4
	incq	%rbp
	cmpq	$32, %rbp
	jne	.L3
	leaq	32(%rsp), %rdi
	movl	$262144, %esi
	call	cudaMalloc@PLT
	leaq	40(%rsp), %rdi
	movl	$262144, %esi
	call	cudaMalloc@PLT
	movq	32(%rsp), %rdi
	movl	$1, %ecx
	movq	%r13, %rsi
	movl	$262144, %edx
	call	cudaMemcpy@PLT
	movl	$1024, 52(%rsp)
	leaq	52(%rsp), %rdx
	leaq	28(%rsp), %rdi
	pushq	%rax
	.cfi_def_cfa_offset 120
	movl	$1, %esi
	movl	$1024, %r9d
	xorl	%ecx, %ecx
	pushq	$32
	.cfi_def_cfa_offset 128
	movl	$1, %r8d
	pushq	$41
	.cfi_def_cfa_offset 136
	pushq	$1024
	.cfi_def_cfa_offset 144
	pushq	$1
	.cfi_def_cfa_offset 152
	pushq	$0
	.cfi_def_cfa_offset 160
	call	cufftPlanMany@PLT
	addq	$48, %rsp
	.cfi_def_cfa_offset 112
	leaq	.LC3(%rip), %rsi
	testl	%eax, %eax
	jne	.L17
	movq	40(%rsp), %rdx
	movq	32(%rsp), %rsi
	orl	$-1, %ecx
	movl	28(%rsp), %edi
	call	cufftExecC2C@PLT
	testl	%eax, %eax
	je	.L8
	leaq	.LC4(%rip), %rsi
.L17:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %eax
	jmp	.L2
.L8:
	movq	40(%rsp), %rsi
	movl	$2, %ecx
	movq	%r12, %rdi
	xorl	%ebp, %ebp
	movl	$262144, %edx
	leaq	.LC5(%rip), %r14
	call	cudaMemcpy@PLT
.L10:
	movq	%rbp, %r13
	xorl	%ebx, %ebx
	salq	$13, %r13
	addq	%r12, %r13
.L11:
	cvtss2sd	0(%r13,%rbx,8), %xmm0
	movq	%r14, %rsi
	movl	$2, %edi
	movb	$1, %al
	call	__printf_chk@PLT
	incq	%rbx
	cmpq	$10, %rbx
	jne	.L11
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incq	%rbp
	call	__printf_chk@PLT
	cmpq	$32, %rbp
	jne	.L10
	xorl	%eax, %eax
.L2:
	movq	56(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L12
	call	__stack_chk_fail@PLT
.L12:
	addq	$64, %rsp
	.cfi_def_cfa_offset 48
	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
.LFE2050:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2076:
	.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
.LFE2076:
	.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	0
	.long	1062207488
	.align 8
.LC2:
	.long	-1065151889
	.long	1075388874
	.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_complex.h>
#include <stdio.h>
#define N_SIGS 32
#define SIG_LEN 1024

int main(){

  hipFloatComplex *h_signal, *d_signal, *h_result, *d_result;

  h_signal = (hipFloatComplex *)malloc(N_SIGS*SIG_LEN*sizeof(hipFloatComplex));
  h_result = (hipFloatComplex *)malloc(N_SIGS*SIG_LEN*sizeof(hipFloatComplex));
  for (int i = 0; i < N_SIGS; i ++)
    for (int j = 0; j < SIG_LEN; j++)
      h_signal[(i*SIG_LEN) + j] = make_hipFloatComplex(sin((i+1)*6.283*j/SIG_LEN), 0); //this is how to put data into cuFloatComplex type variable
  hipMalloc(&d_signal, N_SIGS*SIG_LEN*sizeof(hipFloatComplex));
  hipMalloc(&d_result, N_SIGS*SIG_LEN*sizeof(hipFloatComplex));

  hipMemcpy(d_signal, h_signal, N_SIGS*SIG_LEN*sizeof(hipFloatComplex), hipMemcpyHostToDevice);
  hipfftHandle plan;
  int n[1] = {SIG_LEN};

  hipfftResult res = hipfftPlanMany(&plan, 1, n,
     NULL, 1, SIG_LEN,  //advanced data layout, NULL shuts it off
     NULL, 1, SIG_LEN,  //advanced data layout, NULL shuts it off
     HIPFFT_C2C, N_SIGS);
  if (res != HIPFFT_SUCCESS) {printf("plan create fail\n"); return 1;}

  res = hipfftExecC2C(plan, d_signal, d_result, HIPFFT_FORWARD);
  if (res != HIPFFT_SUCCESS) {printf("forward transform fail\n"); return 1;}
  hipMemcpy(h_result, d_result, N_SIGS*SIG_LEN*sizeof(hipFloatComplex), hipMemcpyDeviceToHost);

  for (int i = 0; i < N_SIGS; i++){
    for (int j = 0; j < 10; j++)
      printf("%.3f ", hipCrealf(h_result[(i*SIG_LEN)+j]));
    printf("\n"); }

  return 0;
}

	.text
	.file	"example_2d_cuda.hip"
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI0_0:
	.quad	0x401921cac083126f              # double 6.2830000000000004
.LCPI0_1:
	.quad	0x3f50000000000000              # double 9.765625E-4
	.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
	movl	$262144, %edi                   # imm = 0x40000
	callq	malloc
	movq	%rax, %rbx
	xorl	%r15d, %r15d
	movsd	.LCPI0_1(%rip), %xmm1           # xmm1 = mem[0],zero
	movq	%r14, %r12
.LBB0_1:                                # %.preheader28
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_2 Depth 2
	incq	%r15
	xorps	%xmm0, %xmm0
	cvtsi2sd	%r15d, %xmm0
	mulsd	.LCPI0_0(%rip), %xmm0
	movsd	%xmm0, 16(%rsp)                 # 8-byte Spill
	xorl	%r13d, %r13d
.LBB0_2:                                #   Parent Loop BB0_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	xorps	%xmm0, %xmm0
	cvtsi2sd	%r13d, %xmm0
	mulsd	16(%rsp), %xmm0                 # 8-byte Folded Reload
	mulsd	%xmm1, %xmm0
	callq	sin
	movsd	.LCPI0_1(%rip), %xmm1           # xmm1 = mem[0],zero
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, (%rsp)
	movl	$0, 4(%rsp)
	movsd	(%rsp), %xmm0                   # xmm0 = mem[0],zero
	movsd	%xmm0, (%r12,%r13,8)
	incq	%r13
	cmpq	$1024, %r13                     # imm = 0x400
	jne	.LBB0_2
# %bb.3:                                #   in Loop: Header=BB0_1 Depth=1
	addq	$8192, %r12                     # imm = 0x2000
	cmpq	$32, %r15
	jne	.LBB0_1
# %bb.4:
	movq	%rsp, %r15
	movl	$262144, %esi                   # imm = 0x40000
	movq	%r15, %rdi
	callq	hipMalloc
	leaq	8(%rsp), %rdi
	movl	$262144, %esi                   # imm = 0x40000
	callq	hipMalloc
	movq	(%r15), %rdi
	movl	$1, %ebp
	movl	$262144, %edx                   # imm = 0x40000
	movq	%r14, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movl	$1024, %eax                     # imm = 0x400
	leaq	36(%rsp), %rdx
	movl	%eax, (%rdx)
	subq	$8, %rsp
	.cfi_adjust_cfa_offset 8
	leaq	32(%rsp), %rdi
	movl	$1, %esi
	xorl	%ecx, %ecx
	movl	$1, %r8d
	movl	$1024, %r9d                     # imm = 0x400
	pushq	$32
	.cfi_adjust_cfa_offset 8
	pushq	$41
	.cfi_adjust_cfa_offset 8
	pushq	%rax
	.cfi_adjust_cfa_offset 8
	pushq	$1
	.cfi_adjust_cfa_offset 8
	pushq	$0
	.cfi_adjust_cfa_offset 8
	callq	hipfftPlanMany
	addq	$48, %rsp
	.cfi_adjust_cfa_offset -48
	movl	$.Lstr.1, %edi
	testl	%eax, %eax
	jne	.LBB0_11
# %bb.5:
	movq	24(%rsp), %rdi
	movq	(%rsp), %rsi
	movq	8(%rsp), %rdx
	movl	$-1, %ecx
	callq	hipfftExecC2C
	movl	$.Lstr, %edi
	testl	%eax, %eax
	je	.LBB0_6
.LBB0_11:                               # %.loopexit.sink.split
	callq	puts@PLT
.LBB0_12:                               # %.loopexit
	movl	%ebp, %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
.LBB0_6:
	.cfi_def_cfa_offset 96
	movq	8(%rsp), %rsi
	movl	$262144, %edx                   # imm = 0x40000
	movq	%rbx, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	xorl	%r14d, %r14d
.LBB0_7:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_8 Depth 2
	xorl	%r15d, %r15d
.LBB0_8:                                #   Parent Loop BB0_7 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rbx,%r15,8), %xmm0
	movl	$.L.str.2, %edi
	movb	$1, %al
	callq	printf
	incq	%r15
	cmpq	$10, %r15
	jne	.LBB0_8
# %bb.9:                                #   in Loop: Header=BB0_7 Depth=1
	movl	$10, %edi
	callq	putchar@PLT
	incq	%r14
	addq	$8192, %rbx                     # imm = 0x2000
	cmpq	$32, %r14
	jne	.LBB0_7
# %bb.10:
	xorl	%ebp, %ebp
	jmp	.LBB0_12
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str.2,@object                # @.str.2
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.2:
	.asciz	"%.3f "
	.size	.L.str.2, 6

	.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	"forward transform fail"
	.size	.Lstr, 23

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"plan create fail"
	.size	.Lstr.1, 17

	.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
