//
// Created by rafa on 2/5/21.
//

#include "grid.cuh"


Grid *getGrid(double boxsize, int nside, long long npart, const double *positions) {
    Grid *grid;

    grid = (Grid *) malloc(nside * nside * nside * sizeof(Grid));

    for (int i = 0; i < nside * nside * nside; i++) {
        grid[i].np = 0;
    }

    for (long long ii = 0; ii < npart; ii++) {
        int i, j, k;
        int s;

        i = (int) (positions[3 * ii + 0] / boxsize * nside);
        j = (int) (positions[3 * ii + 1] / boxsize * nside);
        k = (int) (positions[3 * ii + 2] / boxsize * nside);

        i %= nside;
        j %= nside;
        k %= nside;

        s = nside * nside * k + nside * j + i;

        grid[s].np++;
    }

    for (int i = 0; i < nside * nside * nside; i++) {
        if (grid[i].np > 0) {
            grid[i].pos = (double *) malloc(3 * grid[i].np * sizeof(double));
        }
        grid[i].np = 0;
    }

    for (long long ii = 0; ii < npart; ii++) {
        int i, j, k;
        int s;
        long long offset;

        i = (int) (positions[3 * ii + 0] / boxsize * nside);
        j = (int) (positions[3 * ii + 1] / boxsize * nside);
        k = (int) (positions[3 * ii + 2] / boxsize * nside);

        i %= nside;
        j %= nside;
        k %= nside;

        s = nside * nside * k + nside * j + i;

        offset = 3 * grid[s].np;
        grid[s].pos[offset + 0] = positions[3 * ii + 0];
        grid[s].pos[offset + 1] = positions[3 * ii + 1];
        grid[s].pos[offset + 2] = positions[3 * ii + 2];
        grid[s].np++;
    }

    return grid;
}

void
gridToOrderedArray(Grid *grid, int nside, double *orderedPositions, long long *numParticlesInGrid, long long *offset) {

    for (int s = 0; s < nside * nside * nside; s++) {
        numParticlesInGrid[s] = grid[s].np;
    }

    offset[0] = 0;
    for (int s = 0; s < nside * nside * nside - 1; s++) {
        offset[s + 1] = grid[s].np;
    }

    for (int s = 0; s < nside * nside * nside - 1; s++) {
        offset[s + 1] += offset[s];
    }

    for (int s = 0; s < nside * nside * nside; s++) {
        for (long long i = 0; i < grid[s].np; i++) {
            orderedPositions[3 * offset[s] + 3 * i] = grid[s].pos[3 * i];
            orderedPositions[3 * offset[s] + 3 * i + 1] = grid[s].pos[3 * i + 1];
            orderedPositions[3 * offset[s] + 3 * i + 2] = grid[s].pos[3 * i + 2];
        }
    }
}
	.file	"tmpxft_002ba9ce_00000000-6_grid.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2012:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2012:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z7getGriddixPKd
	.type	_Z7getGriddixPKd, @function
_Z7getGriddixPKd:
.LFB2008:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movl	%edi, %r14d
	imull	%edi, %r14d
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	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
	movq	%rsi, %rbp
	movl	%r14d, %r15d
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%edi, %ebx
	imull	%edi, %r15d
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	movslq	%r15d, %rdi
	movsd	%xmm0, 8(%rsp)
	salq	$4, %rdi
	call	malloc@PLT
	movsd	8(%rsp), %xmm0
	movq	%rax, %r13
	xorl	%eax, %eax
.L3:
	cmpl	%eax, %r15d
	jle	.L14
	movq	%rax, %rdx
	xorl	%ecx, %ecx
	incq	%rax
	salq	$4, %rdx
	movq	%rcx, 0(%r13,%rdx)
	jmp	.L3
.L14:
	movq	%r12, %rsi
	xorl	%edi, %edi
.L5:
	cmpq	%rbp, %rdi
	jge	.L15
	movsd	(%rsi), %xmm1
	cvtsi2sdl	%ebx, %xmm2
	incq	%rdi
	addq	$24, %rsi
	divsd	%xmm0, %xmm1
	mulsd	%xmm2, %xmm1
	cvttsd2sil	%xmm1, %r8d
	movsd	-16(%rsi), %xmm1
	divsd	%xmm0, %xmm1
	mulsd	%xmm2, %xmm1
	cvttsd2sil	%xmm1, %r9d
	movsd	-8(%rsi), %xmm1
	divsd	%xmm0, %xmm1
	mulsd	%xmm2, %xmm1
	cvttsd2sil	%xmm1, %eax
	cltd
	idivl	%ebx
	movl	%r9d, %eax
	movl	%edx, %ecx
	cltd
	idivl	%ebx
	imull	%r14d, %ecx
	movl	%r8d, %eax
	imull	%ebx, %edx
	addl	%edx, %ecx
	cltd
	idivl	%ebx
	addl	%ecx, %edx
	movslq	%edx, %rdx
	salq	$4, %rdx
	incq	0(%r13,%rdx)
	jmp	.L5
.L15:
	movq	%r13, %rdx
	xorl	%ecx, %ecx
.L7:
	cmpl	%ecx, %r15d
	jle	.L16
	movq	(%rdx), %rdi
	testq	%rdi, %rdi
	jle	.L8
	imulq	$24, %rdi, %rdi
	movq	%rdx, 16(%rsp)
	movl	%ecx, 8(%rsp)
	movsd	%xmm0, 24(%rsp)
	call	malloc@PLT
	movq	16(%rsp), %rdx
	movsd	24(%rsp), %xmm0
	movl	8(%rsp), %ecx
	movq	%rax, 8(%rdx)
.L8:
	xorl	%eax, %eax
	incl	%ecx
	addq	$16, %rdx
	movq	%rax, -16(%rdx)
	jmp	.L7
.L16:
	xorl	%esi, %esi
.L10:
	cmpq	%rbp, %rsi
	jge	.L17
	movsd	(%r12), %xmm3
	cvtsi2sdl	%ebx, %xmm2
	incq	%rsi
	addq	$24, %r12
	movaps	%xmm3, %xmm1
	divsd	%xmm0, %xmm1
	mulsd	%xmm2, %xmm1
	cvttsd2sil	%xmm1, %edi
	movsd	-16(%r12), %xmm1
	divsd	%xmm0, %xmm1
	mulsd	%xmm2, %xmm1
	cvttsd2sil	%xmm1, %r8d
	movsd	-8(%r12), %xmm1
	divsd	%xmm0, %xmm1
	mulsd	%xmm2, %xmm1
	cvttsd2sil	%xmm1, %eax
	cltd
	idivl	%ebx
	movl	%r8d, %eax
	movl	%edx, %ecx
	cltd
	idivl	%ebx
	imull	%r14d, %ecx
	movl	%edi, %eax
	imull	%ebx, %edx
	addl	%edx, %ecx
	cltd
	idivl	%ebx
	leal	(%rcx,%rdx), %eax
	cltq
	salq	$4, %rax
	addq	%r13, %rax
	movq	(%rax), %rcx
	movq	8(%rax), %rdi
	leaq	(%rcx,%rcx,2), %rdx
	incq	%rcx
	movsd	%xmm3, (%rdi,%rdx,8)
	movsd	-16(%r12), %xmm1
	incq	%rdx
	movq	%rcx, (%rax)
	movsd	%xmm1, (%rdi,%rdx,8)
	movsd	-8(%r12), %xmm1
	movsd	%xmm1, 8(%rdi,%rdx,8)
	jmp	.L10
.L17:
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	movq	%r13, %rax
	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	_Z7getGriddixPKd, .-_Z7getGriddixPKd
	.globl	_Z18gridToOrderedArrayP4GridiPdPxS2_
	.type	_Z18gridToOrderedArrayP4GridiPdPxS2_, @function
_Z18gridToOrderedArrayP4GridiPdPxS2_:
.LFB2009:
	.cfi_startproc
	endbr64
	movq	%rdi, %rax
	movq	%rdx, %rdi
	movl	%esi, %edx
	imull	%esi, %edx
	imull	%edx, %esi
	xorl	%edx, %edx
.L19:
	cmpl	%edx, %esi
	jle	.L35
	movq	%rdx, %r9
	salq	$4, %r9
	movq	(%rax,%r9), %r9
	movq	%r9, (%rcx,%rdx,8)
	incq	%rdx
	jmp	.L19
.L35:
	xorl	%edx, %edx
	leal	-1(%rsi), %ecx
	movq	%rdx, (%r8)
	xorl	%edx, %edx
.L21:
	cmpl	%edx, %ecx
	jle	.L36
	movq	%rdx, %r9
	salq	$4, %r9
	movq	(%rax,%r9), %r9
	movq	%r9, 8(%r8,%rdx,8)
	incq	%rdx
	jmp	.L21
.L36:
	xorl	%edx, %edx
.L23:
	cmpl	%edx, %ecx
	jle	.L37
	movq	(%r8,%rdx,8), %r9
	addq	%r9, 8(%r8,%rdx,8)
	incq	%rdx
	jmp	.L23
.L37:
	xorl	%r10d, %r10d
	cmpl	%r10d, %esi
	jg	.L33
	ret
.L33:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
.L31:
	movq	(%rax), %rbx
	xorl	%ecx, %ecx
	xorl	%r9d, %r9d
.L28:
	cmpq	%r9, %rbx
	jle	.L38
	movq	(%r8,%r10,8), %rdx
	movq	8(%rax), %r11
	addq	%r9, %rdx
	movsd	(%r11,%rcx), %xmm0
	incq	%r9
	imulq	$24, %rdx, %rbp
	leaq	(%rdx,%rdx,2), %rdx
	incq	%rdx
	movsd	%xmm0, (%rdi,%rbp)
	movsd	8(%r11,%rcx), %xmm0
	movsd	%xmm0, (%rdi,%rdx,8)
	movsd	16(%r11,%rcx), %xmm0
	addq	$24, %rcx
	movsd	%xmm0, 8(%rdi,%rdx,8)
	jmp	.L28
.L38:
	incq	%r10
	addq	$16, %rax
	cmpl	%r10d, %esi
	jg	.L31
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2009:
	.size	_Z18gridToOrderedArrayP4GridiPdPxS2_, .-_Z18gridToOrderedArrayP4GridiPdPxS2_
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2035:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2035:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

//
// Created by rafa on 2/5/21.
//

#ifndef CUPAC_GRID_CUH
#define CUPAC_GRID_CUH

#include "define.cuh"

Grid *getGrid(double boxsize, int nside, long long npart, const double *positions);

void gridToOrderedArray(Grid *grid, int nside, double *orderedPositions, long long *numParticlesInGrid, long long *offset);

#endif //CUPAC_GRID_CUH

	.text
	.file	"grid.hip"
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
