////
//// Created by Chen on 11/8/2020.
////

#include <cufft.h>
#include <cstdio>
#include "common.cuh"


#define PI 3.14159265358979324f

__global__
void waveSliceTransmitKernel(cufftComplex *wave,  cufftReal const *slice, unsigned nPix,
                             float waveLength, float relativityGamma,
                             cufftComplex *waveOut)
/*
 * transmit the wave function in real space through one single slice.
 * waveLength is in Angstroms.
 */
{
    unsigned batch = gridDim.x * blockDim.x;
    unsigned ii;  // the global index of 2D array

    float t_real, t_imag;  // transmission function
    float w_real, w_imag;  // wave
    float factor = waveLength * relativityGamma;
    //slide rightwards
    unsigned gridStartIdx = 0;
    while(gridStartIdx < nPix) {
        ii = gridStartIdx + blockDim.x * blockIdx.x + threadIdx.x;
        if (gridStartIdx > 0)
            printf("ii = %d\n", ii);
        if (ii < nPix) {
            t_real = cosf(slice[ii] * factor);
            t_imag = sinf(slice[ii] * factor);
            w_real = wave[ii].x * t_real - wave[ii].y * t_imag;
            w_imag = wave[ii].x * t_imag + wave[ii].y * t_real;
            waveOut[ii].x = w_real;
            waveOut[ii].y = w_imag;
        }
        gridStartIdx += batch;
    }
}


__global__
void waveSpacePropagateFourierKernel(cufftComplex *waveFourier,
                                     int n1, int n2, float dz, float d1, float d2,
                                     float waveLength,
                                     cufftComplex *waveOut)
{
    unsigned batch = gridDim.x * blockDim.x;
    unsigned nPix = n1 * n2;
    float dfx = 1.0f / d1 / float(n1);
    float dfy = 1.0f / d2 / float(n2);
    //Nyquist frequency and 1/3 filter
    float fMax = 0.5f / (d1 >= d2 ? d1 : d2);
    float filter = 0.6667f * fMax;

    unsigned ii;  // the global index of 2D array
    int i, j;     // the dual index
    int is, js;   // the ifftshifted indexes, signed
    float fx, fy; // the corresponding spatial frequency to is, js
    float f2;     // the squared spatial frequency f2 = fx*fx + fy*fy

    float p_real, p_imag;  // spatial propagator
    float w_real, w_imag;  // wave
    //slide rightwards
    unsigned gridStartIdx = 0;
    while(gridStartIdx < nPix) {
        ii = gridStartIdx + blockDim.x * blockIdx.x + threadIdx.x;
        if (ii < nPix) {
            i = (int)ii / n2;
            j = (int)ii % n2;
            is = i + (i<(n1+1)/2? n1/2: -(n1+1)/2);
            js = j + (j<(n2+1)/2? n2/2: -(n2+1)/2);
            fx = ((float)is - floorf((float)n1/2.0f)) * dfx;
            fy = ((float)js - floorf((float)n2/2.0f)) * dfy;
            f2 = fx*fx + fy*fy;
            if (f2 <= filter * filter) {
                p_real = cosf(waveLength * PI * dz * f2);
                p_imag = -sinf(waveLength * PI * dz * f2);
                w_real = waveFourier[ii].x * p_real - waveFourier[ii].y * p_imag;
                w_imag = waveFourier[ii].x * p_imag + waveFourier[ii].y * p_real;
                waveOut[ii].x = w_real / (float)nPix;
                waveOut[ii].y = w_imag / (float)nPix;
            } else {
                waveOut[ii].x = 0;
                waveOut[ii].y = 0;
            }
        }
        gridStartIdx += batch;
    }
}


__global__
void waveLensPropagateKernel(cufftComplex *waveFourier, int n1, int n2, float d1, float d2,
                             float waveLength, float cs_mm, float defocus, float aperture,
                             cufftComplex *waveOut)
{
    unsigned batch = gridDim.x * blockDim.x;
    unsigned nPix = n1 * n2;
    float dfx = 1.0f / d1 / float(n1);
    float dfy = 1.0f / d2 / float(n2);
    float fAper = aperture / waveLength;
    float c1 = 0.5f * PI * cs_mm * 1e7f * waveLength * waveLength * waveLength;
    float c2 = PI * defocus * waveLength;

    unsigned ii;  // the global index of 2D array
    int i, j;     // the dual index
    int is, js;   // the ifftshifted indexes
    float fx, fy; // the corresponding spatial frequency to is, js
    float f2;     // the squared spatial frequency f2 = fx*fx + fy*fy

    float h_real, h_imag;  // modulation transfer function
    float w_real, w_imag;  // wave
    float aberr;           // optic aberration
    unsigned gridStartIdx = 0;
    while(gridStartIdx < nPix) {
        ii = gridStartIdx + blockDim.x * blockIdx.x + threadIdx.x;
        if (ii < nPix) {
            i = (int)ii / n2;
            j = (int)ii % n2;
            is = i + (i<(n1+1)/2? n1/2: -(n1+1)/2);
            js = j + (j<(n2+1)/2? n2/2: -(n2+1)/2);
            fx = ((float)is - floorf((float)n1/2.0f)) * dfx;
            fy = ((float)js - floorf((float)n2/2.0f)) * dfy;
            f2 = fx*fx + fy*fy;
            if (f2 <= fAper * fAper) {
                aberr = c1 * f2 * f2 - c2 * f2;
                h_real = cosf(aberr);
                h_imag = -sinf(aberr);
                w_real = waveFourier[ii].x * h_real - waveFourier[ii].y * h_imag;
                w_imag = waveFourier[ii].x * h_imag + waveFourier[ii].y * h_real;
                waveOut[ii].x = w_real / (float)nPix;
                waveOut[ii].y = w_imag / (float)nPix;
            } else {
                waveOut[ii].x = 0;
                waveOut[ii].y = 0;
            }
        }
        gridStartIdx += batch;
    }
}

namespace emsim { namespace cuda {
    void waveSliceTransmit(cufftComplex *wave, cufftReal const *slice, int nPix,
                           float waveLength, float relativityGamma,
                           cufftComplex *waveOut) {

        unsigned blockDimX = maxThreadsPerBlock;
        if (blockDimX > nPix) blockDimX = nPix;
        auto gridDimX = (unsigned) ceilf((float) nPix / (float) blockDimX);
        gridDimX = gridDimX > 2147483647 ? 2147483647 : gridDimX;
        waveSliceTransmitKernel<<<gridDimX, blockDimX>>>(wave, slice, nPix, waveLength, relativityGamma, waveOut);
    }


    void waveSpacePropagateFourier(cufftComplex *waveFourier,
                            int n1, int n2, float dz, float d1, float d2,
                            float waveLength,
                            cufftComplex *waveOut) {
        unsigned nPix = n1 * n2;
        unsigned blockDimX = maxThreadsPerBlock;
        if (blockDimX > nPix) blockDimX = nPix;
        auto gridDimX = (unsigned) ceilf((float) nPix / (float) blockDimX);
        gridDimX = gridDimX > 2147483647 ? 2147483647 : gridDimX;

        waveSpacePropagateFourierKernel<<<gridDimX, blockDimX>>>(waveFourier, n1, n2,
                                                          dz, d1, d2, waveLength,
                                                          waveOut);
    }

    void waveLensPropagate(cufftComplex *waveFourier, int n1, int n2, float d1, float d2,
                             float waveLength, float cs_mm, float defocus, float aperture,
                             cufftComplex *waveOut) {
        unsigned nPix = n1 * n2;
        unsigned blockDimX = maxThreadsPerBlock;
        if (blockDimX > nPix) blockDimX = nPix;
        auto gridDimX = (unsigned) ceilf((float) nPix / (float) blockDimX);
        gridDimX = gridDimX > 2147483647 ? 2147483647 : gridDimX;

        waveLensPropagateKernel<<<gridDimX, blockDimX>>>(waveFourier, n1, n2,
                                                         d1, d2, waveLength, cs_mm, defocus, aperture,
                                                         waveOut);
    }
} }

	.file	"tmpxft_0027ae67_00000000-6_WavePropagator_kernel.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2055:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2055:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z58__device_stub__Z23waveSliceTransmitKernelP6float2PKfjffS0_P6float2PKfjffS0_
	.type	_Z58__device_stub__Z23waveSliceTransmitKernelP6float2PKfjffS0_P6float2PKfjffS0_, @function
_Z58__device_stub__Z23waveSliceTransmitKernelP6float2PKfjffS0_P6float2PKfjffS0_:
.LFB2077:
	.cfi_startproc
	endbr64
	subq	$184, %rsp
	.cfi_def_cfa_offset 192
	movq	%rdi, 40(%rsp)
	leaq	72(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	84(%rsp), %rsi
	movl	%edx, 28(%rsp)
	leaq	56(%rsp), %rdx
	movq	%rcx, 8(%rsp)
	leaq	64(%rsp), %rcx
	movss	%xmm0, 24(%rsp)
	movss	%xmm1, 20(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 168(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 80(%rsp)
	movq	%rax, 120(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	28(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	20(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 160(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 72(%rsp)
	movq	%rax, 84(%rsp)
	movl	$1, 92(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	64(%rsp)
	.cfi_def_cfa_offset 200
	leaq	_Z23waveSliceTransmitKernelP6float2PKfjffS0_(%rip), %rdi
	pushq	64(%rsp)
	.cfi_def_cfa_offset 208
	movq	100(%rsp), %rcx
	movl	108(%rsp), %r8d
	movq	88(%rsp), %rsi
	movl	96(%rsp), %edx
	leaq	136(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 200
	popq	%rdx
	.cfi_def_cfa_offset 192
.L2:
	movq	168(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$184, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2077:
	.size	_Z58__device_stub__Z23waveSliceTransmitKernelP6float2PKfjffS0_P6float2PKfjffS0_, .-_Z58__device_stub__Z23waveSliceTransmitKernelP6float2PKfjffS0_P6float2PKfjffS0_
	.globl	_Z23waveSliceTransmitKernelP6float2PKfjffS0_
	.type	_Z23waveSliceTransmitKernelP6float2PKfjffS0_, @function
_Z23waveSliceTransmitKernelP6float2PKfjffS0_:
.LFB2078:
	.cfi_startproc
	endbr64
	jmp	_Z58__device_stub__Z23waveSliceTransmitKernelP6float2PKfjffS0_P6float2PKfjffS0_
	.cfi_endproc
.LFE2078:
	.size	_Z23waveSliceTransmitKernelP6float2PKfjffS0_, .-_Z23waveSliceTransmitKernelP6float2PKfjffS0_
	.globl	_ZN5emsim4cuda17waveSliceTransmitEP6float2PKfiffS2_
	.type	_ZN5emsim4cuda17waveSliceTransmitEP6float2PKfiffS2_, @function
_ZN5emsim4cuda17waveSliceTransmitEP6float2PKfiffS2_:
.LFB2050:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movd	%xmm0, %r15d
	cvtsi2ssl	%edx, %xmm0
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%rcx, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rsi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	$1024, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%edx, %ebx
	subq	$56, %rsp
	.cfi_def_cfa_offset 112
	cmpl	%ebp, %edx
	cmovbe	%edx, %ebp
	movss	%xmm1, 12(%rsp)
	cvtsi2ssl	%ebp, %xmm2
	divss	%xmm2, %xmm0
	call	ceilf@PLT
	movl	$2147483647, %ecx
	movl	%ebp, 36(%rsp)
	movabsq	$4294967297, %rdx
	cvttss2siq	%xmm0, %rax
	movq	%rdx, 28(%rsp)
	movl	32(%rsp), %esi
	movq	%rdx, 40(%rsp)
	movq	36(%rsp), %rdx
	cmpl	%ecx, %eax
	cmova	%ecx, %eax
	movl	44(%rsp), %ecx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	%eax, 24(%rsp)
	movq	24(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	movss	12(%rsp), %xmm1
	testl	%eax, %eax
	jne	.L8
	addq	$56, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 56
	movq	%r14, %rcx
	movd	%r15d, %xmm0
	movl	%ebx, %edx
	movq	%r13, %rsi
	popq	%rbx
	.cfi_def_cfa_offset 48
	movq	%r12, %rdi
	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	_Z58__device_stub__Z23waveSliceTransmitKernelP6float2PKfjffS0_P6float2PKfjffS0_
.L8:
	.cfi_restore_state
	addq	$56, %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
.LFE2050:
	.size	_ZN5emsim4cuda17waveSliceTransmitEP6float2PKfiffS2_, .-_ZN5emsim4cuda17waveSliceTransmitEP6float2PKfiffS2_
	.globl	_Z66__device_stub__Z31waveSpacePropagateFourierKernelP6float2iiffffS0_P6float2iiffffS0_
	.type	_Z66__device_stub__Z31waveSpacePropagateFourierKernelP6float2iiffffS0_P6float2iiffffS0_, @function
_Z66__device_stub__Z31waveSpacePropagateFourierKernelP6float2iiffffS0_P6float2iiffffS0_:
.LFB2079:
	.cfi_startproc
	endbr64
	subq	$200, %rsp
	.cfi_def_cfa_offset 208
	movq	%rdi, 40(%rsp)
	leaq	72(%rsp), %rdi
	movl	%esi, 36(%rsp)
	leaq	84(%rsp), %rsi
	movl	%edx, 32(%rsp)
	leaq	56(%rsp), %rdx
	movq	%rcx, 8(%rsp)
	leaq	64(%rsp), %rcx
	movss	%xmm0, 28(%rsp)
	movss	%xmm1, 24(%rsp)
	movss	%xmm2, 20(%rsp)
	movss	%xmm3, 16(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 184(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 80(%rsp)
	movq	%rax, 120(%rsp)
	leaq	36(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	28(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	20(%rsp), %rax
	movq	%rax, 160(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 168(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 176(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 72(%rsp)
	movq	%rax, 84(%rsp)
	movl	$1, 92(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L11
	pushq	64(%rsp)
	.cfi_def_cfa_offset 216
	leaq	_Z31waveSpacePropagateFourierKernelP6float2iiffffS0_(%rip), %rdi
	pushq	64(%rsp)
	.cfi_def_cfa_offset 224
	movq	100(%rsp), %rcx
	movl	108(%rsp), %r8d
	movq	88(%rsp), %rsi
	movl	96(%rsp), %edx
	leaq	136(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 216
	popq	%rdx
	.cfi_def_cfa_offset 208
.L11:
	movq	184(%rsp), %rax
	subq	%fs:40, %rax
	je	.L13
	call	__stack_chk_fail@PLT
.L13:
	addq	$200, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2079:
	.size	_Z66__device_stub__Z31waveSpacePropagateFourierKernelP6float2iiffffS0_P6float2iiffffS0_, .-_Z66__device_stub__Z31waveSpacePropagateFourierKernelP6float2iiffffS0_P6float2iiffffS0_
	.globl	_Z31waveSpacePropagateFourierKernelP6float2iiffffS0_
	.type	_Z31waveSpacePropagateFourierKernelP6float2iiffffS0_, @function
_Z31waveSpacePropagateFourierKernelP6float2iiffffS0_:
.LFB2080:
	.cfi_startproc
	endbr64
	jmp	_Z66__device_stub__Z31waveSpacePropagateFourierKernelP6float2iiffffS0_P6float2iiffffS0_
	.cfi_endproc
.LFE2080:
	.size	_Z31waveSpacePropagateFourierKernelP6float2iiffffS0_, .-_Z31waveSpacePropagateFourierKernelP6float2iiffffS0_
	.globl	_ZN5emsim4cuda25waveSpacePropagateFourierEP6float2iiffffS2_
	.type	_ZN5emsim4cuda25waveSpacePropagateFourierEP6float2iiffffS2_, @function
_ZN5emsim4cuda25waveSpacePropagateFourierEP6float2iiffffS2_:
.LFB2051:
	.cfi_startproc
	endbr64
	movl	%esi, %eax
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movd	%xmm0, %r15d
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	imull	%edx, %eax
	movl	$1024, %r14d
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movl	%edx, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rcx, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	cvtsi2ssq	%rax, %xmm0
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%esi, %ebx
	subq	$56, %rsp
	.cfi_def_cfa_offset 112
	cmpl	%r14d, %eax
	cmovbe	%eax, %r14d
	movss	%xmm1, 12(%rsp)
	movss	%xmm2, 8(%rsp)
	cvtsi2ssl	%r14d, %xmm4
	movss	%xmm3, 4(%rsp)
	divss	%xmm4, %xmm0
	call	ceilf@PLT
	movl	$2147483647, %ecx
	movl	%r14d, 36(%rsp)
	movabsq	$4294967297, %rdx
	cvttss2siq	%xmm0, %rax
	movq	%rdx, 28(%rsp)
	movl	32(%rsp), %esi
	movq	%rdx, 40(%rsp)
	movq	36(%rsp), %rdx
	cmpl	%ecx, %eax
	cmova	%ecx, %eax
	movl	44(%rsp), %ecx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	%eax, 24(%rsp)
	movq	24(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	movss	4(%rsp), %xmm3
	movss	8(%rsp), %xmm2
	testl	%eax, %eax
	movss	12(%rsp), %xmm1
	jne	.L16
	addq	$56, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 56
	movq	%r12, %rcx
	movd	%r15d, %xmm0
	movl	%r13d, %edx
	movl	%ebx, %esi
	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	_Z66__device_stub__Z31waveSpacePropagateFourierKernelP6float2iiffffS0_P6float2iiffffS0_
.L16:
	.cfi_restore_state
	addq	$56, %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
.LFE2051:
	.size	_ZN5emsim4cuda25waveSpacePropagateFourierEP6float2iiffffS2_, .-_ZN5emsim4cuda25waveSpacePropagateFourierEP6float2iiffffS2_
	.globl	_Z60__device_stub__Z23waveLensPropagateKernelP6float2iiffffffS0_P6float2iiffffffS0_
	.type	_Z60__device_stub__Z23waveLensPropagateKernelP6float2iiffffffS0_P6float2iiffffffS0_, @function
_Z60__device_stub__Z23waveLensPropagateKernelP6float2iiffffffS0_P6float2iiffffffS0_:
.LFB2081:
	.cfi_startproc
	endbr64
	subq	$216, %rsp
	.cfi_def_cfa_offset 224
	movq	%rdi, 40(%rsp)
	leaq	72(%rsp), %rdi
	movl	%esi, 36(%rsp)
	leaq	84(%rsp), %rsi
	movl	%edx, 32(%rsp)
	leaq	56(%rsp), %rdx
	movq	%rcx, (%rsp)
	leaq	64(%rsp), %rcx
	movss	%xmm0, 28(%rsp)
	movss	%xmm1, 24(%rsp)
	movss	%xmm2, 20(%rsp)
	movss	%xmm3, 16(%rsp)
	movss	%xmm4, 12(%rsp)
	movss	%xmm5, 8(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 200(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 80(%rsp)
	movq	%rax, 120(%rsp)
	leaq	36(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	28(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	20(%rsp), %rax
	movq	%rax, 160(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 168(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 176(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 184(%rsp)
	movq	%rsp, %rax
	movq	%rax, 192(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 72(%rsp)
	movq	%rax, 84(%rsp)
	movl	$1, 92(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L21
	pushq	64(%rsp)
	.cfi_def_cfa_offset 232
	leaq	_Z23waveLensPropagateKernelP6float2iiffffffS0_(%rip), %rdi
	pushq	64(%rsp)
	.cfi_def_cfa_offset 240
	movq	100(%rsp), %rcx
	movl	108(%rsp), %r8d
	movq	88(%rsp), %rsi
	movl	96(%rsp), %edx
	leaq	136(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 232
	popq	%rdx
	.cfi_def_cfa_offset 224
.L21:
	movq	200(%rsp), %rax
	subq	%fs:40, %rax
	je	.L23
	call	__stack_chk_fail@PLT
.L23:
	addq	$216, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2081:
	.size	_Z60__device_stub__Z23waveLensPropagateKernelP6float2iiffffffS0_P6float2iiffffffS0_, .-_Z60__device_stub__Z23waveLensPropagateKernelP6float2iiffffffS0_P6float2iiffffffS0_
	.globl	_Z23waveLensPropagateKernelP6float2iiffffffS0_
	.type	_Z23waveLensPropagateKernelP6float2iiffffffS0_, @function
_Z23waveLensPropagateKernelP6float2iiffffffS0_:
.LFB2082:
	.cfi_startproc
	endbr64
	jmp	_Z60__device_stub__Z23waveLensPropagateKernelP6float2iiffffffS0_P6float2iiffffffS0_
	.cfi_endproc
.LFE2082:
	.size	_Z23waveLensPropagateKernelP6float2iiffffffS0_, .-_Z23waveLensPropagateKernelP6float2iiffffffS0_
	.globl	_ZN5emsim4cuda17waveLensPropagateEP6float2iiffffffS2_
	.type	_ZN5emsim4cuda17waveLensPropagateEP6float2iiffffffS2_, @function
_ZN5emsim4cuda17waveLensPropagateEP6float2iiffffffS2_:
.LFB2052:
	.cfi_startproc
	endbr64
	movl	%esi, %eax
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movd	%xmm0, %r15d
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	imull	%edx, %eax
	movl	$1024, %r14d
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rdi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%edx, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	cvtsi2ssq	%rax, %xmm0
	movq	%rcx, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%esi, %ebx
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	cmpl	%r14d, %eax
	cmovbe	%eax, %r14d
	movss	%xmm1, 28(%rsp)
	movss	%xmm2, 24(%rsp)
	cvtsi2ssl	%r14d, %xmm6
	movss	%xmm3, 20(%rsp)
	movss	%xmm4, 16(%rsp)
	movss	%xmm5, 12(%rsp)
	divss	%xmm6, %xmm0
	call	ceilf@PLT
	movl	$2147483647, %ecx
	movl	%r14d, 52(%rsp)
	movabsq	$4294967297, %rdx
	cvttss2siq	%xmm0, %rax
	movq	%rdx, 44(%rsp)
	movl	48(%rsp), %esi
	movq	%rdx, 56(%rsp)
	movq	52(%rsp), %rdx
	cmpl	%ecx, %eax
	cmova	%ecx, %eax
	movl	60(%rsp), %ecx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	%eax, 40(%rsp)
	movq	40(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	movss	12(%rsp), %xmm5
	movss	16(%rsp), %xmm4
	testl	%eax, %eax
	movss	20(%rsp), %xmm3
	movss	24(%rsp), %xmm2
	movss	28(%rsp), %xmm1
	jne	.L26
	addq	$72, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 56
	movq	%rbp, %rcx
	movd	%r15d, %xmm0
	movl	%r12d, %edx
	movl	%ebx, %esi
	movq	%r13, %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	_Z60__device_stub__Z23waveLensPropagateKernelP6float2iiffffffS0_P6float2iiffffffS0_
.L26:
	.cfi_restore_state
	addq	$72, %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
.LFE2052:
	.size	_ZN5emsim4cuda17waveLensPropagateEP6float2iiffffffS2_, .-_ZN5emsim4cuda17waveLensPropagateEP6float2iiffffffS2_
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"_Z23waveLensPropagateKernelP6float2iiffffffS0_"
.LC1:
	.string	"_Z31waveSpacePropagateFourierKernelP6float2iiffffS0_"
.LC2:
	.string	"_Z23waveSliceTransmitKernelP6float2PKfjffS0_"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2084:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC0(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z23waveLensPropagateKernelP6float2iiffffffS0_(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r9d, %r9d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC1(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z31waveSpacePropagateFourierKernelP6float2iiffffS0_(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 40
	pushq	$0
	.cfi_def_cfa_offset 48
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r9d, %r9d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC2(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z23waveSliceTransmitKernelP6float2PKfjffS0_(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 40
	pushq	$0
	.cfi_def_cfa_offset 48
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2084:
	.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:

//
// Created by Chen on 11/8/2020.
//

#ifndef EMSIM_WAVEPROPAGATOR_KERNEL_CUH
#define EMSIM_WAVEPROPAGATOR_KERNEL_CUH

struct float2;
typedef float2 hipfftComplex;
typedef float hipfftReal;

namespace emsim { namespace cuda {

    void waveSliceTransmit(hipfftComplex *wave, hipfftReal const *slice, int nPix,
                           float waveLength, float relativityGamma, hipfftComplex *waveOut);

    void waveSpacePropagateFourier(hipfftComplex *waveFourier,
                            int n1, int n2, float dz, float d1, float d2,
                            float waveLength,  hipfftComplex *waveOut);

    void waveLensPropagate(hipfftComplex *waveFourier, int n1, int n2, float d1, float d2,
                           float waveLength, float cs_mm, float defocus, float aperture, hipfftComplex *waveOut);
} }


#endif //EMSIM_WAVEPROPAGATOR_KERNEL_CUH

	.text
	.file	"WavePropagator_kernel.hip"
	.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
