/* ==================================================================
   Programmer: Yicheng Tu (ytu@cse.usf.edu)
   The basic SDH algorithm implementation for 3D data
   To compile: nvcc SDH.c -o SDH in the C4 lab machines
   ==================================================================
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <sys/time.h>


const long BOX_SIZE = 23000; /* size of the data box on one dimension */

/* descriptors for single atom in the tree */
typedef struct atomdesc {
    double x_pos;
    double y_pos;
    double z_pos;
} atom;

typedef struct hist_entry {
    //float min;
    //float max;
    unsigned long long d_cnt;   /* need a long long type as the count might be huge */
} bucket;

struct debuginfo {
    int idx;
    int ran;
    int i;
    int j;
    float dist;
    int which_bucket;
};


bucket * histogram;  /* list of all buckets in the histogram   */
unsigned long long PDH_acnt; /* total number of data points            */
int num_buckets;  /* total number of buckets in the histogram */
double PDH_res;  /* value of w                             */
atom * atom_list;  /* list of all data points                */

/* These are for an old way of tracking time */
struct timezone Idunno;
struct timeval startTime, endTime;


/*
 distance of two points in the atom_list
*/
double p2p_distance(int ind1, int ind2) {

    double x1 = atom_list[ind1].x_pos;
    double x2 = atom_list[ind2].x_pos;
    double y1 = atom_list[ind1].y_pos;
    double y2 = atom_list[ind2].y_pos;
    double z1 = atom_list[ind1].z_pos;
    double z2 = atom_list[ind2].z_pos;

    return sqrt((x1 - x2)*(x1-x2) + (y1 - y2)*(y1 - y2) + (z1 - z2)*(z1 - z2));
}


/*
 brute-force SDH solution in a single CPU thread
*/
int PDH_baseline() {
    int i, j, h_pos;
    double dist;

    for(i = 0; i < PDH_acnt; i++) {
        for(j = i+1; j < PDH_acnt; j++) {
            dist = p2p_distance(i,j);
            h_pos = (int) (dist / PDH_res);
            if (h_pos >= 0 && h_pos < num_buckets)
                histogram[h_pos].d_cnt++;
            else
                printf("Warning: value %lf falls outside histogram", dist);
        }
    }
    return 0;
}

__global__
void PDH_kernel(long n_threads, bucket *d_buckets, int n_buckets, const atom *d_atoms, double w
#ifdef DEBUG
                , struct debuginfo *d_dinfo
#endif
    ) {
    int idx = blockIdx.x*blockDim.x + threadIdx.x;
    if (idx >= n_threads)
        return;
    
    // Please don't make me explain. It was 2am and I scribbled some math and it works
    int i = (sqrt(8.0*idx + 1.0) - 1.0)/2;
    int j = idx - i*(i + 1)/2;
    i++;

    double deltax = d_atoms[i].x_pos - d_atoms[j].x_pos;
    double deltay = d_atoms[i].y_pos - d_atoms[j].y_pos;
    double deltaz = d_atoms[i].z_pos - d_atoms[j].z_pos;

    double dist = sqrt(deltax*deltax + deltay*deltay + deltaz*deltaz);
    int h_pos = (int) (dist / w);
    if (h_pos >= 0 && h_pos < n_buckets)
        // atomicAdd(&d_buckets[h_pos].d_cnt, 1);
        d_buckets[h_pos*n_threads + idx].d_cnt++; // Coalesce!



#ifdef DEBUG
    d_dinfo[idx].idx = idx;
    d_dinfo[idx].i = i;
    d_dinfo[idx].j = j;
    d_dinfo[idx].ran = 1;
    d_dinfo[idx].dist = dist;
    d_dinfo[idx].which_bucket = (int) (dist / w);
#endif
}

void PDH_gpu() {
    unsigned long num_threads = PDH_acnt*(PDH_acnt - 1)/2;

    // allocate histogram
    bucket *d_buckets;
    cudaMalloc(&d_buckets, sizeof(*histogram) * num_buckets);
    cudaMemset(d_buckets, 0, sizeof(*histogram) * num_buckets);

#ifdef DEBUG
    // allocate debuginfo
    struct debuginfo *d_dinfo;
    cudaMalloc(&d_dinfo, sizeof(*d_dinfo) * num_threads);
    cudaMemset(d_dinfo, 0, sizeof(*d_dinfo) * num_threads);
#endif

    // Copy atoms to device
    atom *d_atoms;
    cudaMalloc(&d_atoms, sizeof(*atom_list) * PDH_acnt);
    cudaMemcpy(d_atoms, atom_list, sizeof(*atom_list) * PDH_acnt, cudaMemcpyHostToDevice);

    PDH_kernel<<<(num_threads + 255)/256, 256>>>(num_threads, d_buckets, num_buckets, d_atoms, PDH_res
    // PDH_kernel<<<1, num_threads>>>(num_threads, d_buckets, num_buckets, d_atoms, PDH_res
#ifdef DEBUG
                                                 , d_dinfo
#endif
        );
    // cudaDeviceSynchronize();

    cudaError_t err = cudaGetLastError();
    if (err != cudaSuccess) {
        printf("CUDA ERROR: %s\n", cudaGetErrorString(err));
        puts("This is probably due to a too-large block count");
    }

    // Copy histogram from device and cleanup
    cudaFree(d_atoms);
    cudaMemcpy(histogram, d_buckets, sizeof(*histogram) * num_buckets, cudaMemcpyDeviceToHost);
    cudaFree(d_buckets);

#ifdef DEBUG
    // Copy debuginfo from device and cleanup
    struct debuginfo *h_dinfo = (struct debuginfo *) malloc(sizeof(*h_dinfo) * num_threads);
    cudaMemcpy(h_dinfo, d_dinfo, sizeof(*h_dinfo) * num_threads, cudaMemcpyDeviceToHost);
    cudaFree(d_dinfo);

    for (unsigned long long i=0; i<num_threads; i++) {
        printf("%llu: idx=%d, ran=%d, i=%d, j=%d, dist=%f, bucket=%d\n",
               i, h_dinfo[i].idx, h_dinfo[i].ran, h_dinfo[i].i, h_dinfo[i].j,
               h_dinfo[i].dist, h_dinfo[i].which_bucket);
    }
#endif
}


/*
 set a checkpoint and show the (natural) running time in seconds
*/
double report_running_time(const char *type) {
    long sec_diff, usec_diff;
    gettimeofday(&endTime, &Idunno);
    sec_diff = endTime.tv_sec - startTime.tv_sec;
    usec_diff= endTime.tv_usec - startTime.tv_usec;
    if (usec_diff < 0) {
        sec_diff--;
        usec_diff += 1000000;
    }
    printf("Running time for %s version: %ld.%06ld\n", type, sec_diff, usec_diff);
    return (double)(sec_diff*1.0 + usec_diff/1000000.0);
}


/*
 print the counts in all buckets of the histogram
*/
void output_histogram() {
    int i;
    unsigned long long total_cnt = 0;
    for(i=0; i<num_buckets; i++) {
        if (i%5 == 0) /* we print 5 buckets in a row */
            printf("\n%02d: ", i);
        printf("%15lld ", histogram[i].d_cnt);
        total_cnt += histogram[i].d_cnt;
        /* we also want to make sure the total distance count is correct */
        if (i == num_buckets - 1)
            printf("\n T:%lld \n", total_cnt);
        else printf("| ");
    }
}


int main(int argc, char **argv)
{
    int i;

    PDH_acnt = atoi(argv[1]);
    PDH_res = atof(argv[2]);
    // printf("args are %d and %f\n", PDH_acnt, PDH_res);

    num_buckets = (int)(BOX_SIZE * 1.732 / PDH_res) + 1;
    histogram = (bucket *)malloc(sizeof(bucket)*num_buckets);

    atom_list = (atom *)malloc(sizeof(atom)*PDH_acnt);


    srand(1);
    /* generate data following a uniform distribution */
    for(i = 0; i < PDH_acnt; i++) {
        atom_list[i].x_pos = ((double)(rand()) / RAND_MAX) * BOX_SIZE;
        atom_list[i].y_pos = ((double)(rand()) / RAND_MAX) * BOX_SIZE;
        atom_list[i].z_pos = ((double)(rand()) / RAND_MAX) * BOX_SIZE;
    }

    // CPU implementation
    puts("Running CPU version...");
    memset(histogram, 0, sizeof(*histogram) * num_buckets);
    gettimeofday(&startTime, &Idunno);
    PDH_baseline();
    report_running_time("CPU");
    output_histogram();

    // GPU implementation
    puts("\nRunning GPU version...");
    memset(histogram, 0, sizeof(*histogram) * num_buckets);
    gettimeofday(&startTime, &Idunno);
    PDH_gpu();
    report_running_time("GPU");
    output_histogram();


    return 0;
}

	.file	"tmpxft_00291e00_00000000-6_SDH.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2035:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2035:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z12p2p_distanceii
	.type	_Z12p2p_distanceii, @function
_Z12p2p_distanceii:
.LFB2027:
	.cfi_startproc
	endbr64
	movslq	%edi, %rdi
	movslq	%esi, %rsi
	movq	atom_list(%rip), %rax
	imulq	$24, %rdi, %rdi
	imulq	$24, %rsi, %rsi
	addq	%rax, %rdi
	addq	%rsi, %rax
	movsd	(%rdi), %xmm0
	movsd	8(%rdi), %xmm2
	subsd	(%rax), %xmm0
	subsd	8(%rax), %xmm2
	movsd	16(%rdi), %xmm1
	subsd	16(%rax), %xmm1
	mulsd	%xmm0, %xmm0
	mulsd	%xmm2, %xmm2
	mulsd	%xmm1, %xmm1
	addsd	%xmm2, %xmm0
	addsd	%xmm1, %xmm0
	jmp	sqrt@PLT
	.cfi_endproc
.LFE2027:
	.size	_Z12p2p_distanceii, .-_Z12p2p_distanceii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Warning: value %lf falls outside histogram"
	.text
	.globl	_Z12PDH_baselinev
	.type	_Z12PDH_baselinev, @function
_Z12PDH_baselinev:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	leaq	.LC0(%rip), %r14
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	xorl	%ebx, %ebx
.L4:
	cmpq	PDH_acnt(%rip), %rbx
	movl	%ebx, %r13d
	jnb	.L12
	leal	1(%rbx), %r12d
	incq	%rbx
	movq	%rbx, %rbp
.L5:
	cmpq	PDH_acnt(%rip), %rbp
	jnb	.L4
	movl	%r12d, %esi
	movl	%r13d, %edi
	call	_Z12p2p_distanceii
	movaps	%xmm0, %xmm1
	divsd	PDH_res(%rip), %xmm1
	cvttsd2sil	%xmm1, %eax
	testl	%eax, %eax
	js	.L6
	cmpl	%eax, num_buckets(%rip)
	jle	.L6
	movq	histogram(%rip), %rdx
	cltq
	incq	(%rdx,%rax,8)
	jmp	.L7
.L6:
	movq	%r14, %rsi
	movl	$2, %edi
	movb	$1, %al
	call	__printf_chk@PLT
.L7:
	incl	%r12d
	incq	%rbp
	jmp	.L5
.L12:
	popq	%rbx
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z12PDH_baselinev, .-_Z12PDH_baselinev
	.section	.rodata.str1.1
.LC1:
	.string	"Running time for %s version: %ld.%06ld\n"
	.text
	.globl	_Z19report_running_timePKc
	.type	_Z19report_running_timePKc, @function
_Z19report_running_timePKc:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	leaq	Idunno(%rip), %rsi
	movq	%rdi, %r12
	leaq	endTime(%rip), %rdi
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	call	gettimeofday@PLT
	movq	endTime(%rip), %rbp
	movq	8+endTime(%rip), %rbx
	subq	startTime(%rip), %rbp
	subq	8+startTime(%rip), %rbx
	jns	.L14
	decq	%rbp
	addq	$1000000, %rbx
.L14:
	movq	%rbx, %r8
	movq	%rbp, %rcx
	movq	%r12, %rdx
	movl	$2, %edi
	leaq	.LC1(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	cvtsi2sdq	%rbp, %xmm1
	cvtsi2sdq	%rbx, %xmm0
	popq	%rbx
	.cfi_def_cfa_offset 24
	divsd	.LC2(%rip), %xmm0
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	addsd	%xmm1, %xmm0
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z19report_running_timePKc, .-_Z19report_running_timePKc
	.section	.rodata.str1.1
.LC3:
	.string	"\n%02d: "
.LC4:
	.string	"%15lld "
.LC5:
	.string	"\n T:%lld \n"
.LC6:
	.string	"| "
	.text
	.globl	_Z16output_histogramv
	.type	_Z16output_histogramv, @function
_Z16output_histogramv:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	leaq	.LC3(%rip), %r13
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	xorl	%ebx, %ebx
	pushq	%rdx
	.cfi_def_cfa_offset 48
.L17:
	cmpl	%ebx, num_buckets(%rip)
	movl	%ebx, %ebp
	jle	.L23
	movl	%ebp, %eax
	movl	$5, %ecx
	cltd
	idivl	%ecx
	testl	%edx, %edx
	jne	.L18
	movl	%ebp, %edx
	movq	%r13, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L18:
	movq	histogram(%rip), %rax
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	movq	(%rax,%rbx,8), %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	histogram(%rip), %rax
	addq	(%rax,%rbx,8), %r12
	movl	num_buckets(%rip), %eax
	decl	%eax
	cmpl	%ebp, %eax
	jne	.L19
	movq	%r12, %rdx
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L20
.L19:
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L20:
	incq	%rbx
	jmp	.L17
.L23:
	popq	%rax
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.size	_Z16output_histogramv, .-_Z16output_histogramv
	.globl	_Z55__device_stub__Z10PDH_kernellP10hist_entryiPK8atomdescdlP10hist_entryiPK8atomdescd
	.type	_Z55__device_stub__Z10PDH_kernellP10hist_entryiPK8atomdescdlP10hist_entryiPK8atomdescd, @function
_Z55__device_stub__Z10PDH_kernellP10hist_entryiPK8atomdescdlP10hist_entryiPK8atomdescd:
.LFB2057:
	.cfi_startproc
	endbr64
	subq	$168, %rsp
	.cfi_def_cfa_offset 176
	movq	%rdi, 40(%rsp)
	leaq	64(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	76(%rsp), %rsi
	movl	%edx, 28(%rsp)
	leaq	48(%rsp), %rdx
	movq	%rcx, 16(%rsp)
	leaq	56(%rsp), %rcx
	movsd	%xmm0, 8(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 152(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 72(%rsp)
	movq	%rax, 112(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	28(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 144(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 64(%rsp)
	movq	%rax, 76(%rsp)
	movl	$1, 84(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L24
	pushq	56(%rsp)
	.cfi_def_cfa_offset 184
	leaq	_Z10PDH_kernellP10hist_entryiPK8atomdescd(%rip), %rdi
	pushq	56(%rsp)
	.cfi_def_cfa_offset 192
	movq	92(%rsp), %rcx
	movl	100(%rsp), %r8d
	movq	80(%rsp), %rsi
	movl	88(%rsp), %edx
	leaq	128(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 184
	popq	%rdx
	.cfi_def_cfa_offset 176
.L24:
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	je	.L26
	call	__stack_chk_fail@PLT
.L26:
	addq	$168, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2057:
	.size	_Z55__device_stub__Z10PDH_kernellP10hist_entryiPK8atomdescdlP10hist_entryiPK8atomdescd, .-_Z55__device_stub__Z10PDH_kernellP10hist_entryiPK8atomdescdlP10hist_entryiPK8atomdescd
	.globl	_Z10PDH_kernellP10hist_entryiPK8atomdescd
	.type	_Z10PDH_kernellP10hist_entryiPK8atomdescd, @function
_Z10PDH_kernellP10hist_entryiPK8atomdescd:
.LFB2058:
	.cfi_startproc
	endbr64
	jmp	_Z55__device_stub__Z10PDH_kernellP10hist_entryiPK8atomdescdlP10hist_entryiPK8atomdescd
	.cfi_endproc
.LFE2058:
	.size	_Z10PDH_kernellP10hist_entryiPK8atomdescd, .-_Z10PDH_kernellP10hist_entryiPK8atomdescd
	.section	.rodata.str1.1
.LC7:
	.string	"CUDA ERROR: %s\n"
.LC8:
	.string	"This is probably due to a too-large block count"
	.text
	.globl	_Z7PDH_gpuv
	.type	_Z7PDH_gpuv, @function
_Z7PDH_gpuv:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	subq	$48, %rsp
	.cfi_def_cfa_offset 64
	movslq	num_buckets(%rip), %rsi
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	movq	PDH_acnt(%rip), %rax
	movq	%rsp, %rdi
	salq	$3, %rsi
	leaq	-1(%rax), %rbx
	imulq	%rax, %rbx
	call	cudaMalloc@PLT
	movslq	num_buckets(%rip), %rdx
	movq	(%rsp), %rdi
	xorl	%esi, %esi
	salq	$3, %rdx
	shrq	%rbx
	call	cudaMemset@PLT
	imulq	$24, PDH_acnt(%rip), %rsi
	leaq	8(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	8(%rsp), %rdi
	movl	$1, %ecx
	imulq	$24, PDH_acnt(%rip), %rdx
	movq	atom_list(%rip), %rsi
	call	cudaMemcpy@PLT
	movl	$16777217, %edx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	leaq	255(%rbx), %rax
	salq	$8, %rdx
	movl	$1, %ecx
	shrq	$8, %rax
	movl	%eax, 16(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 20(%rsp)
	movq	16(%rsp), %rdi
	movl	24(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L30
	movsd	PDH_res(%rip), %xmm0
	movq	8(%rsp), %rcx
	movq	%rbx, %rdi
	movl	num_buckets(%rip), %edx
	movq	(%rsp), %rsi
	call	_Z55__device_stub__Z10PDH_kernellP10hist_entryiPK8atomdescdlP10hist_entryiPK8atomdescd
.L30:
	call	cudaGetLastError@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L31
	call	cudaGetErrorString@PLT
	movl	$2, %edi
	leaq	.LC7(%rip), %rsi
	movq	%rax, %rdx
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC8(%rip), %rdi
	call	puts@PLT
.L31:
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movslq	num_buckets(%rip), %rdx
	movq	(%rsp), %rsi
	movl	$2, %ecx
	movq	histogram(%rip), %rdi
	salq	$3, %rdx
	call	cudaMemcpy@PLT
	movq	(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L32
	call	__stack_chk_fail@PLT
.L32:
	addq	$48, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z7PDH_gpuv, .-_Z7PDH_gpuv
	.section	.rodata.str1.1
.LC12:
	.string	"Running CPU version..."
.LC13:
	.string	"CPU"
.LC14:
	.string	"\nRunning GPU version..."
.LC15:
	.string	"GPU"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2032:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movq	%rsi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movq	8(%rsi), %rdi
	call	atoi@PLT
	movq	16(%rbp), %rdi
	movslq	%eax, %rbx
	movq	%rbx, PDH_acnt(%rip)
	call	atof@PLT
	movsd	.LC9(%rip), %xmm1
	movsd	%xmm0, PDH_res(%rip)
	divsd	%xmm0, %xmm1
	cvttsd2sil	%xmm1, %edi
	incl	%edi
	movl	%edi, num_buckets(%rip)
	movslq	%edi, %rdi
	salq	$3, %rdi
	call	malloc@PLT
	imulq	$24, %rbx, %rdi
	xorl	%ebx, %ebx
	movq	%rax, histogram(%rip)
	call	malloc@PLT
	movl	$1, %edi
	movq	%rax, atom_list(%rip)
	call	srand@PLT
.L38:
	cmpq	PDH_acnt(%rip), %rbx
	jnb	.L41
	call	rand@PLT
	imulq	$24, %rbx, %rbp
	incq	%rbx
	cvtsi2sdl	%eax, %xmm0
	divsd	.LC10(%rip), %xmm0
	mulsd	.LC11(%rip), %xmm0
	movq	atom_list(%rip), %rax
	movsd	%xmm0, (%rax,%rbp)
	call	rand@PLT
	cvtsi2sdl	%eax, %xmm0
	divsd	.LC10(%rip), %xmm0
	mulsd	.LC11(%rip), %xmm0
	movq	atom_list(%rip), %rax
	movsd	%xmm0, 8(%rax,%rbp)
	call	rand@PLT
	cvtsi2sdl	%eax, %xmm0
	divsd	.LC10(%rip), %xmm0
	mulsd	.LC11(%rip), %xmm0
	movq	atom_list(%rip), %rax
	movsd	%xmm0, 16(%rax,%rbp)
	jmp	.L38
.L41:
	leaq	.LC12(%rip), %rdi
	xorl	%ebp, %ebp
	leaq	Idunno(%rip), %r12
	call	puts@PLT
	movslq	num_buckets(%rip), %rcx
	movl	%ebp, %eax
	movq	histogram(%rip), %rdi
	leaq	startTime(%rip), %rbx
	movq	%r12, %rsi
	salq	$3, %rcx
	rep stosb
	movq	%rbx, %rdi
	call	gettimeofday@PLT
	call	_Z12PDH_baselinev
	leaq	.LC13(%rip), %rdi
	call	_Z19report_running_timePKc
	call	_Z16output_histogramv
	leaq	.LC14(%rip), %rdi
	call	puts@PLT
	movslq	num_buckets(%rip), %rcx
	movl	%ebp, %eax
	movq	%r12, %rsi
	movq	histogram(%rip), %rdi
	salq	$3, %rcx
	rep stosb
	movq	%rbx, %rdi
	call	gettimeofday@PLT
	call	_Z7PDH_gpuv
	leaq	.LC15(%rip), %rdi
	call	_Z19report_running_timePKc
	call	_Z16output_histogramv
	popq	%rbx
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2032:
	.size	main, .-main
	.section	.rodata.str1.1
.LC16:
	.string	"_Z10PDH_kernellP10hist_entryiPK8atomdescd"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2060:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 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	.LC16(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z10PDH_kernellP10hist_entryiPK8atomdescd(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	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	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2060:
	.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
	.globl	endTime
	.bss
	.align 16
	.type	endTime, @object
	.size	endTime, 16
endTime:
	.zero	16
	.globl	startTime
	.align 16
	.type	startTime, @object
	.size	startTime, 16
startTime:
	.zero	16
	.globl	Idunno
	.align 8
	.type	Idunno, @object
	.size	Idunno, 8
Idunno:
	.zero	8
	.globl	atom_list
	.align 8
	.type	atom_list, @object
	.size	atom_list, 8
atom_list:
	.zero	8
	.globl	PDH_res
	.align 8
	.type	PDH_res, @object
	.size	PDH_res, 8
PDH_res:
	.zero	8
	.globl	num_buckets
	.align 4
	.type	num_buckets, @object
	.size	num_buckets, 4
num_buckets:
	.zero	4
	.globl	PDH_acnt
	.align 8
	.type	PDH_acnt, @object
	.size	PDH_acnt, 8
PDH_acnt:
	.zero	8
	.globl	histogram
	.align 8
	.type	histogram, @object
	.size	histogram, 8
histogram:
	.zero	8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC2:
	.long	0
	.long	1093567616
	.align 8
.LC9:
	.long	0
	.long	1088648064
	.align 8
.LC10:
	.long	-4194304
	.long	1105199103
	.align 8
.LC11:
	.long	0
	.long	1087796736
	.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:

/* ==================================================================
   Programmer: Yicheng Tu (ytu@cse.usf.edu)
   The basic SDH algorithm implementation for 3D data
   To compile: nvcc SDH.c -o SDH in the C4 lab machines
   ==================================================================
*/


#include <hip/hip_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <sys/time.h>


const long BOX_SIZE = 23000; /* size of the data box on one dimension */

/* descriptors for single atom in the tree */
typedef struct atomdesc {
    double x_pos;
    double y_pos;
    double z_pos;
} atom;

typedef struct hist_entry {
    //float min;
    //float max;
    unsigned long long d_cnt;   /* need a long long type as the count might be huge */
} bucket;

struct debuginfo {
    int idx;
    int ran;
    int i;
    int j;
    float dist;
    int which_bucket;
};


bucket * histogram;  /* list of all buckets in the histogram   */
unsigned long long PDH_acnt; /* total number of data points            */
int num_buckets;  /* total number of buckets in the histogram */
double PDH_res;  /* value of w                             */
atom * atom_list;  /* list of all data points                */

/* These are for an old way of tracking time */
struct timezone Idunno;
struct timeval startTime, endTime;


/*
 distance of two points in the atom_list
*/
double p2p_distance(int ind1, int ind2) {

    double x1 = atom_list[ind1].x_pos;
    double x2 = atom_list[ind2].x_pos;
    double y1 = atom_list[ind1].y_pos;
    double y2 = atom_list[ind2].y_pos;
    double z1 = atom_list[ind1].z_pos;
    double z2 = atom_list[ind2].z_pos;

    return sqrt((x1 - x2)*(x1-x2) + (y1 - y2)*(y1 - y2) + (z1 - z2)*(z1 - z2));
}


/*
 brute-force SDH solution in a single CPU thread
*/
int PDH_baseline() {
    int i, j, h_pos;
    double dist;

    for(i = 0; i < PDH_acnt; i++) {
        for(j = i+1; j < PDH_acnt; j++) {
            dist = p2p_distance(i,j);
            h_pos = (int) (dist / PDH_res);
            if (h_pos >= 0 && h_pos < num_buckets)
                histogram[h_pos].d_cnt++;
            else
                printf("Warning: value %lf falls outside histogram", dist);
        }
    }
    return 0;
}

__global__
void PDH_kernel(long n_threads, bucket *d_buckets, int n_buckets, const atom *d_atoms, double w
#ifdef DEBUG
                , struct debuginfo *d_dinfo
#endif
    ) {
    int idx = blockIdx.x*blockDim.x + threadIdx.x;
    if (idx >= n_threads)
        return;
    
    // Please don't make me explain. It was 2am and I scribbled some math and it works
    int i = (sqrt(8.0*idx + 1.0) - 1.0)/2;
    int j = idx - i*(i + 1)/2;
    i++;

    double deltax = d_atoms[i].x_pos - d_atoms[j].x_pos;
    double deltay = d_atoms[i].y_pos - d_atoms[j].y_pos;
    double deltaz = d_atoms[i].z_pos - d_atoms[j].z_pos;

    double dist = sqrt(deltax*deltax + deltay*deltay + deltaz*deltaz);
    int h_pos = (int) (dist / w);
    if (h_pos >= 0 && h_pos < n_buckets)
        // atomicAdd(&d_buckets[h_pos].d_cnt, 1);
        d_buckets[h_pos*n_threads + idx].d_cnt++; // Coalesce!



#ifdef DEBUG
    d_dinfo[idx].idx = idx;
    d_dinfo[idx].i = i;
    d_dinfo[idx].j = j;
    d_dinfo[idx].ran = 1;
    d_dinfo[idx].dist = dist;
    d_dinfo[idx].which_bucket = (int) (dist / w);
#endif
}

void PDH_gpu() {
    unsigned long num_threads = PDH_acnt*(PDH_acnt - 1)/2;

    // allocate histogram
    bucket *d_buckets;
    hipMalloc(&d_buckets, sizeof(*histogram) * num_buckets);
    hipMemset(d_buckets, 0, sizeof(*histogram) * num_buckets);

#ifdef DEBUG
    // allocate debuginfo
    struct debuginfo *d_dinfo;
    hipMalloc(&d_dinfo, sizeof(*d_dinfo) * num_threads);
    hipMemset(d_dinfo, 0, sizeof(*d_dinfo) * num_threads);
#endif

    // Copy atoms to device
    atom *d_atoms;
    hipMalloc(&d_atoms, sizeof(*atom_list) * PDH_acnt);
    hipMemcpy(d_atoms, atom_list, sizeof(*atom_list) * PDH_acnt, hipMemcpyHostToDevice);

    PDH_kernel<<<(num_threads + 255)/256, 256>>>(num_threads, d_buckets, num_buckets, d_atoms, PDH_res
    // PDH_kernel<<<1, num_threads>>>(num_threads, d_buckets, num_buckets, d_atoms, PDH_res
#ifdef DEBUG
                                                 , d_dinfo
#endif
        );
    // cudaDeviceSynchronize();

    hipError_t err = hipGetLastError();
    if (err != hipSuccess) {
        printf("CUDA ERROR: %s\n", hipGetErrorString(err));
        puts("This is probably due to a too-large block count");
    }

    // Copy histogram from device and cleanup
    hipFree(d_atoms);
    hipMemcpy(histogram, d_buckets, sizeof(*histogram) * num_buckets, hipMemcpyDeviceToHost);
    hipFree(d_buckets);

#ifdef DEBUG
    // Copy debuginfo from device and cleanup
    struct debuginfo *h_dinfo = (struct debuginfo *) malloc(sizeof(*h_dinfo) * num_threads);
    hipMemcpy(h_dinfo, d_dinfo, sizeof(*h_dinfo) * num_threads, hipMemcpyDeviceToHost);
    hipFree(d_dinfo);

    for (unsigned long long i=0; i<num_threads; i++) {
        printf("%llu: idx=%d, ran=%d, i=%d, j=%d, dist=%f, bucket=%d\n",
               i, h_dinfo[i].idx, h_dinfo[i].ran, h_dinfo[i].i, h_dinfo[i].j,
               h_dinfo[i].dist, h_dinfo[i].which_bucket);
    }
#endif
}


/*
 set a checkpoint and show the (natural) running time in seconds
*/
double report_running_time(const char *type) {
    long sec_diff, usec_diff;
    gettimeofday(&endTime, &Idunno);
    sec_diff = endTime.tv_sec - startTime.tv_sec;
    usec_diff= endTime.tv_usec - startTime.tv_usec;
    if (usec_diff < 0) {
        sec_diff--;
        usec_diff += 1000000;
    }
    printf("Running time for %s version: %ld.%06ld\n", type, sec_diff, usec_diff);
    return (double)(sec_diff*1.0 + usec_diff/1000000.0);
}


/*
 print the counts in all buckets of the histogram
*/
void output_histogram() {
    int i;
    unsigned long long total_cnt = 0;
    for(i=0; i<num_buckets; i++) {
        if (i%5 == 0) /* we print 5 buckets in a row */
            printf("\n%02d: ", i);
        printf("%15lld ", histogram[i].d_cnt);
        total_cnt += histogram[i].d_cnt;
        /* we also want to make sure the total distance count is correct */
        if (i == num_buckets - 1)
            printf("\n T:%lld \n", total_cnt);
        else printf("| ");
    }
}


int main(int argc, char **argv)
{
    int i;

    PDH_acnt = atoi(argv[1]);
    PDH_res = atof(argv[2]);
    // printf("args are %d and %f\n", PDH_acnt, PDH_res);

    num_buckets = (int)(BOX_SIZE * 1.732 / PDH_res) + 1;
    histogram = (bucket *)malloc(sizeof(bucket)*num_buckets);

    atom_list = (atom *)malloc(sizeof(atom)*PDH_acnt);


    srand(1);
    /* generate data following a uniform distribution */
    for(i = 0; i < PDH_acnt; i++) {
        atom_list[i].x_pos = ((double)(rand()) / RAND_MAX) * BOX_SIZE;
        atom_list[i].y_pos = ((double)(rand()) / RAND_MAX) * BOX_SIZE;
        atom_list[i].z_pos = ((double)(rand()) / RAND_MAX) * BOX_SIZE;
    }

    // CPU implementation
    puts("Running CPU version...");
    memset(histogram, 0, sizeof(*histogram) * num_buckets);
    gettimeofday(&startTime, &Idunno);
    PDH_baseline();
    report_running_time("CPU");
    output_histogram();

    // GPU implementation
    puts("\nRunning GPU version...");
    memset(histogram, 0, sizeof(*histogram) * num_buckets);
    gettimeofday(&startTime, &Idunno);
    PDH_gpu();
    report_running_time("GPU");
    output_histogram();


    return 0;
}

	.text
	.file	"SDH.hip"
	.globl	_Z12p2p_distanceii              # -- Begin function _Z12p2p_distanceii
	.type	_Z12p2p_distanceii,@function
_Z12p2p_distanceii:                     # @_Z12p2p_distanceii
	.cfi_startproc
# %bb.0:
	movq	atom_list(%rip), %rax
	movslq	%edi, %rcx
	leaq	(%rcx,%rcx,2), %rcx
	movslq	%esi, %rdx
	leaq	(%rdx,%rdx,2), %rdx
	movsd	(%rax,%rcx,8), %xmm0            # xmm0 = mem[0],zero
	movsd	8(%rax,%rcx,8), %xmm1           # xmm1 = mem[0],zero
	subsd	(%rax,%rdx,8), %xmm0
	movsd	16(%rax,%rcx,8), %xmm2          # xmm2 = mem[0],zero
	mulsd	%xmm0, %xmm0
	subsd	8(%rax,%rdx,8), %xmm1
	mulsd	%xmm1, %xmm1
	addsd	%xmm0, %xmm1
	subsd	16(%rax,%rdx,8), %xmm2
	mulsd	%xmm2, %xmm2
	addsd	%xmm1, %xmm2
	xorps	%xmm0, %xmm0
	sqrtsd	%xmm2, %xmm0
	retq
.Lfunc_end0:
	.size	_Z12p2p_distanceii, .Lfunc_end0-_Z12p2p_distanceii
	.cfi_endproc
                                        # -- End function
	.globl	_Z12PDH_baselinev               # -- Begin function _Z12PDH_baselinev
	.type	_Z12PDH_baselinev,@function
_Z12PDH_baselinev:                      # @_Z12PDH_baselinev
	.cfi_startproc
# %bb.0:
	movq	PDH_acnt(%rip), %rax
	testq	%rax, %rax
	je	.LBB1_11
# %bb.1:                                # %.lr.ph16.preheader
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$16, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	$1, %r15d
	xorl	%r12d, %r12d
.LBB1_3:                                # %.lr.ph16
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_5 Depth 2
	movq	%r12, %rbx
	incq	%r12
	cmpq	%r12, %rax
	jbe	.LBB1_2
# %bb.4:                                # %.lr.ph.preheader
                                        #   in Loop: Header=BB1_3 Depth=1
	movsd	PDH_res(%rip), %xmm0            # xmm0 = mem[0],zero
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	movl	num_buckets(%rip), %ebp
	movq	%r15, %r14
.LBB1_5:                                # %.lr.ph
                                        #   Parent Loop BB1_3 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	%ebx, %edi
	movl	%r14d, %esi
	callq	_Z12p2p_distanceii
	movapd	%xmm0, %xmm1
	divsd	8(%rsp), %xmm1                  # 8-byte Folded Reload
	cvttsd2si	%xmm1, %eax
	testl	%eax, %eax
	js	.LBB1_8
# %bb.6:                                # %.lr.ph
                                        #   in Loop: Header=BB1_5 Depth=2
	cmpl	%eax, %ebp
	jle	.LBB1_8
# %bb.7:                                #   in Loop: Header=BB1_5 Depth=2
	movq	histogram(%rip), %rcx
	movl	%eax, %eax
	incq	(%rcx,%rax,8)
	jmp	.LBB1_9
.LBB1_8:                                #   in Loop: Header=BB1_5 Depth=2
	movl	$.L.str, %edi
	movb	$1, %al
	callq	printf
	movsd	PDH_res(%rip), %xmm0            # xmm0 = mem[0],zero
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	movl	num_buckets(%rip), %ebp
.LBB1_9:                                #   in Loop: Header=BB1_5 Depth=2
	incq	%r14
	movq	PDH_acnt(%rip), %rax
	cmpq	%r14, %rax
	ja	.LBB1_5
.LBB1_2:                                # %.loopexit
                                        #   in Loop: Header=BB1_3 Depth=1
	incq	%r15
	cmpq	%r12, %rax
	ja	.LBB1_3
# %bb.10:
	addq	$16, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.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
	.cfi_restore %rbx
	.cfi_restore %r12
	.cfi_restore %r14
	.cfi_restore %r15
	.cfi_restore %rbp
.LBB1_11:                               # %._crit_edge
	xorl	%eax, %eax
	retq
.Lfunc_end1:
	.size	_Z12PDH_baselinev, .Lfunc_end1-_Z12PDH_baselinev
	.cfi_endproc
                                        # -- End function
	.globl	_Z25__device_stub__PDH_kernellP10hist_entryiPK8atomdescd # -- Begin function _Z25__device_stub__PDH_kernellP10hist_entryiPK8atomdescd
	.type	_Z25__device_stub__PDH_kernellP10hist_entryiPK8atomdescd,@function
_Z25__device_stub__PDH_kernellP10hist_entryiPK8atomdescd: # @_Z25__device_stub__PDH_kernellP10hist_entryiPK8atomdescd
	.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	$144, %rsp
	.cfi_def_cfa_offset 192
	.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	48(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	12(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	40(%rsp), %rdx
	movq	%rcx, (%rdx)
	leaq	32(%rsp), %rcx
	movsd	%xmm0, (%rcx)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	leaq	80(%rsp), %r14
	leaq	64(%rsp), %r15
	leaq	24(%rsp), %r12
	leaq	16(%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	$_Z10PDH_kernellP10hist_entryiPK8atomdescd, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$160, %rsp
	.cfi_adjust_cfa_offset -160
	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_end2:
	.size	_Z25__device_stub__PDH_kernellP10hist_entryiPK8atomdescd, .Lfunc_end2-_Z25__device_stub__PDH_kernellP10hist_entryiPK8atomdescd
	.cfi_endproc
                                        # -- End function
	.globl	_Z7PDH_gpuv                     # -- Begin function _Z7PDH_gpuv
	.type	_Z7PDH_gpuv,@function
_Z7PDH_gpuv:                            # @_Z7PDH_gpuv
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	subq	$24, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	movq	PDH_acnt(%rip), %rax
	leaq	-1(%rax), %rbx
	imulq	%rax, %rbx
	shrq	%rbx
	movslq	num_buckets(%rip), %rsi
	shlq	$3, %rsi
	leaq	8(%rsp), %r14
	movq	%r14, %rdi
	callq	hipMalloc
	movq	(%r14), %rdi
	movslq	num_buckets(%rip), %rdx
	shlq	$3, %rdx
	xorl	%esi, %esi
	callq	hipMemset
	movq	PDH_acnt(%rip), %rax
	shlq	$3, %rax
	leaq	(%rax,%rax,2), %rsi
	leaq	16(%rsp), %r14
	movq	%r14, %rdi
	callq	hipMalloc
	movq	(%r14), %rdi
	movq	atom_list(%rip), %rsi
	movq	PDH_acnt(%rip), %rax
	shlq	$3, %rax
	leaq	(%rax,%rax,2), %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	255(%rbx), %rax
	shrq	$8, %rax
	movl	%eax, %edi
	btsq	$32, %rdi
	movabsq	$4294967296, %rdx               # imm = 0x100000000
	orq	$256, %rdx                      # imm = 0x100
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_2
# %bb.1:
	movq	8(%rsp), %rsi
	movl	num_buckets(%rip), %edx
	movq	16(%rsp), %rcx
	movsd	PDH_res(%rip), %xmm0            # xmm0 = mem[0],zero
	movq	%rbx, %rdi
	callq	_Z25__device_stub__PDH_kernellP10hist_entryiPK8atomdescd
.LBB3_2:
	callq	hipGetLastError
	testl	%eax, %eax
	je	.LBB3_4
# %bb.3:
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.1, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.2, %edi
	callq	puts
.LBB3_4:
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	histogram(%rip), %rdi
	movq	8(%rsp), %rsi
	movslq	num_buckets(%rip), %rdx
	shlq	$3, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	8(%rsp), %rdi
	callq	hipFree
	addq	$24, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	_Z7PDH_gpuv, .Lfunc_end3-_Z7PDH_gpuv
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z19report_running_timePKc
.LCPI4_0:
	.quad	0x412e848000000000              # double 1.0E+6
	.text
	.globl	_Z19report_running_timePKc
	.type	_Z19report_running_timePKc,@function
_Z19report_running_timePKc:             # @_Z19report_running_timePKc
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %rbx
	movl	$endTime, %edi
	movl	$Idunno, %esi
	callq	gettimeofday
	movq	endTime(%rip), %rax
	subq	startTime(%rip), %rax
	movq	endTime+8(%rip), %rcx
	subq	startTime+8(%rip), %rcx
	leaq	1000000(%rcx), %r14
	movq	%rcx, %r15
	sarq	$63, %r15
	addq	%rax, %r15
	testq	%rcx, %rcx
	cmovnsq	%rcx, %r14
	movl	$.L.str.3, %edi
	movq	%rbx, %rsi
	movq	%r15, %rdx
	movq	%r14, %rcx
	xorl	%eax, %eax
	callq	printf
	cvtsi2sd	%r15, %xmm1
	cvtsi2sd	%r14, %xmm0
	divsd	.LCPI4_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end4:
	.size	_Z19report_running_timePKc, .Lfunc_end4-_Z19report_running_timePKc
	.cfi_endproc
                                        # -- End function
	.globl	_Z16output_histogramv           # -- Begin function _Z16output_histogramv
	.type	_Z16output_histogramv,@function
_Z16output_histogramv:                  # @_Z16output_histogramv
	.cfi_startproc
# %bb.0:
	cmpl	$0, num_buckets(%rip)
	jle	.LBB5_9
# %bb.1:                                # %.lr.ph.preheader
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$3435973837, %r15d              # imm = 0xCCCCCCCD
	xorl	%ebx, %ebx
	xorl	%r14d, %r14d
.LBB5_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	%ebx, %eax
	imulq	%r15, %rax
	shrq	$34, %rax
	leal	(%rax,%rax,4), %eax
	cmpl	%eax, %ebx
	jne	.LBB5_4
# %bb.3:                                #   in Loop: Header=BB5_2 Depth=1
	movl	$.L.str.4, %edi
	movl	%ebx, %esi
	xorl	%eax, %eax
	callq	printf
.LBB5_4:                                #   in Loop: Header=BB5_2 Depth=1
	movq	histogram(%rip), %rax
	movq	(%rax,%rbx,8), %rsi
	movl	$.L.str.5, %edi
	xorl	%eax, %eax
	callq	printf
	movq	histogram(%rip), %rax
	addq	(%rax,%rbx,8), %r14
	movl	num_buckets(%rip), %eax
	decl	%eax
	cmpq	%rax, %rbx
	jne	.LBB5_6
# %bb.5:                                #   in Loop: Header=BB5_2 Depth=1
	movl	$.L.str.6, %edi
	movq	%r14, %rsi
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB5_7
.LBB5_6:                                #   in Loop: Header=BB5_2 Depth=1
	movl	$.L.str.7, %edi
	xorl	%eax, %eax
	callq	printf
.LBB5_7:                                #   in Loop: Header=BB5_2 Depth=1
	incq	%rbx
	movslq	num_buckets(%rip), %rax
	cmpq	%rax, %rbx
	jl	.LBB5_2
# %bb.8:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r14
	.cfi_restore %r15
.LBB5_9:                                # %._crit_edge
	retq
.Lfunc_end5:
	.size	_Z16output_histogramv, .Lfunc_end5-_Z16output_histogramv
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI6_0:
	.quad	0x40e3738000000000              # double 39836
.LCPI6_1:
	.quad	0x41dfffffffc00000              # double 2147483647
.LCPI6_2:
	.quad	0x40d6760000000000              # double 23000
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	pushq	%rax
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	movq	%rsi, %rbx
	movq	8(%rsi), %rdi
	callq	atoi
	movslq	%eax, %r14
	movq	%r14, PDH_acnt(%rip)
	movq	16(%rbx), %rdi
	callq	atof
	movsd	%xmm0, PDH_res(%rip)
	movsd	.LCPI6_0(%rip), %xmm1           # xmm1 = mem[0],zero
	divsd	%xmm0, %xmm1
	cvttsd2si	%xmm1, %eax
	leal	1(%rax), %ecx
	movl	%ecx, num_buckets(%rip)
	cltq
	leaq	8(,%rax,8), %rdi
	callq	malloc
	movq	%rax, histogram(%rip)
	shlq	$3, %r14
	leaq	(%r14,%r14,2), %rdi
	callq	malloc
	movq	%rax, atom_list(%rip)
	movl	$1, %edi
	callq	srand
	cmpq	$0, PDH_acnt(%rip)
	je	.LBB6_3
# %bb.1:                                # %.lr.ph.preheader
	xorl	%ebx, %ebx
	xorl	%r14d, %r14d
.LBB6_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	movsd	.LCPI6_1(%rip), %xmm1           # xmm1 = mem[0],zero
	divsd	%xmm1, %xmm0
	movsd	.LCPI6_2(%rip), %xmm1           # xmm1 = mem[0],zero
	mulsd	%xmm1, %xmm0
	movq	atom_list(%rip), %rax
	movsd	%xmm0, (%rax,%rbx)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	divsd	.LCPI6_1(%rip), %xmm0
	mulsd	.LCPI6_2(%rip), %xmm0
	movq	atom_list(%rip), %rax
	movsd	%xmm0, 8(%rax,%rbx)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	divsd	.LCPI6_1(%rip), %xmm0
	mulsd	.LCPI6_2(%rip), %xmm0
	movq	atom_list(%rip), %rax
	movsd	%xmm0, 16(%rax,%rbx)
	incq	%r14
	addq	$24, %rbx
	cmpq	%r14, PDH_acnt(%rip)
	ja	.LBB6_2
.LBB6_3:                                # %._crit_edge
	movl	$.L.str.8, %edi
	callq	puts
	movq	histogram(%rip), %rdi
	movslq	num_buckets(%rip), %rdx
	shlq	$3, %rdx
	xorl	%esi, %esi
	callq	memset@PLT
	movl	$startTime, %edi
	movl	$Idunno, %esi
	callq	gettimeofday
	callq	_Z12PDH_baselinev
	movl	$.L.str.9, %edi
	callq	_Z19report_running_timePKc
	callq	_Z16output_histogramv
	movl	$.L.str.10, %edi
	callq	puts
	movq	histogram(%rip), %rdi
	movslq	num_buckets(%rip), %rdx
	shlq	$3, %rdx
	xorl	%esi, %esi
	callq	memset@PLT
	movl	$startTime, %edi
	movl	$Idunno, %esi
	callq	gettimeofday
	callq	_Z7PDH_gpuv
	movl	$.L.str.11, %edi
	callq	_Z19report_running_timePKc
	callq	_Z16output_histogramv
	xorl	%eax, %eax
	addq	$8, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end6:
	.size	main, .Lfunc_end6-main
	.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:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB7_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB7_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z10PDH_kernellP10hist_entryiPK8atomdescd, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end7:
	.size	__hip_module_ctor, .Lfunc_end7-__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	.LBB8_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
.LBB8_2:
	retq
.Lfunc_end8:
	.size	__hip_module_dtor, .Lfunc_end8-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	histogram,@object               # @histogram
	.bss
	.globl	histogram
	.p2align	3, 0x0
histogram:
	.quad	0
	.size	histogram, 8

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

	.type	num_buckets,@object             # @num_buckets
	.globl	num_buckets
	.p2align	2, 0x0
num_buckets:
	.long	0                               # 0x0
	.size	num_buckets, 4

	.type	PDH_res,@object                 # @PDH_res
	.globl	PDH_res
	.p2align	3, 0x0
PDH_res:
	.quad	0x0000000000000000              # double 0
	.size	PDH_res, 8

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

	.type	Idunno,@object                  # @Idunno
	.globl	Idunno
	.p2align	2, 0x0
Idunno:
	.zero	8
	.size	Idunno, 8

	.type	startTime,@object               # @startTime
	.globl	startTime
	.p2align	3, 0x0
startTime:
	.zero	16
	.size	startTime, 16

	.type	endTime,@object                 # @endTime
	.globl	endTime
	.p2align	3, 0x0
endTime:
	.zero	16
	.size	endTime, 16

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Warning: value %lf falls outside histogram"
	.size	.L.str, 43

	.type	_Z10PDH_kernellP10hist_entryiPK8atomdescd,@object # @_Z10PDH_kernellP10hist_entryiPK8atomdescd
	.section	.rodata,"a",@progbits
	.globl	_Z10PDH_kernellP10hist_entryiPK8atomdescd
	.p2align	3, 0x0
_Z10PDH_kernellP10hist_entryiPK8atomdescd:
	.quad	_Z25__device_stub__PDH_kernellP10hist_entryiPK8atomdescd
	.size	_Z10PDH_kernellP10hist_entryiPK8atomdescd, 8

	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"CUDA ERROR: %s\n"
	.size	.L.str.1, 16

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"This is probably due to a too-large block count"
	.size	.L.str.2, 48

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Running time for %s version: %ld.%06ld\n"
	.size	.L.str.3, 40

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"\n%02d: "
	.size	.L.str.4, 8

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"%15lld "
	.size	.L.str.5, 8

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"\n T:%lld \n"
	.size	.L.str.6, 11

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"| "
	.size	.L.str.7, 3

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"Running CPU version..."
	.size	.L.str.8, 23

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"CPU"
	.size	.L.str.9, 4

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"\nRunning GPU version..."
	.size	.L.str.10, 24

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"GPU"
	.size	.L.str.11, 4

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z10PDH_kernellP10hist_entryiPK8atomdescd"
	.size	.L__unnamed_1, 42

	.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 _Z25__device_stub__PDH_kernellP10hist_entryiPK8atomdescd
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym Idunno
	.addrsig_sym startTime
	.addrsig_sym endTime
	.addrsig_sym _Z10PDH_kernellP10hist_entryiPK8atomdescd
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z10PDH_kernellP10hist_entryiPK8atomdescd ; -- Begin function _Z10PDH_kernellP10hist_entryiPK8atomdescd
	.globl	_Z10PDH_kernellP10hist_entryiPK8atomdescd
	.p2align	8
	.type	_Z10PDH_kernellP10hist_entryiPK8atomdescd,@function
_Z10PDH_kernellP10hist_entryiPK8atomdescd: ; @_Z10PDH_kernellP10hist_entryiPK8atomdescd
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x34
	s_load_b128 s[4:7], s[0:1], 0x0
	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[1:2], null, s15, s2, v[0:1]
	s_mov_b32 s2, exec_lo
	v_ashrrev_i32_e32 v2, 31, v1
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_gt_i64_e64 s[4:5], v[1:2]
	s_cbranch_execz .LBB0_3
; %bb.1:
	v_cvt_f64_i32_e32 v[3:4], v1
	s_clause 0x1
	s_load_b128 s[8:11], s[0:1], 0x18
	s_load_b32 s0, s[0:1], 0x10
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[3:4], 0x40200000, v[3:4], 1.0
	v_cmp_gt_f64_e32 vcc_lo, 0x10000000, v[3:4]
	v_cndmask_b32_e64 v0, 0, 1, vcc_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b32_e32 v0, 8, v0
	v_ldexp_f64 v[3:4], v[3:4], v0
	v_cndmask_b32_e64 v0, 0, 0xffffff80, vcc_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_4) | instid1(VALU_DEP_1)
	v_rsq_f64_e32 v[5:6], v[3:4]
	v_cmp_class_f64_e64 vcc_lo, v[3:4], 0x260
	s_waitcnt_depctr 0xfff
	v_mul_f64 v[7:8], v[3:4], v[5:6]
	v_mul_f64 v[5:6], v[5:6], 0.5
	v_fma_f64 v[9:10], -v[5:6], v[7:8], 0.5
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_fma_f64 v[7:8], v[7:8], v[9:10], v[7:8]
	v_fma_f64 v[5:6], v[5:6], v[9:10], v[5:6]
	v_fma_f64 v[9:10], -v[7:8], v[7:8], v[3:4]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[7:8], v[9:10], v[5:6], v[7:8]
	v_fma_f64 v[9:10], -v[7:8], v[7:8], v[3:4]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[5:6], v[9:10], v[5:6], v[7:8]
	v_ldexp_f64 v[5:6], v[5:6], v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_dual_cndmask_b32 v3, v5, v3 :: v_dual_cndmask_b32 v4, v6, v4
	v_add_f64 v[3:4], v[3:4], -1.0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f64 v[3:4], v[3:4], 0.5
	v_cvt_i32_f64_e32 v0, v[3:4]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_mad_u64_u32 v[3:4], null, v0, v0, v[0:1]
	s_waitcnt lgkmcnt(0)
	v_mad_i64_i32 v[11:12], null, v0, 24, s[8:9]
	v_lshrrev_b32_e32 v4, 31, v3
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_nc_u32_e32 v3, v3, v4
	v_ashrrev_i32_e32 v3, 1, v3
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_sub_nc_u32_e32 v3, v1, v3
	v_mad_i64_i32 v[13:14], null, v3, 24, s[8:9]
	s_clause 0x3
	global_load_b128 v[3:6], v[11:12], off offset:24
	global_load_b128 v[7:10], v[13:14], off
	global_load_b64 v[11:12], v[11:12], off offset:40
	global_load_b64 v[13:14], v[13:14], off offset:16
	s_waitcnt vmcnt(2)
	v_add_f64 v[5:6], v[5:6], -v[9:10]
	v_add_f64 v[3:4], v[3:4], -v[7:8]
	s_waitcnt vmcnt(0)
	v_add_f64 v[7:8], v[11:12], -v[13:14]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f64 v[5:6], v[5:6], v[5:6]
	v_fma_f64 v[3:4], v[3:4], v[3:4], v[5:6]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[3:4], v[7:8], v[7:8], v[3:4]
	v_cmp_gt_f64_e32 vcc_lo, 0x10000000, v[3:4]
	v_cndmask_b32_e64 v0, 0, 1, vcc_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b32_e32 v0, 8, v0
	v_ldexp_f64 v[3:4], v[3:4], v0
	v_cndmask_b32_e64 v0, 0, 0xffffff80, vcc_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_4) | instid1(VALU_DEP_1)
	v_rsq_f64_e32 v[5:6], v[3:4]
	v_cmp_class_f64_e64 vcc_lo, v[3:4], 0x260
	s_waitcnt_depctr 0xfff
	v_mul_f64 v[7:8], v[3:4], v[5:6]
	v_mul_f64 v[5:6], v[5:6], 0.5
	v_fma_f64 v[9:10], -v[5:6], v[7:8], 0.5
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_fma_f64 v[7:8], v[7:8], v[9:10], v[7:8]
	v_fma_f64 v[5:6], v[5:6], v[9:10], v[5:6]
	v_fma_f64 v[9:10], -v[7:8], v[7:8], v[3:4]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[7:8], v[9:10], v[5:6], v[7:8]
	v_fma_f64 v[9:10], -v[7:8], v[7:8], v[3:4]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[5:6], v[9:10], v[5:6], v[7:8]
	v_ldexp_f64 v[5:6], v[5:6], v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_dual_cndmask_b32 v3, v5, v3 :: v_dual_cndmask_b32 v4, v6, v4
	v_div_scale_f64 v[5:6], null, s[10:11], s[10:11], v[3:4]
	v_div_scale_f64 v[11:12], vcc_lo, v[3:4], s[10:11], v[3:4]
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f64_e32 v[7:8], v[5:6]
	s_waitcnt_depctr 0xfff
	v_fma_f64 v[9:10], -v[5:6], v[7:8], 1.0
	v_fma_f64 v[7:8], v[7:8], v[9:10], v[7:8]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[9:10], -v[5:6], v[7:8], 1.0
	v_fma_f64 v[7:8], v[7:8], v[9:10], v[7:8]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f64 v[9:10], v[11:12], v[7:8]
	v_fma_f64 v[5:6], -v[5:6], v[9:10], v[11:12]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_div_fmas_f64 v[5:6], v[5:6], v[7:8], v[9:10]
	v_div_fixup_f64 v[3:4], v[5:6], s[10:11], v[3:4]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cvt_i32_f64_e32 v0, v[3:4]
	v_cmp_lt_i32_e32 vcc_lo, -1, v0
	v_cmp_gt_i32_e64 s0, s0, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s0, vcc_lo, s0
	s_and_b32 exec_lo, exec_lo, s0
	s_cbranch_execz .LBB0_3
; %bb.2:
	v_mad_u64_u32 v[3:4], null, v0, s4, 0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_mad_u64_u32 v[5:6], null, v0, s5, v[4:5]
	v_lshlrev_b64 v[0:1], 3, v[1:2]
	v_mov_b32_e32 v4, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[3:4], 3, v[3:4]
	v_add_co_u32 v2, vcc_lo, s6, v3
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_ci_u32_e32 v3, vcc_lo, s7, v4, vcc_lo
	v_add_co_u32 v0, vcc_lo, v2, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v1, vcc_lo, v3, v1, vcc_lo
	global_load_b64 v[2:3], v[0:1], off
	s_waitcnt vmcnt(0)
	v_add_co_u32 v2, vcc_lo, v2, 1
	v_add_co_ci_u32_e32 v3, vcc_lo, 0, v3, vcc_lo
	global_store_b64 v[0:1], v[2:3], off
.LBB0_3:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z10PDH_kernellP10hist_entryiPK8atomdescd
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 296
		.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 15
		.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_end0:
	.size	_Z10PDH_kernellP10hist_entryiPK8atomdescd, .Lfunc_end0-_Z10PDH_kernellP10hist_entryiPK8atomdescd
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 892
; NumSgprs: 18
; NumVgprs: 15
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 15
; Occupancy: 16
; WaveLimiterHint : 0
; 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:
      - .offset:         0
        .size:           8
        .value_kind:     by_value
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .offset:         16
        .size:           4
        .value_kind:     by_value
      - .address_space:  global
        .offset:         24
        .size:           8
        .value_kind:     global_buffer
      - .offset:         32
        .size:           8
        .value_kind:     by_value
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         44
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         48
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         52
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         54
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         56
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         58
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         60
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         62
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         104
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 296
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z10PDH_kernellP10hist_entryiPK8atomdescd
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z10PDH_kernellP10hist_entryiPK8atomdescd.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     15
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
