#include <stdint.h> 

__global__ void adjust_hue_hwc(const int height, const int width,
		uint8_t * const __restrict__ input, uint8_t * const __restrict__ output, const float hue_delta) {

	// multiply by 3 since we're dealing with contiguous RGB bytes for each pixel
	const int idx = (blockDim.x * blockIdx.x + threadIdx.x) * 3;

	// bounds check
	if (idx > height * width * 3) {
		return;
	}

	// RGB to HSV
	const float r = input[idx];
	const float g = input[idx + 1];
	const float b = input[idx + 2];
	const float M = fmaxf(r, fmaxf(g, b));
	const float m = fminf(r, fminf(g, b));
	const float chroma = M - m;

    // v is the same as M
	float h = 0.0f, s = 0.0f; // v = 0.0;

    // hue
	if (chroma > 0.0f) {
		if (M == r) {

            const float num = (g - b) / chroma;
            const float sgn = num < 0.0f;
            const float sign = powf(-1.0f, sgn);
            h = (sgn * 6.0f + sign * fmodf(sign * num, 6.0f)) / 6.0f;
			
		} else if (M == g) {

			h = ((b - r) / chroma + 2.0f) / 6.0f;

		} else {

			h = ((r - g) / chroma + 4.0f) / 6.0f;
		}        

	} else {

		h = 0.0f;
	}

    // saturation
	if (M > 0.0f) {
		s = chroma / M;

	} else {

		s = 0.0f;
	}


    // hue adjustment
	h = fmodf(h + hue_delta, 1.0f); 

   ////////////////////////////////////////////
   // Murmurhash - based random adjustment
   // uint32_t k = idx;
   // uint32_t seed = 42;

   // k *= 0xcc9e2d51;
   // k = (k << 15) | (k >> 17);
   // k *= 0x1b873593;
   // seed ^= k;

   // seed ^= 1;
   // seed ^= seed >> 16;
   // seed *= 0x85ebca6b;
   // seed ^= seed >> 13;
   // seed *= 0xc2b2ae35;
   // seed ^= seed >> 16;

   // // TODO: Add scaling factor
   // // TODO: Cover shifts both up and down with wrap-around
   // float rand_delta = (seed >> 8) / (float) (1 << 24);
   // h = fmod(h + rand_delta, 1.0f);

   ////////////////////////////////////////////




	// HSV to RGB
	const float new_h = h * 6.0f;
	const float new_chroma = M * s;
	const float x = chroma * (1.0 - fabs(fmod(new_h, 2.0f) - 1.0f));
	const float new_m = M - chroma;

	const bool between_0_and_1 = new_h >= 0.0f && new_h < 1.0f;
	const bool between_1_and_2 = new_h >= 1.0f && new_h < 2.0f;
	const bool between_2_and_3 = new_h >= 2.0f && new_h < 3.0f;
	const bool between_3_and_4 = new_h >= 3.0f && new_h < 4.0f;
	const bool between_4_and_5 = new_h >= 4.0f && new_h < 5.0f;
	const bool between_5_and_6 = new_h >= 5.0f && new_h < 6.0f;

    output[idx] = roundf(new_chroma * (between_0_and_1 || between_5_and_6) +
	  x * (between_1_and_2 || between_4_and_5) + new_m);

	output[idx + 1] = roundf(new_chroma * (between_1_and_2 || between_2_and_3) +
      x * (between_0_and_1 || between_3_and_4) + new_m);	  

	output[idx + 2] =  roundf(new_chroma * (between_3_and_4 || between_4_and_5) +
	  x * (between_2_and_3 || between_5_and_6) + new_m);

}

	.file	"tmpxft_00210556_00000000-6_gpu_impl.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	_Z39__device_stub__Z14adjust_hue_hwciiPhS_fiiPhS_f
	.type	_Z39__device_stub__Z14adjust_hue_hwciiPhS_fiiPhS_f, @function
_Z39__device_stub__Z14adjust_hue_hwciiPhS_fiiPhS_f:
.LFB2032:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movl	%edi, 12(%rsp)
	leaq	48(%rsp), %rdi
	movl	%esi, 8(%rsp)
	leaq	60(%rsp), %rsi
	movss	%xmm0, 4(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	12(%rsp), %rax
	movq	%rdx, 16(%rsp)
	leaq	32(%rsp), %rdx
	movq	%rax, 96(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	4(%rsp), %rax
	movq	%rcx, 24(%rsp)
	leaq	40(%rsp), %rcx
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movl	$1, 56(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	40(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z14adjust_hue_hwciiPhS_f(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 176
	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 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
.LFE2032:
	.size	_Z39__device_stub__Z14adjust_hue_hwciiPhS_fiiPhS_f, .-_Z39__device_stub__Z14adjust_hue_hwciiPhS_fiiPhS_f
	.globl	_Z14adjust_hue_hwciiPhS_f
	.type	_Z14adjust_hue_hwciiPhS_f, @function
_Z14adjust_hue_hwciiPhS_f:
.LFB2033:
	.cfi_startproc
	endbr64
	jmp	_Z39__device_stub__Z14adjust_hue_hwciiPhS_fiiPhS_f
	.cfi_endproc
.LFE2033:
	.size	_Z14adjust_hue_hwciiPhS_f, .-_Z14adjust_hue_hwciiPhS_f
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"_Z14adjust_hue_hwciiPhS_f"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2035:
	.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	_Z14adjust_hue_hwciiPhS_f(%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
.LFE2035:
	.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>
#include <stdint.h>

#ifndef __ADJUST_HUE_CUDA
#define __ADJUST_HUE_CUDA

__global__ void adjust_hue_hwc(const int height, const int width,
		uint8_t * const input, uint8_t * const output, const float hue_delta);

#endif

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