#include "constants.cuh"
#include "mandelbrot.cuh"


/**
 * Translate the coordinates from the block coordinates into their real and imaginary values in the zoom space
 * @param r Data table for coordinate storage
 * @param i Data table for coordinate storage
 */
__global__ void translate(
        double *r, double *i,
        const double width, const double height,
        const double offset_r, const double offset_i
) {
    r[blockIdx.y * SIZE + blockIdx.x] = (double(blockIdx.x) * width  / double(SIZE)) - (width / 2.) + offset_r;
    i[blockIdx.y * SIZE + blockIdx.x] = (double(blockIdx.y) * height / double(SIZE)) - (height / 2.) + offset_i;
}

/*
 * Compute how much iterations are needed to escape the mandelbrot set
 */
__global__ void mandelbrot(int *iteration_table, const double *r, const double *i, const int max) {
    // Retrieve the coordinate from the pixel map
    double  c_r = r[blockIdx.x * SIZE + blockIdx.y],
            c_i = i[blockIdx.x * SIZE + blockIdx.y];

    double temp_z_r, z_r = c_r;
    double temp_z_i, z_i = c_i;

    int iteration;
    for (iteration = 0; iteration < max && (z_r * z_r + z_i * z_i <= 16.); iteration++) {
        // z*z + c
        temp_z_r = (z_r * z_r) - (z_i * z_i) + c_r;
        temp_z_i = (z_r * z_i * 2.0        ) + c_i;

        z_r = temp_z_r, z_i = temp_z_i;
    }

    iteration_table[blockIdx.x * SIZE + blockIdx.y] = iteration;
}

/**
 * Transform an integer table into their 3 component color [255, 255, 255]
 * @param pixel_table 3 * pixel numbers
 * @param iteration_table
 * @param max_loop
 */
__global__ void colorize(unsigned char *pixel_table, const int *iteration_table, const int max_loop) {
    int iteration = iteration_table[blockIdx.x * SIZE + blockIdx.y];

    if (iteration == max_loop) {
        RGB(pixel_table, (blockIdx.x * SIZE + blockIdx.y) * 3, 0u, 0u, 0u)
        return;
    }

    double h = cfmod(iteration * 360. / COLOR_LOOP, 360.);
    double hp = h / 60.0;
    double z = 1.0 - cabs(cfmod(hp, 2.) - 1.);

    if (hp < 1.0) {
        RGB(pixel_table, (blockIdx.x * SIZE + blockIdx.y) * 3, F_FACTOR, C_FACTOR * z, Z_FACTOR)
    } else if (hp < 2.0) {
        RGB(pixel_table, (blockIdx.x * SIZE + blockIdx.y) * 3, C_FACTOR * z, F_FACTOR, Z_FACTOR)
    } else if (hp < 3.0) {
        RGB(pixel_table, (blockIdx.x * SIZE + blockIdx.y) * 3, Z_FACTOR, F_FACTOR, C_FACTOR * z)
    } else if (hp < 4.0) {
        RGB(pixel_table, (blockIdx.x * SIZE + blockIdx.y) * 3, Z_FACTOR, C_FACTOR * z, F_FACTOR)
    } else if (hp < 5.0) {
        RGB(pixel_table, (blockIdx.x * SIZE + blockIdx.y) * 3, C_FACTOR * z, Z_FACTOR, F_FACTOR)
    } else if (hp < 6.0) {
        RGB(pixel_table, (blockIdx.x * SIZE + blockIdx.y) * 3, F_FACTOR, Z_FACTOR, C_FACTOR * z)
    } else {
        RGB(pixel_table, (blockIdx.x * SIZE + blockIdx.y) * 3, Z_FACTOR, Z_FACTOR, Z_FACTOR)
    }
}
	.file	"tmpxft_0028f833_00000000-6_mandelbrot.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2010:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2010:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z34__device_stub__Z9translatePdS_ddddPdS_dddd
	.type	_Z34__device_stub__Z9translatePdS_ddddPdS_dddd, @function
_Z34__device_stub__Z9translatePdS_ddddPdS_dddd:
.LFB2032:
	.cfi_startproc
	endbr64
	subq	$184, %rsp
	.cfi_def_cfa_offset 192
	movq	%rdi, 40(%rsp)
	leaq	64(%rsp), %rcx
	leaq	56(%rsp), %rdx
	movq	%rsi, 32(%rsp)
	leaq	72(%rsp), %rdi
	leaq	84(%rsp), %rsi
	movsd	%xmm0, 24(%rsp)
	movsd	%xmm1, 16(%rsp)
	movsd	%xmm2, 8(%rsp)
	movsd	%xmm3, (%rsp)
	movq	%fs:40, %rax
	movq	%rax, 168(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 80(%rsp)
	movq	%rax, 120(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 152(%rsp)
	movq	%rsp, %rax
	movq	%rax, 160(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 72(%rsp)
	movq	%rax, 84(%rsp)
	movl	$1, 92(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	64(%rsp)
	.cfi_def_cfa_offset 200
	leaq	_Z9translatePdS_dddd(%rip), %rdi
	pushq	64(%rsp)
	.cfi_def_cfa_offset 208
	movq	100(%rsp), %rcx
	movl	108(%rsp), %r8d
	movq	88(%rsp), %rsi
	movl	96(%rsp), %edx
	leaq	136(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 200
	popq	%rdx
	.cfi_def_cfa_offset 192
.L2:
	movq	168(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$184, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2032:
	.size	_Z34__device_stub__Z9translatePdS_ddddPdS_dddd, .-_Z34__device_stub__Z9translatePdS_ddddPdS_dddd
	.globl	_Z9translatePdS_dddd
	.type	_Z9translatePdS_dddd, @function
_Z9translatePdS_dddd:
.LFB2033:
	.cfi_startproc
	endbr64
	jmp	_Z34__device_stub__Z9translatePdS_ddddPdS_dddd
	.cfi_endproc
.LFE2033:
	.size	_Z9translatePdS_dddd, .-_Z9translatePdS_dddd
	.globl	_Z37__device_stub__Z10mandelbrotPiPKdS1_iPiPKdS1_i
	.type	_Z37__device_stub__Z10mandelbrotPiPKdS1_iPiPKdS1_i, @function
_Z37__device_stub__Z10mandelbrotPiPKdS1_iPiPKdS1_i:
.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
	movq	%rdx, 8(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 4(%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	8(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	4(%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	.L8
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z10mandelbrotPiPKdS1_i(%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
.L8:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L10
	call	__stack_chk_fail@PLT
.L10:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2034:
	.size	_Z37__device_stub__Z10mandelbrotPiPKdS1_iPiPKdS1_i, .-_Z37__device_stub__Z10mandelbrotPiPKdS1_iPiPKdS1_i
	.globl	_Z10mandelbrotPiPKdS1_i
	.type	_Z10mandelbrotPiPKdS1_i, @function
_Z10mandelbrotPiPKdS1_i:
.LFB2035:
	.cfi_startproc
	endbr64
	jmp	_Z37__device_stub__Z10mandelbrotPiPKdS1_iPiPKdS1_i
	.cfi_endproc
.LFE2035:
	.size	_Z10mandelbrotPiPKdS1_i, .-_Z10mandelbrotPiPKdS1_i
	.globl	_Z31__device_stub__Z8colorizePhPKiiPhPKii
	.type	_Z31__device_stub__Z8colorizePhPKiiPhPKii, @function
_Z31__device_stub__Z8colorizePhPKiiPhPKii:
.LFB2036:
	.cfi_startproc
	endbr64
	subq	$136, %rsp
	.cfi_def_cfa_offset 144
	movq	%rdi, 24(%rsp)
	leaq	40(%rsp), %rcx
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	32(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 112(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L13
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z8colorizePhPKii(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 160
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 152
	popq	%rdx
	.cfi_def_cfa_offset 144
.L13:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L15
	call	__stack_chk_fail@PLT
.L15:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2036:
	.size	_Z31__device_stub__Z8colorizePhPKiiPhPKii, .-_Z31__device_stub__Z8colorizePhPKiiPhPKii
	.globl	_Z8colorizePhPKii
	.type	_Z8colorizePhPKii, @function
_Z8colorizePhPKii:
.LFB2037:
	.cfi_startproc
	endbr64
	jmp	_Z31__device_stub__Z8colorizePhPKiiPhPKii
	.cfi_endproc
.LFE2037:
	.size	_Z8colorizePhPKii, .-_Z8colorizePhPKii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"_Z8colorizePhPKii"
.LC1:
	.string	"_Z10mandelbrotPiPKdS1_i"
.LC2:
	.string	"_Z9translatePdS_dddd"
	.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	_Z8colorizePhPKii(%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	_Z10mandelbrotPiPKdS1_i(%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
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	movq	%rbx, %rdi
	xorl	%r9d, %r9d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC2(%rip), %rdx
	orl	$-1, %r8d
	leaq	_Z9translatePdS_dddd(%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:


#include <hip/hip_runtime.h>
/*
 * Utility function for use in __global__ GPU context
 */
#ifndef MANDELBROT_FUN
#define MANDELBROT_FUN
// Store the RGB component into a 3 component flat array
#define RGB(TABLE, POSITION, R, G, B) \
    TABLE[(POSITION) + 0] = R; \
    TABLE[(POSITION) + 1] = G; \
    TABLE[(POSITION) + 2] = B; \

// Compute a float modulus
#define cfmod(a, b) (a - (b * int(a/b)))

// Compute the absolute value
#define cabs(N) (((N)<0)?(-(N)):(N))
#endif //MANDELBROT_FUN


#ifndef CUDAMANDEL_MANDELBROT_CUH
#define CUDAMANDEL_MANDELBROT_CUH

__global__ void translate(
        double *r, double *i,
        double width, double height,
        double offset_r, double offset_i
);

__global__ void mandelbrot(int *iteration_table, const double *r, const double *i, int max);

__global__ void colorize(unsigned char *pixel_table, const int *iteration_table, int max_loop);

#endif //CUDAMANDEL_MANDELBROT_CUH

	.text
	.file	"mandelbrot.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
