#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <cuda.h>
#include <cufft.h>


#include <iostream>

// #include <complex>


#include <cuda_runtime.h>

#define imin(a,b) (a<b?a:b)     

 

/*--------- function called from main fortran programn ---------------*/
// -------------  Onli 1D, 2D or 3D  cuFFT transform !!!!!!!!!!!!!!!!!!!


extern "C" void kernel_imestfft_t_(cuDoubleComplex *wtilde,int *NX,int *NY,int *NZ,  int *Xcase)
{
           cuDoubleComplex  *psi_d;

        int NDX = *NX;
        int NDY = *NY;
        int NDZ = *NZ;
        int NDIM= NDX*NDY*NDZ;
        int dcase= *Xcase;


//----------------------------------------------------------------------------------------------
//                 printf("IN T GPU _Transform  \n");  

         cufftHandle plan;
         cudaMalloc( (void **)&psi_d, sizeof(cuDoubleComplex) * NDIM );
          cudaMemcpy( psi_d, wtilde, sizeof(cuDoubleComplex)*NDIM, cudaMemcpyHostToDevice );

 //                printf("Kernel GPU _Transform  \n");  

      if (dcase == 1)  cufftPlan1d(&plan, NDX, CUFFT_Z2Z, 1); 
     
     if (dcase == 2)   cufftPlan2d(&plan, NDY, NDX,      CUFFT_Z2Z); 
   
     if (dcase == 3)   cufftPlan3d(&plan, NDZ, NDY, NDX, CUFFT_Z2Z); 

          cufftExecZ2Z(plan,  psi_d,  psi_d, CUFFT_FORWARD);

 
        /*  copy vectors from GPU to CPU   */
           
            cudaMemcpy(wtilde, psi_d, sizeof(cuDoubleComplex) * NDIM, cudaMemcpyDeviceToHost);

           cufftDestroy(plan);
           cudaFree(psi_d);
        return;

}

	.file	"tmpxft_0028f630_00000000-6_modul_IMEST_FFT_T_CUDACPP.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
	.globl	kernel_imestfft_t_
	.type	kernel_imestfft_t_, @function
kernel_imestfft_t_:
.LFB3658:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	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
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	movl	(%rsi), %r12d
	movl	(%rdx), %r13d
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	movl	(%rcx), %r15d
	leaq	16(%rsp), %rdi
	movl	(%r8), %r14d
	movl	%r12d, %ebx
	imull	%r13d, %ebx
	imull	%r15d, %ebx
	movslq	%ebx, %rbx
	salq	$4, %rbx
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movq	16(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%rbp, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	cmpl	$1, %r14d
	jne	.L3
	leaq	12(%rsp), %rdi
	movl	$1, %ecx
	movl	$105, %edx
	movl	%r12d, %esi
	call	cufftPlan1d@PLT
	jmp	.L4
.L3:
	cmpl	$2, %r14d
	jne	.L5
	leaq	12(%rsp), %rdi
	movl	$105, %ecx
	movl	%r12d, %edx
	movl	%r13d, %esi
	call	cufftPlan2d@PLT
	jmp	.L4
.L5:
	cmpl	$3, %r14d
	jne	.L4
	leaq	12(%rsp), %rdi
	movl	%r12d, %ecx
	movl	%r13d, %edx
	movl	%r15d, %esi
	movl	$105, %r8d
	call	cufftPlan3d@PLT
.L4:
	movq	16(%rsp), %rsi
	movl	12(%rsp), %edi
	orl	$-1, %ecx
	movq	%rsi, %rdx
	call	cufftExecZ2Z@PLT
	movq	16(%rsp), %rsi
	movq	%rbx, %rdx
	movq	%rbp, %rdi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movl	12(%rsp), %edi
	call	cufftDestroy@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L6
	call	__stack_chk_fail@PLT
.L6:
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	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
.LFE3658:
	.size	kernel_imestfft_t_, .-kernel_imestfft_t_
	.section	.text.startup,"ax",@progbits
	.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
	.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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <hip/hip_runtime.h>
#include <hipfft/hipfft.h>


#include <iostream>

// #include <complex>




#define imin(a,b) (a<b?a:b)     

 

/*--------- function called from main fortran programn ---------------*/
// -------------  Onli 1D, 2D or 3D  cuFFT transform !!!!!!!!!!!!!!!!!!!


extern "C" void kernel_imestfft_t_(hipDoubleComplex *wtilde,int *NX,int *NY,int *NZ,  int *Xcase)
{
           hipDoubleComplex  *psi_d;

        int NDX = *NX;
        int NDY = *NY;
        int NDZ = *NZ;
        int NDIM= NDX*NDY*NDZ;
        int dcase= *Xcase;


//----------------------------------------------------------------------------------------------
//                 printf("IN T GPU _Transform  \n");  

         hipfftHandle plan;
         hipMalloc( (void **)&psi_d, sizeof(hipDoubleComplex) * NDIM );
          hipMemcpy( psi_d, wtilde, sizeof(hipDoubleComplex)*NDIM, hipMemcpyHostToDevice );

 //                printf("Kernel GPU _Transform  \n");  

      if (dcase == 1)  hipfftPlan1d(&plan, NDX, HIPFFT_Z2Z, 1); 
     
     if (dcase == 2)   hipfftPlan2d(&plan, NDY, NDX,      HIPFFT_Z2Z); 
   
     if (dcase == 3)   hipfftPlan3d(&plan, NDZ, NDY, NDX, HIPFFT_Z2Z); 

          hipfftExecZ2Z(plan,  psi_d,  psi_d, HIPFFT_FORWARD);

 
        /*  copy vectors from GPU to CPU   */
           
            hipMemcpy(wtilde, psi_d, sizeof(hipDoubleComplex) * NDIM, hipMemcpyDeviceToHost);

           hipfftDestroy(plan);
           hipFree(psi_d);
        return;

}

	.text
	.file	"modul_IMEST_FFT_T_CUDACPP.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.globl	kernel_imestfft_t_              # -- Begin function kernel_imestfft_t_
	.type	kernel_imestfft_t_,@function
kernel_imestfft_t_:                     # @kernel_imestfft_t_
	.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	$24, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rdi, %rbp
	movl	(%rsi), %esi
	movl	(%rdx), %r15d
	movl	(%rcx), %r12d
	movl	%r15d, %eax
	movl	%esi, 4(%rsp)                   # 4-byte Spill
	imull	%esi, %eax
	imull	%r12d, %eax
	movl	(%r8), %ebx
	movslq	%eax, %r14
	shlq	$4, %r14
	leaq	16(%rsp), %r13
	movq	%r13, %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movq	(%r13), %rdi
	movq	%rbp, %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	cmpl	$3, %ebx
	je	.LBB0_5
# %bb.1:
	cmpl	$2, %ebx
	je	.LBB0_4
# %bb.2:
	cmpl	$1, %ebx
	jne	.LBB0_6
# %bb.3:
	leaq	8(%rsp), %rdi
	movl	4(%rsp), %esi                   # 4-byte Reload
	movl	$105, %edx
	movl	$1, %ecx
	callq	hipfftPlan1d
	jmp	.LBB0_6
.LBB0_4:
	leaq	8(%rsp), %rdi
	movl	%r15d, %esi
	movl	4(%rsp), %edx                   # 4-byte Reload
	movl	$105, %ecx
	callq	hipfftPlan2d
	jmp	.LBB0_6
.LBB0_5:
	leaq	8(%rsp), %rdi
	movl	%r12d, %esi
	movl	%r15d, %edx
	movl	4(%rsp), %ecx                   # 4-byte Reload
	movl	$105, %r8d
	callq	hipfftPlan3d
.LBB0_6:
	movq	8(%rsp), %rdi
	movq	16(%rsp), %rdx
	movq	%rdx, %rsi
	movl	$-1, %ecx
	callq	hipfftExecZ2Z
	movq	16(%rsp), %rsi
	movq	%rbp, %rdi
	movq	%r14, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	8(%rsp), %rdi
	callq	hipfftDestroy
	movq	16(%rsp), %rdi
	callq	hipFree
	addq	$24, %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	kernel_imestfft_t_, .Lfunc_end0-kernel_imestfft_t_
	.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
