#include <assert.h>
#include <pthread.h>
#include <stdio.h>

#define THREADS 4

int intervalsT = 100000000;
double store, base;
double partialStore[] = {0.0, 0.0, 0.0, 0.0};

void* threadRoutine(void* param) {
    int i;
    int* threadId = (int*)param;
    int partialInterval = intervalsT / THREADS;
    double height;
    double x;

    for (i = (*threadId) * partialInterval, partialStore[*threadId] = 0.0;
         i < (partialInterval * (*threadId + 1)); i++) {
        x = i * base;
        height = 4 / (1 + x * x);
        partialStore[*threadId] += base * height;
    }
    return 0;
}

void calculatePIHostMultiple() {
    int i;
    pthread_t threads[THREADS];
    int threadId[THREADS];

    for (i = 0; i < THREADS; i++)
        threadId[i] = i;

    base = (double)(1.0 / intervalsT);

    for (i = 0; i < THREADS; i++)
        pthread_create(&threads[i], NULL, threadRoutine, &threadId[i]);

    for (i = 0; i < THREADS; i++)
        pthread_join(threads[i], NULL);

    store = 0.0;
    for (i = 0; i < THREADS; i++)
        store += partialStore[i];

    printf("PI (multiple th) =%f\n", store);
}

void calculatePIHostSingle() {
    int i;
    double height, x;
    double store, base;

    int intervals = 100000000;
    base = (double)(1.0 / intervals);

    for (i = 0, store = 0.0, x = 0.0; i < intervals; i++) {
        x = i * base;
        height = 4 / (1 + x * x);
        store += base * height;
    }

    printf("PI (single th) =%f \n", store);
}

int main() {
    calculatePIHostSingle();

    calculatePIHostMultiple();
}

	.file	"tmpxft_0030a1f9_00000000-6_03pi.cudafe1.cpp"
	.text
#APP

#NO_APP
	.globl	_Z13threadRoutinePv
	.type	_Z13threadRoutinePv, @function
_Z13threadRoutinePv:
.LFB2036:
	.cfi_startproc
	endbr64
	movl	intervalsT(%rip), %eax
	movl	$4, %ecx
	leaq	partialStore(%rip), %rsi
	movsd	base(%rip), %xmm2
	movsd	.LC1(%rip), %xmm3
	movsd	.LC2(%rip), %xmm4
	cltd
	idivl	%ecx
	movslq	(%rdi), %rdx
	movq	$0x000000000, (%rsi,%rdx,8)
	movl	%edx, %ecx
	imull	%eax, %ecx
	addl	%ecx, %eax
.L2:
	cmpl	%ecx, %eax
	jle	.L5
	cvtsi2sdl	%ecx, %xmm0
	movaps	%xmm4, %xmm1
	incl	%ecx
	mulsd	%xmm2, %xmm0
	mulsd	%xmm0, %xmm0
	addsd	%xmm3, %xmm0
	divsd	%xmm0, %xmm1
	movsd	(%rsi,%rdx,8), %xmm0
	mulsd	%xmm2, %xmm1
	addsd	%xmm1, %xmm0
	movsd	%xmm0, (%rsi,%rdx,8)
	jmp	.L2
.L5:
	xorl	%eax, %eax
	ret
	.cfi_endproc
.LFE2036:
	.size	_Z13threadRoutinePv, .-_Z13threadRoutinePv
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2042:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2042:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC3:
	.string	"PI (multiple th) =%f\n"
	.text
	.globl	_Z23calculatePIHostMultiplev
	.type	_Z23calculatePIHostMultiplev, @function
_Z23calculatePIHostMultiplev:
.LFB2037:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	leaq	_Z13threadRoutinePv(%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
	subq	$64, %rsp
	.cfi_def_cfa_offset 112
	cvtsi2sdl	intervalsT(%rip), %xmm1
	movsd	.LC1(%rip), %xmm0
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	movabsq	$4294967296, %rax
	leaq	24(%rsp), %rbx
	leaq	8(%rsp), %rbp
	leaq	56(%rsp), %r13
	movq	%rbx, %r12
	divsd	%xmm1, %xmm0
	movq	%rax, 8(%rsp)
	movabsq	$12884901890, %rax
	movq	%rax, 16(%rsp)
	movsd	%xmm0, base(%rip)
.L8:
	movq	%rbp, %rcx
	movq	%rbx, %rdi
	movq	%r14, %rdx
	xorl	%esi, %esi
	call	pthread_create@PLT
	addq	$8, %rbx
	addq	$4, %rbp
	cmpq	%r13, %rbx
	jne	.L8
	xorl	%ebx, %ebx
.L9:
	movq	(%r12,%rbx,8), %rdi
	xorl	%esi, %esi
	incq	%rbx
	call	pthread_join@PLT
	cmpq	$4, %rbx
	jne	.L9
	xorps	%xmm0, %xmm0
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	addsd	partialStore(%rip), %xmm0
	addsd	8+partialStore(%rip), %xmm0
	addsd	16+partialStore(%rip), %xmm0
	addsd	24+partialStore(%rip), %xmm0
	movsd	%xmm0, store(%rip)
	call	__printf_chk@PLT
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L10
	call	__stack_chk_fail@PLT
.L10:
	addq	$64, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	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
.LFE2037:
	.size	_Z23calculatePIHostMultiplev, .-_Z23calculatePIHostMultiplev
	.section	.rodata.str1.1
.LC5:
	.string	"PI (single th) =%f \n"
	.text
	.globl	_Z21calculatePIHostSinglev
	.type	_Z21calculatePIHostSinglev, @function
_Z21calculatePIHostSinglev:
.LFB2038:
	.cfi_startproc
	endbr64
	movsd	.LC4(%rip), %xmm3
	movsd	.LC1(%rip), %xmm5
	xorps	%xmm1, %xmm1
	xorl	%eax, %eax
	movsd	.LC2(%rip), %xmm4
.L15:
	cvtsi2sdl	%eax, %xmm0
	movaps	%xmm4, %xmm2
	incl	%eax
	mulsd	%xmm3, %xmm0
	mulsd	%xmm0, %xmm0
	addsd	%xmm5, %xmm0
	divsd	%xmm0, %xmm2
	mulsd	%xmm3, %xmm2
	addsd	%xmm2, %xmm1
	cmpl	$100000000, %eax
	jne	.L15
	movaps	%xmm1, %xmm0
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2038:
	.size	_Z21calculatePIHostSinglev, .-_Z21calculatePIHostSinglev
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2039:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	call	_Z21calculatePIHostSinglev
	call	_Z23calculatePIHostMultiplev
	xorl	%eax, %eax
	popq	%rdx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2039:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2065:
	.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
.LFE2065:
	.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	partialStore
	.bss
	.align 32
	.type	partialStore, @object
	.size	partialStore, 32
partialStore:
	.zero	32
	.globl	base
	.align 8
	.type	base, @object
	.size	base, 8
base:
	.zero	8
	.globl	store
	.align 8
	.type	store, @object
	.size	store, 8
store:
	.zero	8
	.globl	intervalsT
	.data
	.align 4
	.type	intervalsT, @object
	.size	intervalsT, 4
intervalsT:
	.long	100000000
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC1:
	.long	0
	.long	1072693248
	.align 8
.LC2:
	.long	0
	.long	1074790400
	.align 8
.LC4:
	.long	-500134854
	.long	1044740494
	.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 <assert.h>
#include <pthread.h>
#include <stdio.h>

#define THREADS 4

int intervalsT = 100000000;
double store, base;
double partialStore[] = {0.0, 0.0, 0.0, 0.0};

void* threadRoutine(void* param) {
    int i;
    int* threadId = (int*)param;
    int partialInterval = intervalsT / THREADS;
    double height;
    double x;

    for (i = (*threadId) * partialInterval, partialStore[*threadId] = 0.0;
         i < (partialInterval * (*threadId + 1)); i++) {
        x = i * base;
        height = 4 / (1 + x * x);
        partialStore[*threadId] += base * height;
    }
    return 0;
}

void calculatePIHostMultiple() {
    int i;
    pthread_t threads[THREADS];
    int threadId[THREADS];

    for (i = 0; i < THREADS; i++)
        threadId[i] = i;

    base = (double)(1.0 / intervalsT);

    for (i = 0; i < THREADS; i++)
        pthread_create(&threads[i], NULL, threadRoutine, &threadId[i]);

    for (i = 0; i < THREADS; i++)
        pthread_join(threads[i], NULL);

    store = 0.0;
    for (i = 0; i < THREADS; i++)
        store += partialStore[i];

    printf("PI (multiple th) =%f\n", store);
}

void calculatePIHostSingle() {
    int i;
    double height, x;
    double store, base;

    int intervals = 100000000;
    base = (double)(1.0 / intervals);

    for (i = 0, store = 0.0, x = 0.0; i < intervals; i++) {
        x = i * base;
        height = 4 / (1 + x * x);
        store += base * height;
    }

    printf("PI (single th) =%f \n", store);
}

int main() {
    calculatePIHostSingle();

    calculatePIHostMultiple();
}

	.text
	.file	"03pi.hip"
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z13threadRoutinePv
.LCPI0_0:
	.quad	0x3ff0000000000000              # double 1
.LCPI0_1:
	.quad	0x4010000000000000              # double 4
	.text
	.globl	_Z13threadRoutinePv
	.type	_Z13threadRoutinePv,@function
_Z13threadRoutinePv:                    # @_Z13threadRoutinePv
	.cfi_startproc
# %bb.0:
	movl	intervalsT(%rip), %ecx
	leal	3(%rcx), %eax
	testl	%ecx, %ecx
	cmovnsl	%ecx, %eax
	sarl	$2, %eax
	movslq	(%rdi), %rcx
	movl	%ecx, %edx
	imull	%eax, %edx
	movq	$0, partialStore(,%rcx,8)
	leal	1(%rcx), %esi
	imull	%eax, %esi
	cmpl	%esi, %edx
	jge	.LBB0_4
# %bb.1:                                # %.lr.ph
	xorpd	%xmm0, %xmm0
	movsd	base(%rip), %xmm1               # xmm1 = mem[0],zero
	movsd	.LCPI0_0(%rip), %xmm2           # xmm2 = mem[0],zero
	movsd	.LCPI0_1(%rip), %xmm3           # xmm3 = mem[0],zero
.LBB0_2:                                # =>This Inner Loop Header: Depth=1
	xorps	%xmm4, %xmm4
	cvtsi2sd	%edx, %xmm4
	mulsd	%xmm1, %xmm4
	mulsd	%xmm4, %xmm4
	addsd	%xmm2, %xmm4
	movapd	%xmm3, %xmm5
	divsd	%xmm4, %xmm5
	mulsd	%xmm1, %xmm5
	addsd	%xmm5, %xmm0
	incl	%edx
	decl	%eax
	jne	.LBB0_2
# %bb.3:                                # %._crit_edge
	movsd	%xmm0, partialStore(,%rcx,8)
.LBB0_4:
	xorl	%eax, %eax
	retq
.Lfunc_end0:
	.size	_Z13threadRoutinePv, .Lfunc_end0-_Z13threadRoutinePv
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z23calculatePIHostMultiplev
.LCPI1_0:
	.quad	0x3ff0000000000000              # double 1
	.text
	.globl	_Z23calculatePIHostMultiplev
	.type	_Z23calculatePIHostMultiplev,@function
_Z23calculatePIHostMultiplev:           # @_Z23calculatePIHostMultiplev
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	subq	$56, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	xorl	%eax, %eax
.LBB1_1:                                # =>This Inner Loop Header: Depth=1
	movl	%eax, (%rsp,%rax,4)
	incq	%rax
	cmpq	$4, %rax
	jne	.LBB1_1
# %bb.2:
	cvtsi2sdl	intervalsT(%rip), %xmm0
	movsd	.LCPI1_0(%rip), %xmm1           # xmm1 = mem[0],zero
	divsd	%xmm0, %xmm1
	movsd	%xmm1, base(%rip)
	leaq	16(%rsp), %rbx
	xorl	%r14d, %r14d
.LBB1_3:                                # =>This Inner Loop Header: Depth=1
	leaq	(%rsp,%r14), %rcx
	movl	$_Z13threadRoutinePv, %edx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	callq	pthread_create
	addq	$4, %r14
	addq	$8, %rbx
	cmpq	$16, %r14
	jne	.LBB1_3
# %bb.4:                                # %.preheader.preheader
	xorl	%ebx, %ebx
.LBB1_5:                                # %.preheader
                                        # =>This Inner Loop Header: Depth=1
	movq	16(%rsp,%rbx,8), %rdi
	xorl	%esi, %esi
	callq	pthread_join
	incq	%rbx
	cmpq	$4, %rbx
	jne	.LBB1_5
# %bb.6:                                # %.preheader31.preheader
	xorpd	%xmm0, %xmm0
	movq	$-32, %rax
.LBB1_7:                                # %.preheader31
                                        # =>This Inner Loop Header: Depth=1
	addsd	partialStore+32(%rax), %xmm0
	addq	$8, %rax
	jne	.LBB1_7
# %bb.8:
	movsd	%xmm0, store(%rip)
	movl	$.L.str, %edi
	movb	$1, %al
	callq	printf
	addq	$56, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z23calculatePIHostMultiplev, .Lfunc_end1-_Z23calculatePIHostMultiplev
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z21calculatePIHostSinglev
.LCPI2_0:
	.quad	0x3e45798ee2308c3a              # double 1.0E-8
.LCPI2_1:
	.quad	0x3ff0000000000000              # double 1
.LCPI2_2:
	.quad	0x4010000000000000              # double 4
	.text
	.globl	_Z21calculatePIHostSinglev
	.type	_Z21calculatePIHostSinglev,@function
_Z21calculatePIHostSinglev:             # @_Z21calculatePIHostSinglev
	.cfi_startproc
# %bb.0:
	xorpd	%xmm1, %xmm1
	movl	$100000000, %eax                # imm = 0x5F5E100
	movsd	.LCPI2_0(%rip), %xmm2           # xmm2 = mem[0],zero
	movsd	.LCPI2_1(%rip), %xmm3           # xmm3 = mem[0],zero
	movsd	.LCPI2_2(%rip), %xmm4           # xmm4 = mem[0],zero
	xorpd	%xmm0, %xmm0
.LBB2_1:                                # =>This Inner Loop Header: Depth=1
	movapd	%xmm1, %xmm5
	mulsd	%xmm2, %xmm5
	mulsd	%xmm5, %xmm5
	addsd	%xmm3, %xmm5
	movapd	%xmm4, %xmm6
	divsd	%xmm5, %xmm6
	mulsd	%xmm2, %xmm6
	addsd	%xmm6, %xmm0
	addsd	%xmm3, %xmm1
	decl	%eax
	jne	.LBB2_1
# %bb.2:
	movl	$.L.str.1, %edi
	movb	$1, %al
	jmp	printf                          # TAILCALL
.Lfunc_end2:
	.size	_Z21calculatePIHostSinglev, .Lfunc_end2-_Z21calculatePIHostSinglev
	.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
	callq	_Z21calculatePIHostSinglev
	callq	_Z23calculatePIHostMultiplev
	xorl	%eax, %eax
	popq	%rcx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	main, .Lfunc_end3-main
	.cfi_endproc
                                        # -- End function
	.type	intervalsT,@object              # @intervalsT
	.data
	.globl	intervalsT
	.p2align	2, 0x0
intervalsT:
	.long	100000000                       # 0x5f5e100
	.size	intervalsT, 4

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

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

	.type	partialStore,@object            # @partialStore
	.globl	partialStore
	.p2align	4, 0x0
partialStore:
	.zero	32
	.size	partialStore, 32

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"PI (multiple th) =%f\n"
	.size	.L.str, 22

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"PI (single th) =%f \n"
	.size	.L.str.1, 21

	.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 _Z13threadRoutinePv
	.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
