#include "CudaLife.cuh"

#include <cuda_runtime.h>
#include <device_launch_parameters.h>

__global__ void cudaDecKernel(
	cudaSurfaceObject_t surfaceIn,
	cudaSurfaceObject_t surfaceOut,
	unsigned int width,
	unsigned int height
) {
	int x = blockIdx.x * blockDim.x + threadIdx.x;
	int y = blockIdx.y * blockDim.y + threadIdx.y;

	if (x >= width || y >= height)
		return;

	uchar4 cell;
	surf2Dread(&cell, surfaceIn, x * 4, y);
	if (cell.x != 0 && cell.x != 0x80)
		cell = make_uchar4(cell.x - 1, cell.x - 1, cell.x - 1, 0xff);
	surf2Dwrite(cell, surfaceOut, x * 4, y);
}

__global__ void cudaLifeKernel(
	cudaSurfaceObject_t surfaceIn,
	cudaSurfaceObject_t surfaceOut,
	unsigned int width,
	unsigned int height
)
{
	int x = blockIdx.x * blockDim.x + threadIdx.x;
	int y = blockIdx.y * blockDim.y + threadIdx.y;

	if (x >= width || y >= height)
		return;

	int neighbors = 0;
	if (x == 0 || x == width - 1 || y == 0 || y == height - 1) {
		for (int yo = -1; yo <= 1; ++yo)
			for (int xo = -1; xo <= 1; ++xo)
				if (yo != 0 || xo != 0) {
					int ix = x + xo;
					int iy = y + yo;
					if (ix < 0) ix += width;
					if (ix >= width) ix -= width;
					if (iy < 0) iy += height;
					if (iy >= height) iy -= height;

					uchar4 data;
					surf2Dread(&data, surfaceIn, ix * 4, iy);
					if (data.x > 0x7f)
						++neighbors;
				}
	}
	else
	{
		for (int yo = -1; yo <= 1; ++yo)
			for (int xo = -1; xo <= 1; ++xo)
				if (yo != 0 || xo != 0) {
					int ix = x + xo;
					int iy = y + yo;
					uchar4 data;
					surf2Dread(&data, surfaceIn, ix * 4, iy);
					if (data.x > 0x7f)
						++neighbors;
				}
	}
	uchar4 cell;
	surf2Dread(&cell, surfaceIn, x * 4, y);

	uchar4 data;
	if (neighbors == 2)
		data = make_uchar4(cell.x, cell.x, cell.x, 0xff);
	else if (neighbors == 3)
		if (cell.x > 0x7f)
			data = make_uchar4(cell.x, cell.x, cell.x, 0xff);
		else
			data = make_uchar4(0xff, 0xff, 0xff, 0xff);
	else
		if (cell.x > 0x7f)
			data = make_uchar4(0x7f, 0x7f, 0x7f, 0xff);
		else
			data = make_uchar4(cell.x, cell.x, cell.x, 0xff);

	surf2Dwrite(data, surfaceOut, x * 4, y);
}

extern "C" void doDecKernel(cudaSurfaceObject_t surfaceIn, cudaSurfaceObject_t surfaceOut, unsigned int width, unsigned int height) {
	dim3 threads(32, 32);
	dim3 blocks(width / threads.x, height / threads.y);
	if (blocks.x * threads.x < width) ++blocks.x;
	if (blocks.y * threads.y < height) ++blocks.y;

	cudaDecKernel << <blocks, threads >> > (surfaceIn, surfaceOut, width, height);
}

extern "C" void doLifeKernel(cudaSurfaceObject_t surfaceIn, cudaSurfaceObject_t surfaceOut, unsigned int width, unsigned int height) {
	dim3 threads(32, 32);
	dim3 blocks(width / threads.x, height / threads.y);
	if (blocks.x * threads.x < width) ++blocks.x;
	if (blocks.y * threads.y < height) ++blocks.y;

	cudaLifeKernel << <blocks, threads >> > (surfaceIn, surfaceOut, width, height);
}
	.file	"tmpxft_0038296b_00000000-6_CudaLife.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2012:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2012:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z35__device_stub__Z13cudaDecKernelyyjjyyjj
	.type	_Z35__device_stub__Z13cudaDecKernelyyjjyyjj, @function
_Z35__device_stub__Z13cudaDecKernelyyjjyyjj:
.LFB2034:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 8(%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z13cudaDecKernelyyjj(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L2:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2034:
	.size	_Z35__device_stub__Z13cudaDecKernelyyjjyyjj, .-_Z35__device_stub__Z13cudaDecKernelyyjjyyjj
	.globl	_Z13cudaDecKernelyyjj
	.type	_Z13cudaDecKernelyyjj, @function
_Z13cudaDecKernelyyjj:
.LFB2035:
	.cfi_startproc
	endbr64
	jmp	_Z35__device_stub__Z13cudaDecKernelyyjjyyjj
	.cfi_endproc
.LFE2035:
	.size	_Z13cudaDecKernelyyjj, .-_Z13cudaDecKernelyyjj
	.globl	doDecKernel
	.type	doDecKernel, @function
doDecKernel:
.LFB2008:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movl	%ecx, %eax
	movq	%rsi, %r13
	movl	$1, %esi
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	shrl	$5, %eax
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movl	%ecx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movl	%edx, %ebx
	shrl	$5, %edx
	movl	%ebx, %ecx
	andl	$-32, %ecx
	subq	$40, %rsp
	.cfi_def_cfa_offset 80
	cmpl	%ebx, %ecx
	movl	%ebp, %ecx
	adcl	$0, %edx
	andl	$-32, %ecx
	cmpl	%ebp, %ecx
	movl	%edx, 20(%rsp)
	movl	$1, %ecx
	movabsq	$137438953504, %rdx
	adcl	$0, %eax
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	%eax, 24(%rsp)
	movq	20(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L8
	addq	$40, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 40
	movl	%ebp, %ecx
	movl	%ebx, %edx
	movq	%r13, %rsi
	popq	%rbx
	.cfi_def_cfa_offset 32
	movq	%r12, %rdi
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	jmp	_Z35__device_stub__Z13cudaDecKernelyyjjyyjj
.L8:
	.cfi_restore_state
	addq	$40, %rsp
	.cfi_def_cfa_offset 40
	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
.LFE2008:
	.size	doDecKernel, .-doDecKernel
	.globl	_Z36__device_stub__Z14cudaLifeKernelyyjjyyjj
	.type	_Z36__device_stub__Z14cudaLifeKernelyyjjyyjj, @function
_Z36__device_stub__Z14cudaLifeKernelyyjjyyjj:
.LFB2036:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 8(%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L13
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z14cudaLifeKernelyyjj(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L13:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L15
	call	__stack_chk_fail@PLT
.L15:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2036:
	.size	_Z36__device_stub__Z14cudaLifeKernelyyjjyyjj, .-_Z36__device_stub__Z14cudaLifeKernelyyjjyyjj
	.globl	_Z14cudaLifeKernelyyjj
	.type	_Z14cudaLifeKernelyyjj, @function
_Z14cudaLifeKernelyyjj:
.LFB2037:
	.cfi_startproc
	endbr64
	jmp	_Z36__device_stub__Z14cudaLifeKernelyyjjyyjj
	.cfi_endproc
.LFE2037:
	.size	_Z14cudaLifeKernelyyjj, .-_Z14cudaLifeKernelyyjj
	.globl	doLifeKernel
	.type	doLifeKernel, @function
doLifeKernel:
.LFB2009:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movl	%ecx, %eax
	movq	%rsi, %r13
	movl	$1, %esi
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	shrl	$5, %eax
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movl	%ecx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movl	%edx, %ebx
	shrl	$5, %edx
	movl	%ebx, %ecx
	andl	$-32, %ecx
	subq	$40, %rsp
	.cfi_def_cfa_offset 80
	cmpl	%ebx, %ecx
	movl	%ebp, %ecx
	adcl	$0, %edx
	andl	$-32, %ecx
	cmpl	%ebp, %ecx
	movl	%edx, 20(%rsp)
	movl	$1, %ecx
	movabsq	$137438953504, %rdx
	adcl	$0, %eax
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	%eax, 24(%rsp)
	movq	20(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L18
	addq	$40, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 40
	movl	%ebp, %ecx
	movl	%ebx, %edx
	movq	%r13, %rsi
	popq	%rbx
	.cfi_def_cfa_offset 32
	movq	%r12, %rdi
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	jmp	_Z36__device_stub__Z14cudaLifeKernelyyjjyyjj
.L18:
	.cfi_restore_state
	addq	$40, %rsp
	.cfi_def_cfa_offset 40
	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
.LFE2009:
	.size	doLifeKernel, .-doLifeKernel
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"_Z14cudaLifeKernelyyjj"
.LC1:
	.string	"_Z13cudaDecKernelyyjj"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2039:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -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
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_Z14cudaLifeKernelyyjj(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r9d, %r9d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC1(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z13cudaDecKernelyyjj(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 40
	pushq	$0
	.cfi_def_cfa_offset 48
	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	%rbx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2039:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#pragma once
#include <hip/hip_runtime.h>


extern "C" void doDecKernel(hipSurfaceObject_t surfaceIn, hipSurfaceObject_t surfaceOut, unsigned int width, unsigned int height);
extern "C" void doLifeKernel(hipSurfaceObject_t surfaceIn, hipSurfaceObject_t surfaceOut, unsigned int width, unsigned int height);
	.text
	.file	"CudaLife.hip"
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
