/* datatools.c - support functions for the matrix examples

 */
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

// Using single indexed arrays
void init_matrix(int m, int n, double *A, double value) {

  int i, j;

  for (i = 0; i < m; i++)
    for (j = 0; j < n; j++) {
      A[i * n + j] = value;
    }
}

void print_matrix(int m, int n, double **A) {
  int i, j;
  printf("----------------------------------\n");
  for (i = 0; i < m; i++) {
    for (j = 0; j < n; j++) {
      printf("%6.2f, ", A[i * m + j]);
    }
    printf("\n");
  }
  printf("----------------------------------\n");
}

void init_vector(int m, double *V) {
  int i;

  for (i = 0; i < m; i++)
    *(V + i) = 1.0;
}
/*
int
check_results(char *comment, int m, int n, double **A) {

   double relerr;
   double *a = A[0];
   double ref = 3.0;
   int    i, errors = 0;
   char   *marker;
   double TOL   = 100.0 * DBL_EPSILON;
   double SMALL = 100.0 * DBL_MIN;

   if ( (marker=(char *)malloc(m*n*sizeof(char))) == NULL ) {
        perror("array marker");
        exit(-1);
   }

   for (i=0; i<m*n; i++)
   {
       relerr = fabs((a[i]-ref));
       if ( relerr <= TOL )
       {
          marker[i] = ' ';
       }
       else
       {
          errors++;
          marker[i] = '*';
       }
   }
   if ( errors > 0 )
   {
     printf("Routine: %s\n",comment);
     printf("Found %d differences in results for m=%d n=%d:\n",
             errors,m,n);
     for (i=0; i<m*n; i++)
         printf("\t%c a[%d]=%f ref[%d]=%f\n",marker[i],i,a[i],i,ref);
   }
   return(errors);
}
*/
/* Routine for allocating two-dimensional array */
/*
double **
malloc_2d(int m, int n)
{
    int i;

    if (m <= 0 || n <= 0)
        return NULL;

    double **A = malloc(m * sizeof(double *));
    if (A == NULL)
        return NULL;

    A[0] = malloc(m*n*sizeof(double));
    if (A[0] == NULL) {
        free(A);
        return NULL;
    }
    for (i = 1; i < m; i++)
        A[i] = A[0] + i * n;

    return A;
}
*/
void free_2d(double **A) {
  free(A[0]);
  free(A);
}

	.file	"tmpxft_0021e806_00000000-6_datatools.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2033:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2033:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z11init_matrixiiPdd
	.type	_Z11init_matrixiiPdd, @function
_Z11init_matrixiiPdd:
.LFB2027:
	.cfi_startproc
	endbr64
	xorl	%ecx, %ecx
	xorl	%r8d, %r8d
.L3:
	cmpl	%edi, %r8d
	jge	.L2
	movslq	%ecx, %rax
	leaq	(%rdx,%rax,8), %r9
	xorl	%eax, %eax
.L6:
	cmpl	%eax, %esi
	jle	.L8
	movsd	%xmm0, (%r9,%rax,8)
	incq	%rax
	jmp	.L6
.L8:
	incl	%r8d
	addl	%esi, %ecx
	jmp	.L3
.L2:
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z11init_matrixiiPdd, .-_Z11init_matrixiiPdd
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"----------------------------------\n"
.LC1:
	.string	"%6.2f, "
.LC2:
	.string	"\n"
	.text
	.globl	_Z12print_matrixiiPPd
	.type	_Z12print_matrixiiPPd, @function
_Z12print_matrixiiPPd:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	xorl	%eax, %eax
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	xorl	%r13d, %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
	movq	%rdx, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%edi, %ebx
	movl	$2, %edi
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	movl	%esi, 12(%rsp)
	leaq	.LC0(%rip), %rsi
	call	__printf_chk@PLT
.L10:
	cmpl	%ebx, %r13d
	jge	.L12
	movslq	%r12d, %rax
	xorl	%r15d, %r15d
	leaq	0(%rbp,%rax,8), %r14
.L13:
	cmpl	%r15d, 12(%rsp)
	jle	.L15
	movq	(%r14,%r15,8), %rdx
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incq	%r15
	call	__printf_chk@PLT
	jmp	.L13
.L15:
	leaq	.LC2(%rip), %rsi
	xorl	%eax, %eax
	incl	%r13d
	addl	%ebx, %r12d
	movl	$2, %edi
	call	__printf_chk@PLT
	jmp	.L10
.L12:
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	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
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2028:
	.size	_Z12print_matrixiiPPd, .-_Z12print_matrixiiPPd
	.globl	_Z11init_vectoriPd
	.type	_Z11init_vectoriPd, @function
_Z11init_vectoriPd:
.LFB2029:
	.cfi_startproc
	endbr64
	movsd	.LC3(%rip), %xmm0
	xorl	%eax, %eax
.L17:
	cmpl	%eax, %edi
	jle	.L19
	movsd	%xmm0, (%rsi,%rax,8)
	incq	%rax
	jmp	.L17
.L19:
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z11init_vectoriPd, .-_Z11init_vectoriPd
	.globl	_Z7free_2dPPd
	.type	_Z7free_2dPPd, @function
_Z7free_2dPPd:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	movq	(%rdi), %rdi
	call	free@PLT
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	free@PLT
	.cfi_endproc
.LFE2030:
	.size	_Z7free_2dPPd, .-_Z7free_2dPPd
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2056:
	.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
.LFE2056:
	.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
.LC3:
	.long	0
	.long	1072693248
	.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:

/* datatools.c - support functions for the matrix examples

 */

#include <hip/hip_runtime.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

// Using single indexed arrays
void init_matrix(int m, int n, double *A, double value) {

  int i, j;

  for (i = 0; i < m; i++)
    for (j = 0; j < n; j++) {
      A[i * n + j] = value;
    }
}

void print_matrix(int m, int n, double **A) {
  int i, j;
  printf("----------------------------------\n");
  for (i = 0; i < m; i++) {
    for (j = 0; j < n; j++) {
      printf("%6.2f, ", A[i * m + j]);
    }
    printf("\n");
  }
  printf("----------------------------------\n");
}

void init_vector(int m, double *V) {
  int i;

  for (i = 0; i < m; i++)
    *(V + i) = 1.0;
}
/*
int
check_results(char *comment, int m, int n, double **A) {

   double relerr;
   double *a = A[0];
   double ref = 3.0;
   int    i, errors = 0;
   char   *marker;
   double TOL   = 100.0 * DBL_EPSILON;
   double SMALL = 100.0 * DBL_MIN;

   if ( (marker=(char *)malloc(m*n*sizeof(char))) == NULL ) {
        perror("array marker");
        exit(-1);
   }

   for (i=0; i<m*n; i++)
   {
       relerr = fabs((a[i]-ref));
       if ( relerr <= TOL )
       {
          marker[i] = ' ';
       }
       else
       {
          errors++;
          marker[i] = '*';
       }
   }
   if ( errors > 0 )
   {
     printf("Routine: %s\n",comment);
     printf("Found %d differences in results for m=%d n=%d:\n",
             errors,m,n);
     for (i=0; i<m*n; i++)
         printf("\t%c a[%d]=%f ref[%d]=%f\n",marker[i],i,a[i],i,ref);
   }
   return(errors);
}
*/
/* Routine for allocating two-dimensional array */
/*
double **
malloc_2d(int m, int n)
{
    int i;

    if (m <= 0 || n <= 0)
        return NULL;

    double **A = malloc(m * sizeof(double *));
    if (A == NULL)
        return NULL;

    A[0] = malloc(m*n*sizeof(double));
    if (A[0] == NULL) {
        free(A);
        return NULL;
    }
    for (i = 1; i < m; i++)
        A[i] = A[0] + i * n;

    return A;
}
*/
void free_2d(double **A) {
  free(A[0]);
  free(A);
}

	.text
	.file	"datatools.hip"
	.globl	_Z11init_matrixiiPdd            # -- Begin function _Z11init_matrixiiPdd
	.type	_Z11init_matrixiiPdd,@function
_Z11init_matrixiiPdd:                   # @_Z11init_matrixiiPdd
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jle	.LBB0_6
# %bb.1:                                # %.preheader.lr.ph
	movl	%edi, %eax
	movl	%esi, %ecx
	xorl	%edi, %edi
	xorl	%r8d, %r8d
.LBB0_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_4 Depth 2
	testl	%esi, %esi
	jle	.LBB0_5
# %bb.3:                                # %.lr.ph
                                        #   in Loop: Header=BB0_2 Depth=1
	movl	%edi, %r9d
	leaq	(%rdx,%r9,8), %r9
	xorl	%r10d, %r10d
.LBB0_4:                                #   Parent Loop BB0_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movsd	%xmm0, (%r9,%r10,8)
	incq	%r10
	cmpq	%r10, %rcx
	jne	.LBB0_4
.LBB0_5:                                # %._crit_edge
                                        #   in Loop: Header=BB0_2 Depth=1
	incq	%r8
	addl	%esi, %edi
	cmpq	%rax, %r8
	jne	.LBB0_2
.LBB0_6:                                # %._crit_edge14
	retq
.Lfunc_end0:
	.size	_Z11init_matrixiiPdd, .Lfunc_end0-_Z11init_matrixiiPdd
	.cfi_endproc
                                        # -- End function
	.globl	_Z12print_matrixiiPPd           # -- Begin function _Z12print_matrixiiPPd
	.type	_Z12print_matrixiiPPd,@function
_Z12print_matrixiiPPd:                  # @_Z12print_matrixiiPPd
	.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	$24, %rsp
	.cfi_def_cfa_offset 80
	.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	%rdx, 8(%rsp)                   # 8-byte Spill
	movl	%esi, 4(%rsp)                   # 4-byte Spill
	movl	%edi, %ebx
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	testl	%ebx, %ebx
	jle	.LBB1_6
# %bb.1:                                # %.preheader.lr.ph
	movl	%ebx, %r15d
	movl	%ebx, %eax
	movq	%rax, 16(%rsp)                  # 8-byte Spill
	movl	4(%rsp), %r12d                  # 4-byte Reload
	xorl	%r13d, %r13d
	xorl	%ebx, %ebx
.LBB1_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_4 Depth 2
	cmpl	$0, 4(%rsp)                     # 4-byte Folded Reload
	jle	.LBB1_5
# %bb.3:                                # %.lr.ph
                                        #   in Loop: Header=BB1_2 Depth=1
	movl	%r13d, %eax
	movq	8(%rsp), %rcx                   # 8-byte Reload
	leaq	(%rcx,%rax,8), %rbp
	xorl	%r14d, %r14d
.LBB1_4:                                #   Parent Loop BB1_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	(%rbp,%r14,8), %rsi
	movl	$.L.str.1, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%r14
	cmpq	%r14, %r12
	jne	.LBB1_4
.LBB1_5:                                # %._crit_edge
                                        #   in Loop: Header=BB1_2 Depth=1
	movl	$10, %edi
	callq	putchar@PLT
	incq	%rbx
	addl	%r15d, %r13d
	cmpq	16(%rsp), %rbx                  # 8-byte Folded Reload
	jne	.LBB1_2
.LBB1_6:                                # %._crit_edge14
	movl	$.Lstr.1, %edi
	addq	$24, %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
	jmp	puts@PLT                        # TAILCALL
.Lfunc_end1:
	.size	_Z12print_matrixiiPPd, .Lfunc_end1-_Z12print_matrixiiPPd
	.cfi_endproc
                                        # -- End function
	.globl	_Z11init_vectoriPd              # -- Begin function _Z11init_vectoriPd
	.type	_Z11init_vectoriPd,@function
_Z11init_vectoriPd:                     # @_Z11init_vectoriPd
	.cfi_startproc
# %bb.0:
	testl	%edi, %edi
	jle	.LBB2_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%edi, %eax
	xorl	%ecx, %ecx
	movabsq	$4607182418800017408, %rdx      # imm = 0x3FF0000000000000
.LBB2_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	%rdx, (%rsi,%rcx,8)
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB2_2
.LBB2_3:                                # %._crit_edge
	retq
.Lfunc_end2:
	.size	_Z11init_vectoriPd, .Lfunc_end2-_Z11init_vectoriPd
	.cfi_endproc
                                        # -- End function
	.globl	_Z7free_2dPPd                   # -- Begin function _Z7free_2dPPd
	.type	_Z7free_2dPPd,@function
_Z7free_2dPPd:                          # @_Z7free_2dPPd
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movq	(%rdi), %rdi
	callq	free
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	free                            # TAILCALL
.Lfunc_end3:
	.size	_Z7free_2dPPd, .Lfunc_end3-_Z7free_2dPPd
	.cfi_endproc
                                        # -- End function
	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"%6.2f, "
	.size	.L.str.1, 8

	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.type	.Lstr.1,@object                 # @str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr.1:
	.asciz	"----------------------------------"
	.size	.Lstr.1, 35

	.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
