#include <cuda.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//----------------------------------------------
// 排序 N 個 float 元素 (N=1~1025)
// 使用到的記憶體大小為 SIZE 個 bytes
// 只使用單一區塊
// 區塊大小為 N/2
//----------------------------------------------
#define N      1024
#define SIZE   (N*sizeof(float))

#define GRID   1
#define BLOCK  (N/2)

#define testLoop 1000  //測試效能時的 loop 數

//----------------------------------------------
// 交換函式 (host 和 kernel 都可以使用) 
// 因為加了 __host__ 和 __device__ 兩個標籤
//----------------------------------------------
inline __host__ __device__ void swap(float& a, float& b){
        float c=a;
        a=b;
        b=c;
}

//----------------------------------------------
// 泡泡的 kernel (由小到大排列 N 個元素 a->r)
//----------------------------------------------
__global__ void bubble(float *r, float *a){
        //*** blockDim=N/2 ***
        int j=threadIdx.x;      //j=0,1,2,...blockDim-1 
        int k=2*threadIdx.x;    //k=0,2,4,...2*(blockDim-1) 配對的基底索引

        //配置共享記憶體
        __shared__ float s[N+20];

        //載入資料到共享記憶體
        __syncthreads();   //同步化執行緒, 加速載入速度 (合併讀取 coalesced)
        s[j]=a[j];         //使用全部執行緒一起載入前半段 (0~N/2-1)
        s[j+N/2]=a[j+N/2]; //使用全部執行緒一起載入後半段 (N/2~N-1)
        if(j==0){
                //若 N 為奇數時, 還要多載入一個尾巴, 只使用第 0 個執行緒
                s[N-1]=a[N-1];
        }

        //開始泡泡排序
        for(int loop=0; loop<=N/2; loop++){
                //排列 0 based 配對資料 (0,1) (2,3) (4,5) .... 
                __syncthreads();  //同步化確保共享記憶體已寫入
                if(s[k]>s[k+1]){
                        swap(s[k],s[k+1]);
                }

                //排列 1 based 配對資料 (1,2) (3,4) (5,6) .... 
                __syncthreads();  //同步化確保共享記憶體已寫入
                if(s[k+1]>s[k+2]){
                        if(k<N-2) //若 N 為偶數時, 最後一個執行緒不作用
                        swap(s[k+1],s[k+2]);
                }
        }

        //轉出資料到全域記憶體
        __syncthreads();
        r[j]=s[j];
        r[j+N/2]=s[j+N/2];
        if(j==0){
                r[N-1]=s[N-1];
        }

}


//----------------------------------------------
// 泡泡的 host 函數 
//----------------------------------------------
void bubble_host(float *r, float *a){
        //載入資料
        for(int k=0; k<N; k++){
                r[k]=a[k];
        }

        for(int loop=0; loop<=N/2; loop++){
                //排列 0 based 配對資料
                for(int k=0; k<N-1; k+=2){
                        if(r[k]>r[k+1]){
                                swap(r[k],r[k+1]);
                        }
                }
                //排列 1 based 配對資料
                for(int k=1; k<N-1; k+=2){
                        if(r[k]>r[k+1]){
                                swap(r[k],r[k+1]);
                        }
                }
        }
}


//----------------------------------------------
// 主程式
//----------------------------------------------
int main(){
        //配置 host 記憶體
        float *a=(float*)malloc(SIZE);
        float *b=(float*)malloc(SIZE);
        float *c=(float*)malloc(SIZE);


        //初始化
        for(int k=0; k<N; k++){
                a[k]=k;
                c[k]=0;
        }

        //對陣列 a 洗牌
        srand(time(0));
        for(int k=0; k<2*N; k++){
                int i=rand()%N;
                int j=rand()%N;
                swap(a[i],a[j]);
        }

        //配置 device 記憶體
        float  *ga, *gc;
        cudaMalloc((void**)&ga, SIZE);
        cudaMalloc((void**)&gc, SIZE);

        //載入 (順便載入 c 來清空裝置記憶體內容)
        cudaMemcpy(ga, a, SIZE, cudaMemcpyHostToDevice);
        cudaMemcpy(gc, c, SIZE, cudaMemcpyHostToDevice);


        //測試 kernel 效能
        double t0=(double)clock()/CLOCKS_PER_SEC;
                for(int k=0; k<testLoop; k++){
                        //呼叫 kernel (此為單一 block 的版本)
                        bubble<<<1,BLOCK>>>(gc,ga);

                        //同步化執行緒, 避免還沒做完, 量到不正確的時間
                        cudaThreadSynchronize();
                }
        t0=((double)clock()/CLOCKS_PER_SEC-t0)/testLoop;

        //測試 host 效能
        double t1=(double)clock()/CLOCKS_PER_SEC;
                for(int k=0; k<testLoop; k++){
                        bubble_host(b,a);
                }
        t1=((double)clock()/CLOCKS_PER_SEC-t1)/testLoop;

        //顯示計算時間, 並比較
        printf("time[gpu]: %g ms\n",t0*1000);
        printf("time[host]: %g ms\n",t1*1000);
        printf("ratio: %g x\n",t1/t0);

        //讀出 device 資料
        cudaMemcpy(c, gc, SIZE, cudaMemcpyDeviceToHost);


        //測試 device 結果的正確性
        printf("------------------------\n");
        bool flag=true;
        for(int k=0; k<N; k++){
                if(c[k]!=k){
                        flag=false;
                        break;
                }
        }
        printf("test[gpu]: %s\n",flag?"pass":"fail");

        //測試 host 結果的正確性
        flag=true;
        for(int k=0; k<N; k++){
                if(b[k]!=k){
                        flag=false;
                        break;
                }
        }
        printf("test[host]: %s\n",flag?"pass":"fail");

        //釋放記憶體
        cudaFree(ga);
        cudaFree(gc);
        free(a);
        free(b);
        free(c);


        return 0;
}

	.file	"tmpxft_002d00b7_00000000-6_code.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
	.globl	_Z11bubble_hostPfS_
	.type	_Z11bubble_hostPfS_, @function
_Z11bubble_hostPfS_:
.LFB2028:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
.L3:
	movss	(%rsi,%rax), %xmm0
	movss	%xmm0, (%rdi,%rax)
	addq	$4, %rax
	cmpq	$4096, %rax
	jne	.L3
	movl	$513, %edx
	leaq	4096(%rdi), %rsi
	leaq	4092(%rdi), %rcx
.L4:
	movq	%rdi, %rax
.L7:
	movss	(%rax), %xmm0
	movss	4(%rax), %xmm1
	comiss	%xmm1, %xmm0
	jbe	.L5
	movss	%xmm1, (%rax)
	movss	%xmm0, 4(%rax)
.L5:
	addq	$8, %rax
	cmpq	%rsi, %rax
	jne	.L7
	leaq	4(%rdi), %rax
.L10:
	movss	(%rax), %xmm0
	movss	4(%rax), %xmm1
	comiss	%xmm1, %xmm0
	jbe	.L8
	movss	%xmm1, (%rax)
	movss	%xmm0, 4(%rax)
.L8:
	addq	$8, %rax
	cmpq	%rcx, %rax
	jne	.L10
	decl	%edx
	jne	.L4
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z11bubble_hostPfS_, .-_Z11bubble_hostPfS_
	.globl	_Z27__device_stub__Z6bubblePfS_PfS_
	.type	_Z27__device_stub__Z6bubblePfS_PfS_, @function
_Z27__device_stub__Z6bubblePfS_PfS_:
.LFB2054:
	.cfi_startproc
	endbr64
	subq	$120, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	32(%rsp), %rcx
	leaq	24(%rsp), %rdx
	movq	%rsi, (%rsp)
	leaq	40(%rsp), %rdi
	leaq	52(%rsp), %rsi
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 48(%rsp)
	movq	%rax, 88(%rsp)
	movq	%rsp, %rax
	movq	%rax, 96(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 40(%rsp)
	movq	%rax, 52(%rsp)
	movl	$1, 60(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L18
	pushq	32(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_Z6bubblePfS_(%rip), %rdi
	pushq	32(%rsp)
	.cfi_def_cfa_offset 144
	movq	68(%rsp), %rcx
	movl	76(%rsp), %r8d
	movq	56(%rsp), %rsi
	movl	64(%rsp), %edx
	leaq	104(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 136
	popq	%rdx
	.cfi_def_cfa_offset 128
.L18:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L20
	call	__stack_chk_fail@PLT
.L20:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2054:
	.size	_Z27__device_stub__Z6bubblePfS_PfS_, .-_Z27__device_stub__Z6bubblePfS_PfS_
	.globl	_Z6bubblePfS_
	.type	_Z6bubblePfS_, @function
_Z6bubblePfS_:
.LFB2055:
	.cfi_startproc
	endbr64
	jmp	_Z27__device_stub__Z6bubblePfS_PfS_
	.cfi_endproc
.LFE2055:
	.size	_Z6bubblePfS_, .-_Z6bubblePfS_
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"pass"
.LC1:
	.string	"fail"
.LC4:
	.string	"time[gpu]: %g ms\n"
.LC5:
	.string	"time[host]: %g ms\n"
.LC6:
	.string	"ratio: %g x\n"
.LC7:
	.string	"------------------------\n"
.LC8:
	.string	"test[gpu]: %s\n"
.LC9:
	.string	"test[host]: %s\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
	movl	$4096, %edi
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	movl	$4096, %edi
	movq	%rax, %rbp
	call	malloc@PLT
	movl	$4096, %edi
	movq	%rax, %r12
	call	malloc@PLT
	xorl	%ecx, %ecx
	movq	%rax, %rbx
.L24:
	cvtsi2ssl	%ecx, %xmm0
	movss	%xmm0, 0(%rbp,%rcx,4)
	incq	%rcx
	cmpq	$1024, %rcx
	jne	.L24
	xorl	%eax, %eax
	movq	%rbx, %rdi
	movl	$2048, %r15d
	movl	$1024, %r14d
	rep stosl
	xorl	%edi, %edi
	call	time@PLT
	movl	%eax, %edi
	call	srand@PLT
.L25:
	call	rand@PLT
	movl	%eax, %r13d
	call	rand@PLT
	cltd
	idivl	%r14d
	movl	%r13d, %eax
	movslq	%edx, %rdx
	leaq	0(%rbp,%rdx,4), %rcx
	cltd
	idivl	%r14d
	movss	(%rcx), %xmm1
	movslq	%edx, %rdx
	leaq	0(%rbp,%rdx,4), %rax
	movss	(%rax), %xmm0
	movss	%xmm1, (%rax)
	movss	%xmm0, (%rcx)
	decl	%r15d
	jne	.L25
	leaq	16(%rsp), %rdi
	movl	$4096, %esi
	movl	$1000, %r13d
	movabsq	$4294967808, %r14
	call	cudaMalloc@PLT
	leaq	24(%rsp), %rdi
	movl	$4096, %esi
	call	cudaMalloc@PLT
	movq	16(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbp, %rsi
	movl	$4096, %edx
	call	cudaMemcpy@PLT
	movq	24(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbx, %rsi
	movl	$4096, %edx
	call	cudaMemcpy@PLT
	call	clock@PLT
	cvtsi2sdq	%rax, %xmm0
	divsd	.LC2(%rip), %xmm0
	movsd	%xmm0, (%rsp)
.L27:
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	%r14, %rdx
	movl	$1, %ecx
	movabsq	$4294967297, %rdi
	movl	$1, %esi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L26
	movq	16(%rsp), %rsi
	movq	24(%rsp), %rdi
	call	_Z27__device_stub__Z6bubblePfS_PfS_
.L26:
	call	cudaThreadSynchronize@PLT
	decl	%r13d
	jne	.L27
	call	clock@PLT
	cvtsi2sdq	%rax, %xmm0
	divsd	.LC2(%rip), %xmm0
	subsd	(%rsp), %xmm0
	divsd	.LC3(%rip), %xmm0
	movsd	%xmm0, (%rsp)
	call	clock@PLT
	movl	$1000, %r8d
	cvtsi2sdq	%rax, %xmm0
	divsd	.LC2(%rip), %xmm0
	movsd	%xmm0, 8(%rsp)
.L28:
	movq	%rbp, %rsi
	movq	%r12, %rdi
	call	_Z11bubble_hostPfS_
	decl	%r8d
	jne	.L28
	call	clock@PLT
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	cvtsi2sdq	%rax, %xmm0
	divsd	.LC2(%rip), %xmm0
	movb	$1, %al
	subsd	8(%rsp), %xmm0
	divsd	.LC3(%rip), %xmm0
	movsd	%xmm0, 8(%rsp)
	movsd	.LC3(%rip), %xmm0
	mulsd	(%rsp), %xmm0
	call	__printf_chk@PLT
	movsd	.LC3(%rip), %xmm0
	mulsd	8(%rsp), %xmm0
	movb	$1, %al
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	movsd	8(%rsp), %xmm0
	divsd	(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	24(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbx, %rdi
	movl	$4096, %edx
	call	cudaMemcpy@PLT
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%eax, %eax
.L31:
	cvtsi2ssl	%eax, %xmm0
	ucomiss	(%rbx,%rax,4), %xmm0
	jp	.L36
	jne	.L36
	incq	%rax
	cmpq	$1024, %rax
	jne	.L31
	leaq	.LC0(%rip), %rdx
	jmp	.L29
.L36:
	leaq	.LC1(%rip), %rdx
.L29:
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%eax, %eax
.L34:
	cvtsi2ssl	%eax, %xmm0
	ucomiss	(%r12,%rax,4), %xmm0
	jp	.L37
	jne	.L37
	incq	%rax
	cmpq	$1024, %rax
	jne	.L34
	leaq	.LC0(%rip), %rdx
	jmp	.L32
.L37:
	leaq	.LC1(%rip), %rdx
.L32:
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	%rbp, %rdi
	call	free@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	%rbx, %rdi
	call	free@PLT
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L35
	call	__stack_chk_fail@PLT
.L35:
	addq	$72, %rsp
	.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
	.section	.rodata.str1.1
.LC10:
	.string	"_Z6bubblePfS_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2057:
	.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	.LC10(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z6bubblePfS_(%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
.LFE2057:
	.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
.LC2:
	.long	0
	.long	1093567616
	.align 8
.LC3:
	.long	0
	.long	1083129856
	.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 <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//----------------------------------------------
// 排序 N 個 float 元素 (N=1~1025)
// 使用到的記憶體大小為 SIZE 個 bytes
// 只使用單一區塊
// 區塊大小為 N/2
//----------------------------------------------
#define N      1024
#define SIZE   (N*sizeof(float))

#define GRID   1
#define BLOCK  (N/2)

#define testLoop 1000  //測試效能時的 loop 數

//----------------------------------------------
// 交換函式 (host 和 kernel 都可以使用) 
// 因為加了 __host__ 和 __device__ 兩個標籤
//----------------------------------------------
inline __host__ __device__ void swap(float& a, float& b){
        float c=a;
        a=b;
        b=c;
}

//----------------------------------------------
// 泡泡的 kernel (由小到大排列 N 個元素 a->r)
//----------------------------------------------
__global__ void bubble(float *r, float *a){
        //*** blockDim=N/2 ***
        int j=threadIdx.x;      //j=0,1,2,...blockDim-1 
        int k=2*threadIdx.x;    //k=0,2,4,...2*(blockDim-1) 配對的基底索引

        //配置共享記憶體
        __shared__ float s[N+20];

        //載入資料到共享記憶體
        __syncthreads();   //同步化執行緒, 加速載入速度 (合併讀取 coalesced)
        s[j]=a[j];         //使用全部執行緒一起載入前半段 (0~N/2-1)
        s[j+N/2]=a[j+N/2]; //使用全部執行緒一起載入後半段 (N/2~N-1)
        if(j==0){
                //若 N 為奇數時, 還要多載入一個尾巴, 只使用第 0 個執行緒
                s[N-1]=a[N-1];
        }

        //開始泡泡排序
        for(int loop=0; loop<=N/2; loop++){
                //排列 0 based 配對資料 (0,1) (2,3) (4,5) .... 
                __syncthreads();  //同步化確保共享記憶體已寫入
                if(s[k]>s[k+1]){
                        swap(s[k],s[k+1]);
                }

                //排列 1 based 配對資料 (1,2) (3,4) (5,6) .... 
                __syncthreads();  //同步化確保共享記憶體已寫入
                if(s[k+1]>s[k+2]){
                        if(k<N-2) //若 N 為偶數時, 最後一個執行緒不作用
                        swap(s[k+1],s[k+2]);
                }
        }

        //轉出資料到全域記憶體
        __syncthreads();
        r[j]=s[j];
        r[j+N/2]=s[j+N/2];
        if(j==0){
                r[N-1]=s[N-1];
        }

}


//----------------------------------------------
// 泡泡的 host 函數 
//----------------------------------------------
void bubble_host(float *r, float *a){
        //載入資料
        for(int k=0; k<N; k++){
                r[k]=a[k];
        }

        for(int loop=0; loop<=N/2; loop++){
                //排列 0 based 配對資料
                for(int k=0; k<N-1; k+=2){
                        if(r[k]>r[k+1]){
                                swap(r[k],r[k+1]);
                        }
                }
                //排列 1 based 配對資料
                for(int k=1; k<N-1; k+=2){
                        if(r[k]>r[k+1]){
                                swap(r[k],r[k+1]);
                        }
                }
        }
}


//----------------------------------------------
// 主程式
//----------------------------------------------
int main(){
        //配置 host 記憶體
        float *a=(float*)malloc(SIZE);
        float *b=(float*)malloc(SIZE);
        float *c=(float*)malloc(SIZE);


        //初始化
        for(int k=0; k<N; k++){
                a[k]=k;
                c[k]=0;
        }

        //對陣列 a 洗牌
        srand(time(0));
        for(int k=0; k<2*N; k++){
                int i=rand()%N;
                int j=rand()%N;
                swap(a[i],a[j]);
        }

        //配置 device 記憶體
        float  *ga, *gc;
        hipMalloc((void**)&ga, SIZE);
        hipMalloc((void**)&gc, SIZE);

        //載入 (順便載入 c 來清空裝置記憶體內容)
        hipMemcpy(ga, a, SIZE, hipMemcpyHostToDevice);
        hipMemcpy(gc, c, SIZE, hipMemcpyHostToDevice);


        //測試 kernel 效能
        double t0=(double)clock()/CLOCKS_PER_SEC;
                for(int k=0; k<testLoop; k++){
                        //呼叫 kernel (此為單一 block 的版本)
                        bubble<<<1,BLOCK>>>(gc,ga);

                        //同步化執行緒, 避免還沒做完, 量到不正確的時間
                        hipDeviceSynchronize();
                }
        t0=((double)clock()/CLOCKS_PER_SEC-t0)/testLoop;

        //測試 host 效能
        double t1=(double)clock()/CLOCKS_PER_SEC;
                for(int k=0; k<testLoop; k++){
                        bubble_host(b,a);
                }
        t1=((double)clock()/CLOCKS_PER_SEC-t1)/testLoop;

        //顯示計算時間, 並比較
        printf("time[gpu]: %g ms\n",t0*1000);
        printf("time[host]: %g ms\n",t1*1000);
        printf("ratio: %g x\n",t1/t0);

        //讀出 device 資料
        hipMemcpy(c, gc, SIZE, hipMemcpyDeviceToHost);


        //測試 device 結果的正確性
        printf("------------------------\n");
        bool flag=true;
        for(int k=0; k<N; k++){
                if(c[k]!=k){
                        flag=false;
                        break;
                }
        }
        printf("test[gpu]: %s\n",flag?"pass":"fail");

        //測試 host 結果的正確性
        flag=true;
        for(int k=0; k<N; k++){
                if(b[k]!=k){
                        flag=false;
                        break;
                }
        }
        printf("test[host]: %s\n",flag?"pass":"fail");

        //釋放記憶體
        hipFree(ga);
        hipFree(gc);
        free(a);
        free(b);
        free(c);


        return 0;
}

	.text
	.file	"code.hip"
	.globl	_Z21__device_stub__bubblePfS_   # -- Begin function _Z21__device_stub__bubblePfS_
	.type	_Z21__device_stub__bubblePfS_,@function
_Z21__device_stub__bubblePfS_:          # @_Z21__device_stub__bubblePfS_
	.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	$80, %rsp
	.cfi_def_cfa_offset 128
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	16(%rsp), %rcx
	movq	%rsi, (%rcx)
	leaq	64(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%rsp), %r15
	leaq	8(%rsp), %r12
	movq	%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	$_Z6bubblePfS_, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$96, %rsp
	.cfi_adjust_cfa_offset -96
	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_end0:
	.size	_Z21__device_stub__bubblePfS_, .Lfunc_end0-_Z21__device_stub__bubblePfS_
	.cfi_endproc
                                        # -- End function
	.globl	_Z11bubble_hostPfS_             # -- Begin function _Z11bubble_hostPfS_
	.type	_Z11bubble_hostPfS_,@function
_Z11bubble_hostPfS_:                    # @_Z11bubble_hostPfS_
	.cfi_startproc
# %bb.0:
	xorl	%eax, %eax
.LBB1_1:                                # =>This Inner Loop Header: Depth=1
	movss	(%rsi,%rax,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, (%rdi,%rax,4)
	incq	%rax
	cmpq	$1024, %rax                     # imm = 0x400
	jne	.LBB1_1
# %bb.2:                                # %.preheader30.preheader
	xorl	%eax, %eax
.LBB1_3:                                # %.preheader30
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_4 Depth 2
                                        #     Child Loop BB1_8 Depth 2
	movq	$-2, %rcx
.LBB1_4:                                #   Parent Loop BB1_3 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	8(%rdi,%rcx,4), %xmm0           # xmm0 = mem[0],zero,zero,zero
	movss	12(%rdi,%rcx,4), %xmm1          # xmm1 = mem[0],zero,zero,zero
	ucomiss	%xmm1, %xmm0
	jbe	.LBB1_6
# %bb.5:                                #   in Loop: Header=BB1_4 Depth=2
	movss	%xmm1, 8(%rdi,%rcx,4)
	movss	%xmm0, 12(%rdi,%rcx,4)
.LBB1_6:                                #   in Loop: Header=BB1_4 Depth=2
	addq	$2, %rcx
	cmpq	$1021, %rcx                     # imm = 0x3FD
	jb	.LBB1_4
# %bb.7:                                # %.preheader.preheader
                                        #   in Loop: Header=BB1_3 Depth=1
	movq	$-1, %rcx
.LBB1_8:                                # %.preheader
                                        #   Parent Loop BB1_3 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movss	8(%rdi,%rcx,4), %xmm0           # xmm0 = mem[0],zero,zero,zero
	movss	12(%rdi,%rcx,4), %xmm1          # xmm1 = mem[0],zero,zero,zero
	ucomiss	%xmm1, %xmm0
	jbe	.LBB1_10
# %bb.9:                                #   in Loop: Header=BB1_8 Depth=2
	movss	%xmm1, 8(%rdi,%rcx,4)
	movss	%xmm0, 12(%rdi,%rcx,4)
.LBB1_10:                               #   in Loop: Header=BB1_8 Depth=2
	addq	$2, %rcx
	cmpq	$1021, %rcx                     # imm = 0x3FD
	jb	.LBB1_8
# %bb.11:                               #   in Loop: Header=BB1_3 Depth=1
	incl	%eax
	cmpl	$513, %eax                      # imm = 0x201
	jne	.LBB1_3
# %bb.12:
	retq
.Lfunc_end1:
	.size	_Z11bubble_hostPfS_, .Lfunc_end1-_Z11bubble_hostPfS_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI2_0:
	.quad	0x412e848000000000              # double 1.0E+6
.LCPI2_1:
	.quad	0x408f400000000000              # double 1000
	.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	$40, %rsp
	.cfi_def_cfa_offset 96
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	$4096, %edi                     # imm = 0x1000
	callq	malloc
	movq	%rax, %rbx
	movl	$4096, %edi                     # imm = 0x1000
	callq	malloc
	movq	%rax, %r14
	movl	$1, %edi
	movl	$4096, %esi                     # imm = 0x1000
	callq	calloc@PLT
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	xorl	%eax, %eax
.LBB2_1:                                # =>This Inner Loop Header: Depth=1
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	movss	%xmm0, (%rbx,%rax,4)
	incq	%rax
	cmpq	$1024, %rax                     # imm = 0x400
	jne	.LBB2_1
# %bb.2:
	xorl	%edi, %edi
	callq	time
	movl	%eax, %edi
	callq	srand
	movl	$2048, %ebp                     # imm = 0x800
	movl	$1023, %r13d                    # imm = 0x3FF
	movl	$-1024, %r15d                   # imm = 0xFC00
.LBB2_3:                                # =>This Inner Loop Header: Depth=1
	callq	rand
	movl	%eax, %r12d
	leal	(%r12,%r13), %eax
	testl	%r12d, %r12d
	cmovnsl	%r12d, %eax
	andl	%r15d, %eax
	subl	%eax, %r12d
	callq	rand
                                        # kill: def $eax killed $eax def $rax
	leal	(%rax,%r13), %ecx
	testl	%eax, %eax
	cmovnsl	%eax, %ecx
	andl	%r15d, %ecx
	subl	%ecx, %eax
	movslq	%r12d, %rcx
	cltq
	movss	(%rbx,%rcx,4), %xmm0            # xmm0 = mem[0],zero,zero,zero
	movss	(%rbx,%rax,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	movss	%xmm1, (%rbx,%rcx,4)
	movss	%xmm0, (%rbx,%rax,4)
	decl	%ebp
	jne	.LBB2_3
# %bb.4:
	movabsq	$4294967297, %r12               # imm = 0x100000001
	leaq	32(%rsp), %r13
	movl	$4096, %esi                     # imm = 0x1000
	movq	%r13, %rdi
	callq	hipMalloc
	leaq	16(%rsp), %rbp
	movl	$4096, %esi                     # imm = 0x1000
	movq	%rbp, %rdi
	callq	hipMalloc
	movq	(%r13), %rdi
	movl	$4096, %edx                     # imm = 0x1000
	movq	%rbx, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%rbp), %rdi
	movl	$4096, %edx                     # imm = 0x1000
	movq	24(%rsp), %r15                  # 8-byte Reload
	movq	%r15, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	callq	clock
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rax, %xmm0
	movsd	%xmm0, (%rsp)                   # 8-byte Spill
	movl	$1000, %ebp                     # imm = 0x3E8
	leaq	511(%r12), %r13
.LBB2_5:                                # =>This Inner Loop Header: Depth=1
	movq	%r12, %rdi
	movl	$1, %esi
	movq	%r13, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_7
# %bb.6:                                #   in Loop: Header=BB2_5 Depth=1
	movq	16(%rsp), %rdi
	movq	32(%rsp), %rsi
	callq	_Z21__device_stub__bubblePfS_
.LBB2_7:                                #   in Loop: Header=BB2_5 Depth=1
	callq	hipDeviceSynchronize
	decl	%ebp
	jne	.LBB2_5
# %bb.8:
	movsd	(%rsp), %xmm0                   # 8-byte Reload
                                        # xmm0 = mem[0],zero
	divsd	.LCPI2_0(%rip), %xmm0
	movsd	%xmm0, (%rsp)                   # 8-byte Spill
	callq	clock
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rax, %xmm0
	divsd	.LCPI2_0(%rip), %xmm0
	subsd	(%rsp), %xmm0                   # 8-byte Folded Reload
	movsd	%xmm0, (%rsp)                   # 8-byte Spill
	callq	clock
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rax, %xmm0
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	movl	$1000, %ebp                     # imm = 0x3E8
.LBB2_9:                                # =>This Inner Loop Header: Depth=1
	movq	%r14, %rdi
	movq	%rbx, %rsi
	callq	_Z11bubble_hostPfS_
	decl	%ebp
	jne	.LBB2_9
# %bb.10:
	movsd	(%rsp), %xmm0                   # 8-byte Reload
                                        # xmm0 = mem[0],zero
	divsd	.LCPI2_1(%rip), %xmm0
	movsd	%xmm0, (%rsp)                   # 8-byte Spill
	movsd	.LCPI2_0(%rip), %xmm1           # xmm1 = mem[0],zero
	movsd	8(%rsp), %xmm0                  # 8-byte Reload
                                        # xmm0 = mem[0],zero
	divsd	%xmm1, %xmm0
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	callq	clock
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rax, %xmm0
	divsd	.LCPI2_0(%rip), %xmm0
	subsd	8(%rsp), %xmm0                  # 8-byte Folded Reload
	movsd	.LCPI2_1(%rip), %xmm1           # xmm1 = mem[0],zero
	divsd	%xmm1, %xmm0
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	movsd	(%rsp), %xmm0                   # 8-byte Reload
                                        # xmm0 = mem[0],zero
	mulsd	%xmm1, %xmm0
	movl	$.L.str, %edi
	movb	$1, %al
	callq	printf
	movsd	.LCPI2_1(%rip), %xmm0           # xmm0 = mem[0],zero
	mulsd	8(%rsp), %xmm0                  # 8-byte Folded Reload
	movl	$.L.str.1, %edi
	movb	$1, %al
	callq	printf
	movsd	8(%rsp), %xmm0                  # 8-byte Reload
                                        # xmm0 = mem[0],zero
	divsd	(%rsp), %xmm0                   # 8-byte Folded Reload
	movl	$.L.str.2, %edi
	movb	$1, %al
	callq	printf
	movq	16(%rsp), %rsi
	movl	$4096, %edx                     # imm = 0x1000
	movq	%r15, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movl	$.Lstr, %edi
	callq	puts@PLT
	xorl	%eax, %eax
.LBB2_11:                               # =>This Inner Loop Header: Depth=1
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	movss	(%r15,%rax,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	ucomiss	%xmm0, %xmm1
	jne	.LBB2_14
	jp	.LBB2_14
# %bb.12:                               #   in Loop: Header=BB2_11 Depth=1
	incq	%rax
	cmpq	$1024, %rax                     # imm = 0x400
	jne	.LBB2_11
# %bb.13:
	movl	$.L.str.5, %esi
	jmp	.LBB2_15
.LBB2_14:
	movl	$.L.str.6, %esi
.LBB2_15:
	xorl	%r15d, %r15d
	movl	$.L.str.4, %edi
	xorl	%eax, %eax
	callq	printf
.LBB2_16:                               # =>This Inner Loop Header: Depth=1
	xorps	%xmm0, %xmm0
	cvtsi2ss	%r15d, %xmm0
	movss	(%r14,%r15,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	ucomiss	%xmm0, %xmm1
	jne	.LBB2_19
	jp	.LBB2_19
# %bb.17:                               #   in Loop: Header=BB2_16 Depth=1
	incq	%r15
	cmpq	$1024, %r15                     # imm = 0x400
	jne	.LBB2_16
# %bb.18:
	movl	$.L.str.5, %esi
	jmp	.LBB2_20
.LBB2_19:
	movl	$.L.str.6, %esi
.LBB2_20:
	movl	$.L.str.7, %edi
	xorl	%eax, %eax
	callq	printf
	movq	32(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	movq	24(%rsp), %rdi                  # 8-byte Reload
	callq	free
	xorl	%eax, %eax
	addq	$40, %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
.Lfunc_end2:
	.size	main, .Lfunc_end2-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	.LBB3_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB3_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z6bubblePfS_, %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_end3:
	.size	__hip_module_ctor, .Lfunc_end3-__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	.LBB4_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
.LBB4_2:
	retq
.Lfunc_end4:
	.size	__hip_module_dtor, .Lfunc_end4-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z6bubblePfS_,@object           # @_Z6bubblePfS_
	.section	.rodata,"a",@progbits
	.globl	_Z6bubblePfS_
	.p2align	3, 0x0
_Z6bubblePfS_:
	.quad	_Z21__device_stub__bubblePfS_
	.size	_Z6bubblePfS_, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"time[gpu]: %g ms\n"
	.size	.L.str, 18

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"time[host]: %g ms\n"
	.size	.L.str.1, 19

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"ratio: %g x\n"
	.size	.L.str.2, 13

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"test[gpu]: %s\n"
	.size	.L.str.4, 15

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

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"fail"
	.size	.L.str.6, 5

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"test[host]: %s\n"
	.size	.L.str.7, 16

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z6bubblePfS_"
	.size	.L__unnamed_1, 14

	.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	"------------------------"
	.size	.Lstr, 25

	.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 _Z21__device_stub__bubblePfS_
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z6bubblePfS_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z6bubblePfS_           ; -- Begin function _Z6bubblePfS_
	.globl	_Z6bubblePfS_
	.p2align	8
	.type	_Z6bubblePfS_,@function
_Z6bubblePfS_:                          ; @_Z6bubblePfS_
; %bb.0:
	s_load_b128 s[4:7], s[0:1], 0x0
	v_dual_mov_b32 v1, 0 :: v_dual_lshlrev_b32 v2, 2, v0
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	v_cmp_eq_u32_e32 vcc_lo, 0, v0
	s_clause 0x1
	global_load_b32 v3, v2, s[6:7]
	global_load_b32 v4, v2, s[6:7] offset:2048
	s_waitcnt vmcnt(0)
	ds_store_2addr_stride64_b32 v2, v3, v4 offset1:8
	s_and_saveexec_b32 s0, vcc_lo
	s_cbranch_execz .LBB0_2
; %bb.1:
	s_load_b32 s1, s[6:7], 0xffc
	s_waitcnt lgkmcnt(0)
	v_mov_b32_e32 v3, s1
	ds_store_b32 v1, v3 offset:4092
.LBB0_2:
	s_or_b32 exec_lo, exec_lo, s0
	v_cmp_gt_u32_e64 s0, 0x1ff, v0
	v_add_nc_u32_e32 v3, v2, v2
	s_movk_i32 s2, 0x201
.LBB0_3:                                ; =>This Inner Loop Header: Depth=1
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	ds_load_b64 v[0:1], v3
	s_mov_b32 s3, exec_lo
	s_waitcnt lgkmcnt(0)
	v_cmpx_gt_f32_e32 v0, v1
	s_cbranch_execz .LBB0_5
; %bb.4:                                ;   in Loop: Header=BB0_3 Depth=1
	v_dual_mov_b32 v4, v1 :: v_dual_mov_b32 v5, v0
	ds_store_b64 v3, v[4:5]
.LBB0_5:                                ;   in Loop: Header=BB0_3 Depth=1
	s_or_b32 exec_lo, exec_lo, s3
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	ds_load_2addr_b32 v[0:1], v3 offset0:1 offset1:2
	s_waitcnt lgkmcnt(0)
	v_cmp_gt_f32_e64 s1, v0, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s3, s0, s1
	s_and_saveexec_b32 s1, s3
	s_cbranch_execz .LBB0_7
; %bb.6:                                ;   in Loop: Header=BB0_3 Depth=1
	ds_store_2addr_b32 v3, v1, v0 offset0:1 offset1:2
.LBB0_7:                                ;   in Loop: Header=BB0_3 Depth=1
	s_or_b32 exec_lo, exec_lo, s1
	s_add_i32 s2, s2, -1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_eq_u32 s2, 0
	s_cbranch_scc0 .LBB0_3
; %bb.8:
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	ds_load_2addr_stride64_b32 v[0:1], v2 offset1:8
	s_waitcnt lgkmcnt(0)
	s_clause 0x1
	global_store_b32 v2, v0, s[4:5]
	global_store_b32 v2, v1, s[4:5] offset:2048
	s_and_saveexec_b32 s0, vcc_lo
	s_cbranch_execz .LBB0_10
; %bb.9:
	v_mov_b32_e32 v0, 0
	ds_load_b32 v1, v0 offset:4092
	s_waitcnt lgkmcnt(0)
	global_store_b32 v0, v1, s[4:5] offset:4092
.LBB0_10:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z6bubblePfS_
		.amdhsa_group_segment_fixed_size 4176
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 16
		.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 6
		.amdhsa_next_free_sgpr 8
		.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	_Z6bubblePfS_, .Lfunc_end0-_Z6bubblePfS_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 356
; NumSgprs: 10
; NumVgprs: 6
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 4176 bytes/workgroup (compile time only)
; SGPRBlocks: 1
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 10
; NumVGPRsForWavesPerEU: 6
; 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
    .group_segment_fixed_size: 4176
    .kernarg_segment_align: 8
    .kernarg_segment_size: 16
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z6bubblePfS_
    .private_segment_fixed_size: 0
    .sgpr_count:     10
    .sgpr_spill_count: 0
    .symbol:         _Z6bubblePfS_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     6
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
