#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "string.h"
#include "bitonic_sorts.cuh"
#include "utils.cuh"

void check_sort(int* array, unsigned long size, char* type)
{
    for (int i=0 ; i < size - 1; i++)
    {
        if (array[i] > array[i + 1]){
            fprintf(stderr, "Array on %s with size %ld sorted incorrectly\n", type, size);
            return;
        }
    }
    printf("Array on %s with size %ld sorted right\n", type, size);
}

void check_content_equals(int* array, int* other, unsigned long size, char* type)
{
    bool elements_less = false;
    bool elements_more = false;
    
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            if (array[i] == other[j])
            {
                elements_less = true;
            }
            if (array[j] == other[i])
            {
                elements_more = true;
            }
        }
        if (!elements_less)
        {
            printf("Sorted array on %s with size %ld hasn't some elements \
                    which contain in initial array\n", type, size);
            return;
        }
        if (!elements_more)
        {
            printf("Sorted array on %s with size %ld has some elements \
                    which doesn't contain in initial array\n", type, size);
            return;
        }
    }
    printf("Sorted array on %s with size %ld has equal content as initial array\n", type, size);
}

int main()
{
    unsigned long upperBound = 1024 << 10;
    char* typeCPU = "CPU";
    char* typeGPU = "GPU";    

    for (unsigned long size = 1; size <= upperBound; size <<= 1)
    {
        srand(time(NULL));

        size_t size_mem_array = upperBound*sizeof(int);
        int* array = (int*) malloc(size_mem_array);
        int* temp_array = (int*) malloc(size_mem_array);

        generate_random_array(array, size);
            
        memcpy(temp_array, array, size_mem_array);

        bitonic_sort(temp_array, size);

        check_sort(temp_array, size, typeCPU);
        check_content_equals(array, temp_array, size, typeCPU);
            
        memcpy(temp_array, array, size * sizeof(int));

        bitonic_sort_gpu(temp_array, size);

        check_sort(temp_array, size, typeGPU);
        check_content_equals(array, temp_array, size, typeGPU);
        
        free(array);
        free(temp_array);
        printf("\n");
    }

    return 0;
}
	.file	"tmpxft_0039b376_00000000-6_tests.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2032:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2032:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Array on %s with size %ld sorted incorrectly\n"
.LC1:
	.string	"Array on %s with size %ld sorted right\n"
	.text
	.globl	_Z10check_sortPimPc
	.type	_Z10check_sortPimPc, @function
_Z10check_sortPimPc:
.LFB2027:
	.cfi_startproc
	endbr64
	movq	%rsi, %r8
	xorl	%eax, %eax
	leaq	-1(%rsi), %rcx
.L3:
	cmpq	%rcx, %rax
	jnb	.L7
	movl	(%rdi,%rax,4), %esi
	incq	%rax
	cmpl	(%rdi,%rax,4), %esi
	jle	.L3
	movq	stderr(%rip), %rdi
	movq	%rdx, %rcx
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC0(%rip), %rdx
	jmp	__fprintf_chk@PLT
.L7:
	movq	%r8, %rcx
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2027:
	.size	_Z10check_sortPimPc, .-_Z10check_sortPimPc
	.section	.rodata.str1.1
.LC2:
	.string	"Sorted array on %s with size %ld hasn't some elements                     which contain in initial array\n"
.LC3:
	.string	"Sorted array on %s with size %ld has some elements                     which doesn't contain in initial array\n"
.LC4:
	.string	"Sorted array on %s with size %ld has equal content as initial array\n"
	.text
	.globl	_Z20check_content_equalsPiS_mPc
	.type	_Z20check_content_equalsPiS_mPc, @function
_Z20check_content_equalsPiS_mPc:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rdi, %r8
	movq	%rdx, %r9
	xorl	%eax, %eax
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movq	%rcx, %rdx
	xorl	%edi, %edi
	movb	$1, %r11b
.L9:
	cmpq	%r9, %rax
	je	.L14
	movl	(%r8,%rax,4), %ebp
	movl	(%rsi,%rax,4), %ebx
	movl	%edi, %r10d
	xorl	%ecx, %ecx
.L12:
	cmpl	(%rsi,%rcx,4), %ebp
	cmove	%r11d, %r10d
	cmpl	%ebx, (%r8,%rcx,4)
	cmove	%r11d, %edi
	incq	%rcx
	cmpq	%r9, %rcx
	jne	.L12
	testb	%r10b, %r10b
	jne	.L13
	leaq	.LC2(%rip), %rsi
	jmp	.L20
.L13:
	incq	%rax
	testb	%dil, %dil
	jne	.L9
	leaq	.LC3(%rip), %rsi
	jmp	.L20
.L14:
	movq	%rax, %rcx
	leaq	.LC4(%rip), %rsi
.L20:
	popq	%rbx
	.cfi_def_cfa_offset 16
	movl	$2, %edi
	xorl	%eax, %eax
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2028:
	.size	_Z20check_content_equalsPiS_mPc, .-_Z20check_content_equalsPiS_mPc
	.section	.rodata.str1.1
.LC5:
	.string	"CPU"
.LC6:
	.string	"GPU"
.LC7:
	.string	"\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2029:
	.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	$21, %r14d
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	leaq	.LC5(%rip), %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	$1, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	pushq	%rcx
	.cfi_def_cfa_offset 64
.L22:
	xorl	%edi, %edi
	leaq	.LC6(%rip), %r15
	call	time@PLT
	movl	%eax, %edi
	call	srand@PLT
	movl	$4194304, %edi
	call	malloc@PLT
	movl	$4194304, %edi
	movq	%rax, %r12
	call	malloc@PLT
	movl	%ebp, %esi
	movq	%r12, %rdi
	movq	%rax, %rbx
	call	_Z21generate_random_arrayPii@PLT
	movl	$1048576, %ecx
	movq	%rbx, %rdi
	movq	%r12, %rsi
	rep movsl
	movq	%rbp, %rsi
	movq	%rbx, %rdi
	call	_Z12bitonic_sortPim@PLT
	movq	%rbp, %rsi
	movq	%r13, %rdx
	movq	%rbx, %rdi
	call	_Z10check_sortPimPc
	movq	%rbp, %rdx
	movq	%r13, %rcx
	movq	%rbx, %rsi
	movq	%r12, %rdi
	call	_Z20check_content_equalsPiS_mPc
	leal	0(,%rbp,4), %ecx
	movq	%rbx, %rdi
	movq	%r12, %rsi
	rep movsb
	movq	%rbp, %rsi
	movq	%rbx, %rdi
	call	_Z16bitonic_sort_gpuPim@PLT
	movq	%rbp, %rsi
	movq	%r15, %rdx
	movq	%rbx, %rdi
	call	_Z10check_sortPimPc
	movq	%rbp, %rdx
	movq	%r15, %rcx
	movq	%rbx, %rsi
	movq	%r12, %rdi
	addq	%rbp, %rbp
	call	_Z20check_content_equalsPiS_mPc
	movq	%r12, %rdi
	call	free@PLT
	movq	%rbx, %rdi
	call	free@PLT
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	decl	%r14d
	jne	.L22
	popq	%rdx
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	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
.LFE2029:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2055:
	.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
.LFE2055:
	.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:


#include <hip/hip_runtime.h>
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "string.h"
#include "bitonic_sorts.cuh"
#include "utils.cuh"

void check_sort(int* array, unsigned long size, char* type)
{
    for (int i=0 ; i < size - 1; i++)
    {
        if (array[i] > array[i + 1]){
            fprintf(stderr, "Array on %s with size %ld sorted incorrectly\n", type, size);
            return;
        }
    }
    printf("Array on %s with size %ld sorted right\n", type, size);
}

void check_content_equals(int* array, int* other, unsigned long size, char* type)
{
    bool elements_less = false;
    bool elements_more = false;
    
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            if (array[i] == other[j])
            {
                elements_less = true;
            }
            if (array[j] == other[i])
            {
                elements_more = true;
            }
        }
        if (!elements_less)
        {
            printf("Sorted array on %s with size %ld hasn't some elements \
                    which contain in initial array\n", type, size);
            return;
        }
        if (!elements_more)
        {
            printf("Sorted array on %s with size %ld has some elements \
                    which doesn't contain in initial array\n", type, size);
            return;
        }
    }
    printf("Sorted array on %s with size %ld has equal content as initial array\n", type, size);
}

int main()
{
    unsigned long upperBound = 1024 << 10;
    char* typeCPU = "CPU";
    char* typeGPU = "GPU";    

    for (unsigned long size = 1; size <= upperBound; size <<= 1)
    {
        srand(time(NULL));

        size_t size_mem_array = upperBound*sizeof(int);
        int* array = (int*) malloc(size_mem_array);
        int* temp_array = (int*) malloc(size_mem_array);

        generate_random_array(array, size);
            
        memcpy(temp_array, array, size_mem_array);

        bitonic_sort(temp_array, size);

        check_sort(temp_array, size, typeCPU);
        check_content_equals(array, temp_array, size, typeCPU);
            
        memcpy(temp_array, array, size * sizeof(int));

        bitonic_sort_gpu(temp_array, size);

        check_sort(temp_array, size, typeGPU);
        check_content_equals(array, temp_array, size, typeGPU);
        
        free(array);
        free(temp_array);
        printf("\n");
    }

    return 0;
}
	.text
	.file	"tests.hip"
	.globl	_Z10check_sortPimPc             # -- Begin function _Z10check_sortPimPc
	.type	_Z10check_sortPimPc,@function
_Z10check_sortPimPc:                    # @_Z10check_sortPimPc
	.cfi_startproc
# %bb.0:
	movq	%rsi, %rcx
	movl	$1, %eax
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
	cmpq	%rax, %rcx
	je	.LBB0_4
# %bb.2:                                #   in Loop: Header=BB0_1 Depth=1
	movl	-4(%rdi,%rax,4), %esi
	leaq	1(%rax), %r8
	cmpl	(%rdi,%rax,4), %esi
	movq	%r8, %rax
	jle	.LBB0_1
# %bb.3:
	movq	stderr(%rip), %rdi
	movl	$.L.str, %esi
	xorl	%eax, %eax
	jmp	fprintf                         # TAILCALL
.LBB0_4:
	movl	$.L.str.1, %edi
	movq	%rdx, %rsi
	movq	%rcx, %rdx
	xorl	%eax, %eax
	jmp	printf                          # TAILCALL
.Lfunc_end0:
	.size	_Z10check_sortPimPc, .Lfunc_end0-_Z10check_sortPimPc
	.cfi_endproc
                                        # -- End function
	.globl	_Z20check_content_equalsPiS_mPc # -- Begin function _Z20check_content_equalsPiS_mPc
	.type	_Z20check_content_equalsPiS_mPc,@function
_Z20check_content_equalsPiS_mPc:        # @_Z20check_content_equalsPiS_mPc
	.cfi_startproc
# %bb.0:
	movq	%rdi, %rax
	movl	$.L.str.4, %edi
	testq	%rdx, %rdx
	je	.LBB1_10
# %bb.1:                                # %.preheader.preheader
	pushq	%rbp
	.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 %rbp, -16
	xorl	%r8d, %r8d
	movl	$1, %r9d
	xorl	%r10d, %r10d
	xorl	%r11d, %r11d
.LBB1_3:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_4 Depth 2
	movl	(%rax,%r8,4), %ebx
	movl	(%rsi,%r8,4), %ebp
	xorl	%r14d, %r14d
.LBB1_4:                                #   Parent Loop BB1_3 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	cmpl	(%rsi,%r14,4), %ebx
	movzbl	%r11b, %r11d
	cmovel	%r9d, %r11d
	cmpl	%ebp, (%rax,%r14,4)
	movzbl	%r10b, %r10d
	cmovel	%r9d, %r10d
	incq	%r14
	cmpq	%r14, %rdx
	jne	.LBB1_4
# %bb.5:                                #   in Loop: Header=BB1_3 Depth=1
	testb	$1, %r11b
	je	.LBB1_6
# %bb.7:                                #   in Loop: Header=BB1_3 Depth=1
	testb	$1, %r10b
	je	.LBB1_8
# %bb.2:                                #   in Loop: Header=BB1_3 Depth=1
	incq	%r8
	cmpq	%rdx, %r8
	jne	.LBB1_3
	jmp	.LBB1_9
.LBB1_6:
	movl	$.L.str.2, %edi
	jmp	.LBB1_9
.LBB1_8:
	movl	$.L.str.3, %edi
.LBB1_9:
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	.cfi_restore %rbx
	.cfi_restore %r14
	.cfi_restore %rbp
.LBB1_10:                               # %.critedge
	movq	%rcx, %rsi
	xorl	%eax, %eax
	jmp	printf                          # TAILCALL
.Lfunc_end1:
	.size	_Z20check_content_equalsPiS_mPc, .Lfunc_end1-_Z20check_content_equalsPiS_mPc
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.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
	movl	$1, %ebx
.LBB2_1:                                # =>This Inner Loop Header: Depth=1
	xorl	%edi, %edi
	callq	time
	movl	%eax, %edi
	callq	srand
	movl	$4194304, %edi                  # imm = 0x400000
	callq	malloc
	movq	%rax, %r14
	movl	$4194304, %edi                  # imm = 0x400000
	callq	malloc
	movq	%rax, %r15
	movq	%r14, %rdi
	movl	%ebx, %esi
	callq	_Z21generate_random_arrayPii
	movl	$4194304, %edx                  # imm = 0x400000
	movq	%r15, %rdi
	movq	%r14, %rsi
	callq	memcpy@PLT
	movq	%r15, %rdi
	movq	%rbx, %rsi
	callq	_Z12bitonic_sortPim
	movl	$.L.str.5, %edx
	movq	%r15, %rdi
	movq	%rbx, %rsi
	callq	_Z10check_sortPimPc
	movl	$.L.str.5, %ecx
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%rbx, %rdx
	callq	_Z20check_content_equalsPiS_mPc
	leaq	(,%rbx,4), %rdx
	movq	%r15, %rdi
	movq	%r14, %rsi
	callq	memcpy@PLT
	movq	%r15, %rdi
	movq	%rbx, %rsi
	callq	_Z16bitonic_sort_gpuPim
	movl	$.L.str.6, %edx
	movq	%r15, %rdi
	movq	%rbx, %rsi
	callq	_Z10check_sortPimPc
	movl	$.L.str.6, %ecx
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%rbx, %rdx
	callq	_Z20check_content_equalsPiS_mPc
	movq	%r14, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	movl	$10, %edi
	callq	putchar@PLT
	leaq	(%rbx,%rbx), %rax
	cmpq	$524289, %rbx                   # imm = 0x80001
	movq	%rax, %rbx
	jb	.LBB2_1
# %bb.2:
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	main, .Lfunc_end2-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Array on %s with size %ld sorted incorrectly\n"
	.size	.L.str, 46

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Array on %s with size %ld sorted right\n"
	.size	.L.str.1, 40

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Sorted array on %s with size %ld hasn't some elements                     which contain in initial array\n"
	.size	.L.str.2, 106

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Sorted array on %s with size %ld has some elements                     which doesn't contain in initial array\n"
	.size	.L.str.3, 111

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Sorted array on %s with size %ld has equal content as initial array\n"
	.size	.L.str.4, 69

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

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

	.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
