/*
 * A try at implementing the MED algorithm on a GPU.
 *
 * ===  med-kernel:  ===
 * Essentially an implementation of the MED algorithm from Jurafsky
 * and Martin tailored to a GPU.
 * Each string to be compared is first converted into an int array, 
 * where each element is the index into the vocabulary _dd_ table that
 * was built beforehand.
 * Before the kernel is called, the device is prepared with a very large
 * int array representing all target strings (all strings in the alert_data
 * table) laid out end to end.  A few other small pieces of information that
 * remain constant are also sent:  ntargets and tbi.  On the device is
 * also reserved space for an edits array where each block will store its
 * output at the end of every cycle.
 * Before each kernel invocation, a new orig array _od_ (also an int array
 * representing indices of tokens into the vocabulary table) and an olen int
 * are reserved and CudaMemCopied to the device.  Also, space for each block to 
 * make its distance calculations is reserved.
 * _distance_ is an array with [ntargets * (olen + 1) * 2] elements.  Each half of
 * the array is treated as one column of the J&M distance matrix (see below).
 * Where the original MED algorithm uses a row++ loop inside a column++ loop,
 * this algorithm swaps between 'columns'.  I can do this because I'm only
 * concerned with the edit distance -- not the moves required to change the
 * string.
 * The number of rows each thread handles is the length of the original string,
 * thus each iteration of the inner loop will take the same number of steps for
 * each thread.  The number of columns each thread handles, however, is a
 * function of the length of the target string, which will vary from one thread
 * to the next.  Hence, some threads will make more or fewer outer loop
 * iterations with respect to other threads.
 * Conceptually, think of the relationship between threads and loops as such:
 *
 * |     |     |     |       |  |
 * |     |     |     |       |  |
 * |     |     |     |       |  |
 * |     |     |     |       |  |
 * |     |     |     |       |  |
 * ====  ==    ===== ======= == ====
 * tid=0 tid=1 tid=2 tid=3 ...
 *
 * In fact, the number of rows in _distance_ is olen * ntargets == olen *
 * nblocks.  In this diagram, think of each vertical line as being an original string
 * that will be used by one thread.  Each vertical line is in fact a segment of the
 * array named _distance_.
 * The x axis is truer to reality - this is the targets array of all alert
 * strings laid end-to-end.
 * Each delimited cartesian space is the responsibility of one thread.
 * Each vertical line is an int array representing the original string, so there are the
 * same number of rows across the board.  Since the length of each target
 * string is variable, the space calculated by one thread is not constant.
 * 
 * I've designed the 2-column distance table to be a linear array representing
 * 2 columns.  This is because of the greater simplicity of allocating the necessary
 * memory on the device and of accessing its elements.  To accomodate the new
 * circumstances, I've added two pointers cur_col and last_col which will point to
 * either the head of distance (the distance pointer itself) or to the address halfway
 * into the distance array, depending on which column of the distance table is being
 * filled versus which was just filled.  I have also added macros current and last
 * to return the correct pointer as a function of cindex % 2.
 */

#include <math.h>
#include <cuda.h>
#include <cuda_runtime.h>

// The next two kernel definitions are variations on the same theme.  They accomplish the
// same task -- one with a while loop, the other with a for loop.  Duplication strictly for
// experimentation purposes to test performance.
__global__ void med_kernel_while( int *orig,      // original string; must be freed at kernel termination
                            int olen,      // length of orig
                            int *edits,     // result array where each block stores its calculation
                            int *targets,   // compound array of all other target strings
                            int *distance, // scratch pad for blocks to build MED table; must be
                                            // freed at kernel termination
                            int *tbi,        // (target-begin-index) array of indices where each block
                                            // will find the start of its
                                            // target string
                            int stargets,    // total number of elements in targets array
                            int ntargets
                            ) {
    int tid = threadIdx.x;
    int tlength = tbi[tid+1] - tbi[tid];
    int *target = &targets[tbi[tid]];
    int *dcol1 = &distance[(olen+1)*tid];
    int *dcol2 = &distance[(olen+1)*tid + (olen+1)*ntargets];
    int *current = dcol1;
    int *last = dcol2;
    int row, col, n1, n2, n3, petitmin, grandmin;
    // initialize first column
    row = col = 0;
    current[row] = 0;
    for (row=1; row<olen+1; row++) current[row] = current[row-1] + 1;
    // fill in distance matrix
    while ( col<tlength ) {
        current = (current == dcol1 ? dcol2 : dcol1);
        last    = (last    == dcol1 ? dcol2 : dcol1);
        // bottom row must be initialized piecemeal
        row = 0;
        current[row] = last[row] + 1;
        for (row=1; row<olen+1; row++) {
            // calculate three possible values...
            n1 = last[row] + 1;
            n2 = current[row-1] + 1;
            n3 = (orig[row-1] == target[col] ? last[row-1] : last[row-1] + 2);
            // ...and find minimum
            petitmin = (n1 < n2 ? n1 : n2);
            grandmin = (petitmin < n3 ? petitmin : n3);
            // add row
            current[row] = grandmin;
        }  // end inner loop = build rows of a column
        col++;
    }  // end outer loop = build columns of target

    // wait for all threads to finish and submit MED
    edits[tid] = current[olen];
    __syncthreads();

}  // end med_kernel
 
__global__ void med_kernel_for( int *orig,      // original string; must be freed at kernel termination
                            int olen,      // length of orig
                            int *edits,     // result array where each block stores its calculation
                            int *targets,   // compound array of all other target strings
                            int *distance, // scratch pad for blocks to build MED table; must be
                                            // freed at kernel termination
                            int *tbi,        // (target-begin-index) array of indices where each block
                                            // will find the start of its
                                            // target string
                            int stargets,    // total number of elements in targets array
                            int ntargets
                            ) {
    int tid = threadIdx.x + blockIdx.x * blockDim.x;
    if (! (tid < ntargets)) return;
    int tlength = tbi[tid+1] - tbi[tid];
    int *target = &targets[tbi[tid]];
    int *dcol1 = &distance[(olen+1)*tid];
    int *dcol2 = &distance[(olen+1)*tid + (olen+1)*ntargets];
    int *current = dcol1;
    int *last = dcol2;
    int row, col, n1, n2, n3, petitmin, grandmin;
    // initialize first column
    row = col = 0;
    current[row] = 0;
    for (row=1; row<olen+1; row++) current[row] = current[row-1] + 1;
    // fill in distance matrix
    for (col=0; col<tlength; col++) {
        current = (current == dcol1 ? dcol2 : dcol1);
        last    = (last    == dcol1 ? dcol2 : dcol1);
        // bottom row must be initialized piecemeal
        row = 0;
        current[row] = last[row] + 1;
        for (row=1; row<olen+1; row++) {
            // calculate three possible values...
            n1 = last[row] + 1;
            n2 = current[row-1] + 1;
            n3 = (orig[row-1] == target[col] ? last[row-1] : last[row-1] + 2);
            // ...and find minimum
            petitmin = (n1 < n2 ? n1 : n2);
            grandmin = (petitmin < n3 ? petitmin : n3);
            // add row
            current[row] = grandmin;
        }  // end inner loop = build rows of a column
    }  // end outer loop = build columns of target

    // wait for all threads to finish and submit MED
    __syncthreads();
    edits[tid] = current[olen];
    //edits[tid] = grandmin;

}  // end med_kernel
 
extern "C"
void call_med_kernel(
        int *orig,      // original string; must be freed at kernel termination
        int olen,      // length of orig
        int *edits,     // result array where each block stores its calculation
        int *targets,   // compound array of all other target strings
        int *distance, // scratch pad for blocks to build MED table; must be
        int *tbi,
        int starg,
        int ntarg
        ) {
    cudaDeviceProp prop;
    cudaGetDeviceProperties(&prop, 0);
    int maxthr, blocks;
    maxthr = prop.maxThreadsPerBlock;
    blocks = ntarg / maxthr + 1;

    med_kernel_for<<<blocks, maxthr>>>( orig, olen, edits, targets, distance, tbi, starg, ntarg );
}


extern "C"
void call_cudaMalloc(int **d_ptr, int bytes) {
    cudaMalloc( (void**) d_ptr, bytes );
}

extern "C"
void call_cudaMemcpy(int *to_ptr, int *from_ptr, int bytes, int flag){
    cudaMemcpy(to_ptr, from_ptr, bytes, (flag ? cudaMemcpyDeviceToHost : cudaMemcpyHostToDevice));
}

extern "C"
void call_cudaFree(int *dev_ptr) {
    cudaFree(dev_ptr);
}

	.file	"tmpxft_003266db_00000000-6_cuda_f.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2014:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2014:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	call_cudaMalloc
	.type	call_cudaMalloc, @function
call_cudaMalloc:
.LFB2009:
	.cfi_startproc
	endbr64
	movslq	%esi, %rsi
	jmp	cudaMalloc@PLT
	.cfi_endproc
.LFE2009:
	.size	call_cudaMalloc, .-call_cudaMalloc
	.globl	call_cudaMemcpy
	.type	call_cudaMemcpy, @function
call_cudaMemcpy:
.LFB2010:
	.cfi_startproc
	endbr64
	cmpl	$1, %ecx
	movslq	%edx, %rdx
	sbbl	%ecx, %ecx
	addl	$2, %ecx
	jmp	cudaMemcpy@PLT
	.cfi_endproc
.LFE2010:
	.size	call_cudaMemcpy, .-call_cudaMemcpy
	.globl	call_cudaFree
	.type	call_cudaFree, @function
call_cudaFree:
.LFB2011:
	.cfi_startproc
	endbr64
	jmp	cudaFree@PLT
	.cfi_endproc
.LFE2011:
	.size	call_cudaFree, .-call_cudaFree
	.globl	_Z47__device_stub__Z16med_kernel_whilePiiS_S_S_S_iiPiiS_S_S_S_ii
	.type	_Z47__device_stub__Z16med_kernel_whilePiiS_S_S_S_iiPiiS_S_S_S_ii, @function
_Z47__device_stub__Z16med_kernel_whilePiiS_S_S_S_iiPiiS_S_S_S_ii:
.LFB2036:
	.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
	movq	%rdx, 24(%rsp)
	leaq	56(%rsp), %rdx
	movq	%rcx, 16(%rsp)
	leaq	64(%rsp), %rcx
	movq	%r8, 8(%rsp)
	movq	%r9, (%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	24(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 152(%rsp)
	movq	%rsp, %rax
	movq	%rax, 160(%rsp)
	leaq	208(%rsp), %rax
	movq	%rax, 168(%rsp)
	leaq	216(%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	.L7
	pushq	64(%rsp)
	.cfi_def_cfa_offset 216
	leaq	_Z16med_kernel_whilePiiS_S_S_S_ii(%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
.L7:
	movq	184(%rsp), %rax
	subq	%fs:40, %rax
	je	.L9
	call	__stack_chk_fail@PLT
.L9:
	addq	$200, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2036:
	.size	_Z47__device_stub__Z16med_kernel_whilePiiS_S_S_S_iiPiiS_S_S_S_ii, .-_Z47__device_stub__Z16med_kernel_whilePiiS_S_S_S_iiPiiS_S_S_S_ii
	.globl	_Z16med_kernel_whilePiiS_S_S_S_ii
	.type	_Z16med_kernel_whilePiiS_S_S_S_ii, @function
_Z16med_kernel_whilePiiS_S_S_S_ii:
.LFB2037:
	.cfi_startproc
	endbr64
	jmp	_Z47__device_stub__Z16med_kernel_whilePiiS_S_S_S_iiPiiS_S_S_S_ii
	.cfi_endproc
.LFE2037:
	.size	_Z16med_kernel_whilePiiS_S_S_S_ii, .-_Z16med_kernel_whilePiiS_S_S_S_ii
	.globl	_Z45__device_stub__Z14med_kernel_forPiiS_S_S_S_iiPiiS_S_S_S_ii
	.type	_Z45__device_stub__Z14med_kernel_forPiiS_S_S_S_iiPiiS_S_S_S_ii, @function
_Z45__device_stub__Z14med_kernel_forPiiS_S_S_S_iiPiiS_S_S_S_ii:
.LFB2038:
	.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
	movq	%rdx, 24(%rsp)
	leaq	56(%rsp), %rdx
	movq	%rcx, 16(%rsp)
	leaq	64(%rsp), %rcx
	movq	%r8, 8(%rsp)
	movq	%r9, (%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	24(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 152(%rsp)
	movq	%rsp, %rax
	movq	%rax, 160(%rsp)
	leaq	208(%rsp), %rax
	movq	%rax, 168(%rsp)
	leaq	216(%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	.L13
	pushq	64(%rsp)
	.cfi_def_cfa_offset 216
	leaq	_Z14med_kernel_forPiiS_S_S_S_ii(%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
.L13:
	movq	184(%rsp), %rax
	subq	%fs:40, %rax
	je	.L15
	call	__stack_chk_fail@PLT
.L15:
	addq	$200, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2038:
	.size	_Z45__device_stub__Z14med_kernel_forPiiS_S_S_S_iiPiiS_S_S_S_ii, .-_Z45__device_stub__Z14med_kernel_forPiiS_S_S_S_iiPiiS_S_S_S_ii
	.globl	_Z14med_kernel_forPiiS_S_S_S_ii
	.type	_Z14med_kernel_forPiiS_S_S_S_ii, @function
_Z14med_kernel_forPiiS_S_S_S_ii:
.LFB2039:
	.cfi_startproc
	endbr64
	jmp	_Z45__device_stub__Z14med_kernel_forPiiS_S_S_S_iiPiiS_S_S_S_ii
	.cfi_endproc
.LFE2039:
	.size	_Z14med_kernel_forPiiS_S_S_S_ii, .-_Z14med_kernel_forPiiS_S_S_S_ii
	.globl	call_med_kernel
	.type	call_med_kernel, @function
call_med_kernel:
.LFB2008:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%r9, %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%r8, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rcx, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rdx, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%esi, %ebp
	xorl	%esi, %esi
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rdi, %rbx
	subq	$1080, %rsp
	.cfi_def_cfa_offset 1136
	movq	%fs:40, %rax
	movq	%rax, 1064(%rsp)
	xorl	%eax, %eax
	leaq	32(%rsp), %rdi
	call	cudaGetDeviceProperties_v2@PLT
	movl	1144(%rsp), %eax
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	352(%rsp), %ecx
	movabsq	$4294967297, %rsi
	cltd
	movq	%rsi, 24(%rsp)
	idivl	%ecx
	movl	%ecx, 20(%rsp)
	movl	28(%rsp), %ecx
	movq	%rsi, 12(%rsp)
	movq	20(%rsp), %rdx
	movl	16(%rsp), %esi
	incl	%eax
	movl	%eax, 8(%rsp)
	movq	8(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L18
	movl	1144(%rsp), %eax
	movq	%r12, %rdx
	movq	%r15, %r9
	movq	%r14, %r8
	movq	%r13, %rcx
	movl	%ebp, %esi
	movq	%rbx, %rdi
	pushq	%rax
	.cfi_def_cfa_offset 1144
	movl	1144(%rsp), %eax
	pushq	%rax
	.cfi_def_cfa_offset 1152
	call	_Z45__device_stub__Z14med_kernel_forPiiS_S_S_S_iiPiiS_S_S_S_ii
	popq	%rax
	.cfi_def_cfa_offset 1144
	popq	%rdx
	.cfi_def_cfa_offset 1136
.L18:
	movq	1064(%rsp), %rax
	subq	%fs:40, %rax
	je	.L20
	call	__stack_chk_fail@PLT
.L20:
	addq	$1080, %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
.LFE2008:
	.size	call_med_kernel, .-call_med_kernel
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"_Z14med_kernel_forPiiS_S_S_S_ii"
.LC1:
	.string	"_Z16med_kernel_whilePiiS_S_S_S_ii"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2041:
	.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	_Z14med_kernel_forPiiS_S_S_S_ii(%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	_Z16med_kernel_whilePiiS_S_S_S_ii(%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
.LFE2041:
	.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:

/*
 * A try at implementing the MED algorithm on a GPU.
 *
 * ===  med-kernel:  ===
 * Essentially an implementation of the MED algorithm from Jurafsky
 * and Martin tailored to a GPU.
 * Each string to be compared is first converted into an int array, 
 * where each element is the index into the vocabulary _dd_ table that
 * was built beforehand.
 * Before the kernel is called, the device is prepared with a very large
 * int array representing all target strings (all strings in the alert_data
 * table) laid out end to end.  A few other small pieces of information that
 * remain constant are also sent:  ntargets and tbi.  On the device is
 * also reserved space for an edits array where each block will store its
 * output at the end of every cycle.
 * Before each kernel invocation, a new orig array _od_ (also an int array
 * representing indices of tokens into the vocabulary table) and an olen int
 * are reserved and CudaMemCopied to the device.  Also, space for each block to 
 * make its distance calculations is reserved.
 * _distance_ is an array with [ntargets * (olen + 1) * 2] elements.  Each half of
 * the array is treated as one column of the J&M distance matrix (see below).
 * Where the original MED algorithm uses a row++ loop inside a column++ loop,
 * this algorithm swaps between 'columns'.  I can do this because I'm only
 * concerned with the edit distance -- not the moves required to change the
 * string.
 * The number of rows each thread handles is the length of the original string,
 * thus each iteration of the inner loop will take the same number of steps for
 * each thread.  The number of columns each thread handles, however, is a
 * function of the length of the target string, which will vary from one thread
 * to the next.  Hence, some threads will make more or fewer outer loop
 * iterations with respect to other threads.
 * Conceptually, think of the relationship between threads and loops as such:
 *
 * |     |     |     |       |  |
 * |     |     |     |       |  |
 * |     |     |     |       |  |
 * |     |     |     |       |  |
 * |     |     |     |       |  |
 * ====  ==    ===== ======= == ====
 * tid=0 tid=1 tid=2 tid=3 ...
 *
 * In fact, the number of rows in _distance_ is olen * ntargets == olen *
 * nblocks.  In this diagram, think of each vertical line as being an original string
 * that will be used by one thread.  Each vertical line is in fact a segment of the
 * array named _distance_.
 * The x axis is truer to reality - this is the targets array of all alert
 * strings laid end-to-end.
 * Each delimited cartesian space is the responsibility of one thread.
 * Each vertical line is an int array representing the original string, so there are the
 * same number of rows across the board.  Since the length of each target
 * string is variable, the space calculated by one thread is not constant.
 * 
 * I've designed the 2-column distance table to be a linear array representing
 * 2 columns.  This is because of the greater simplicity of allocating the necessary
 * memory on the device and of accessing its elements.  To accomodate the new
 * circumstances, I've added two pointers cur_col and last_col which will point to
 * either the head of distance (the distance pointer itself) or to the address halfway
 * into the distance array, depending on which column of the distance table is being
 * filled versus which was just filled.  I have also added macros current and last
 * to return the correct pointer as a function of cindex % 2.
 */

#include <math.h>
#include <hip/hip_runtime.h>


// The next two kernel definitions are variations on the same theme.  They accomplish the
// same task -- one with a while loop, the other with a for loop.  Duplication strictly for
// experimentation purposes to test performance.
__global__ void med_kernel_while( int *orig,      // original string; must be freed at kernel termination
                            int olen,      // length of orig
                            int *edits,     // result array where each block stores its calculation
                            int *targets,   // compound array of all other target strings
                            int *distance, // scratch pad for blocks to build MED table; must be
                                            // freed at kernel termination
                            int *tbi,        // (target-begin-index) array of indices where each block
                                            // will find the start of its
                                            // target string
                            int stargets,    // total number of elements in targets array
                            int ntargets
                            ) {
    int tid = threadIdx.x;
    int tlength = tbi[tid+1] - tbi[tid];
    int *target = &targets[tbi[tid]];
    int *dcol1 = &distance[(olen+1)*tid];
    int *dcol2 = &distance[(olen+1)*tid + (olen+1)*ntargets];
    int *current = dcol1;
    int *last = dcol2;
    int row, col, n1, n2, n3, petitmin, grandmin;
    // initialize first column
    row = col = 0;
    current[row] = 0;
    for (row=1; row<olen+1; row++) current[row] = current[row-1] + 1;
    // fill in distance matrix
    while ( col<tlength ) {
        current = (current == dcol1 ? dcol2 : dcol1);
        last    = (last    == dcol1 ? dcol2 : dcol1);
        // bottom row must be initialized piecemeal
        row = 0;
        current[row] = last[row] + 1;
        for (row=1; row<olen+1; row++) {
            // calculate three possible values...
            n1 = last[row] + 1;
            n2 = current[row-1] + 1;
            n3 = (orig[row-1] == target[col] ? last[row-1] : last[row-1] + 2);
            // ...and find minimum
            petitmin = (n1 < n2 ? n1 : n2);
            grandmin = (petitmin < n3 ? petitmin : n3);
            // add row
            current[row] = grandmin;
        }  // end inner loop = build rows of a column
        col++;
    }  // end outer loop = build columns of target

    // wait for all threads to finish and submit MED
    edits[tid] = current[olen];
    __syncthreads();

}  // end med_kernel
 
__global__ void med_kernel_for( int *orig,      // original string; must be freed at kernel termination
                            int olen,      // length of orig
                            int *edits,     // result array where each block stores its calculation
                            int *targets,   // compound array of all other target strings
                            int *distance, // scratch pad for blocks to build MED table; must be
                                            // freed at kernel termination
                            int *tbi,        // (target-begin-index) array of indices where each block
                                            // will find the start of its
                                            // target string
                            int stargets,    // total number of elements in targets array
                            int ntargets
                            ) {
    int tid = threadIdx.x + blockIdx.x * blockDim.x;
    if (! (tid < ntargets)) return;
    int tlength = tbi[tid+1] - tbi[tid];
    int *target = &targets[tbi[tid]];
    int *dcol1 = &distance[(olen+1)*tid];
    int *dcol2 = &distance[(olen+1)*tid + (olen+1)*ntargets];
    int *current = dcol1;
    int *last = dcol2;
    int row, col, n1, n2, n3, petitmin, grandmin;
    // initialize first column
    row = col = 0;
    current[row] = 0;
    for (row=1; row<olen+1; row++) current[row] = current[row-1] + 1;
    // fill in distance matrix
    for (col=0; col<tlength; col++) {
        current = (current == dcol1 ? dcol2 : dcol1);
        last    = (last    == dcol1 ? dcol2 : dcol1);
        // bottom row must be initialized piecemeal
        row = 0;
        current[row] = last[row] + 1;
        for (row=1; row<olen+1; row++) {
            // calculate three possible values...
            n1 = last[row] + 1;
            n2 = current[row-1] + 1;
            n3 = (orig[row-1] == target[col] ? last[row-1] : last[row-1] + 2);
            // ...and find minimum
            petitmin = (n1 < n2 ? n1 : n2);
            grandmin = (petitmin < n3 ? petitmin : n3);
            // add row
            current[row] = grandmin;
        }  // end inner loop = build rows of a column
    }  // end outer loop = build columns of target

    // wait for all threads to finish and submit MED
    __syncthreads();
    edits[tid] = current[olen];
    //edits[tid] = grandmin;

}  // end med_kernel
 
extern "C"
void call_med_kernel(
        int *orig,      // original string; must be freed at kernel termination
        int olen,      // length of orig
        int *edits,     // result array where each block stores its calculation
        int *targets,   // compound array of all other target strings
        int *distance, // scratch pad for blocks to build MED table; must be
        int *tbi,
        int starg,
        int ntarg
        ) {
    hipDeviceProp_t prop;
    hipGetDeviceProperties(&prop, 0);
    int maxthr, blocks;
    maxthr = prop.maxThreadsPerBlock;
    blocks = ntarg / maxthr + 1;

    med_kernel_for<<<blocks, maxthr>>>( orig, olen, edits, targets, distance, tbi, starg, ntarg );
}


extern "C"
void call_cudaMalloc(int **d_ptr, int bytes) {
    hipMalloc( (void**) d_ptr, bytes );
}

extern "C"
void call_cudaMemcpy(int *to_ptr, int *from_ptr, int bytes, int flag){
    hipMemcpy(to_ptr, from_ptr, bytes, (flag ? hipMemcpyDeviceToHost : hipMemcpyHostToDevice));
}

extern "C"
void call_cudaFree(int *dev_ptr) {
    hipFree(dev_ptr);
}

	.text
	.file	"cuda_f.hip"
	.globl	_Z31__device_stub__med_kernel_whilePiiS_S_S_S_ii # -- Begin function _Z31__device_stub__med_kernel_whilePiiS_S_S_S_ii
	.type	_Z31__device_stub__med_kernel_whilePiiS_S_S_S_ii,@function
_Z31__device_stub__med_kernel_whilePiiS_S_S_S_ii: # @_Z31__device_stub__med_kernel_whilePiiS_S_S_S_ii
	.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	$160, %rsp
	.cfi_def_cfa_offset 208
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	56(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	4(%rsp), %rdi
	movl	%esi, (%rdi)
	leaq	48(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	40(%rsp), %rdx
	movq	%rcx, (%rdx)
	leaq	32(%rsp), %rcx
	movq	%r8, (%rcx)
	leaq	24(%rsp), %r8
	movq	%r9, (%r8)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	movq	%r8, 40(%rbx)
	leaq	208(%rsp), %rax
	movq	%rax, 48(%rbx)
	leaq	216(%rsp), %rax
	movq	%rax, 56(%rbx)
	leaq	80(%rsp), %r14
	leaq	64(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z16med_kernel_whilePiiS_S_S_S_ii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$176, %rsp
	.cfi_adjust_cfa_offset -176
	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	_Z31__device_stub__med_kernel_whilePiiS_S_S_S_ii, .Lfunc_end0-_Z31__device_stub__med_kernel_whilePiiS_S_S_S_ii
	.cfi_endproc
                                        # -- End function
	.globl	_Z29__device_stub__med_kernel_forPiiS_S_S_S_ii # -- Begin function _Z29__device_stub__med_kernel_forPiiS_S_S_S_ii
	.type	_Z29__device_stub__med_kernel_forPiiS_S_S_S_ii,@function
_Z29__device_stub__med_kernel_forPiiS_S_S_S_ii: # @_Z29__device_stub__med_kernel_forPiiS_S_S_S_ii
	.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	$160, %rsp
	.cfi_def_cfa_offset 208
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	56(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	4(%rsp), %rdi
	movl	%esi, (%rdi)
	leaq	48(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	40(%rsp), %rdx
	movq	%rcx, (%rdx)
	leaq	32(%rsp), %rcx
	movq	%r8, (%rcx)
	leaq	24(%rsp), %r8
	movq	%r9, (%r8)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	movq	%r8, 40(%rbx)
	leaq	208(%rsp), %rax
	movq	%rax, 48(%rbx)
	leaq	216(%rsp), %rax
	movq	%rax, 56(%rbx)
	leaq	80(%rsp), %r14
	leaq	64(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z14med_kernel_forPiiS_S_S_S_ii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$176, %rsp
	.cfi_adjust_cfa_offset -176
	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_end1:
	.size	_Z29__device_stub__med_kernel_forPiiS_S_S_S_ii, .Lfunc_end1-_Z29__device_stub__med_kernel_forPiiS_S_S_S_ii
	.cfi_endproc
                                        # -- End function
	.globl	call_med_kernel                 # -- Begin function call_med_kernel
	.type	call_med_kernel,@function
call_med_kernel:                        # @call_med_kernel
	.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	$1496, %rsp                     # imm = 0x5D8
	.cfi_def_cfa_offset 1552
	.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	%r9, 16(%rsp)                   # 8-byte Spill
	movq	%r8, 8(%rsp)                    # 8-byte Spill
	movq	%rcx, %r15
	movq	%rdx, %r13
	movl	%esi, %ebp
	movq	%rdi, %r12
	movl	1560(%rsp), %r14d
	leaq	24(%rsp), %rbx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	hipGetDevicePropertiesR0600
	movl	320(%rbx), %ecx
	movl	%r14d, %eax
	cltd
	idivl	%ecx
                                        # kill: def $eax killed $eax def $rax
	leal	1(%rax), %edi
	btsq	$32, %rdi
	btsq	$32, %rcx
	movl	$1, %esi
	movq	%rcx, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_2
# %bb.1:
	movl	1552(%rsp), %eax
	movq	%r12, %rdi
	movl	%ebp, %esi
	movq	%r13, %rdx
	movq	%r15, %rcx
	movq	8(%rsp), %r8                    # 8-byte Reload
	movq	16(%rsp), %r9                   # 8-byte Reload
	pushq	%r14
	.cfi_adjust_cfa_offset 8
	pushq	%rax
	.cfi_adjust_cfa_offset 8
	callq	_Z29__device_stub__med_kernel_forPiiS_S_S_S_ii
	addq	$16, %rsp
	.cfi_adjust_cfa_offset -16
.LBB2_2:
	addq	$1496, %rsp                     # imm = 0x5D8
	.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_end2:
	.size	call_med_kernel, .Lfunc_end2-call_med_kernel
	.cfi_endproc
                                        # -- End function
	.globl	call_cudaMalloc                 # -- Begin function call_cudaMalloc
	.type	call_cudaMalloc,@function
call_cudaMalloc:                        # @call_cudaMalloc
	.cfi_startproc
# %bb.0:
	movslq	%esi, %rsi
	jmp	hipMalloc                       # TAILCALL
.Lfunc_end3:
	.size	call_cudaMalloc, .Lfunc_end3-call_cudaMalloc
	.cfi_endproc
                                        # -- End function
	.globl	call_cudaMemcpy                 # -- Begin function call_cudaMemcpy
	.type	call_cudaMemcpy,@function
call_cudaMemcpy:                        # @call_cudaMemcpy
	.cfi_startproc
# %bb.0:
	movslq	%edx, %rdx
	cmpl	$1, %ecx
	movl	$2, %ecx
	sbbl	$0, %ecx
	jmp	hipMemcpy                       # TAILCALL
.Lfunc_end4:
	.size	call_cudaMemcpy, .Lfunc_end4-call_cudaMemcpy
	.cfi_endproc
                                        # -- End function
	.globl	call_cudaFree                   # -- Begin function call_cudaFree
	.type	call_cudaFree,@function
call_cudaFree:                          # @call_cudaFree
	.cfi_startproc
# %bb.0:
	jmp	hipFree                         # TAILCALL
.Lfunc_end5:
	.size	call_cudaFree, .Lfunc_end5-call_cudaFree
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$32, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -16
	movq	__hip_gpubin_handle(%rip), %rbx
	testq	%rbx, %rbx
	jne	.LBB6_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rbx
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB6_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z16med_kernel_whilePiiS_S_S_S_ii, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z14med_kernel_forPiiS_S_S_S_ii, %esi
	movl	$.L__unnamed_2, %edx
	movl	$.L__unnamed_2, %ecx
	movq	%rbx, %rdi
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end6:
	.size	__hip_module_ctor, .Lfunc_end6-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB7_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB7_2:
	retq
.Lfunc_end7:
	.size	__hip_module_dtor, .Lfunc_end7-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z16med_kernel_whilePiiS_S_S_S_ii,@object # @_Z16med_kernel_whilePiiS_S_S_S_ii
	.section	.rodata,"a",@progbits
	.globl	_Z16med_kernel_whilePiiS_S_S_S_ii
	.p2align	3, 0x0
_Z16med_kernel_whilePiiS_S_S_S_ii:
	.quad	_Z31__device_stub__med_kernel_whilePiiS_S_S_S_ii
	.size	_Z16med_kernel_whilePiiS_S_S_S_ii, 8

	.type	_Z14med_kernel_forPiiS_S_S_S_ii,@object # @_Z14med_kernel_forPiiS_S_S_S_ii
	.globl	_Z14med_kernel_forPiiS_S_S_S_ii
	.p2align	3, 0x0
_Z14med_kernel_forPiiS_S_S_S_ii:
	.quad	_Z29__device_stub__med_kernel_forPiiS_S_S_S_ii
	.size	_Z14med_kernel_forPiiS_S_S_S_ii, 8

	.type	.L__unnamed_1,@object           # @0
	.section	.rodata.str1.1,"aMS",@progbits,1
.L__unnamed_1:
	.asciz	"_Z16med_kernel_whilePiiS_S_S_S_ii"
	.size	.L__unnamed_1, 34

	.type	.L__unnamed_2,@object           # @1
.L__unnamed_2:
	.asciz	"_Z14med_kernel_forPiiS_S_S_S_ii"
	.size	.L__unnamed_2, 32

	.type	__hip_fatbin_wrapper,@object    # @__hip_fatbin_wrapper
	.section	.hipFatBinSegment,"a",@progbits
	.p2align	3, 0x0
__hip_fatbin_wrapper:
	.long	1212764230                      # 0x48495046
	.long	1                               # 0x1
	.quad	__hip_fatbin
	.quad	0
	.size	__hip_fatbin_wrapper, 24

	.type	__hip_gpubin_handle,@object     # @__hip_gpubin_handle
	.local	__hip_gpubin_handle
	.comm	__hip_gpubin_handle,8,8
	.section	.init_array,"aw",@init_array
	.p2align	3, 0x0
	.quad	__hip_module_ctor
	.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 _Z31__device_stub__med_kernel_whilePiiS_S_S_S_ii
	.addrsig_sym _Z29__device_stub__med_kernel_forPiiS_S_S_S_ii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z16med_kernel_whilePiiS_S_S_S_ii
	.addrsig_sym _Z14med_kernel_forPiiS_S_S_S_ii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z16med_kernel_whilePiiS_S_S_S_ii ; -- Begin function _Z16med_kernel_whilePiiS_S_S_S_ii
	.globl	_Z16med_kernel_whilePiiS_S_S_S_ii
	.p2align	8
	.type	_Z16med_kernel_whilePiiS_S_S_S_ii,@function
_Z16med_kernel_whilePiiS_S_S_S_ii:      ; @_Z16med_kernel_whilePiiS_S_S_S_ii
; %bb.0:
	s_load_b256 s[4:11], s[0:1], 0x10
	v_lshlrev_b32_e32 v15, 2, v0
	s_load_b32 s2, s[0:1], 0x8
	v_mov_b32_e32 v7, 0
	s_waitcnt lgkmcnt(0)
	global_load_b64 v[3:4], v15, s[10:11]
	s_mov_b32 s10, 0
	s_add_i32 s11, s2, 1
	s_cmp_gt_i32 s2, 0
	v_mul_lo_u32 v1, s11, v0
	s_cselect_b32 s3, -1, 0
	s_cmp_lt_i32 s2, 1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v2, 31, v1
	v_lshlrev_b64 v[5:6], 2, v[1:2]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v1, vcc_lo, s8, v5
	v_add_co_ci_u32_e32 v2, vcc_lo, s9, v6, vcc_lo
	global_store_b32 v[1:2], v7, off
	s_cbranch_scc1 .LBB0_3
; %bb.1:                                ; %.lr.ph.preheader
	v_add_co_u32 v5, vcc_lo, v5, s8
	v_add_co_ci_u32_e32 v6, vcc_lo, s9, v6, vcc_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v5, vcc_lo, v5, 4
	v_add_co_ci_u32_e32 v6, vcc_lo, 0, v6, vcc_lo
.LBB0_2:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	s_add_i32 s10, s10, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	v_mov_b32_e32 v7, s10
	s_cmp_eq_u32 s2, s10
	global_store_b32 v[5:6], v7, off
	v_add_co_u32 v5, vcc_lo, v5, 4
	v_add_co_ci_u32_e32 v6, vcc_lo, 0, v6, vcc_lo
	s_cbranch_scc0 .LBB0_2
.LBB0_3:                                ; %.preheader
	s_waitcnt vmcnt(0)
	v_sub_nc_u32_e32 v16, v4, v3
	s_mov_b32 s10, exec_lo
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_lt_i32_e32 0, v16
	s_cbranch_execz .LBB0_10
; %bb.4:                                ; %.lr.ph87.preheader
	s_clause 0x1
	s_load_b32 s12, s[0:1], 0x34
	s_load_b64 s[0:1], s[0:1], 0x0
	v_ashrrev_i32_e32 v4, 31, v3
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[7:8], 2, v[3:4]
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v0, s12, v0
	v_mul_lo_u32 v5, v0, s11
	s_mov_b32 s11, 0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v6, 31, v5
	v_lshlrev_b64 v[5:6], 2, v[5:6]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v3, vcc_lo, s8, v5
	v_add_co_ci_u32_e32 v4, vcc_lo, s9, v6, vcc_lo
	v_add_co_u32 v0, vcc_lo, s6, v7
	v_add_co_ci_u32_e32 v17, vcc_lo, s7, v8, vcc_lo
	v_dual_mov_b32 v6, v2 :: v_dual_mov_b32 v5, v1
	s_delay_alu instid0(VALU_DEP_4) | instskip(SKIP_2) | instid1(SALU_CYCLE_1)
	v_mov_b32_e32 v8, v4
	v_dual_mov_b32 v18, 0 :: v_dual_mov_b32 v7, v3
	s_mov_b32 s7, 0
	s_mov_b32 s6, s7
.LBB0_5:                                ; %.lr.ph87
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_7 Depth 2
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_eq_u64_e32 vcc_lo, v[7:8], v[1:2]
	v_dual_cndmask_b32 v8, v2, v4 :: v_dual_cndmask_b32 v7, v1, v3
	v_cmp_eq_u64_e32 vcc_lo, v[5:6], v[1:2]
	global_load_b32 v9, v[7:8], off
	v_dual_cndmask_b32 v6, v2, v4 :: v_dual_cndmask_b32 v5, v1, v3
	s_and_not1_b32 vcc_lo, exec_lo, s3
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v19, 1, v9
	global_store_b32 v[5:6], v19, off
	s_cbranch_vccnz .LBB0_8
; %bb.6:                                ; %.lr.ph83
                                        ;   in Loop: Header=BB0_5 Depth=1
	s_lshl_b64 s[8:9], s[6:7], 2
	s_mov_b32 s12, s2
	v_add_co_u32 v9, vcc_lo, v0, s8
	v_add_co_ci_u32_e32 v10, vcc_lo, s9, v17, vcc_lo
	v_add_co_u32 v11, vcc_lo, v7, 4
	v_add_co_ci_u32_e32 v12, vcc_lo, 0, v8, vcc_lo
	v_add_co_u32 v13, vcc_lo, v5, 4
	v_add_co_ci_u32_e32 v14, vcc_lo, 0, v6, vcc_lo
	s_mov_b64 s[8:9], s[0:1]
.LBB0_7:                                ;   Parent Loop BB0_5 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	global_load_b64 v[20:21], v[11:12], off offset:-4
	global_load_b32 v22, v18, s[8:9]
	global_load_b32 v23, v[9:10], off
	v_add_co_u32 v11, vcc_lo, v11, 4
	v_add_co_ci_u32_e32 v12, vcc_lo, 0, v12, vcc_lo
	s_add_u32 s8, s8, 4
	s_addc_u32 s9, s9, 0
	s_add_i32 s12, s12, -1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_lg_u32 s12, 0
	s_waitcnt vmcnt(2)
	v_add_nc_u32_e32 v24, 2, v20
	v_add_nc_u32_e32 v21, 1, v21
	s_waitcnt vmcnt(0)
	v_cmp_eq_u32_e32 vcc_lo, v22, v23
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_dual_cndmask_b32 v20, v24, v20 :: v_dual_add_nc_u32 v19, 1, v19
	v_min3_i32 v19, v21, v19, v20
	global_store_b32 v[13:14], v19, off
	v_add_co_u32 v13, vcc_lo, v13, 4
	v_add_co_ci_u32_e32 v14, vcc_lo, 0, v14, vcc_lo
	s_cbranch_scc1 .LBB0_7
.LBB0_8:                                ; %._crit_edge
                                        ;   in Loop: Header=BB0_5 Depth=1
	s_add_i32 s6, s6, 1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_cmp_ge_i32_e32 vcc_lo, s6, v16
	s_or_b32 s11, vcc_lo, s11
	s_and_not1_b32 exec_lo, exec_lo, s11
	s_cbranch_execnz .LBB0_5
; %bb.9:                                ; %Flow123
	s_or_b32 exec_lo, exec_lo, s11
	v_dual_mov_b32 v1, v5 :: v_dual_mov_b32 v2, v6
.LBB0_10:                               ; %Flow124
	s_or_b32 exec_lo, exec_lo, s10
	s_ashr_i32 s3, s2, 31
	s_delay_alu instid0(SALU_CYCLE_1)
	s_lshl_b64 s[0:1], s[2:3], 2
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_add_co_u32 v0, vcc_lo, v1, s0
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v2, vcc_lo
	global_load_b32 v0, v[0:1], off
	s_waitcnt vmcnt(0)
	global_store_b32 v15, v0, s[4:5]
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z16med_kernel_whilePiiS_S_S_S_ii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 56
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 25
		.amdhsa_next_free_sgpr 13
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z16med_kernel_whilePiiS_S_S_S_ii, .Lfunc_end0-_Z16med_kernel_whilePiiS_S_S_S_ii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 668
; NumSgprs: 15
; NumVgprs: 25
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 1
; VGPRBlocks: 3
; NumSGPRsForWavesPerEU: 15
; NumVGPRsForWavesPerEU: 25
; Occupancy: 16
; WaveLimiterHint : 1
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.protected	_Z14med_kernel_forPiiS_S_S_S_ii ; -- Begin function _Z14med_kernel_forPiiS_S_S_S_ii
	.globl	_Z14med_kernel_forPiiS_S_S_S_ii
	.p2align	8
	.type	_Z14med_kernel_forPiiS_S_S_S_ii,@function
_Z14med_kernel_forPiiS_S_S_S_ii:        ; @_Z14med_kernel_forPiiS_S_S_S_ii
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x44
	s_load_b32 s12, s[0:1], 0x34
	s_waitcnt lgkmcnt(0)
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[4:5], null, s15, s2, v[0:1]
	s_mov_b32 s2, exec_lo
	v_cmpx_gt_i32_e64 s12, v4
	s_cbranch_execz .LBB1_12
; %bb.1:
	s_load_b256 s[4:11], s[0:1], 0x10
	v_ashrrev_i32_e32 v5, 31, v4
	s_load_b32 s2, s[0:1], 0x8
	v_mov_b32_e32 v9, 0
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[0:1], 2, v[4:5]
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v2, vcc_lo, s10, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v3, vcc_lo, s11, v1, vcc_lo
	s_add_i32 s11, s2, 1
	s_cmp_gt_i32 s2, 0
	s_mov_b32 s10, 0
	global_load_b64 v[5:6], v[2:3], off
	v_mul_lo_u32 v2, v4, s11
	s_cselect_b32 s3, -1, 0
	s_cmp_lt_i32 s2, 1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v3, 31, v2
	v_lshlrev_b64 v[7:8], 2, v[2:3]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v2, vcc_lo, s8, v7
	v_add_co_ci_u32_e32 v3, vcc_lo, s9, v8, vcc_lo
	global_store_b32 v[2:3], v9, off
	s_cbranch_scc1 .LBB1_4
; %bb.2:                                ; %.lr.ph.preheader
	v_add_co_u32 v7, vcc_lo, v7, s8
	v_add_co_ci_u32_e32 v8, vcc_lo, s9, v8, vcc_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v7, vcc_lo, v7, 4
	v_add_co_ci_u32_e32 v8, vcc_lo, 0, v8, vcc_lo
.LBB1_3:                                ; %.lr.ph
                                        ; =>This Inner Loop Header: Depth=1
	s_add_i32 s10, s10, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	v_mov_b32_e32 v9, s10
	s_cmp_eq_u32 s2, s10
	global_store_b32 v[7:8], v9, off
	v_add_co_u32 v7, vcc_lo, v7, 4
	v_add_co_ci_u32_e32 v8, vcc_lo, 0, v8, vcc_lo
	s_cbranch_scc0 .LBB1_3
.LBB1_4:                                ; %.preheader
	s_waitcnt vmcnt(0)
	v_sub_nc_u32_e32 v16, v6, v5
	s_mov_b32 s10, exec_lo
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_lt_i32_e32 0, v16
	s_cbranch_execz .LBB1_11
; %bb.5:                                ; %.lr.ph92.preheader
	v_dual_mov_b32 v19, 0 :: v_dual_add_nc_u32 v4, s12, v4
	s_load_b64 s[0:1], s[0:1], 0x0
	v_ashrrev_i32_e32 v6, 31, v5
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_mul_lo_u32 v7, v4, s11
	s_mov_b32 s11, 0
	v_lshlrev_b64 v[9:10], 2, v[5:6]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v8, 31, v7
	v_lshlrev_b64 v[7:8], 2, v[7:8]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v4, vcc_lo, s8, v7
	v_add_co_ci_u32_e32 v5, vcc_lo, s9, v8, vcc_lo
	v_add_co_u32 v17, vcc_lo, s6, v9
	v_add_co_ci_u32_e32 v18, vcc_lo, s7, v10, vcc_lo
	v_dual_mov_b32 v7, v3 :: v_dual_mov_b32 v6, v2
	s_delay_alu instid0(VALU_DEP_4) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_dual_mov_b32 v9, v5 :: v_dual_mov_b32 v8, v4
	s_mov_b32 s7, 0
	s_mov_b32 s6, s7
.LBB1_6:                                ; %.lr.ph92
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB1_8 Depth 2
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_eq_u64_e32 vcc_lo, v[8:9], v[2:3]
	v_dual_cndmask_b32 v9, v3, v5 :: v_dual_cndmask_b32 v8, v2, v4
	v_cmp_eq_u64_e32 vcc_lo, v[6:7], v[2:3]
	global_load_b32 v10, v[8:9], off
	v_dual_cndmask_b32 v7, v3, v5 :: v_dual_cndmask_b32 v6, v2, v4
	s_and_not1_b32 vcc_lo, exec_lo, s3
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v20, 1, v10
	global_store_b32 v[6:7], v20, off
	s_cbranch_vccnz .LBB1_9
; %bb.7:                                ; %.lr.ph88
                                        ;   in Loop: Header=BB1_6 Depth=1
	s_lshl_b64 s[8:9], s[6:7], 2
	s_mov_b32 s12, s2
	v_add_co_u32 v10, vcc_lo, v17, s8
	v_add_co_ci_u32_e32 v11, vcc_lo, s9, v18, vcc_lo
	v_add_co_u32 v12, vcc_lo, v8, 4
	v_add_co_ci_u32_e32 v13, vcc_lo, 0, v9, vcc_lo
	v_add_co_u32 v14, vcc_lo, v6, 4
	v_add_co_ci_u32_e32 v15, vcc_lo, 0, v7, vcc_lo
	s_waitcnt lgkmcnt(0)
	s_mov_b64 s[8:9], s[0:1]
.LBB1_8:                                ;   Parent Loop BB1_6 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	global_load_b64 v[21:22], v[12:13], off offset:-4
	global_load_b32 v23, v19, s[8:9]
	global_load_b32 v24, v[10:11], off
	v_add_co_u32 v12, vcc_lo, v12, 4
	v_add_co_ci_u32_e32 v13, vcc_lo, 0, v13, vcc_lo
	s_add_u32 s8, s8, 4
	s_addc_u32 s9, s9, 0
	s_add_i32 s12, s12, -1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_lg_u32 s12, 0
	s_waitcnt vmcnt(2)
	v_add_nc_u32_e32 v25, 2, v21
	v_add_nc_u32_e32 v22, 1, v22
	s_waitcnt vmcnt(0)
	v_cmp_eq_u32_e32 vcc_lo, v23, v24
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_dual_cndmask_b32 v21, v25, v21 :: v_dual_add_nc_u32 v20, 1, v20
	v_min3_i32 v20, v22, v20, v21
	global_store_b32 v[14:15], v20, off
	v_add_co_u32 v14, vcc_lo, v14, 4
	v_add_co_ci_u32_e32 v15, vcc_lo, 0, v15, vcc_lo
	s_cbranch_scc1 .LBB1_8
.LBB1_9:                                ; %._crit_edge
                                        ;   in Loop: Header=BB1_6 Depth=1
	s_add_i32 s6, s6, 1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_cmp_ge_i32_e32 vcc_lo, s6, v16
	s_or_b32 s11, vcc_lo, s11
	s_and_not1_b32 exec_lo, exec_lo, s11
	s_cbranch_execnz .LBB1_6
; %bb.10:                               ; %Flow128
	s_or_b32 exec_lo, exec_lo, s11
	v_dual_mov_b32 v2, v6 :: v_dual_mov_b32 v3, v7
.LBB1_11:                               ; %Flow129
	s_or_b32 exec_lo, exec_lo, s10
	s_ashr_i32 s3, s2, 31
	s_waitcnt lgkmcnt(0)
	s_waitcnt_vscnt null, 0x0
	s_lshl_b64 s[0:1], s[2:3], 2
	s_barrier
	v_add_co_u32 v2, vcc_lo, v2, s0
	v_add_co_ci_u32_e32 v3, vcc_lo, s1, v3, vcc_lo
	buffer_gl0_inv
	v_add_co_u32 v0, vcc_lo, s4, v0
	global_load_b32 v2, v[2:3], off
	v_add_co_ci_u32_e32 v1, vcc_lo, s5, v1, vcc_lo
	s_waitcnt vmcnt(0)
	global_store_b32 v[0:1], v2, off
.LBB1_12:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z14med_kernel_forPiiS_S_S_S_ii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 312
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 26
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end1:
	.size	_Z14med_kernel_forPiiS_S_S_S_ii, .Lfunc_end1-_Z14med_kernel_forPiiS_S_S_S_ii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 760
; NumSgprs: 18
; NumVgprs: 26
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 3
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 26
; Occupancy: 16
; WaveLimiterHint : 1
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.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_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .offset:         8
        .size:           4
        .value_kind:     by_value
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         24
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         32
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         40
        .size:           8
        .value_kind:     global_buffer
      - .offset:         48
        .size:           4
        .value_kind:     by_value
      - .offset:         52
        .size:           4
        .value_kind:     by_value
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 56
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z16med_kernel_whilePiiS_S_S_S_ii
    .private_segment_fixed_size: 0
    .sgpr_count:     15
    .sgpr_spill_count: 0
    .symbol:         _Z16med_kernel_whilePiiS_S_S_S_ii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     25
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .offset:         8
        .size:           4
        .value_kind:     by_value
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         24
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         32
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         40
        .size:           8
        .value_kind:     global_buffer
      - .offset:         48
        .size:           4
        .value_kind:     by_value
      - .offset:         52
        .size:           4
        .value_kind:     by_value
      - .offset:         56
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         60
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         64
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         68
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         70
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         72
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         74
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         76
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         78
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         104
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         112
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         120
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 312
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z14med_kernel_forPiiS_S_S_S_ii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z14med_kernel_forPiiS_S_S_S_ii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     26
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
