#include <stdio.h>
#include <math.h>

// Algoritmo Criba de Eratóstenes
void primos(unsigned long max)
{
   unsigned long i, j, c=0;
   max++;
   char *arr = new char[max];
   cudaEvent_t inicio, fin;
   float tiempo;

   cudaEventCreate( &inicio );
   cudaEventCreate( &fin );
   cudaEventRecord( inicio, 0 );

   if (max >= 2)
   {
      for (i=0; i<max; i++)
         arr[i] = 0;
      arr[0] = 1;
      arr[1] = 1;

      unsigned long raiz = sqrt(max);

      for (j=4; j<max; j+=2)
         arr[j] = 1;

      for (i=3; i<=raiz; i+=2) // impares
         if (arr[i] == 0)
            for (j=i*i; j<max; j+=i)
               arr[j] = 1;

      cudaEventRecord( fin, 0 );
      cudaEventSynchronize( fin );
      cudaEventElapsedTime( &tiempo, inicio, fin );

      for (i=0; i<max; i++)
         if (arr[i] == 0)
         {
//            printf("%ld ", i);
            c++;
         }
      printf("\n total:%ld\n", c);
   }
   free(arr);
   printf("tiempo total en ms: %f\n", tiempo);
}

int main(int argc, char *argv[])
{
   primos(100000000);
   return 1;
}

	.file	"tmpxft_00295754_00000000-6_cpu-practica1.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2031:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2031:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"\n total:%ld\n"
.LC2:
	.string	"tiempo total en ms: %f\n"
	.text
	.globl	_Z6primosm
	.type	_Z6primosm, @function
_Z6primosm:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	leaq	1(%rdi), %rbx
	movq	%rbx, %rdi
	subq	$40, %rsp
	.cfi_def_cfa_offset 64
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	call	_Znam@PLT
	leaq	8(%rsp), %rdi
	movq	%rax, %rbp
	call	cudaEventCreate@PLT
	leaq	16(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	8(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	cmpq	$1, %rbx
	jbe	.L3
	xorl	%eax, %eax
	movq	%rbp, %rdi
	movq	%rbx, %rcx
	rep stosb
	movw	$257, 0(%rbp)
	testq	%rbx, %rbx
	js	.L4
	cvtsi2sdq	%rbx, %xmm0
	jmp	.L5
.L4:
	movq	%rbx, %rax
	movq	%rbx, %rdx
	shrq	%rax
	andl	$1, %edx
	orq	%rdx, %rax
	cvtsi2sdq	%rax, %xmm0
	addsd	%xmm0, %xmm0
.L5:
	call	sqrt@PLT
	movsd	.LC0(%rip), %xmm1
	comisd	%xmm1, %xmm0
	jnb	.L6
	cvttsd2siq	%xmm0, %rdx
	jmp	.L7
.L6:
	subsd	%xmm1, %xmm0
	cvttsd2siq	%xmm0, %rdx
	btcq	$63, %rdx
.L7:
	movl	$4, %eax
.L8:
	cmpq	%rbx, %rax
	jnb	.L22
	movb	$1, 0(%rbp,%rax)
	addq	$2, %rax
	jmp	.L8
.L22:
	movl	$3, %eax
.L10:
	cmpq	%rax, %rdx
	jb	.L23
	cmpb	$0, 0(%rbp,%rax)
	je	.L11
.L14:
	addq	$2, %rax
	jmp	.L10
.L11:
	movq	%rax, %rcx
	imulq	%rax, %rcx
.L12:
	cmpq	%rbx, %rcx
	jnb	.L14
	movb	$1, 0(%rbp,%rcx)
	addq	%rax, %rcx
	jmp	.L12
.L23:
	movq	16(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	16(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	16(%rsp), %rdx
	movq	8(%rsp), %rsi
	leaq	4(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	xorl	%edx, %edx
	xorl	%eax, %eax
.L17:
	cmpb	$0, 0(%rbp,%rax)
	jne	.L16
	incq	%rdx
.L16:
	incq	%rax
	cmpq	%rbx, %rax
	jb	.L17
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L3:
	movq	%rbp, %rdi
	call	free@PLT
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	4(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L18
	call	__stack_chk_fail@PLT
.L18:
	addq	$40, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z6primosm, .-_Z6primosm
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	movl	$100000000, %edi
	call	_Z6primosm
	movl	$1, %eax
	popq	%rdx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2054:
	.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
.LFE2054:
	.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
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC0:
	.long	0
	.long	1138753536
	.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 <hip/hip_runtime.h>
#include <stdio.h>
#include <math.h>

// Algoritmo Criba de Eratóstenes
void primos(unsigned long max)
{
   unsigned long i, j, c=0;
   max++;
   char *arr = new char[max];
   hipEvent_t inicio, fin;
   float tiempo;

   hipEventCreate( &inicio );
   hipEventCreate( &fin );
   hipEventRecord( inicio, 0 );

   if (max >= 2)
   {
      for (i=0; i<max; i++)
         arr[i] = 0;
      arr[0] = 1;
      arr[1] = 1;

      unsigned long raiz = sqrt(max);

      for (j=4; j<max; j+=2)
         arr[j] = 1;

      for (i=3; i<=raiz; i+=2) // impares
         if (arr[i] == 0)
            for (j=i*i; j<max; j+=i)
               arr[j] = 1;

      hipEventRecord( fin, 0 );
      hipEventSynchronize( fin );
      hipEventElapsedTime( &tiempo, inicio, fin );

      for (i=0; i<max; i++)
         if (arr[i] == 0)
         {
//            printf("%ld ", i);
            c++;
         }
      printf("\n total:%ld\n", c);
   }
   free(arr);
   printf("tiempo total en ms: %f\n", tiempo);
}

int main(int argc, char *argv[])
{
   primos(100000000);
   return 1;
}

	.text
	.file	"cpu-practica1.hip"
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0                          # -- Begin function _Z6primosm
.LCPI0_0:
	.long	1127219200                      # 0x43300000
	.long	1160773632                      # 0x45300000
	.long	0                               # 0x0
	.long	0                               # 0x0
.LCPI0_1:
	.quad	0x4330000000000000              # double 4503599627370496
	.quad	0x4530000000000000              # double 1.9342813113834067E+25
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI0_2:
	.quad	0x43e0000000000000              # double 9.2233720368547758E+18
	.text
	.globl	_Z6primosm
	.type	_Z6primosm,@function
_Z6primosm:                             # @_Z6primosm
	.cfi_startproc
# %bb.0:                                # %.preheader41.preheader
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	subq	$32, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %r14
	incq	%r14
	movq	%r14, %rdi
	callq	_Znam
	movq	%rax, %rbx
	leaq	24(%rsp), %r15
	movq	%r15, %rdi
	callq	hipEventCreate
	movq	%rsp, %rdi
	callq	hipEventCreate
	movq	(%r15), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	%rbx, %rdi
	xorl	%esi, %esi
	movq	%r14, %rdx
	callq	memset@PLT
	movq	%r14, %xmm0
	punpckldq	.LCPI0_0(%rip), %xmm0   # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
	subpd	.LCPI0_1(%rip), %xmm0
	movw	$257, (%rbx)                    # imm = 0x101
	movapd	%xmm0, %xmm1
	unpckhpd	%xmm0, %xmm1                    # xmm1 = xmm1[1],xmm0[1]
	addsd	%xmm0, %xmm1
	xorps	%xmm0, %xmm0
	sqrtsd	%xmm1, %xmm0
	cvttsd2si	%xmm0, %rcx
	movq	%rcx, %rdx
	subsd	.LCPI0_2(%rip), %xmm0
	cvttsd2si	%xmm0, %rax
	sarq	$63, %rdx
	andq	%rdx, %rax
	orq	%rcx, %rax
	cmpq	$5, %r14
	jb	.LBB0_3
# %bb.1:                                # %.lr.ph.preheader
	movl	$4, %ecx
.LBB0_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movb	$1, (%rbx,%rcx)
	addq	$2, %rcx
	cmpq	%r14, %rcx
	jb	.LBB0_2
.LBB0_3:                                # %.preheader
	cmpq	$3, %rax
	jb	.LBB0_10
# %bb.4:                                # %.lr.ph49.preheader
	movq	%rbx, %rcx
	addq	$9, %rcx
	movl	$3, %edx
	movl	$16, %esi
.LBB0_5:                                # %.lr.ph49
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_8 Depth 2
	cmpb	$0, (%rbx,%rdx)
	jne	.LBB0_9
# %bb.6:                                # %.lr.ph49
                                        #   in Loop: Header=BB0_5 Depth=1
	movq	%rdx, %rdi
	imulq	%rdx, %rdi
	cmpq	%r14, %rdi
	jae	.LBB0_9
# %bb.7:                                # %.lr.ph45.preheader
                                        #   in Loop: Header=BB0_5 Depth=1
	movq	%rcx, %r8
.LBB0_8:                                # %.lr.ph45
                                        #   Parent Loop BB0_5 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movb	$1, (%r8)
	addq	%rdx, %rdi
	addq	%rdx, %r8
	cmpq	%r14, %rdi
	jb	.LBB0_8
.LBB0_9:                                # %.loopexit
                                        #   in Loop: Header=BB0_5 Depth=1
	addq	$2, %rdx
	addq	%rsi, %rcx
	addq	$8, %rsi
	cmpq	%rax, %rdx
	jbe	.LBB0_5
.LBB0_10:                               # %._crit_edge
	movq	(%rsp), %rdi
	xorl	%r15d, %r15d
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%rsp), %rdi
	callq	hipEventSynchronize
	movq	24(%rsp), %rsi
	movq	(%rsp), %rdx
	leaq	12(%rsp), %rdi
	callq	hipEventElapsedTime
	xorl	%esi, %esi
.LBB0_11:                               # =>This Inner Loop Header: Depth=1
	cmpb	$1, (%rbx,%r15)
	adcq	$0, %rsi
	incq	%r15
	cmpq	%r15, %r14
	jne	.LBB0_11
# %bb.12:
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	printf
	xorps	%xmm0, %xmm0
	cvtss2sd	12(%rsp), %xmm0
	movsd	%xmm0, 16(%rsp)                 # 8-byte Spill
	movq	%rbx, %rdi
	callq	free
	movl	$.L.str.1, %edi
	movsd	16(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movb	$1, %al
	callq	printf
	addq	$32, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z6primosm, .Lfunc_end0-_Z6primosm
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movl	$100000000, %edi                # imm = 0x5F5E100
	callq	_Z6primosm
	movl	$1, %eax
	popq	%rcx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"\n total:%ld\n"
	.size	.L.str, 13

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"tiempo total en ms: %f\n"
	.size	.L.str.1, 24

	.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
