/*
** Originally copied from
** https://github.com/CodedK/CUDA-by-Example-source-code-for-the-book-s-examples-/blob/master/chapter06/ray_noconst.cu
** With a few bugs fixed
*/
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
#include <fstream>
#include <iostream>
using namespace std;

#define INF 2e10f
#define rnd(x) (x * rand() / RAND_MAX)
#define SPHERES 20
#define DIM 1024

#define uchar unsigned char
#define uint32_t unsigned int
#define uint16_t unsigned short
#define int32_t int

#pragma pack(push, 1)
struct BMPFileHeader {
    uint16_t file_type;          // File type always BM which is 0x4D42
    uint32_t file_size;               // Size of the file (in bytes)
    uint16_t reserved1;               // Reserved, always 0
    uint16_t reserved2;               // Reserved, always 0
    uint32_t offset_data;            // Start position of pixel data (bytes from the beginning of the file)
    BMPFileHeader() {
        file_type = 0x4D42;
        file_size = 0;
        reserved1 = 0;
        reserved2 = 0;
        offset_data = 54;
    };
};

struct BMPInfoHeader {
     uint32_t size;                      // Size of this header (in bytes)
     int32_t width;                      // width of bitmap in pixels
     int32_t height;                     // width of bitmap in pixels
                                              //       (if positive, bottom-up, with origin in lower left corner)
                                              //       (if negative, top-down, with origin in upper left corner)
     uint16_t planes;                    // No. of planes for the target device, this is always 1
     uint16_t bit_count;                 // No. of bits per pixel
     uint32_t compression;               // 0 or 3 - uncompressed. THIS PROGRAM CONSIDERS ONLY UNCOMPRESSED BMP images
     uint32_t size_image;                // 0 - for uncompressed images
     int32_t x_pixels_per_meter;
     int32_t y_pixels_per_meter;
     uint32_t colors_used;               // No. color indexes in the color table. Use 0 for the max number of colors allowed by bit_count
     uint32_t colors_important;          // No. of colors used for displaying the bitmap. If 0 all colors are required
     BMPInfoHeader() {
         size = 40;
         width = DIM;
         height = DIM;
         planes = 1;
         bit_count = 24;
         compression = 0;
         size_image = 0;
         x_pixels_per_meter = 0;
         y_pixels_per_meter = 0;
         colors_used = 0;
         colors_important = 0;
     }
 };

struct Header {
    BMPFileHeader fileheader;
    BMPInfoHeader infoheader;
    Header() {
        fileheader.file_size = 54 + 3 * DIM * DIM;
    }
};
#pragma pack(pop)

struct Sphere {
    float r, b, g;
    float radius;
    float x, y, z;
    
    __device__ float hit(float ox, float oy, float *n) {
        float dx = ox - x;
        float dy = oy - y;
        if (dx * dx + dy * dy < radius * radius) {
            float dz = sqrtf(radius * radius - dx * dx - dy * dy);
            *n = dz / sqrtf(radius * radius);
            return dz + z;
        }
        return -INF;
    }
};

__global__ void kernel(Sphere *, uchar *);

int main() {
    cudaEvent_t start, stop;  // Capture the start time
    cudaEventCreate(&start);
    cudaEventCreate(&stop);
    cudaEventRecord(start, 0);

    uchar *bitmap = new uchar[3 * DIM * DIM];
    uchar *dev_bitmap;
    Sphere *s;

    cudaMalloc((void**)&dev_bitmap, sizeof(uchar) * 3 * DIM * DIM);
    cudaMalloc((void**)&s, sizeof(Sphere) * SPHERES);
    
    Sphere *temp_s = new Sphere[SPHERES];
    for (int i = 0; i < SPHERES; i++) {
        temp_s[i].r = rnd(1.f);
        temp_s[i].g = rnd(1.f);
        temp_s[i].b = rnd(1.f);
        temp_s[i].x = rnd(1000.f);
        temp_s[i].y = rnd(1000.f);
        temp_s[i].z = rnd(1000.f);
        temp_s[i].radius = rnd(100.f) + 20;
    }
    
    cudaMemcpy(s, temp_s, sizeof(Sphere) * SPHERES, cudaMemcpyHostToDevice);
    delete[] temp_s;

    dim3 grids(DIM / 16, DIM / 16);
    dim3 threads(16, 16);
    kernel<<<grids, threads>>>(s, dev_bitmap);
    cudaMemcpy(bitmap, dev_bitmap, sizeof(uchar) * 3 * DIM * DIM, cudaMemcpyDeviceToHost);

    cudaEventRecord(stop, 0);
    cudaEventSynchronize(stop);
    float elapsedTime;
    cudaEventElapsedTime(&elapsedTime, start, stop);
    cout << "Time to generate: " << elapsedTime << " ms\n";

    cudaEventDestroy(start);
    cudaEventDestroy(stop);

    // draw
    Header header;
    ofstream out;
    out.open("with_pure_CUDA/nonconst.bmp", ios::binary);
    out.write((char*)&header, sizeof(Header));
    out.write((char*)bitmap, 3 * DIM * DIM);

    cudaFree(dev_bitmap);
    cudaFree(s);
    delete[] bitmap;
}

__global__ void kernel(Sphere *s, uchar *ptr) {
    int x = threadIdx.x + blockIdx.x * blockDim.x;
    int y = threadIdx.y + blockIdx.y * blockDim.y;
    int offset = x + y * blockDim.x * gridDim.x;
    // float ox = x - DIM / 2;
    // float oy = y - DIM / 2;
    float ox = x, oy = y;
    float r = 0, g = 0, b = 0;
    float maxz = -INF;

    for (int i = 0; i < SPHERES; i++) {
        float n;
        float t = s[i].hit(ox, oy, &n);
        if (t > maxz) {
            float fscale = n;
            r = s[i].r * fscale;
            g = s[i].g * fscale;
            b = s[i].b * fscale;
            maxz = t;
        }
    }
    
    ptr[offset * 3 + 0] = (int)(b * 255);
    ptr[offset * 3 + 1] = (int)(g * 255);
    ptr[offset * 3 + 2] = (int)(r * 255);
}
	.file	"tmpxft_0025837e_00000000-6_nonconst.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3779:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3779:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z33__device_stub__Z6kernelP6SpherePhP6SpherePh
	.type	_Z33__device_stub__Z6kernelP6SpherePhP6SpherePh, @function
_Z33__device_stub__Z6kernelP6SpherePhP6SpherePh:
.LFB3801:
	.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	.L2
	pushq	32(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_Z6kernelP6SpherePh(%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
.L2:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3801:
	.size	_Z33__device_stub__Z6kernelP6SpherePhP6SpherePh, .-_Z33__device_stub__Z6kernelP6SpherePhP6SpherePh
	.globl	_Z6kernelP6SpherePh
	.type	_Z6kernelP6SpherePh, @function
_Z6kernelP6SpherePh:
.LFB3802:
	.cfi_startproc
	endbr64
	jmp	_Z33__device_stub__Z6kernelP6SpherePhP6SpherePh
	.cfi_endproc
.LFE3802:
	.size	_Z6kernelP6SpherePh, .-_Z6kernelP6SpherePh
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"_Z6kernelP6SpherePh"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3804:
	.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	.LC0(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z6kernelP6SpherePh(%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
.LFE3804:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.rodata.str1.1
.LC5:
	.string	"Time to generate: "
.LC6:
	.string	" ms\n"
.LC7:
	.string	"with_pure_CUDA/nonconst.bmp"
	.section	.text.startup
	.globl	main
	.type	main, @function
main:
.LFB3776:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA3776
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	subq	$648, %rsp
	.cfi_def_cfa_offset 688
	movq	%fs:40, %rax
	movq	%rax, 632(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rdi
.LEHB0:
	call	cudaEventCreate@PLT
	leaq	16(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	8(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	$3145728, %edi
	call	_Znam@PLT
	leaq	24(%rsp), %rdi
	movl	$3145728, %esi
	movq	%rax, %r12
	call	cudaMalloc@PLT
	leaq	32(%rsp), %rdi
	movl	$560, %esi
	call	cudaMalloc@PLT
	movl	$560, %edi
	call	_Znam@PLT
	movq	%rax, %rbp
	movq	%rax, %rbx
	leaq	560(%rax), %r13
.L11:
	call	rand@PLT
	addq	$28, %rbx
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC1(%rip), %xmm0
	movss	%xmm0, -28(%rbx)
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC1(%rip), %xmm0
	movss	%xmm0, -20(%rbx)
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC1(%rip), %xmm0
	movss	%xmm0, -24(%rbx)
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC2(%rip), %xmm0
	mulss	.LC1(%rip), %xmm0
	movss	%xmm0, -12(%rbx)
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC2(%rip), %xmm0
	mulss	.LC1(%rip), %xmm0
	movss	%xmm0, -8(%rbx)
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC2(%rip), %xmm0
	mulss	.LC1(%rip), %xmm0
	movss	%xmm0, -4(%rbx)
	call	rand@PLT
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC3(%rip), %xmm0
	mulss	.LC1(%rip), %xmm0
	addss	.LC4(%rip), %xmm0
	movss	%xmm0, -16(%rbx)
	cmpq	%r13, %rbx
	jne	.L11
	movq	32(%rsp), %rdi
	movl	$1, %ecx
	movl	$560, %edx
	movq	%rbp, %rsi
	call	cudaMemcpy@PLT
	movq	%rbp, %rdi
	call	_ZdaPv@PLT
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	$1, %ecx
	movabsq	$68719476752, %rdx
	movl	$1, %esi
	movabsq	$274877907008, %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L12
	movq	24(%rsp), %rsi
	movq	32(%rsp), %rdi
	call	_Z33__device_stub__Z6kernelP6SpherePhP6SpherePh
.L12:
	movq	24(%rsp), %rsi
	movl	$2, %ecx
	movl	$3145728, %edx
	movq	%r12, %rdi
	leaq	120(%rsp), %rbx
	call	cudaMemcpy@PLT
	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
	leaq	.LC5(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	cvtss2sd	4(%rsp), %xmm0
	movq	%rax, %rdi
	call	_ZNSo9_M_insertIdEERSoT_@PLT
	leaq	.LC6(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	8(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	16(%rsp), %rdi
	call	cudaEventDestroy@PLT
	xorl	%edx, %edx
	movq	%rbx, %rdi
	movabsq	$206161988930, %rax
	movq	%rax, 66(%rsp)
	movabsq	$11258999071965184, %rax
	movq	%rax, 74(%rsp)
	movabsq	$288230376218820608, %rax
	movq	%rax, 82(%rsp)
	movl	$1572865, %eax
	salq	$16, %rax
	movl	%edx, 114(%rsp)
	movq	%rax, 90(%rsp)
	xorl	%eax, %eax
	movq	%rax, 98(%rsp)
	movq	%rax, 106(%rsp)
	movw	$0, 118(%rsp)
	call	_ZNSt14basic_ofstreamIcSt11char_traitsIcEEC1Ev@PLT
.LEHE0:
	movl	$4, %edx
	leaq	.LC7(%rip), %rsi
	movq	%rbx, %rdi
.LEHB1:
	call	_ZNSt14basic_ofstreamIcSt11char_traitsIcEE4openEPKcSt13_Ios_Openmode@PLT
	leaq	66(%rsp), %rsi
	movl	$54, %edx
	movq	%rbx, %rdi
	call	_ZNSo5writeEPKcl@PLT
	movl	$3145728, %edx
	movq	%r12, %rsi
	movq	%rbx, %rdi
	call	_ZNSo5writeEPKcl@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
.LEHE1:
	movq	%r12, %rdi
	call	_ZdaPv@PLT
	movq	%rbx, %rdi
	call	_ZNSt14basic_ofstreamIcSt11char_traitsIcEED1Ev@PLT
	movq	632(%rsp), %rax
	subq	%fs:40, %rax
	je	.L15
	jmp	.L18
.L16:
	endbr64
	movq	%rax, %rbp
.L13:
	movq	%rbx, %rdi
	call	_ZNSt14basic_ofstreamIcSt11char_traitsIcEED1Ev@PLT
	movq	632(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L18
	movq	%rbp, %rdi
.LEHB2:
	call	_Unwind_Resume@PLT
.LEHE2:
.L18:
	call	__stack_chk_fail@PLT
.L15:
	addq	$648, %rsp
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3776:
	.globl	__gxx_personality_v0
	.section	.gcc_except_table,"a",@progbits
.LLSDA3776:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE3776-.LLSDACSB3776
.LLSDACSB3776:
	.uleb128 .LEHB0-.LFB3776
	.uleb128 .LEHE0-.LEHB0
	.uleb128 0
	.uleb128 0
	.uleb128 .LEHB1-.LFB3776
	.uleb128 .LEHE1-.LEHB1
	.uleb128 .L16-.LFB3776
	.uleb128 0
	.uleb128 .LEHB2-.LFB3776
	.uleb128 .LEHE2-.LEHB2
	.uleb128 0
	.uleb128 0
.LLSDACSE3776:
	.section	.text.startup
	.size	main, .-main
	.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.cst4,"aM",@progbits,4
	.align 4
.LC1:
	.long	805306368
	.align 4
.LC2:
	.long	1148846080
	.align 4
.LC3:
	.long	1120403456
	.align 4
.LC4:
	.long	1101004800
	.hidden	DW.ref.__gxx_personality_v0
	.weak	DW.ref.__gxx_personality_v0
	.section	.data.rel.local.DW.ref.__gxx_personality_v0,"awG",@progbits,DW.ref.__gxx_personality_v0,comdat
	.align 8
	.type	DW.ref.__gxx_personality_v0, @object
	.size	DW.ref.__gxx_personality_v0, 8
DW.ref.__gxx_personality_v0:
	.quad	__gxx_personality_v0
	.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:

/*
** Originally copied from
** https://github.com/CodedK/CUDA-by-Example-source-code-for-the-book-s-examples-/blob/master/chapter06/ray_noconst.cu
** With a few bugs fixed
*/
#include <hip/hip_runtime.h>

#include <fstream>
#include <iostream>
using namespace std;

#define INF 2e10f
#define rnd(x) (x * rand() / RAND_MAX)
#define SPHERES 20
#define DIM 1024

#define uchar unsigned char
#define uint32_t unsigned int
#define uint16_t unsigned short
#define int32_t int

#pragma pack(push, 1)
struct BMPFileHeader {
    uint16_t file_type;          // File type always BM which is 0x4D42
    uint32_t file_size;               // Size of the file (in bytes)
    uint16_t reserved1;               // Reserved, always 0
    uint16_t reserved2;               // Reserved, always 0
    uint32_t offset_data;            // Start position of pixel data (bytes from the beginning of the file)
    BMPFileHeader() {
        file_type = 0x4D42;
        file_size = 0;
        reserved1 = 0;
        reserved2 = 0;
        offset_data = 54;
    };
};

struct BMPInfoHeader {
     uint32_t size;                      // Size of this header (in bytes)
     int32_t width;                      // width of bitmap in pixels
     int32_t height;                     // width of bitmap in pixels
                                              //       (if positive, bottom-up, with origin in lower left corner)
                                              //       (if negative, top-down, with origin in upper left corner)
     uint16_t planes;                    // No. of planes for the target device, this is always 1
     uint16_t bit_count;                 // No. of bits per pixel
     uint32_t compression;               // 0 or 3 - uncompressed. THIS PROGRAM CONSIDERS ONLY UNCOMPRESSED BMP images
     uint32_t size_image;                // 0 - for uncompressed images
     int32_t x_pixels_per_meter;
     int32_t y_pixels_per_meter;
     uint32_t colors_used;               // No. color indexes in the color table. Use 0 for the max number of colors allowed by bit_count
     uint32_t colors_important;          // No. of colors used for displaying the bitmap. If 0 all colors are required
     BMPInfoHeader() {
         size = 40;
         width = DIM;
         height = DIM;
         planes = 1;
         bit_count = 24;
         compression = 0;
         size_image = 0;
         x_pixels_per_meter = 0;
         y_pixels_per_meter = 0;
         colors_used = 0;
         colors_important = 0;
     }
 };

struct Header {
    BMPFileHeader fileheader;
    BMPInfoHeader infoheader;
    Header() {
        fileheader.file_size = 54 + 3 * DIM * DIM;
    }
};
#pragma pack(pop)

struct Sphere {
    float r, b, g;
    float radius;
    float x, y, z;
    
    __device__ float hit(float ox, float oy, float *n) {
        float dx = ox - x;
        float dy = oy - y;
        if (dx * dx + dy * dy < radius * radius) {
            float dz = sqrtf(radius * radius - dx * dx - dy * dy);
            *n = dz / sqrtf(radius * radius);
            return dz + z;
        }
        return -INF;
    }
};

__global__ void kernel(Sphere *, uchar *);

int main() {
    hipEvent_t start, stop;  // Capture the start time
    hipEventCreate(&start);
    hipEventCreate(&stop);
    hipEventRecord(start, 0);

    uchar *bitmap = new uchar[3 * DIM * DIM];
    uchar *dev_bitmap;
    Sphere *s;

    hipMalloc((void**)&dev_bitmap, sizeof(uchar) * 3 * DIM * DIM);
    hipMalloc((void**)&s, sizeof(Sphere) * SPHERES);
    
    Sphere *temp_s = new Sphere[SPHERES];
    for (int i = 0; i < SPHERES; i++) {
        temp_s[i].r = rnd(1.f);
        temp_s[i].g = rnd(1.f);
        temp_s[i].b = rnd(1.f);
        temp_s[i].x = rnd(1000.f);
        temp_s[i].y = rnd(1000.f);
        temp_s[i].z = rnd(1000.f);
        temp_s[i].radius = rnd(100.f) + 20;
    }
    
    hipMemcpy(s, temp_s, sizeof(Sphere) * SPHERES, hipMemcpyHostToDevice);
    delete[] temp_s;

    dim3 grids(DIM / 16, DIM / 16);
    dim3 threads(16, 16);
    kernel<<<grids, threads>>>(s, dev_bitmap);
    hipMemcpy(bitmap, dev_bitmap, sizeof(uchar) * 3 * DIM * DIM, hipMemcpyDeviceToHost);

    hipEventRecord(stop, 0);
    hipEventSynchronize(stop);
    float elapsedTime;
    hipEventElapsedTime(&elapsedTime, start, stop);
    cout << "Time to generate: " << elapsedTime << " ms\n";

    hipEventDestroy(start);
    hipEventDestroy(stop);

    // draw
    Header header;
    ofstream out;
    out.open("with_pure_CUDA/nonconst.bmp", ios::binary);
    out.write((char*)&header, sizeof(Header));
    out.write((char*)bitmap, 3 * DIM * DIM);

    hipFree(dev_bitmap);
    hipFree(s);
    delete[] bitmap;
}

__global__ void kernel(Sphere *s, uchar *ptr) {
    int x = threadIdx.x + blockIdx.x * blockDim.x;
    int y = threadIdx.y + blockIdx.y * blockDim.y;
    int offset = x + y * blockDim.x * gridDim.x;
    // float ox = x - DIM / 2;
    // float oy = y - DIM / 2;
    float ox = x, oy = y;
    float r = 0, g = 0, b = 0;
    float maxz = -INF;

    for (int i = 0; i < SPHERES; i++) {
        float n;
        float t = s[i].hit(ox, oy, &n);
        if (t > maxz) {
            float fscale = n;
            r = s[i].r * fscale;
            g = s[i].g * fscale;
            b = s[i].b * fscale;
            maxz = t;
        }
    }
    
    ptr[offset * 3 + 0] = (int)(b * 255);
    ptr[offset * 3 + 1] = (int)(g * 255);
    ptr[offset * 3 + 2] = (int)(r * 255);
}
	.text
	.file	"nonconst.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI0_0:
	.long	0x30000000                      # float 4.65661287E-10
.LCPI0_1:
	.long	0x447a0000                      # float 1000
.LCPI0_2:
	.long	0x42c80000                      # float 100
.LCPI0_3:
	.long	0x41a00000                      # float 20
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0
.LCPI0_4:
	.long	54                              # 0x36
	.long	40                              # 0x28
	.long	1024                            # 0x400
	.long	1024                            # 0x400
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
.Lfunc_begin0:
	.cfi_startproc
	.cfi_personality 3, __gxx_personality_v0
	.cfi_lsda 3, .Lexception0
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	subq	$608, %rsp                      # imm = 0x260
	.cfi_def_cfa_offset 640
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventCreate
	movq	%rsp, %rdi
	callq	hipEventCreate
	movq	(%rbx), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movl	$3145728, %edi                  # imm = 0x300000
	callq	_Znam
	movq	%rax, %rbx
	leaq	16(%rsp), %rdi
	movl	$3145728, %esi                  # imm = 0x300000
	callq	hipMalloc
	leaq	8(%rsp), %rdi
	movl	$560, %esi                      # imm = 0x230
	callq	hipMalloc
	movl	$560, %edi                      # imm = 0x230
	callq	_Znam
	movq	%rax, %r14
	movl	$24, %r15d
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	movss	.LCPI0_0(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	mulss	%xmm1, %xmm0
	movss	%xmm0, -24(%r14,%r15)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI0_0(%rip), %xmm0
	movss	%xmm0, -16(%r14,%r15)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI0_0(%rip), %xmm0
	movss	%xmm0, -20(%r14,%r15)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	movss	.LCPI0_1(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	mulss	%xmm1, %xmm0
	mulss	.LCPI0_0(%rip), %xmm0
	movss	%xmm0, -8(%r14,%r15)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI0_1(%rip), %xmm0
	mulss	.LCPI0_0(%rip), %xmm0
	movss	%xmm0, -4(%r14,%r15)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI0_1(%rip), %xmm0
	mulss	.LCPI0_0(%rip), %xmm0
	movss	%xmm0, (%r14,%r15)
	callq	rand
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	mulss	.LCPI0_2(%rip), %xmm0
	mulss	.LCPI0_0(%rip), %xmm0
	addss	.LCPI0_3(%rip), %xmm0
	movss	%xmm0, -12(%r14,%r15)
	addq	$28, %r15
	cmpq	$584, %r15                      # imm = 0x248
	jne	.LBB0_1
# %bb.2:
	movq	8(%rsp), %rdi
	movl	$560, %edx                      # imm = 0x230
	movq	%r14, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movq	%r14, %rdi
	callq	_ZdaPv
	movabsq	$274877907008, %rdi             # imm = 0x4000000040
	movabsq	$68719476752, %rdx              # imm = 0x1000000010
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB0_4
# %bb.3:
	movq	8(%rsp), %rdi
	movq	16(%rsp), %rsi
	callq	_Z21__device_stub__kernelP6SpherePh
.LBB0_4:
	movq	16(%rsp), %rsi
	movl	$3145728, %edx                  # imm = 0x300000
	movq	%rbx, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%rsp), %rdi
	callq	hipEventSynchronize
	movq	24(%rsp), %rsi
	movq	(%rsp), %rdx
	leaq	36(%rsp), %r14
	movq	%r14, %rdi
	callq	hipEventElapsedTime
	movl	$_ZSt4cout, %edi
	movl	$.L.str, %esi
	movl	$18, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r14), %xmm0
	movl	$_ZSt4cout, %edi
	callq	_ZNSo9_M_insertIdEERSoT_
	movl	$.L.str.1, %esi
	movl	$4, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	24(%rsp), %rdi
	callq	hipEventDestroy
	movq	(%rsp), %rdi
	callq	hipEventDestroy
	movw	$19778, 42(%rsp)                # imm = 0x4D42
	movq	$3145782, 44(%rsp)              # imm = 0x300036
	movaps	.LCPI0_4(%rip), %xmm0           # xmm0 = [54,40,1024,1024]
	movups	%xmm0, 52(%rsp)
	movl	$1572865, 68(%rsp)              # imm = 0x180001
	xorps	%xmm0, %xmm0
	movups	%xmm0, 72(%rsp)
	movq	$0, 88(%rsp)
	leaq	96(%rsp), %r14
	movq	%r14, %rdi
	callq	_ZNSt14basic_ofstreamIcSt11char_traitsIcEEC1Ev
.Ltmp0:
	movl	$.L.str.2, %esi
	movq	%r14, %rdi
	movl	$4, %edx
	callq	_ZNSt14basic_ofstreamIcSt11char_traitsIcEE4openEPKcSt13_Ios_Openmode
.Ltmp1:
# %bb.5:
.Ltmp2:
	leaq	96(%rsp), %rdi
	leaq	42(%rsp), %rsi
	movl	$54, %edx
	callq	_ZNSo5writeEPKcl
.Ltmp3:
# %bb.6:
.Ltmp4:
	leaq	96(%rsp), %rdi
	movl	$3145728, %edx                  # imm = 0x300000
	movq	%rbx, %rsi
	callq	_ZNSo5writeEPKcl
.Ltmp5:
# %bb.7:
	movq	16(%rsp), %rdi
.Ltmp6:
	callq	hipFree
.Ltmp7:
# %bb.8:
	movq	8(%rsp), %rdi
.Ltmp8:
	callq	hipFree
.Ltmp9:
# %bb.9:
	movq	%rbx, %rdi
	callq	_ZdaPv
	movq	_ZTTSt14basic_ofstreamIcSt11char_traitsIcEE(%rip), %rax
	leaq	104(%rsp), %rdi
	movq	%rax, -8(%rdi)
	movq	_ZTTSt14basic_ofstreamIcSt11char_traitsIcEE+24(%rip), %rcx
	movq	-24(%rax), %rax
	movq	%rcx, 96(%rsp,%rax)
	callq	_ZNSt13basic_filebufIcSt11char_traitsIcEED2Ev
	leaq	344(%rsp), %rdi
	callq	_ZNSt8ios_baseD2Ev
	xorl	%eax, %eax
	addq	$608, %rsp                      # imm = 0x260
	.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
.LBB0_10:
	.cfi_def_cfa_offset 640
.Ltmp10:
	movq	%rax, %rbx
	leaq	96(%rsp), %rdi
	callq	_ZNSt14basic_ofstreamIcSt11char_traitsIcEED1Ev
	movq	%rbx, %rdi
	callq	_Unwind_Resume@PLT
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
	.section	.gcc_except_table,"a",@progbits
	.p2align	2, 0x0
GCC_except_table0:
.Lexception0:
	.byte	255                             # @LPStart Encoding = omit
	.byte	255                             # @TType Encoding = omit
	.byte	1                               # Call site Encoding = uleb128
	.uleb128 .Lcst_end0-.Lcst_begin0
.Lcst_begin0:
	.uleb128 .Lfunc_begin0-.Lfunc_begin0    # >> Call Site 1 <<
	.uleb128 .Ltmp0-.Lfunc_begin0           #   Call between .Lfunc_begin0 and .Ltmp0
	.byte	0                               #     has no landing pad
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp0-.Lfunc_begin0           # >> Call Site 2 <<
	.uleb128 .Ltmp9-.Ltmp0                  #   Call between .Ltmp0 and .Ltmp9
	.uleb128 .Ltmp10-.Lfunc_begin0          #     jumps to .Ltmp10
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp9-.Lfunc_begin0           # >> Call Site 3 <<
	.uleb128 .Lfunc_end0-.Ltmp9             #   Call between .Ltmp9 and .Lfunc_end0
	.byte	0                               #     has no landing pad
	.byte	0                               #   On action: cleanup
.Lcst_end0:
	.p2align	2, 0x0
                                        # -- End function
	.text
	.globl	_Z21__device_stub__kernelP6SpherePh # -- Begin function _Z21__device_stub__kernelP6SpherePh
	.type	_Z21__device_stub__kernelP6SpherePh,@function
_Z21__device_stub__kernelP6SpherePh:    # @_Z21__device_stub__kernelP6SpherePh
	.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	$_Z6kernelP6SpherePh, %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_end1:
	.size	_Z21__device_stub__kernelP6SpherePh, .Lfunc_end1-_Z21__device_stub__kernelP6SpherePh
	.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	.LBB2_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB2_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z6kernelP6SpherePh, %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_end2:
	.size	__hip_module_ctor, .Lfunc_end2-__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	.LBB3_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
.LBB3_2:
	retq
.Lfunc_end3:
	.size	__hip_module_dtor, .Lfunc_end3-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z6kernelP6SpherePh,@object     # @_Z6kernelP6SpherePh
	.section	.rodata,"a",@progbits
	.globl	_Z6kernelP6SpherePh
	.p2align	3, 0x0
_Z6kernelP6SpherePh:
	.quad	_Z21__device_stub__kernelP6SpherePh
	.size	_Z6kernelP6SpherePh, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Time to generate: "
	.size	.L.str, 19

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	" ms\n"
	.size	.L.str.1, 5

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"with_pure_CUDA/nonconst.bmp"
	.size	.L.str.2, 28

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z6kernelP6SpherePh"
	.size	.L__unnamed_1, 20

	.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

	.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__kernelP6SpherePh
	.addrsig_sym __gxx_personality_v0
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Unwind_Resume
	.addrsig_sym _Z6kernelP6SpherePh
	.addrsig_sym _ZSt4cout
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z6kernelP6SpherePh     ; -- Begin function _Z6kernelP6SpherePh
	.globl	_Z6kernelP6SpherePh
	.p2align	8
	.type	_Z6kernelP6SpherePh,@function
_Z6kernelP6SpherePh:                    ; @_Z6kernelP6SpherePh
; %bb.0:
	s_clause 0x2
	s_load_b32 s2, s[0:1], 0x1c
	s_load_b128 s[4:7], s[0:1], 0x0
	s_load_b32 s10, s[0:1], 0x10
	v_and_b32_e32 v2, 0x3ff, v0
	v_bfe_u32 v3, v0, 10, 10
                                        ; implicit-def: $vgpr8
	v_mov_b32_e32 v7, 0xd09502f9
	s_waitcnt lgkmcnt(0)
	s_and_b32 s11, s2, 0xffff
	s_lshr_b32 s0, s2, 16
	v_mad_u64_u32 v[0:1], null, s14, s11, v[2:3]
	v_mad_u64_u32 v[1:2], null, s15, s0, v[3:4]
	v_dual_mov_b32 v3, 0 :: v_dual_mov_b32 v2, 0
	v_mov_b32_e32 v4, 0
	s_mov_b64 s[2:3], 0
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_cvt_f32_i32_e32 v5, v0
	v_cvt_f32_i32_e32 v6, v1
.LBB0_1:                                ; =>This Inner Loop Header: Depth=1
	s_add_u32 s8, s4, s2
	s_addc_u32 s9, s5, s3
	s_clause 0x1
	s_load_b32 s12, s[8:9], 0x14
	s_load_b64 s[0:1], s[8:9], 0xc
	s_waitcnt lgkmcnt(0)
	v_dual_subrev_f32 v9, s12, v6 :: v_dual_subrev_f32 v12, s1, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_mul_f32_e32 v11, v9, v9
	v_mul_f32_e64 v9, s0, s0
	v_fma_f32 v10, v12, v12, v11
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_lt_f32_e32 vcc_lo, v10, v9
	v_mov_b32_e32 v10, 0xd09502f9
	s_and_saveexec_b32 s12, vcc_lo
	s_cbranch_execz .LBB0_3
; %bb.2:                                ;   in Loop: Header=BB0_1 Depth=1
	v_mul_f32_e32 v10, 0x4f800000, v9
	v_cmp_gt_f32_e32 vcc_lo, 0xf800000, v9
	v_mul_f32_e32 v8, v12, v12
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_sub_f32_e32 v8, v9, v8
	v_dual_cndmask_b32 v9, v9, v10 :: v_dual_sub_f32 v8, v8, v11
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_sqrt_f32_e32 v10, v9
	v_mul_f32_e32 v11, 0x4f800000, v8
	v_cmp_gt_f32_e64 s0, 0xf800000, v8
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_4) | instid1(VALU_DEP_2)
	v_cndmask_b32_e64 v8, v8, v11, s0
	s_waitcnt_depctr 0xfff
	v_add_nc_u32_e32 v12, -1, v10
	v_add_nc_u32_e32 v14, 1, v10
	v_sqrt_f32_e32 v11, v8
	v_fma_f32 v15, -v12, v10, v9
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f32 v18, -v14, v10, v9
	v_cmp_ge_f32_e64 s1, 0, v15
	s_waitcnt_depctr 0xfff
	v_add_nc_u32_e32 v13, -1, v11
	v_add_nc_u32_e32 v16, 1, v11
	v_cndmask_b32_e64 v10, v10, v12, s1
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_fma_f32 v17, -v13, v11, v8
	v_fma_f32 v12, -v16, v11, v8
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_ge_f32_e64 s1, 0, v17
	v_cndmask_b32_e64 v11, v11, v13, s1
	v_cmp_lt_f32_e64 s1, 0, v18
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_cndmask_b32_e64 v10, v10, v14, s1
	v_cmp_lt_f32_e64 s1, 0, v12
	v_mul_f32_e32 v12, 0x37800000, v10
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cndmask_b32_e64 v11, v11, v16, s1
	v_dual_cndmask_b32 v10, v10, v12 :: v_dual_mul_f32 v13, 0x37800000, v11
	v_cmp_class_f32_e64 vcc_lo, v9, 0x260
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cndmask_b32_e64 v11, v11, v13, s0
	v_cndmask_b32_e32 v9, v10, v9, vcc_lo
	v_cmp_class_f32_e64 vcc_lo, v8, 0x260
	s_load_b32 s0, s[8:9], 0x18
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cndmask_b32_e32 v10, v11, v8, vcc_lo
	v_div_scale_f32 v8, null, v9, v9, v10
	v_div_scale_f32 v13, vcc_lo, v10, v9, v10
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f32_e32 v11, v8
	s_waitcnt_depctr 0xfff
	v_fma_f32 v12, -v8, v11, 1.0
	v_fmac_f32_e32 v11, v12, v11
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v12, v13, v11
	v_fma_f32 v14, -v8, v12, v13
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmac_f32_e32 v12, v14, v11
	v_fma_f32 v8, -v8, v12, v13
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_div_fmas_f32 v8, v8, v11, v12
	v_div_fixup_f32 v8, v8, v9, v10
	s_waitcnt lgkmcnt(0)
	v_add_f32_e32 v10, s0, v10
.LBB0_3:                                ; %_ZN6Sphere3hitEffPf.exit
                                        ;   in Loop: Header=BB0_1 Depth=1
	s_or_b32 exec_lo, exec_lo, s12
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	s_mov_b32 s0, exec_lo
	v_cmpx_gt_f32_e32 v10, v7
	s_cbranch_execz .LBB0_5
; %bb.4:                                ;   in Loop: Header=BB0_1 Depth=1
	s_clause 0x1
	s_load_b64 s[12:13], s[8:9], 0x0
	s_load_b32 s1, s[8:9], 0x8
	s_waitcnt lgkmcnt(0)
	v_dual_mov_b32 v7, v10 :: v_dual_mul_f32 v2, s12, v8
	v_mul_f32_e32 v3, s1, v8
	v_mul_f32_e32 v4, s13, v8
.LBB0_5:                                ;   in Loop: Header=BB0_1 Depth=1
	s_or_b32 exec_lo, exec_lo, s0
	s_add_u32 s2, s2, 28
	s_addc_u32 s3, s3, 0
	s_cmpk_eq_i32 s2, 0x230
	s_cbranch_scc0 .LBB0_1
; %bb.6:
	s_mul_i32 s10, s10, s11
	v_mul_f32_e32 v2, 0x437f0000, v2
	v_mad_u64_u32 v[5:6], null, s10, v1, v[0:1]
	v_dual_mul_f32 v0, 0x437f0000, v3 :: v_dual_mul_f32 v1, 0x437f0000, v4
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cvt_i32_f32_e32 v2, v2
	v_cvt_i32_f32_e32 v0, v0
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_lshl_add_u32 v3, v5, 1, v5
	v_cvt_i32_f32_e32 v1, v1
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_lshlrev_b16 v4, 8, v0
	v_ashrrev_i32_e32 v5, 31, v3
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_and_b32_e32 v6, 0xff, v1
	v_add_co_u32 v0, vcc_lo, s6, v3
	v_add_co_ci_u32_e32 v1, vcc_lo, s7, v5, vcc_lo
	s_delay_alu instid0(VALU_DEP_3)
	v_or_b32_e32 v3, v6, v4
	s_clause 0x1
	global_store_b8 v[0:1], v2, off offset:2
	global_store_b16 v[0:1], v3, off
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z6kernelP6SpherePh
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 272
		.amdhsa_user_sgpr_count 14
		.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 1
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 1
		.amdhsa_next_free_vgpr 19
		.amdhsa_next_free_sgpr 16
		.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	_Z6kernelP6SpherePh, .Lfunc_end0-_Z6kernelP6SpherePh
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 816
; NumSgprs: 18
; NumVgprs: 19
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 2
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 19
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 1
	.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
      - .offset:         16
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         20
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         28
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         30
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         32
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         34
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         36
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         38
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         56
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         80
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 272
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z6kernelP6SpherePh
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z6kernelP6SpherePh.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     19
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
