#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/times.h>
#include <time.h>
#include <math.h>
#include <cuda_runtime.h>
#define PI 3.14159265358979323846
#define FactorArcosegRad 0.00000484814
#define BLOQUESIZE 4

clock_t timestart, timeend;

/**
@brief Función que transforma un valor en arco segundo a radianes
@param deltax: Valor numérico a transformar
@returns Valor correspondiente a la entrada en radianes */
float arcoseg_radian(float deltax){
	return FactorArcosegRad*deltax;
}

/**
@brief Función que lee el archivo de entrada
@param archivo: puntero al archivo a leer
@param tamano: Numero de visibilidades del archivo a leer
@returns  */
double* readFile(FILE* archivo, int tamano){
	double* elementos =(double*) malloc(sizeof(double)*4*tamano);
	fread(elementos, tamano*4, sizeof(double), archivo);
	return elementos;
}


/**
@brief Función ejecuta el proceso de gridding
@param U: Valores de la coordenada U en el plano de Fourier
@param V: Valores de la coordenada V en el plano de Fourier
@param R: Valores reales de la visibilidad en el plano de Fourier
@param I: Valores imaginarios la visibilidad en el plano de Fourier
@param num_datos: Cantidad de visibilidades ingresadas o dimensión de los vectores anteriores
@param tamano: Lado de la matriz a construir, si tamano es 512 se construye una matriz de 512X512
@param V: Valores de la coordenada V en el plano de Fourier
@param deltaU: Valor delta necesario para determinar la vecindad de cada pixel de la grilla regular
@param r: vector de valores reales de la salida del proceso de gridding
@param k: vector de valores imaginarios de la salida del proceso de gridding
@returns  */
__global__ void gridding_process(float *U, float *V, float *R, float *I, int num_datos, int tamano, float deltaU, float *r, float *k)
{
	int i = blockDim.x * blockIdx.x + threadIdx.x;
	if(i<num_datos)
	{
		float x, y, modx, mody;
		x = U[i]/deltaU+tamano/2;
		y = V[i]/deltaU+tamano/2;
		modx = U[i] - x*deltaU;
		mody = V[i] - y*deltaU;

		if(modx>deltaU/2){	
			x+=1;
		}

		if (mody>deltaU/2)
		{
			y+=1;
		}

		if ((int)x<tamano && (int)y<tamano)
		{
			atomicAdd(&r[(int)y*tamano+(int)x], R[i]);
			atomicAdd(&k[(int)y*tamano+(int)x], I[i]);
		}
	}
}

__host__ unsigned long upper_power_of_two(unsigned long v)
{
    v--;
    v |= v >> 1;
    v |= v >> 2;
    v |= v >> 4;
    v |= v >> 8;
    v |= v >> 16;
    v++;
    return v;

}

int main(int argc, char * const argv[])
{
	int tamano;//tamaño de imagen
	int numdatos;//número de pasos
	float deltaX_arcoseg, deltaX_radian;
	float deltaU; 
	char* archivo_entrada=NULL;
	char* archivo_salida=NULL;
	char* archivo_salida_i;
	int i, c;
	opterr = 0;
	while ((c = getopt (argc, argv, "i:z:d:N:o:")) != -1)
		switch (c)
			{
			case 'i':
				archivo_entrada = optarg;
				break;
			case 'z':
				numdatos = atoi(optarg);
				break;
			case 'd':
				deltaX_arcoseg = atof(optarg);
				break;
			case 'N':
				tamano = atoi(optarg);
				break;
			case 'o':
				archivo_salida = optarg;
				break;
			case '?':
				if (optopt == 'i' ||optopt == 'z' ||optopt == 'd'||optopt == 'N' ||optopt == 'o')
					fprintf (stderr, "Opcion -%c requiere un argumento.\n", optopt);
				else if (isprint (optopt))
					fprintf (stderr, "Opcion desconocida `-%c'.\n", optopt);
				else
					fprintf (stderr,
									"Carater opcion desconocido `\\x%x'.\n",
									optopt);
				return 1;
			default:
				abort ();
			}
	/**
		Comprobación de Inputs
			- Valores mayores que cero
			- Cadenas no nulas
	**/
	if(tamano<=0){
		printf("El parametro -N debe existir y ser mayor que 0\n");
		exit(1);
	}
	if(numdatos==0){
		printf("El parametro -z debe existir y ser mayor que 0\n");
		exit(1);
	}
	if(deltaX_arcoseg==0){
		printf("El parametro -d debe existir y ser mayor que 0\n");
		exit(1);
	}
	if(archivo_entrada==NULL){
		printf("Debe especificarse un archivo de entrada\n");
	}
	if(archivo_salida==NULL){
		printf("Debe especificarse un archivo de salida\n");
	}
	//Transformacion de unidades necesaria para calcular delta U
	deltaX_radian = arcoseg_radian(deltaX_arcoseg);

	//Determina delta U/V a utilizar
	deltaU = 1/(tamano*deltaX_radian);

	//Medición de tiempo de computo
	timestart = clock(); 

	//Lectura de entrada
	FILE *entrada = fopen(archivo_entrada,"r");
	double* data = readFile(entrada,numdatos);
	fclose(entrada);

	//Creando arrays para coordenada X, Y, R e I
	float *X = (float*)malloc(sizeof(float)*numdatos); 
	float *Y = (float*)malloc(sizeof(float)*numdatos); 
	float *R = (float*)malloc(sizeof(float)*numdatos); 
	float *I = (float*)malloc(sizeof(float)*numdatos);	
	//Quizas necesite dos vectores adicionales para el gridding [matrices desenroyadas]
	float *r = (float*)malloc(sizeof(float)*tamano*tamano);
	float *k = (float*)malloc(sizeof(float)*tamano*tamano);
	//Se asigan los valores correspondientes de la lectura
	for (i = 0; i < numdatos; i++)
	{
		X[i] = (float)data[i];
		Y[i] = (float)data[i+numdatos];
		R[i] = (float)data[i+2*numdatos];
		I[i] = (float)data[i+3*numdatos];

	}
	for (i = 0; i < tamano*tamano; ++i)
	{
		r[i] = 0;
		k[i] = 0;
	}
	//se declaran las variables CUDA
	float *C_X;
	float *C_Y;
	float *C_R;
	float *C_I;
	float *C_r;
	float *C_k;
	//Se reserva memoria CUDA
	cudaMalloc( (void**)&C_X, numdatos*sizeof(float)); 
	cudaMalloc( (void**)&C_Y, numdatos*sizeof(float)); 
	cudaMalloc( (void**)&C_R, numdatos*sizeof(float)); 
	cudaMalloc( (void**)&C_I, numdatos*sizeof(float)); 
	cudaMalloc( (void**)&C_r, tamano*tamano*sizeof(float)); 
	cudaMalloc( (void**)&C_k, tamano*tamano*sizeof(float)); 
	//se copia la matriz iniciada en las matrices de trabajo en memoria global GPU
	cudaMemcpy( C_X, X, numdatos*sizeof(float), cudaMemcpyHostToDevice); 
	cudaMemcpy( C_Y, Y, numdatos*sizeof(float), cudaMemcpyHostToDevice); 
	cudaMemcpy( C_R, R, numdatos*sizeof(float), cudaMemcpyHostToDevice); 
	cudaMemcpy( C_I, I, numdatos*sizeof(float), cudaMemcpyHostToDevice); 
	cudaMemcpy( C_r, r, tamano*tamano*sizeof(float), cudaMemcpyHostToDevice); 
	cudaMemcpy( C_k, k, tamano*tamano*sizeof(float), cudaMemcpyHostToDevice); 
	//determino dimension para el kernel
	long data_size_2 = upper_power_of_two(numdatos);
	//Se declaran las dimenciones
	dim3 dimBlock(BLOQUESIZE, 1);
	dim3 dimGrid(data_size_2/BLOQUESIZE, 1);
	//se ejecuta el kernel en la GPU
	//printf("%d - %d - %d\n", numdatos, kernel_size, kernel_size/BLOQUESIZE);

	gridding_process<<<dimGrid, dimBlock>>>(C_X, C_Y, C_R, C_I, numdatos, tamano, deltaU, C_r, C_k);
	//se espera a que terminen
	cudaDeviceSynchronize();
	//se obtiene la memoria de regreso
	cudaMemcpy( r, C_r, tamano*tamano*sizeof(float), cudaMemcpyDeviceToHost); 
	cudaMemcpy( k, C_k, tamano*tamano*sizeof(float), cudaMemcpyDeviceToHost); 
	//se libera la memoria global CUDA para que pueda ser usada por otro proceso
	cudaFree( C_X );
	cudaFree( C_Y );
	cudaFree( C_R );
	cudaFree( C_I );
	cudaFree( C_r );
	cudaFree( C_k );
	//Se imprime salida
	archivo_salida_i = (char*)malloc(sizeof(archivo_salida)*2);
	strcpy(archivo_salida_i, archivo_salida);
	FILE *f = fopen(strcat(archivo_salida, "real.raw"),"wb");
	FILE *g = fopen(strcat(archivo_salida_i, "img.raw"),"wb");
	fwrite(r, tamano*tamano, sizeof(float),f);
	fwrite(k, tamano*tamano, sizeof(float),g);
	fclose(f);
	fclose(g);
	//Se mide el tiempo utilizado
	timeend = clock();
	printf("Total = %f\n", (double) (timeend-timestart)/(double)CLOCKS_PER_SEC);
	return EXIT_SUCCESS;
}
	.file	"tmpxft_00299d38_00000000-6_gridding.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2046:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2046:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z14arcoseg_radianf
	.type	_Z14arcoseg_radianf, @function
_Z14arcoseg_radianf:
.LFB2040:
	.cfi_startproc
	endbr64
	cvtss2sd	%xmm0, %xmm0
	mulsd	.LC0(%rip), %xmm0
	cvtsd2ss	%xmm0, %xmm0
	ret
	.cfi_endproc
.LFE2040:
	.size	_Z14arcoseg_radianf, .-_Z14arcoseg_radianf
	.globl	_Z8readFileP8_IO_FILEi
	.type	_Z8readFileP8_IO_FILEi, @function
_Z8readFileP8_IO_FILEi:
.LFB2041:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movslq	%esi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movq	%r12, %rbx
	salq	$5, %r12
	subq	$16, %rsp
	.cfi_def_cfa_offset 48
	movq	%rdi, 8(%rsp)
	movq	%r12, %rdi
	call	malloc@PLT
	movq	8(%rsp), %r8
	movq	%r12, %rsi
	leal	0(,%rbx,4), %edx
	movslq	%edx, %rdx
	movl	$8, %ecx
	movq	%rax, %rdi
	movq	%rax, %rbp
	call	__fread_chk@PLT
	addq	$16, %rsp
	.cfi_def_cfa_offset 32
	movq	%rbp, %rax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2041:
	.size	_Z8readFileP8_IO_FILEi, .-_Z8readFileP8_IO_FILEi
	.globl	_Z18upper_power_of_twom
	.type	_Z18upper_power_of_twom, @function
_Z18upper_power_of_twom:
.LFB2042:
	.cfi_startproc
	endbr64
	decq	%rdi
	movq	%rdi, %rax
	shrq	%rax
	orq	%rdi, %rax
	movq	%rax, %rdi
	shrq	$2, %rdi
	orq	%rax, %rdi
	movq	%rdi, %rax
	shrq	$4, %rax
	orq	%rdi, %rax
	movq	%rax, %rdx
	shrq	$8, %rdx
	orq	%rax, %rdx
	movq	%rdx, %rax
	shrq	$16, %rax
	orq	%rdx, %rax
	incq	%rax
	ret
	.cfi_endproc
.LFE2042:
	.size	_Z18upper_power_of_twom, .-_Z18upper_power_of_twom
	.globl	_Z49__device_stub__Z16gridding_processPfS_S_S_iifS_S_PfS_S_S_iifS_S_
	.type	_Z49__device_stub__Z16gridding_processPfS_S_S_iifS_S_PfS_S_S_iifS_S_, @function
_Z49__device_stub__Z16gridding_processPfS_S_S_iifS_S_PfS_S_S_iifS_S_:
.LFB2068:
	.cfi_startproc
	endbr64
	subq	$216, %rsp
	.cfi_def_cfa_offset 224
	movq	224(%rsp), %rax
	movq	%rdi, 56(%rsp)
	leaq	80(%rsp), %rdi
	movq	%rsi, 48(%rsp)
	leaq	92(%rsp), %rsi
	movq	%rax, 8(%rsp)
	movq	232(%rsp), %rax
	movq	%rdx, 40(%rsp)
	leaq	64(%rsp), %rdx
	movq	%rcx, 32(%rsp)
	leaq	72(%rsp), %rcx
	movl	%r8d, 28(%rsp)
	movl	%r9d, 24(%rsp)
	movq	%rax, (%rsp)
	movss	%xmm0, 20(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 200(%rsp)
	xorl	%eax, %eax
	leaq	56(%rsp), %rax
	movl	$1, 88(%rsp)
	movq	%rax, 128(%rsp)
	leaq	48(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	40(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	28(%rsp), %rax
	movq	%rax, 160(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 168(%rsp)
	leaq	20(%rsp), %rax
	movq	%rax, 176(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 184(%rsp)
	movq	%rsp, %rax
	movq	%rax, 192(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 80(%rsp)
	movq	%rax, 92(%rsp)
	movl	$1, 100(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L6
	pushq	72(%rsp)
	.cfi_def_cfa_offset 232
	leaq	_Z16gridding_processPfS_S_S_iifS_S_(%rip), %rdi
	pushq	72(%rsp)
	.cfi_def_cfa_offset 240
	movq	108(%rsp), %rcx
	movl	116(%rsp), %r8d
	movq	96(%rsp), %rsi
	movl	104(%rsp), %edx
	leaq	144(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 232
	popq	%rdx
	.cfi_def_cfa_offset 224
.L6:
	movq	200(%rsp), %rax
	subq	%fs:40, %rax
	je	.L8
	call	__stack_chk_fail@PLT
.L8:
	addq	$216, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2068:
	.size	_Z49__device_stub__Z16gridding_processPfS_S_S_iifS_S_PfS_S_S_iifS_S_, .-_Z49__device_stub__Z16gridding_processPfS_S_S_iifS_S_PfS_S_S_iifS_S_
	.globl	_Z16gridding_processPfS_S_S_iifS_S_
	.type	_Z16gridding_processPfS_S_S_iifS_S_, @function
_Z16gridding_processPfS_S_S_iifS_S_:
.LFB2069:
	.cfi_startproc
	endbr64
	jmp	_Z49__device_stub__Z16gridding_processPfS_S_S_iifS_S_PfS_S_S_iifS_S_
	.cfi_endproc
.LFE2069:
	.size	_Z16gridding_processPfS_S_S_iifS_S_, .-_Z16gridding_processPfS_S_S_iifS_S_
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"Opcion -%c requiere un argumento.\n"
.LC2:
	.string	"Opcion desconocida `-%c'.\n"
.LC3:
	.string	"Carater opcion desconocido `\\x%x'.\n"
.LC4:
	.string	"i:z:d:N:o:"
.LC5:
	.string	"El parametro -N debe existir y ser mayor que 0\n"
.LC6:
	.string	"El parametro -z debe existir y ser mayor que 0\n"
.LC8:
	.string	"El parametro -d debe existir y ser mayor que 0\n"
.LC9:
	.string	"Debe especificarse un archivo de entrada\n"
.LC10:
	.string	"Debe especificarse un archivo de salida\n"
.LC11:
	.string	"r"
.LC13:
	.string	"real.raw"
.LC14:
	.string	"wb"
.LC15:
	.string	"img.raw"
.LC17:
	.string	"Total = %f\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2043:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	xorl	%ecx, %ecx
	leaq	.LC4(%rip), %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%rsi, %r14
	xorl	%esi, %esi
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movl	%edi, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$168, %rsp
	.cfi_def_cfa_offset 224
	movq	%fs:40, %rax
	movq	%rax, 152(%rsp)
	xorl	%eax, %eax
	movl	%ecx, opterr(%rip)
	movq	%rsi, 24(%rsp)
.L13:
	movq	%r15, %rdx
	movq	%r14, %rsi
	movl	%r13d, %edi
	call	getopt@PLT
	cmpl	$-1, %eax
	je	.L45
	cmpl	$105, %eax
	je	.L14
	jg	.L15
	cmpl	$78, %eax
	je	.L16
	cmpl	$100, %eax
	je	.L17
	cmpl	$63, %eax
	je	.L18
	jmp	.L19
.L15:
	cmpl	$111, %eax
	je	.L20
	cmpl	$122, %eax
	jne	.L19
	movq	optarg(%rip), %rdi
	call	atoi@PLT
	movl	%eax, %ebp
	jmp	.L13
.L14:
	movq	optarg(%rip), %r12
	jmp	.L13
.L17:
	movq	optarg(%rip), %rdi
	call	atof@PLT
	cvtsd2ss	%xmm0, %xmm2
	movss	%xmm2, 60(%rsp)
	jmp	.L13
.L16:
	movq	optarg(%rip), %rdi
	call	atoi@PLT
	movl	%eax, %ebx
	jmp	.L13
.L20:
	movq	optarg(%rip), %rax
	movq	%rax, 24(%rsp)
	jmp	.L13
.L18:
	movl	optopt(%rip), %ecx
	movq	stderr(%rip), %rbx
	leal	-78(%rcx), %eax
	cmpl	$44, %eax
	ja	.L23
	movabsq	$17600914391041, %rdx
	btq	%rax, %rdx
	jnc	.L23
	leaq	.LC1(%rip), %rdx
.L43:
	movl	$2, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	$1, %eax
	jmp	.L12
.L23:
	movl	%ecx, %edi
	movl	%ecx, 8(%rsp)
	call	isprint@PLT
	movl	8(%rsp), %ecx
	leaq	.LC2(%rip), %rdx
	testl	%eax, %eax
	jne	.L43
	leaq	.LC3(%rip), %rdx
	jmp	.L43
.L19:
	call	abort@PLT
.L45:
	leaq	.LC5(%rip), %rsi
	testl	%ebx, %ebx
	jle	.L44
	testl	%ebp, %ebp
	jne	.L29
	leaq	.LC6(%rip), %rsi
.L44:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L29:
	movss	60(%rsp), %xmm3
	xorps	%xmm0, %xmm0
	ucomiss	%xmm0, %xmm3
	jp	.L30
	leaq	.LC8(%rip), %rsi
	je	.L44
.L30:
	testq	%r12, %r12
	jne	.L32
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L32:
	cmpq	$0, 24(%rsp)
	jne	.L33
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L33:
	call	clock@PLT
	movq	%r12, %rdi
	leaq	.LC11(%rip), %rsi
	movq	%rax, timestart(%rip)
	call	fopen@PLT
	movl	%ebp, %esi
	movq	%rax, %r12
	movq	%rax, %rdi
	call	_Z8readFileP8_IO_FILEi
	movq	%r12, %rdi
	movslq	%ebp, %r12
	movq	%rax, 72(%rsp)
	call	fclose@PLT
	leaq	0(,%r12,4), %rax
	movq	%rax, %rdi
	movq	%rax, 8(%rsp)
	call	malloc@PLT
	leaq	0(,%r12,4), %rdi
	movq	%rax, %r14
	call	malloc@PLT
	leaq	0(,%r12,4), %rdi
	movq	%rax, %r13
	call	malloc@PLT
	leaq	0(,%r12,4), %rdi
	movq	%rax, 64(%rsp)
	call	malloc@PLT
	movq	%rax, %r15
	movslq	%ebx, %rax
	imulq	%rax, %rax
	movq	%rax, 48(%rsp)
	salq	$2, %rax
	movq	%rax, %rdi
	movq	%rax, 16(%rsp)
	call	malloc@PLT
	movq	16(%rsp), %rdi
	movq	%rax, 32(%rsp)
	call	malloc@PLT
	movq	72(%rsp), %rcx
	movq	%r12, %rsi
	movq	%rax, 40(%rsp)
	salq	$4, %rsi
	xorl	%eax, %eax
	leaq	(%rcx,%r12,8), %rdx
.L34:
	cmpl	%eax, %ebp
	jle	.L46
	movq	64(%rsp), %rdi
	cvtsd2ss	(%rcx,%rax,8), %xmm0
	movss	%xmm0, (%r14,%rax,4)
	cvtsd2ss	(%rdx), %xmm0
	movss	%xmm0, 0(%r13,%rax,4)
	cvtsd2ss	(%rdx,%r12,8), %xmm0
	addq	$8, %rdx
	movss	%xmm0, (%rdi,%rax,4)
	cvtsd2ss	-8(%rdx,%rsi), %xmm0
	movss	%xmm0, (%r15,%rax,4)
	incq	%rax
	jmp	.L34
.L46:
	movl	%ebx, %edx
	movq	32(%rsp), %rdi
	xorl	%eax, %eax
	movq	8(%rsp), %rsi
	imull	%ebx, %edx
	salq	$2, %rdx
	movq	%rdx, %rcx
	rep stosb
	movq	40(%rsp), %rdi
	movq	%rdx, %rcx
	rep stosb
	leaq	80(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	8(%rsp), %rsi
	leaq	88(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	8(%rsp), %rsi
	leaq	96(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	8(%rsp), %rsi
	leaq	104(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	16(%rsp), %rsi
	leaq	112(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	16(%rsp), %rsi
	leaq	120(%rsp), %rdi
	call	cudaMalloc@PLT
	movq	8(%rsp), %rdx
	movq	80(%rsp), %rdi
	movq	%r14, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rdx
	movq	88(%rsp), %rdi
	movq	%r13, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rdx
	movq	64(%rsp), %rsi
	movl	$1, %ecx
	movq	96(%rsp), %rdi
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rdx
	movq	104(%rsp), %rdi
	movq	%r15, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	16(%rsp), %rdx
	movq	32(%rsp), %rsi
	movl	$1, %ecx
	movq	112(%rsp), %rdi
	call	cudaMemcpy@PLT
	movq	16(%rsp), %rdx
	movq	40(%rsp), %rsi
	movl	$1, %ecx
	movq	120(%rsp), %rdi
	call	cudaMemcpy@PLT
	movq	%r12, %rdi
	movl	$4, %ecx
	xorl	%r9d, %r9d
	call	_Z18upper_power_of_twom
	xorl	%r8d, %r8d
	cqto
	idivq	%rcx
	movl	$1073741825, %edx
	movl	$1, %ecx
	salq	$2, %rdx
	movl	%eax, 140(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 144(%rsp)
	movq	140(%rsp), %rdi
	movl	148(%rsp), %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L36
	cvtss2sd	60(%rsp), %xmm0
	cvtsi2ssl	%ebx, %xmm1
	pushq	120(%rsp)
	.cfi_def_cfa_offset 232
	movl	%ebx, %r9d
	mulsd	.LC0(%rip), %xmm0
	pushq	120(%rsp)
	.cfi_def_cfa_offset 240
	movl	%ebp, %r8d
	movq	120(%rsp), %rcx
	movq	112(%rsp), %rdx
	movq	104(%rsp), %rsi
	movq	96(%rsp), %rdi
	cvtsd2ss	%xmm0, %xmm0
	mulss	%xmm0, %xmm1
	movss	.LC12(%rip), %xmm0
	divss	%xmm1, %xmm0
	call	_Z49__device_stub__Z16gridding_processPfS_S_S_iifS_S_PfS_S_S_iifS_S_
	popq	%rax
	.cfi_def_cfa_offset 232
	popq	%rdx
	.cfi_def_cfa_offset 224
.L36:
	call	cudaDeviceSynchronize@PLT
	movq	16(%rsp), %rdx
	movq	112(%rsp), %rsi
	movl	$2, %ecx
	movq	32(%rsp), %rdi
	leaq	.LC14(%rip), %r12
	call	cudaMemcpy@PLT
	movq	16(%rsp), %rdx
	movq	120(%rsp), %rsi
	movl	$2, %ecx
	movq	40(%rsp), %rdi
	call	cudaMemcpy@PLT
	movq	80(%rsp), %rdi
	call	cudaFree@PLT
	movq	88(%rsp), %rdi
	call	cudaFree@PLT
	movq	96(%rsp), %rdi
	call	cudaFree@PLT
	movq	104(%rsp), %rdi
	call	cudaFree@PLT
	movq	112(%rsp), %rdi
	call	cudaFree@PLT
	movq	120(%rsp), %rdi
	call	cudaFree@PLT
	movl	$16, %edi
	call	malloc@PLT
	movq	24(%rsp), %rsi
	movl	$16, %edx
	movq	%rax, %rdi
	movq	%rax, %rbx
	call	__strcpy_chk@PLT
	movq	24(%rsp), %rdi
	leaq	.LC13(%rip), %rsi
	call	strcat@PLT
	movq	%r12, %rsi
	movq	%rax, %rdi
	call	fopen@PLT
	movl	$16, %edx
	movq	%rbx, %rdi
	leaq	.LC15(%rip), %rsi
	movq	%rax, %rbp
	call	__strcat_chk@PLT
	movq	%rbx, %rdi
	movq	%r12, %rsi
	call	fopen@PLT
	movq	48(%rsp), %rsi
	movq	32(%rsp), %rdi
	movq	%rbp, %rcx
	movl	$4, %edx
	movq	%rax, %rbx
	call	fwrite@PLT
	movq	48(%rsp), %rsi
	movq	40(%rsp), %rdi
	movq	%rbx, %rcx
	movl	$4, %edx
	call	fwrite@PLT
	movq	%rbp, %rdi
	call	fclose@PLT
	movq	%rbx, %rdi
	call	fclose@PLT
	call	clock@PLT
	leaq	.LC17(%rip), %rsi
	movl	$2, %edi
	movq	%rax, timeend(%rip)
	subq	timestart(%rip), %rax
	cvtsi2sdq	%rax, %xmm0
	movb	$1, %al
	divsd	.LC16(%rip), %xmm0
	call	__printf_chk@PLT
	xorl	%eax, %eax
.L12:
	movq	152(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L37
	call	__stack_chk_fail@PLT
.L37:
	addq	$168, %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
.LFE2043:
	.size	main, .-main
	.section	.rodata.str1.1
.LC18:
	.string	"_Z16gridding_processPfS_S_S_iifS_S_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2071:
	.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	.LC18(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z16gridding_processPfS_S_S_iifS_S_(%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
.LFE2071:
	.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	timeend
	.bss
	.align 8
	.type	timeend, @object
	.size	timeend, 8
timeend:
	.zero	8
	.globl	timestart
	.align 8
	.type	timestart, @object
	.size	timestart, 8
timestart:
	.zero	8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC0:
	.long	-1822047602
	.long	1054102950
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC12:
	.long	1065353216
	.section	.rodata.cst8
	.align 8
.LC16:
	.long	0
	.long	1093567616
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/times.h>
#include <time.h>
#include <math.h>
#include <hip/hip_runtime.h>
#define PI 3.14159265358979323846
#define FactorArcosegRad 0.00000484814
#define BLOQUESIZE 4

clock_t timestart, timeend;

/**
@brief Función que transforma un valor en arco segundo a radianes
@param deltax: Valor numérico a transformar
@returns Valor correspondiente a la entrada en radianes */
float arcoseg_radian(float deltax){
	return FactorArcosegRad*deltax;
}

/**
@brief Función que lee el archivo de entrada
@param archivo: puntero al archivo a leer
@param tamano: Numero de visibilidades del archivo a leer
@returns  */
double* readFile(FILE* archivo, int tamano){
	double* elementos =(double*) malloc(sizeof(double)*4*tamano);
	fread(elementos, tamano*4, sizeof(double), archivo);
	return elementos;
}


/**
@brief Función ejecuta el proceso de gridding
@param U: Valores de la coordenada U en el plano de Fourier
@param V: Valores de la coordenada V en el plano de Fourier
@param R: Valores reales de la visibilidad en el plano de Fourier
@param I: Valores imaginarios la visibilidad en el plano de Fourier
@param num_datos: Cantidad de visibilidades ingresadas o dimensión de los vectores anteriores
@param tamano: Lado de la matriz a construir, si tamano es 512 se construye una matriz de 512X512
@param V: Valores de la coordenada V en el plano de Fourier
@param deltaU: Valor delta necesario para determinar la vecindad de cada pixel de la grilla regular
@param r: vector de valores reales de la salida del proceso de gridding
@param k: vector de valores imaginarios de la salida del proceso de gridding
@returns  */
__global__ void gridding_process(float *U, float *V, float *R, float *I, int num_datos, int tamano, float deltaU, float *r, float *k)
{
	int i = blockDim.x * blockIdx.x + threadIdx.x;
	if(i<num_datos)
	{
		float x, y, modx, mody;
		x = U[i]/deltaU+tamano/2;
		y = V[i]/deltaU+tamano/2;
		modx = U[i] - x*deltaU;
		mody = V[i] - y*deltaU;

		if(modx>deltaU/2){	
			x+=1;
		}

		if (mody>deltaU/2)
		{
			y+=1;
		}

		if ((int)x<tamano && (int)y<tamano)
		{
			atomicAdd(&r[(int)y*tamano+(int)x], R[i]);
			atomicAdd(&k[(int)y*tamano+(int)x], I[i]);
		}
	}
}

__host__ unsigned long upper_power_of_two(unsigned long v)
{
    v--;
    v |= v >> 1;
    v |= v >> 2;
    v |= v >> 4;
    v |= v >> 8;
    v |= v >> 16;
    v++;
    return v;

}

int main(int argc, char * const argv[])
{
	int tamano;//tamaño de imagen
	int numdatos;//número de pasos
	float deltaX_arcoseg, deltaX_radian;
	float deltaU; 
	char* archivo_entrada=NULL;
	char* archivo_salida=NULL;
	char* archivo_salida_i;
	int i, c;
	opterr = 0;
	while ((c = getopt (argc, argv, "i:z:d:N:o:")) != -1)
		switch (c)
			{
			case 'i':
				archivo_entrada = optarg;
				break;
			case 'z':
				numdatos = atoi(optarg);
				break;
			case 'd':
				deltaX_arcoseg = atof(optarg);
				break;
			case 'N':
				tamano = atoi(optarg);
				break;
			case 'o':
				archivo_salida = optarg;
				break;
			case '?':
				if (optopt == 'i' ||optopt == 'z' ||optopt == 'd'||optopt == 'N' ||optopt == 'o')
					fprintf (stderr, "Opcion -%c requiere un argumento.\n", optopt);
				else if (isprint (optopt))
					fprintf (stderr, "Opcion desconocida `-%c'.\n", optopt);
				else
					fprintf (stderr,
									"Carater opcion desconocido `\\x%x'.\n",
									optopt);
				return 1;
			default:
				abort ();
			}
	/**
		Comprobación de Inputs
			- Valores mayores que cero
			- Cadenas no nulas
	**/
	if(tamano<=0){
		printf("El parametro -N debe existir y ser mayor que 0\n");
		exit(1);
	}
	if(numdatos==0){
		printf("El parametro -z debe existir y ser mayor que 0\n");
		exit(1);
	}
	if(deltaX_arcoseg==0){
		printf("El parametro -d debe existir y ser mayor que 0\n");
		exit(1);
	}
	if(archivo_entrada==NULL){
		printf("Debe especificarse un archivo de entrada\n");
	}
	if(archivo_salida==NULL){
		printf("Debe especificarse un archivo de salida\n");
	}
	//Transformacion de unidades necesaria para calcular delta U
	deltaX_radian = arcoseg_radian(deltaX_arcoseg);

	//Determina delta U/V a utilizar
	deltaU = 1/(tamano*deltaX_radian);

	//Medición de tiempo de computo
	timestart = clock(); 

	//Lectura de entrada
	FILE *entrada = fopen(archivo_entrada,"r");
	double* data = readFile(entrada,numdatos);
	fclose(entrada);

	//Creando arrays para coordenada X, Y, R e I
	float *X = (float*)malloc(sizeof(float)*numdatos); 
	float *Y = (float*)malloc(sizeof(float)*numdatos); 
	float *R = (float*)malloc(sizeof(float)*numdatos); 
	float *I = (float*)malloc(sizeof(float)*numdatos);	
	//Quizas necesite dos vectores adicionales para el gridding [matrices desenroyadas]
	float *r = (float*)malloc(sizeof(float)*tamano*tamano);
	float *k = (float*)malloc(sizeof(float)*tamano*tamano);
	//Se asigan los valores correspondientes de la lectura
	for (i = 0; i < numdatos; i++)
	{
		X[i] = (float)data[i];
		Y[i] = (float)data[i+numdatos];
		R[i] = (float)data[i+2*numdatos];
		I[i] = (float)data[i+3*numdatos];

	}
	for (i = 0; i < tamano*tamano; ++i)
	{
		r[i] = 0;
		k[i] = 0;
	}
	//se declaran las variables CUDA
	float *C_X;
	float *C_Y;
	float *C_R;
	float *C_I;
	float *C_r;
	float *C_k;
	//Se reserva memoria CUDA
	hipMalloc( (void**)&C_X, numdatos*sizeof(float)); 
	hipMalloc( (void**)&C_Y, numdatos*sizeof(float)); 
	hipMalloc( (void**)&C_R, numdatos*sizeof(float)); 
	hipMalloc( (void**)&C_I, numdatos*sizeof(float)); 
	hipMalloc( (void**)&C_r, tamano*tamano*sizeof(float)); 
	hipMalloc( (void**)&C_k, tamano*tamano*sizeof(float)); 
	//se copia la matriz iniciada en las matrices de trabajo en memoria global GPU
	hipMemcpy( C_X, X, numdatos*sizeof(float), hipMemcpyHostToDevice); 
	hipMemcpy( C_Y, Y, numdatos*sizeof(float), hipMemcpyHostToDevice); 
	hipMemcpy( C_R, R, numdatos*sizeof(float), hipMemcpyHostToDevice); 
	hipMemcpy( C_I, I, numdatos*sizeof(float), hipMemcpyHostToDevice); 
	hipMemcpy( C_r, r, tamano*tamano*sizeof(float), hipMemcpyHostToDevice); 
	hipMemcpy( C_k, k, tamano*tamano*sizeof(float), hipMemcpyHostToDevice); 
	//determino dimension para el kernel
	long data_size_2 = upper_power_of_two(numdatos);
	//Se declaran las dimenciones
	dim3 dimBlock(BLOQUESIZE, 1);
	dim3 dimGrid(data_size_2/BLOQUESIZE, 1);
	//se ejecuta el kernel en la GPU
	//printf("%d - %d - %d\n", numdatos, kernel_size, kernel_size/BLOQUESIZE);

	gridding_process<<<dimGrid, dimBlock>>>(C_X, C_Y, C_R, C_I, numdatos, tamano, deltaU, C_r, C_k);
	//se espera a que terminen
	hipDeviceSynchronize();
	//se obtiene la memoria de regreso
	hipMemcpy( r, C_r, tamano*tamano*sizeof(float), hipMemcpyDeviceToHost); 
	hipMemcpy( k, C_k, tamano*tamano*sizeof(float), hipMemcpyDeviceToHost); 
	//se libera la memoria global CUDA para que pueda ser usada por otro proceso
	hipFree( C_X );
	hipFree( C_Y );
	hipFree( C_R );
	hipFree( C_I );
	hipFree( C_r );
	hipFree( C_k );
	//Se imprime salida
	archivo_salida_i = (char*)malloc(sizeof(archivo_salida)*2);
	strcpy(archivo_salida_i, archivo_salida);
	FILE *f = fopen(strcat(archivo_salida, "real.raw"),"wb");
	FILE *g = fopen(strcat(archivo_salida_i, "img.raw"),"wb");
	fwrite(r, tamano*tamano, sizeof(float),f);
	fwrite(k, tamano*tamano, sizeof(float),g);
	fclose(f);
	fclose(g);
	//Se mide el tiempo utilizado
	timeend = clock();
	printf("Total = %f\n", (double) (timeend-timestart)/(double)CLOCKS_PER_SEC);
	return EXIT_SUCCESS;
}
	.text
	.file	"gridding.hip"
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z14arcoseg_radianf
.LCPI0_0:
	.quad	0x3ed455a69365c28e              # double 4.8481399999999996E-6
	.text
	.globl	_Z14arcoseg_radianf
	.type	_Z14arcoseg_radianf,@function
_Z14arcoseg_radianf:                    # @_Z14arcoseg_radianf
	.cfi_startproc
# %bb.0:
	cvtss2sd	%xmm0, %xmm0
	mulsd	.LCPI0_0(%rip), %xmm0
	cvtsd2ss	%xmm0, %xmm0
	retq
.Lfunc_end0:
	.size	_Z14arcoseg_radianf, .Lfunc_end0-_Z14arcoseg_radianf
	.cfi_endproc
                                        # -- End function
	.globl	_Z8readFileP8_IO_FILEi          # -- Begin function _Z8readFileP8_IO_FILEi
	.type	_Z8readFileP8_IO_FILEi,@function
_Z8readFileP8_IO_FILEi:                 # @_Z8readFileP8_IO_FILEi
	.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
	movslq	%esi, %r15
	movq	%r15, %rdi
	shlq	$5, %rdi
	callq	malloc
	movq	%rax, %r14
	shll	$2, %r15d
	movslq	%r15d, %rsi
	movl	$8, %edx
	movq	%rax, %rdi
	movq	%rbx, %rcx
	callq	fread
	movq	%r14, %rax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z8readFileP8_IO_FILEi, .Lfunc_end1-_Z8readFileP8_IO_FILEi
	.cfi_endproc
                                        # -- End function
	.globl	_Z31__device_stub__gridding_processPfS_S_S_iifS_S_ # -- Begin function _Z31__device_stub__gridding_processPfS_S_S_iifS_S_
	.type	_Z31__device_stub__gridding_processPfS_S_S_iifS_S_,@function
_Z31__device_stub__gridding_processPfS_S_S_iifS_S_: # @_Z31__device_stub__gridding_processPfS_S_S_iifS_S_
	.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	$176, %rsp
	.cfi_def_cfa_offset 224
	.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	40(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	32(%rsp), %rdx
	movq	%rcx, (%rdx)
	leaq	12(%rsp), %rcx
	movl	%r8d, (%rcx)
	leaq	8(%rsp), %r8
	movl	%r9d, (%r8)
	leaq	4(%rsp), %r9
	movss	%xmm0, (%r9)
	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)
	movq	%r9, 48(%rbx)
	leaq	224(%rsp), %rax
	movq	%rax, 56(%rbx)
	leaq	232(%rsp), %rax
	movq	%rax, 64(%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	$_Z16gridding_processPfS_S_S_iifS_S_, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$192, %rsp
	.cfi_adjust_cfa_offset -192
	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	_Z31__device_stub__gridding_processPfS_S_S_iifS_S_, .Lfunc_end2-_Z31__device_stub__gridding_processPfS_S_S_iifS_S_
	.cfi_endproc
                                        # -- End function
	.globl	_Z18upper_power_of_twom         # -- Begin function _Z18upper_power_of_twom
	.type	_Z18upper_power_of_twom,@function
_Z18upper_power_of_twom:                # @_Z18upper_power_of_twom
	.cfi_startproc
# %bb.0:
	leaq	-1(%rdi), %rax
	movq	%rax, %rcx
	shrq	%rcx
	orq	%rax, %rcx
	movq	%rcx, %rax
	shrq	$2, %rax
	orq	%rcx, %rax
	movq	%rax, %rcx
	shrq	$4, %rcx
	orq	%rax, %rcx
	movq	%rcx, %rdx
	shrq	$8, %rdx
	orq	%rcx, %rdx
	movq	%rdx, %rax
	shrq	$16, %rax
	orq	%rdx, %rax
	incq	%rax
	retq
.Lfunc_end3:
	.size	_Z18upper_power_of_twom, .Lfunc_end3-_Z18upper_power_of_twom
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI4_0:
	.quad	0x3ed455a69365c28e              # double 4.8481399999999996E-6
.LCPI4_2:
	.quad	0x412e848000000000              # double 1.0E+6
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0
.LCPI4_1:
	.long	0x3f800000                      # float 1
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.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	$120, %rsp
	.cfi_def_cfa_offset 176
	.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	%rsi, %rbx
	movl	%edi, %r14d
	movl	$0, opterr(%rip)
	xorl	%r12d, %r12d
	xorl	%r15d, %r15d
                                        # implicit-def: $xmm0
                                        # kill: killed $xmm0
                                        # implicit-def: $r13d
                                        # implicit-def: $ebp
.LBB4_1:                                # =>This Inner Loop Header: Depth=1
	movl	$.L.str, %edx
	movl	%r14d, %edi
	movq	%rbx, %rsi
	callq	getopt
	cmpl	$78, %eax
	jne	.LBB4_2
# %bb.15:                               #   in Loop: Header=BB4_1 Depth=1
	movq	optarg(%rip), %rdi
	callq	atoi
	movl	%eax, %ebp
	jmp	.LBB4_1
.LBB4_2:                                #   in Loop: Header=BB4_1 Depth=1
	cmpl	$104, %eax
	jle	.LBB4_3
# %bb.9:                                #   in Loop: Header=BB4_1 Depth=1
	cmpl	$122, %eax
	je	.LBB4_13
# %bb.10:                               #   in Loop: Header=BB4_1 Depth=1
	cmpl	$111, %eax
	je	.LBB4_16
# %bb.11:                               #   in Loop: Header=BB4_1 Depth=1
	cmpl	$105, %eax
	jne	.LBB4_21
# %bb.12:                               #   in Loop: Header=BB4_1 Depth=1
	movq	optarg(%rip), %r15
	jmp	.LBB4_1
.LBB4_3:                                #   in Loop: Header=BB4_1 Depth=1
	cmpl	$-1, %eax
	je	.LBB4_22
# %bb.4:                                #   in Loop: Header=BB4_1 Depth=1
	cmpl	$100, %eax
	jne	.LBB4_5
# %bb.14:                               #   in Loop: Header=BB4_1 Depth=1
	movq	optarg(%rip), %rdi
	callq	atof
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, 12(%rsp)                 # 4-byte Spill
	jmp	.LBB4_1
.LBB4_13:                               #   in Loop: Header=BB4_1 Depth=1
	movq	optarg(%rip), %rdi
	callq	atoi
	movl	%eax, %r13d
	jmp	.LBB4_1
.LBB4_16:                               #   in Loop: Header=BB4_1 Depth=1
	movq	optarg(%rip), %r12
	jmp	.LBB4_1
.LBB4_22:
	testl	%ebp, %ebp
	jle	.LBB4_23
# %bb.25:
	testl	%r13d, %r13d
	je	.LBB4_26
# %bb.27:
	xorps	%xmm0, %xmm0
	movss	12(%rsp), %xmm1                 # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	ucomiss	%xmm0, %xmm1
	jne	.LBB4_29
	jnp	.LBB4_28
.LBB4_29:
	testq	%r15, %r15
	jne	.LBB4_31
# %bb.30:
	movl	$.Lstr, %edi
	callq	puts@PLT
.LBB4_31:
	movq	%r12, 112(%rsp)                 # 8-byte Spill
	testq	%r12, %r12
	jne	.LBB4_33
# %bb.32:
	movl	$.Lstr.1, %edi
	callq	puts@PLT
.LBB4_33:
	callq	clock
	movq	%rax, timestart(%rip)
	movl	$.L.str.9, %esi
	movq	%r15, %rdi
	callq	fopen
	movq	%rax, %rbx
	movslq	%r13d, %r14
	movq	%r14, %rdi
	shlq	$5, %rdi
	callq	malloc
	movq	%rax, %r15
	leal	(,%r13,4), %eax
	movslq	%eax, %rsi
	movl	$8, %edx
	movq	%r15, %rdi
	movq	%rbx, %rcx
	callq	fread
	movq	%rbx, %rdi
	callq	fclose
	movq	%r14, 96(%rsp)                  # 8-byte Spill
	leaq	(,%r14,4), %rbx
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, %r12
	movq	%rbx, %rdi
	callq	malloc
	movq	%r13, 32(%rsp)                  # 8-byte Spill
	movq	%rax, %r13
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, %r14
	movq	%rbx, 88(%rsp)                  # 8-byte Spill
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, %rbx
	movl	%ebp, 8(%rsp)                   # 4-byte Spill
	movl	%ebp, %ebp
	imulq	%rbp, %rbp
	shlq	$2, %rbp
	movq	%rbp, %rdi
	callq	malloc
	movq	%rax, 80(%rsp)                  # 8-byte Spill
	movq	%rbp, %rdi
	callq	malloc
	movq	%rax, 72(%rsp)                  # 8-byte Spill
	cmpl	$0, 32(%rsp)                    # 4-byte Folded Reload
	jle	.LBB4_36
# %bb.34:                               # %.lr.ph
	movq	32(%rsp), %rax                  # 8-byte Reload
	leal	(%rax,%rax), %edx
	leal	(%rax,%rax,2), %esi
	movl	%eax, %eax
	leaq	(%r15,%rax,8), %rcx
	leaq	(%r15,%rdx,8), %rdx
	leaq	(%r15,%rsi,8), %rsi
	shlq	$2, %rax
	xorl	%edi, %edi
.LBB4_35:                               # =>This Inner Loop Header: Depth=1
	xorps	%xmm0, %xmm0
	cvtsd2ss	(%r15,%rdi,2), %xmm0
	xorps	%xmm1, %xmm1
	cvtsd2ss	(%rcx,%rdi,2), %xmm1
	movss	%xmm0, (%r12,%rdi)
	xorps	%xmm0, %xmm0
	cvtsd2ss	(%rdx,%rdi,2), %xmm0
	movss	%xmm1, (%r13,%rdi)
	xorps	%xmm1, %xmm1
	cvtsd2ss	(%rsi,%rdi,2), %xmm1
	movss	%xmm0, (%r14,%rdi)
	movss	%xmm1, (%rbx,%rdi)
	addq	$4, %rdi
	cmpq	%rdi, %rax
	jne	.LBB4_35
.LBB4_36:                               # %.preheader
	movl	8(%rsp), %eax                   # 4-byte Reload
	movl	%eax, %ebp
	imull	%ebp, %ebp
	cmpq	$1, %rbp
	movq	%rbp, %r15
	adcq	$0, %r15
	shlq	$2, %r15
	movq	80(%rsp), %rdi                  # 8-byte Reload
	xorl	%esi, %esi
	movq	%r15, %rdx
	callq	memset@PLT
	movq	72(%rsp), %rdi                  # 8-byte Reload
	xorl	%esi, %esi
	movq	%r15, %rdx
	callq	memset@PLT
	leaq	64(%rsp), %rdi
	movq	88(%rsp), %r15                  # 8-byte Reload
	movq	%r15, %rsi
	callq	hipMalloc
	leaq	56(%rsp), %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	leaq	48(%rsp), %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	leaq	40(%rsp), %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	movq	%rbp, 104(%rsp)                 # 8-byte Spill
	leaq	(,%rbp,4), %rbp
	leaq	24(%rsp), %rdi
	movq	%rbp, %rsi
	callq	hipMalloc
	leaq	16(%rsp), %rdi
	movq	%rbp, %rsi
	callq	hipMalloc
	leaq	64(%rsp), %rax
	movq	(%rax), %rdi
	movq	%r12, %rsi
	movq	80(%rsp), %r12                  # 8-byte Reload
	movq	%r15, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	56(%rsp), %rax
	movq	(%rax), %rdi
	movq	%r13, %rsi
	movq	%r15, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	48(%rsp), %rax
	movq	(%rax), %rdi
	movq	%r14, %rsi
	movq	%r15, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	40(%rsp), %rax
	movq	(%rax), %rdi
	movq	%rbx, %rsi
	movq	%r15, %rdx
	movq	72(%rsp), %r15                  # 8-byte Reload
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	24(%rsp), %rax
	movq	(%rax), %rdi
	movq	%r12, %rsi
	movq	%rbp, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	16(%rsp), %rax
	movq	(%rax), %rdi
	movq	%r15, %rsi
	movq	%rbp, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	96(%rsp), %rcx                  # 8-byte Reload
	decq	%rcx
	movq	%rcx, %rax
	shrq	%rax
	orq	%rcx, %rax
	movq	%rax, %rcx
	shrq	$2, %rcx
	orq	%rax, %rcx
	movq	%rcx, %rax
	shrq	$4, %rax
	orq	%rcx, %rax
	movq	%rax, %rcx
	shrq	$8, %rcx
	orq	%rax, %rcx
	movq	%rcx, %rax
	shrq	$16, %rax
	orq	%rcx, %rax
	leaq	1(%rax), %rcx
	addq	$4, %rax
	testq	%rcx, %rcx
	cmovnsq	%rcx, %rax
	shrq	$2, %rax
	movl	%eax, %edi
	btsq	$32, %rdi
	movabsq	$4294967296, %rdx               # imm = 0x100000000
	orq	$4, %rdx
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB4_38
# %bb.37:
	cvtss2sd	12(%rsp), %xmm0                 # 4-byte Folded Reload
	mulsd	.LCPI4_0(%rip), %xmm0
	cvtsd2ss	%xmm0, %xmm0
	movl	8(%rsp), %r9d                   # 4-byte Reload
	cvtsi2ss	%r9d, %xmm1
	mulss	%xmm0, %xmm1
	movss	.LCPI4_1(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
	divss	%xmm1, %xmm0
	movq	64(%rsp), %rdi
	movq	56(%rsp), %rsi
	movq	48(%rsp), %rdx
	movq	40(%rsp), %rcx
	movq	32(%rsp), %r8                   # 8-byte Reload
                                        # kill: def $r8d killed $r8d killed $r8
	pushq	16(%rsp)
	.cfi_adjust_cfa_offset 8
	pushq	32(%rsp)
	.cfi_adjust_cfa_offset 8
	callq	_Z31__device_stub__gridding_processPfS_S_S_iifS_S_
	addq	$16, %rsp
	.cfi_adjust_cfa_offset -16
.LBB4_38:
	callq	hipDeviceSynchronize
	movq	24(%rsp), %rsi
	movq	%r12, %rdi
	movq	%rbp, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	16(%rsp), %rsi
	movq	%r15, %rdi
	movq	%rbp, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	64(%rsp), %rdi
	callq	hipFree
	movq	56(%rsp), %rdi
	callq	hipFree
	movq	48(%rsp), %rdi
	callq	hipFree
	movq	40(%rsp), %rdi
	callq	hipFree
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	movl	$16, %edi
	callq	malloc
	movq	%rax, %r14
	movq	%rax, %rdi
	movq	112(%rsp), %rbx                 # 8-byte Reload
	movq	%rbx, %rsi
	callq	strcpy
	movq	%rbx, %rdi
	callq	strlen@PLT
	movabsq	$8602282306966742386, %rcx      # imm = 0x7761722E6C616572
	movq	%rcx, (%rbx,%rax)
	movb	$0, 8(%rbx,%rax)
	movl	$.L.str.11, %esi
	movq	%rbx, %rdi
	callq	fopen
	movq	%rax, %rbx
	movq	%r14, %rdi
	callq	strlen@PLT
	movabsq	$33602665261264233, %rcx        # imm = 0x7761722E676D69
	movq	%rcx, (%r14,%rax)
	movl	$.L.str.11, %esi
	movq	%r14, %rdi
	callq	fopen
	movq	%rax, %r14
	movl	$4, %edx
	movq	%r12, %rdi
	movq	104(%rsp), %r12                 # 8-byte Reload
	movq	%r12, %rsi
	movq	%rbx, %rcx
	callq	fwrite
	movl	$4, %edx
	movq	%r15, %rdi
	movq	%r12, %rsi
	movq	%r14, %rcx
	callq	fwrite
	movq	%rbx, %rdi
	callq	fclose
	movq	%r14, %rdi
	callq	fclose
	callq	clock
	movq	%rax, timeend(%rip)
	subq	timestart(%rip), %rax
	cvtsi2sd	%rax, %xmm0
	divsd	.LCPI4_2(%rip), %xmm0
	movl	$.L.str.13, %edi
	movb	$1, %al
	callq	printf
	xorl	%eax, %eax
.LBB4_39:
                                        # kill: def $eax killed $eax killed $rax
	addq	$120, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB4_5:
	.cfi_def_cfa_offset 176
	cmpl	$63, %eax
	jne	.LBB4_21
# %bb.6:
	movl	optopt(%rip), %ebx
	leal	-78(%rbx), %eax
	cmpl	$44, %eax
	ja	.LBB4_17
# %bb.7:
	movabsq	$17600914391041, %rcx           # imm = 0x100208400001
	btq	%rax, %rcx
	jae	.LBB4_17
# %bb.8:
	movq	stderr(%rip), %rdi
	movl	$.L.str.1, %esi
.LBB4_19:
	movl	%ebx, %edx
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %eax
	jmp	.LBB4_39
.LBB4_17:
	movl	%ebx, %edi
	callq	isprint
	movq	stderr(%rip), %rdi
	testl	%eax, %eax
	jne	.LBB4_18
# %bb.20:
	movl	$.L.str.3, %esi
	jmp	.LBB4_19
.LBB4_18:
	movl	$.L.str.2, %esi
	jmp	.LBB4_19
.LBB4_21:
	callq	abort
.LBB4_23:
	movl	$.Lstr.4, %edi
	jmp	.LBB4_24
.LBB4_26:
	movl	$.Lstr.3, %edi
	jmp	.LBB4_24
.LBB4_28:
	movl	$.Lstr.2, %edi
.LBB4_24:
	callq	puts@PLT
	movl	$1, %edi
	callq	exit
.Lfunc_end4:
	.size	main, .Lfunc_end4-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	.LBB5_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB5_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z16gridding_processPfS_S_S_iifS_S_, %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_end5:
	.size	__hip_module_ctor, .Lfunc_end5-__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	.LBB6_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
.LBB6_2:
	retq
.Lfunc_end6:
	.size	__hip_module_dtor, .Lfunc_end6-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	timestart,@object               # @timestart
	.bss
	.globl	timestart
	.p2align	3, 0x0
timestart:
	.quad	0                               # 0x0
	.size	timestart, 8

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

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

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"i:z:d:N:o:"
	.size	.L.str, 11

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Opcion -%c requiere un argumento.\n"
	.size	.L.str.1, 35

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Opcion desconocida `-%c'.\n"
	.size	.L.str.2, 27

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Carater opcion desconocido `\\x%x'.\n"
	.size	.L.str.3, 36

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"r"
	.size	.L.str.9, 2

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"real.raw"
	.size	.L.str.10, 9

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"wb"
	.size	.L.str.11, 3

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"Total = %f\n"
	.size	.L.str.13, 12

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z16gridding_processPfS_S_S_iifS_S_"
	.size	.L__unnamed_1, 36

	.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

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"Debe especificarse un archivo de entrada"
	.size	.Lstr, 41

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"Debe especificarse un archivo de salida"
	.size	.Lstr.1, 40

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"El parametro -d debe existir y ser mayor que 0"
	.size	.Lstr.2, 47

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"El parametro -z debe existir y ser mayor que 0"
	.size	.Lstr.3, 47

	.type	.Lstr.4,@object                 # @str.4
.Lstr.4:
	.asciz	"El parametro -N debe existir y ser mayor que 0"
	.size	.Lstr.4, 47

	.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__gridding_processPfS_S_S_iifS_S_
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z16gridding_processPfS_S_S_iifS_S_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z16gridding_processPfS_S_S_iifS_S_ ; -- Begin function _Z16gridding_processPfS_S_S_iifS_S_
	.globl	_Z16gridding_processPfS_S_S_iifS_S_
	.p2align	8
	.type	_Z16gridding_processPfS_S_S_iifS_S_,@function
_Z16gridding_processPfS_S_S_iifS_S_:    ; @_Z16gridding_processPfS_S_S_iifS_S_
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x4c
	s_load_b128 s[16:19], s[0:1], 0x20
	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_cmpx_gt_i32_e64 s16, v1
	s_cbranch_execz .LBB0_7
; %bb.1:
	s_load_b256 s[4:11], s[0:1], 0x0
	v_ashrrev_i32_e32 v2, 31, v1
	s_lshr_b32 s2, s17, 31
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	s_add_i32 s2, s17, s2
	v_lshlrev_b64 v[0:1], 2, v[1:2]
	s_ashr_i32 s2, s2, 1
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v2, vcc_lo, s4, v0
	v_add_co_ci_u32_e32 v3, vcc_lo, s5, v1, vcc_lo
	global_load_b32 v2, v[2:3], off
	s_waitcnt vmcnt(0)
	v_div_scale_f32 v3, null, s18, s18, v2
	v_div_scale_f32 v6, vcc_lo, v2, s18, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f32_e32 v4, v3
	s_waitcnt_depctr 0xfff
	v_fma_f32 v5, -v3, v4, 1.0
	v_fmac_f32_e32 v4, v5, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v5, v6, v4
	v_fma_f32 v7, -v3, v5, v6
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v5, v7, v4
	v_fma_f32 v3, -v3, v5, v6
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_div_fmas_f32 v4, v3, v4, v5
	v_cvt_f32_i32_e32 v3, s2
	v_div_fixup_f32 v4, v4, s18, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_add_f32_e32 v5, v4, v3
	v_mul_f32_e64 v4, s18, 0.5
	v_fma_f32 v2, -v5, s18, v2
	v_add_f32_e32 v6, 1.0, v5
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cmp_gt_f32_e32 vcc_lo, v2, v4
	v_cndmask_b32_e32 v2, v5, v6, vcc_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cvt_i32_f32_e32 v2, v2
	v_cmp_gt_i32_e32 vcc_lo, s17, v2
	s_and_b32 exec_lo, exec_lo, vcc_lo
	s_cbranch_execz .LBB0_7
; %bb.2:
	v_add_co_u32 v5, vcc_lo, s6, v0
	v_add_co_ci_u32_e32 v6, vcc_lo, s7, v1, vcc_lo
	global_load_b32 v5, v[5:6], off
	s_waitcnt vmcnt(0)
	v_div_scale_f32 v6, null, s18, s18, v5
	v_div_scale_f32 v9, vcc_lo, v5, s18, v5
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f32_e32 v7, v6
	s_waitcnt_depctr 0xfff
	v_fma_f32 v8, -v6, v7, 1.0
	v_fmac_f32_e32 v7, v8, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v8, v9, v7
	v_fma_f32 v10, -v6, v8, v9
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v8, v10, v7
	v_fma_f32 v6, -v6, v8, v9
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_div_fmas_f32 v6, v6, v7, v8
	v_div_fixup_f32 v6, v6, s18, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_f32_e32 v3, v6, v3
	v_fma_f32 v5, -v3, s18, v5
	v_add_f32_e32 v6, 1.0, v3
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cmp_gt_f32_e32 vcc_lo, v5, v4
	v_cndmask_b32_e32 v3, v3, v6, vcc_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cvt_i32_f32_e32 v3, v3
	v_cmp_gt_i32_e32 vcc_lo, s17, v3
	s_and_b32 exec_lo, exec_lo, vcc_lo
	s_cbranch_execz .LBB0_7
; %bb.3:
	s_load_b128 s[0:3], s[0:1], 0x30
	v_mad_u64_u32 v[4:5], null, v3, s17, v[2:3]
	v_add_co_u32 v6, vcc_lo, s8, v0
	v_add_co_ci_u32_e32 v7, vcc_lo, s9, v1, vcc_lo
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v5, 31, v4
	v_lshlrev_b64 v[2:3], 2, v[4:5]
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v4, vcc_lo, s0, v2
	v_add_co_ci_u32_e32 v5, vcc_lo, s1, v3, vcc_lo
	s_mov_b32 s0, 0
	global_load_b32 v8, v[6:7], off
	global_load_b32 v7, v[4:5], off
.LBB0_4:                                ; %atomicrmw.start
                                        ; =>This Inner Loop Header: Depth=1
	s_waitcnt vmcnt(0)
	v_add_f32_e32 v6, v7, v8
	global_atomic_cmpswap_b32 v6, v[4:5], v[6:7], off glc
	s_waitcnt vmcnt(0)
	v_cmp_eq_u32_e32 vcc_lo, v6, v7
	v_mov_b32_e32 v7, v6
	s_or_b32 s0, vcc_lo, s0
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 exec_lo, exec_lo, s0
	s_cbranch_execnz .LBB0_4
; %bb.5:                                ; %atomicrmw.end
	s_or_b32 exec_lo, exec_lo, s0
	v_add_co_u32 v4, vcc_lo, s10, v0
	v_add_co_ci_u32_e32 v5, vcc_lo, s11, v1, vcc_lo
	v_add_co_u32 v0, vcc_lo, s2, v2
	v_add_co_ci_u32_e32 v1, vcc_lo, s3, v3, vcc_lo
	s_mov_b32 s0, 0
	global_load_b32 v4, v[4:5], off
	global_load_b32 v3, v[0:1], off
.LBB0_6:                                ; %atomicrmw.start51
                                        ; =>This Inner Loop Header: Depth=1
	s_waitcnt vmcnt(0)
	v_add_f32_e32 v2, v3, v4
	global_atomic_cmpswap_b32 v2, v[0:1], v[2:3], off glc
	s_waitcnt vmcnt(0)
	v_cmp_eq_u32_e32 vcc_lo, v2, v3
	v_mov_b32_e32 v3, v2
	s_or_b32 s0, vcc_lo, s0
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 exec_lo, exec_lo, s0
	s_cbranch_execnz .LBB0_6
.LBB0_7:
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z16gridding_processPfS_S_S_iifS_S_
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 320
		.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 11
		.amdhsa_next_free_sgpr 20
		.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	_Z16gridding_processPfS_S_S_iifS_S_, .Lfunc_end0-_Z16gridding_processPfS_S_S_iifS_S_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 676
; NumSgprs: 22
; NumVgprs: 11
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 22
; NumVGPRsForWavesPerEU: 11
; 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
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         24
        .size:           8
        .value_kind:     global_buffer
      - .offset:         32
        .size:           4
        .value_kind:     by_value
      - .offset:         36
        .size:           4
        .value_kind:     by_value
      - .offset:         40
        .size:           4
        .value_kind:     by_value
      - .address_space:  global
        .offset:         48
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         56
        .size:           8
        .value_kind:     global_buffer
      - .offset:         64
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         68
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         72
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         76
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         78
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         80
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         82
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         84
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         86
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         104
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         112
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         120
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         128
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 320
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z16gridding_processPfS_S_S_iifS_S_
    .private_segment_fixed_size: 0
    .sgpr_count:     22
    .sgpr_spill_count: 0
    .symbol:         _Z16gridding_processPfS_S_S_iifS_S_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     11
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
