// Copyright (c) Meta Platforms, Inc. and its affiliates.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

#include "RedwoodNoiseModel.cuh"

#include <algorithm>
#include <cmath>

#include <cuda_runtime.h>
#include <curand_kernel.h>

namespace {
int roundToNearestMultiple(int num, int base) {
  return std::round(static_cast<float>(num) / static_cast<float>(base)) * base;
}

const int MODEL_N_DIMS = 5;
const int MODEL_N_COLS = 80;

// Read about the noise model here: http://www.alexteichman.com/octo/clams/
// Original source code: http://redwood-data.org/indoor/data/simdepth.py
__device__ float undistort(const int _x,
                           const int _y,
                           const float z,
                           const float* __restrict__ model) {
  const int i2 = (z + 1) / 2;
  const int i1 = i2 - 1;
  const float a = (z - (i1 * 2.0f + 1.0f)) / 2.0f;
  const int x = _x / 8;
  const int y = _y / 6;

  const float f =
      (1.0f - a) *
          model[(y * MODEL_N_COLS + x) * MODEL_N_DIMS + min(max(i1, 0), 4)] +
      a * model[(y * MODEL_N_COLS + x) * MODEL_N_DIMS + min(i2, 4)];

  if (f < 1e-5f)
    return 0.0f;
  else
    return z / f;
}

__global__ void redwoodNoiseModelKernel(const float* __restrict__ depth,
                                        const int H,
                                        const int W,
                                        curandState_t* __restrict__ states,
                                        const float* __restrict__ model,
                                        const float noiseMultiplier,
                                        float* __restrict__ noisyDepth) {
  const int ID = blockIdx.x * blockDim.x + threadIdx.x;
  const int STRIDE = gridDim.x * blockDim.x;

  curandState_t curandState = states[ID];

  const float ymax = H - 1;
  const float xmax = W - 1;
  for (int idx = ID; idx < H * W; idx += STRIDE) {
    // Shuffle pixels
    const int y = min(max((idx / W) + curand_normal(&curandState) * 0.25f *
                                          noiseMultiplier,
                          0.0f),
                      ymax) +
                  0.5f;
    const int x = min(max((idx % W) + curand_normal(&curandState) * 0.25f *
                                          noiseMultiplier,
                          0.0f),
                      xmax) +
                  0.5f;

    // downsample
    const float d = depth[(y - y % 2) * W + x - x % 2];
    // If depth is greater than 10m, the sensor will just return a zero
    if (d >= 10.0f) {
      noisyDepth[idx] = 0.0f;
    } else {
      // Distortion
      // The noise model was originally made for a 640x480 sensor,
      // so re-map our arbitrarily sized sensor to that size!
      const float undistorted_d =
          undistort(static_cast<float>(x) / xmax * 639.0f + 0.5f,
                    static_cast<float>(y) / ymax * 479.0f + 0.5f, d, model);

      // quantization and high freq noise
      if (undistorted_d == 0.0f) {
        noisyDepth[idx] = 0.0f;
      } else {
        const float denom =
            round((35.130f / static_cast<double>(undistorted_d) +
                   curand_normal(&curandState) * 0.027778f * noiseMultiplier) *
                  8.0f);
        noisyDepth[idx] = denom > 1e-5 ? (35.130f * 8.0f / denom) : 0.0f;
      }
    }
  }

  states[ID] = curandState;
}

__global__ void curandStatesSetupKernel(curandState_t* states,
                                        int seed,
                                        int n) {
  const int ID = blockIdx.x * blockDim.x + threadIdx.x;
  if (ID < n) {
    curand_init(seed, ID + 1, 0, &states[ID]);
  }
}

}  // namespace

namespace esp {
namespace sensor {
namespace impl {

struct CurandStates {
  void alloc(const int nStates, const int maxThreadsPerBlock) {
    if (nStates > nStates_) {
      release();
      cudaMalloc(&devStates, nStates * sizeof(curandState_t));
      const int nBlocks =
          std::ceil(static_cast<float>(nStates) / maxThreadsPerBlock);
      curandStatesSetupKernel<<<nBlocks, maxThreadsPerBlock>>>(devStates,
                                                               rand(), nStates);
      nStates_ = nStates;
    }
  }

  void release() {
    if (devStates != 0) {
      cudaFree(devStates);
      devStates = 0;
      nStates_ = 0;
    }
  }

  ~CurandStates() { release(); }

  curandState_t* devStates = 0;

 private:
  int nStates_ = 0;
};

CurandStates* getCurandStates() {
  return new CurandStates();
}
void freeCurandStates(CurandStates* curandStates) {
  if (curandStates != 0)
    delete curandStates;
}

void simulateFromGPU(const int maxThreadsPerBlock,
                     const int warpSize,
                     const float* __restrict__ devDepth,
                     const int H,
                     const int W,
                     const float* __restrict__ devModel,
                     CurandStates* curandStates,
                     const float noiseMultiplier,
                     float* __restrict__ devNoisyDepth) {
  const int totalConcurrency = std::ceil(static_cast<float>(H * W) / 4.0f);
  const int nThreads =
      std::min(std::max(roundToNearestMultiple(totalConcurrency, warpSize), 1),
               maxThreadsPerBlock);
  const int nBlocks =
      std::ceil(static_cast<float>(totalConcurrency) / nThreads);

  curandStates->alloc(nBlocks * nThreads, maxThreadsPerBlock);
  redwoodNoiseModelKernel<<<nBlocks, nThreads>>>(
      devDepth, H, W, curandStates->devStates, devModel, noiseMultiplier,
      devNoisyDepth);
}

void simulateFromCPU(const int maxThreadsPerBlock,
                     const int warpSize,
                     const float* __restrict__ depth,
                     const int H,
                     const int W,
                     const float* __restrict__ devModel,
                     CurandStates* curandStates,
                     const float noiseMultiplier,
                     float* __restrict__ noisyDepth) {
  float *devDepth, *devNoisyDepth;
  cudaMalloc(&devDepth, H * W * sizeof(float));
  cudaMalloc(&devNoisyDepth, H * W * sizeof(float));

  cudaMemcpy(devDepth, depth, H * W * sizeof(float), cudaMemcpyHostToDevice);

  simulateFromGPU(maxThreadsPerBlock, warpSize, devDepth, H, W, devModel,
                  curandStates, noiseMultiplier, devNoisyDepth);

  cudaMemcpy(noisyDepth, devNoisyDepth, H * W * sizeof(float),
             cudaMemcpyDeviceToHost);

  cudaFree(devNoisyDepth);
  cudaFree(devDepth);
}
}  // namespace impl
}  // namespace sensor
}  // namespace esp

	.file	"tmpxft_00383894_00000000-6_RedwoodNoiseModel.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2518:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2518:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab22roundToNearestMultipleEii
	.hidden	_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab22roundToNearestMultipleEii
	.type	_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab22roundToNearestMultipleEii, @function
_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab22roundToNearestMultipleEii:
.LFB2502:
	.cfi_startproc
	endbr64
	cvtsi2ssl	%esi, %xmm1
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	cvtsi2ssl	%edi, %xmm0
	movss	%xmm1, 12(%rsp)
	divss	%xmm1, %xmm0
	call	roundf@PLT
	movss	12(%rsp), %xmm1
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	mulss	%xmm0, %xmm1
	cvttss2sil	%xmm1, %eax
	ret
	.cfi_endproc
.LFE2502:
	.size	_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab22roundToNearestMultipleEii, .-_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab22roundToNearestMultipleEii
	.globl	_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab9undistortEiifPKf
	.hidden	_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab9undistortEiifPKf
	.type	_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab9undistortEiifPKf, @function
_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab9undistortEiifPKf:
.LFB2503:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2503:
	.size	_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab9undistortEiifPKf, .-_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab9undistortEiifPKf
	.globl	_ZN3esp6sensor4impl15getCurandStatesEv
	.type	_ZN3esp6sensor4impl15getCurandStatesEv, @function
_ZN3esp6sensor4impl15getCurandStatesEv:
.LFB2509:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	movl	$16, %edi
	call	_Znwm@PLT
	xorl	%edx, %edx
	xorl	%ecx, %ecx
	movq	%rdx, (%rax)
	movl	%ecx, 8(%rax)
	popq	%rsi
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2509:
	.size	_ZN3esp6sensor4impl15getCurandStatesEv, .-_ZN3esp6sensor4impl15getCurandStatesEv
	.globl	_ZN3esp6sensor4impl16freeCurandStatesEPNS1_12CurandStatesE
	.type	_ZN3esp6sensor4impl16freeCurandStatesEPNS1_12CurandStatesE, @function
_ZN3esp6sensor4impl16freeCurandStatesEPNS1_12CurandStatesE:
.LFB2513:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA2513
	endbr64
	testq	%rdi, %rdi
	je	.L8
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	movq	(%rdi), %rdi
	testq	%rdi, %rdi
	je	.L10
	call	cudaFree@PLT
.L10:
	movq	%rbx, %rdi
	movl	$16, %esi
	popq	%rbx
	.cfi_restore 3
	.cfi_def_cfa_offset 8
	jmp	_ZdlPvm@PLT
.L8:
	ret
	.cfi_endproc
.LFE2513:
	.globl	__gxx_personality_v0
	.section	.gcc_except_table,"a",@progbits
.LLSDA2513:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE2513-.LLSDACSB2513
.LLSDACSB2513:
.LLSDACSE2513:
	.text
	.size	_ZN3esp6sensor4impl16freeCurandStatesEPNS1_12CurandStatesE, .-_ZN3esp6sensor4impl16freeCurandStatesEPNS1_12CurandStatesE
	.globl	_Z129__device_stub__ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23redwoodNoiseModelKernelEPKfiiP17curandStateXORWOWS1_fPfPKfiiP17curandStateXORWOWS0_fPf
	.hidden	_Z129__device_stub__ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23redwoodNoiseModelKernelEPKfiiP17curandStateXORWOWS1_fPfPKfiiP17curandStateXORWOWS0_fPf
	.type	_Z129__device_stub__ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23redwoodNoiseModelKernelEPKfiiP17curandStateXORWOWS1_fPfPKfiiP17curandStateXORWOWS0_fPf, @function
_Z129__device_stub__ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23redwoodNoiseModelKernelEPKfiiP17curandStateXORWOWS1_fPfPKfiiP17curandStateXORWOWS0_fPf:
.LFB2540:
	.cfi_startproc
	endbr64
	subq	$184, %rsp
	.cfi_def_cfa_offset 192
	movl	%esi, 12(%rsp)
	leaq	76(%rsp), %rsi
	movl	%edx, 8(%rsp)
	leaq	48(%rsp), %rdx
	movss	%xmm0, 4(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 168(%rsp)
	xorl	%eax, %eax
	leaq	16(%rsp), %rax
	movq	%rdi, 16(%rsp)
	leaq	64(%rsp), %rdi
	movq	%rax, 112(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	40(%rsp), %rax
	movq	%rcx, 24(%rsp)
	leaq	56(%rsp), %rcx
	movq	%rax, 160(%rsp)
	movabsq	$4294967297, %rax
	movq	%r8, 32(%rsp)
	movq	%r9, 40(%rsp)
	movq	%rax, 64(%rsp)
	movl	$1, 72(%rsp)
	movq	%rax, 76(%rsp)
	movl	$1, 84(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L18
	pushq	56(%rsp)
	.cfi_def_cfa_offset 200
	leaq	_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23redwoodNoiseModelKernelEPKfiiP17curandStateXORWOWS1_fPf(%rip), %rdi
	pushq	56(%rsp)
	.cfi_def_cfa_offset 208
	movq	92(%rsp), %rcx
	movl	100(%rsp), %r8d
	movq	80(%rsp), %rsi
	movl	88(%rsp), %edx
	leaq	128(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 200
	popq	%rdx
	.cfi_def_cfa_offset 192
.L18:
	movq	168(%rsp), %rax
	subq	%fs:40, %rax
	je	.L20
	call	__stack_chk_fail@PLT
.L20:
	addq	$184, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2540:
	.size	_Z129__device_stub__ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23redwoodNoiseModelKernelEPKfiiP17curandStateXORWOWS1_fPfPKfiiP17curandStateXORWOWS0_fPf, .-_Z129__device_stub__ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23redwoodNoiseModelKernelEPKfiiP17curandStateXORWOWS1_fPfPKfiiP17curandStateXORWOWS0_fPf
	.globl	_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23redwoodNoiseModelKernelEPKfiiP17curandStateXORWOWS1_fPf
	.hidden	_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23redwoodNoiseModelKernelEPKfiiP17curandStateXORWOWS1_fPf
	.type	_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23redwoodNoiseModelKernelEPKfiiP17curandStateXORWOWS1_fPf, @function
_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23redwoodNoiseModelKernelEPKfiiP17curandStateXORWOWS1_fPf:
.LFB2541:
	.cfi_startproc
	endbr64
	jmp	_Z129__device_stub__ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23redwoodNoiseModelKernelEPKfiiP17curandStateXORWOWS1_fPfPKfiiP17curandStateXORWOWS0_fPf
	.cfi_endproc
.LFE2541:
	.size	_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23redwoodNoiseModelKernelEPKfiiP17curandStateXORWOWS1_fPf, .-_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23redwoodNoiseModelKernelEPKfiiP17curandStateXORWOWS1_fPf
	.globl	_Z120__device_stub__ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23curandStatesSetupKernelEP17curandStateXORWOWiiP17curandStateXORWOWii
	.hidden	_Z120__device_stub__ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23curandStatesSetupKernelEP17curandStateXORWOWiiP17curandStateXORWOWii
	.type	_Z120__device_stub__ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23curandStatesSetupKernelEP17curandStateXORWOWiiP17curandStateXORWOWii, @function
_Z120__device_stub__ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23curandStatesSetupKernelEP17curandStateXORWOWiiP17curandStateXORWOWii:
.LFB2542:
	.cfi_startproc
	endbr64
	subq	$120, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	24(%rsp), %rcx
	leaq	32(%rsp), %rdi
	movl	%esi, 4(%rsp)
	leaq	44(%rsp), %rsi
	movl	%edx, (%rsp)
	leaq	16(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 40(%rsp)
	movq	%rax, 80(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 88(%rsp)
	movq	%rsp, %rax
	movq	%rax, 96(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 32(%rsp)
	movq	%rax, 44(%rsp)
	movl	$1, 52(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L23
	pushq	24(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23curandStatesSetupKernelEP17curandStateXORWOWii(%rip), %rdi
	pushq	24(%rsp)
	.cfi_def_cfa_offset 144
	movq	60(%rsp), %rcx
	movl	68(%rsp), %r8d
	movq	48(%rsp), %rsi
	movl	56(%rsp), %edx
	leaq	96(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 136
	popq	%rdx
	.cfi_def_cfa_offset 128
.L23:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L25
	call	__stack_chk_fail@PLT
.L25:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2542:
	.size	_Z120__device_stub__ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23curandStatesSetupKernelEP17curandStateXORWOWiiP17curandStateXORWOWii, .-_Z120__device_stub__ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23curandStatesSetupKernelEP17curandStateXORWOWiiP17curandStateXORWOWii
	.globl	_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23curandStatesSetupKernelEP17curandStateXORWOWii
	.hidden	_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23curandStatesSetupKernelEP17curandStateXORWOWii
	.type	_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23curandStatesSetupKernelEP17curandStateXORWOWii, @function
_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23curandStatesSetupKernelEP17curandStateXORWOWii:
.LFB2543:
	.cfi_startproc
	endbr64
	jmp	_Z120__device_stub__ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23curandStatesSetupKernelEP17curandStateXORWOWiiP17curandStateXORWOWii
	.cfi_endproc
.LFE2543:
	.size	_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23curandStatesSetupKernelEP17curandStateXORWOWii, .-_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23curandStatesSetupKernelEP17curandStateXORWOWii
	.globl	_ZN3esp6sensor4impl15simulateFromGPUEiiPKfiiS3_PNS1_12CurandStatesEfPf
	.type	_ZN3esp6sensor4impl15simulateFromGPUEiiPKfiiS3_PNS1_12CurandStatesEfPf, @function
_ZN3esp6sensor4impl15simulateFromGPUEiiPKfiiS3_PNS1_12CurandStatesEfPf:
.LFB2514:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	%ecx, %r15d
	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
	movl	%edi, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	$1, %ebx
	subq	$88, %rsp
	.cfi_def_cfa_offset 144
	movq	152(%rsp), %rax
	movss	%xmm0, 40(%rsp)
	movq	144(%rsp), %rbp
	movq	%rdx, 16(%rsp)
	movq	%rax, 32(%rsp)
	movl	%r8d, %eax
	imull	%ecx, %eax
	movl	%r8d, 12(%rsp)
	movq	%r9, 24(%rsp)
	movl	%esi, 44(%rsp)
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC0(%rip), %xmm0
	call	ceilf@PLT
	movl	44(%rsp), %esi
	cvttss2sil	%xmm0, %r13d
	movl	%r13d, %edi
	call	_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab22roundToNearestMultipleEii
	cvtsi2ssl	%r13d, %xmm0
	testl	%eax, %eax
	cmovg	%eax, %ebx
	cmpl	%r12d, %ebx
	cmovg	%r12d, %ebx
	cvtsi2ssl	%ebx, %xmm1
	movl	%ebx, %r13d
	divss	%xmm1, %xmm0
	call	ceilf@PLT
	cvttss2sil	%xmm0, %r14d
	imull	%r14d, %r13d
	cmpl	8(%rbp), %r13d
	jle	.L29
	movq	0(%rbp), %rdi
	testq	%rdi, %rdi
	je	.L30
	call	cudaFree@PLT
	xorl	%eax, %eax
	xorl	%edx, %edx
	movq	%rax, 0(%rbp)
	movl	%edx, 8(%rbp)
.L30:
	movslq	%r13d, %rsi
	movq	%rbp, %rdi
	imulq	$48, %rsi, %rsi
	call	cudaMalloc@PLT
	cvtsi2ssl	%r12d, %xmm1
	cvtsi2ssl	%r13d, %xmm0
	divss	%xmm1, %xmm0
	call	ceilf@PLT
	movl	%r12d, 68(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movabsq	$4294967297, %rax
	cvttss2sil	%xmm0, %edx
	movq	%rax, 72(%rsp)
	movl	76(%rsp), %ecx
	movq	%rax, 60(%rsp)
	movl	64(%rsp), %esi
	movl	%edx, 56(%rsp)
	movq	68(%rsp), %rdx
	movq	56(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L31
	call	rand@PLT
	movq	0(%rbp), %rdi
	movl	%r13d, %edx
	movl	%eax, %esi
	call	_Z120__device_stub__ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23curandStatesSetupKernelEP17curandStateXORWOWiiP17curandStateXORWOWii
.L31:
	movl	%r13d, 8(%rbp)
.L29:
	movabsq	$4294967297, %rax
	movl	%ebx, 68(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	%rax, 72(%rsp)
	movl	76(%rsp), %ecx
	movq	%rax, 60(%rsp)
	movq	68(%rsp), %rdx
	movl	%r14d, 56(%rsp)
	movl	64(%rsp), %esi
	movq	56(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L28
	movq	0(%rbp), %rcx
	movq	32(%rsp), %r9
	movl	%r15d, %esi
	movss	40(%rsp), %xmm0
	movq	24(%rsp), %r8
	movl	12(%rsp), %edx
	movq	16(%rsp), %rdi
	addq	$88, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 56
	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
	jmp	_Z129__device_stub__ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23redwoodNoiseModelKernelEPKfiiP17curandStateXORWOWS1_fPfPKfiiP17curandStateXORWOWS0_fPf
.L28:
	.cfi_restore_state
	addq	$88, %rsp
	.cfi_def_cfa_offset 56
	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
.LFE2514:
	.size	_ZN3esp6sensor4impl15simulateFromGPUEiiPKfiiS3_PNS1_12CurandStatesEfPf, .-_ZN3esp6sensor4impl15simulateFromGPUEiiPKfiiS3_PNS1_12CurandStatesEfPf
	.globl	_ZN3esp6sensor4impl15simulateFromCPUEiiPKfiiS3_PNS1_12CurandStatesEfPf
	.type	_ZN3esp6sensor4impl15simulateFromCPUEiiPKfiiS3_PNS1_12CurandStatesEfPf, @function
_ZN3esp6sensor4impl15simulateFromCPUEiiPKfiiS3_PNS1_12CurandStatesEfPf:
.LFB2515:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%rdx, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movl	%esi, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%ecx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%ecx, %ebx
	imull	%r8d, %ebx
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	movq	%r9, 24(%rsp)
	movslq	%ebx, %rbx
	movq	136(%rsp), %r15
	movl	%edi, 12(%rsp)
	salq	$2, %rbx
	leaq	40(%rsp), %rdi
	movq	128(%rsp), %r12
	movss	%xmm0, 20(%rsp)
	movq	%rbx, %rsi
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	movl	%r8d, 16(%rsp)
	call	cudaMalloc@PLT
	leaq	48(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movq	40(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%r14, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	pushq	48(%rsp)
	.cfi_def_cfa_offset 136
	movl	%ebp, %ecx
	movl	%r13d, %esi
	pushq	%r12
	.cfi_def_cfa_offset 144
	movss	36(%rsp), %xmm0
	movq	40(%rsp), %r9
	movl	32(%rsp), %r8d
	movl	28(%rsp), %edi
	movq	56(%rsp), %rdx
	call	_ZN3esp6sensor4impl15simulateFromGPUEiiPKfiiS3_PNS1_12CurandStatesEfPf
	popq	%rax
	.cfi_def_cfa_offset 136
	popq	%rdx
	.cfi_def_cfa_offset 128
	movl	$2, %ecx
	movq	48(%rsp), %rsi
	movq	%rbx, %rdx
	movq	%r15, %rdi
	call	cudaMemcpy@PLT
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L38
	call	__stack_chk_fail@PLT
.L38:
	addq	$72, %rsp
	.cfi_def_cfa_offset 56
	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
.LFE2515:
	.size	_ZN3esp6sensor4impl15simulateFromCPUEiiPKfiiS3_PNS1_12CurandStatesEfPf, .-_ZN3esp6sensor4impl15simulateFromCPUEiiPKfiiS3_PNS1_12CurandStatesEfPf
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23curandStatesSetupKernelEP17curandStateXORWOWii"
.LC2:
	.string	"_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23redwoodNoiseModelKernelEPKfiiP17curandStateXORWOWS1_fPf"
.LC3:
	.string	"precalc_xorwow_matrix"
.LC4:
	.string	"precalc_xorwow_offset_matrix"
.LC5:
	.string	"mrg32k3aM1"
.LC6:
	.string	"mrg32k3aM2"
.LC7:
	.string	"mrg32k3aM1SubSeq"
.LC8:
	.string	"mrg32k3aM2SubSeq"
.LC9:
	.string	"mrg32k3aM1Seq"
.LC10:
	.string	"mrg32k3aM2Seq"
.LC11:
	.string	"__cr_lgamma_table"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2545:
	.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	.LC1(%rip), %rdx
	movq	%rax, %rdi
	movq	%rax, %rbx
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	leaq	_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23curandStatesSetupKernelEP17curandStateXORWOWii(%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	.LC2(%rip), %rdx
	orl	$-1, %r8d
	leaq	_ZN53_GLOBAL__N__86f7f6b6_20_RedwoodNoiseModel_cu_768b7fab23redwoodNoiseModelKernelEPKfiiP17curandStateXORWOWS1_fPf(%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	%r8d, %r8d
	pushq	$0
	.cfi_def_cfa_offset 24
	leaq	.LC3(%rip), %rdx
	movl	$102400, %r9d
	leaq	_ZL21precalc_xorwow_matrix(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 32
	movq	%rdx, %rcx
	call	__cudaRegisterVar@PLT
	popq	%rax
	.cfi_def_cfa_offset 24
	popq	%rdx
	.cfi_def_cfa_offset 16
	pushq	$0
	.cfi_def_cfa_offset 24
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC4(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	movq	%rdx, %rcx
	movl	$102400, %r9d
	leaq	_ZL28precalc_xorwow_offset_matrix(%rip), %rsi
	call	__cudaRegisterVar@PLT
	popq	%rcx
	.cfi_def_cfa_offset 24
	popq	%rsi
	.cfi_def_cfa_offset 16
	pushq	$0
	.cfi_def_cfa_offset 24
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC5(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	movq	%rdx, %rcx
	movl	$2304, %r9d
	leaq	_ZL10mrg32k3aM1(%rip), %rsi
	call	__cudaRegisterVar@PLT
	popq	%rdi
	.cfi_def_cfa_offset 24
	popq	%r8
	.cfi_def_cfa_offset 16
	pushq	$0
	.cfi_def_cfa_offset 24
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC6(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	movq	%rdx, %rcx
	movl	$2304, %r9d
	leaq	_ZL10mrg32k3aM2(%rip), %rsi
	call	__cudaRegisterVar@PLT
	popq	%r9
	.cfi_def_cfa_offset 24
	popq	%r10
	.cfi_def_cfa_offset 16
	pushq	$0
	.cfi_def_cfa_offset 24
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC7(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	movq	%rdx, %rcx
	movl	$2016, %r9d
	leaq	_ZL16mrg32k3aM1SubSeq(%rip), %rsi
	call	__cudaRegisterVar@PLT
	popq	%r11
	.cfi_def_cfa_offset 24
	popq	%rax
	.cfi_def_cfa_offset 16
	pushq	$0
	.cfi_def_cfa_offset 24
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC8(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	movq	%rdx, %rcx
	movl	$2016, %r9d
	leaq	_ZL16mrg32k3aM2SubSeq(%rip), %rsi
	call	__cudaRegisterVar@PLT
	popq	%rax
	.cfi_def_cfa_offset 24
	popq	%rdx
	.cfi_def_cfa_offset 16
	pushq	$0
	.cfi_def_cfa_offset 24
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC9(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	movq	%rdx, %rcx
	movl	$2304, %r9d
	leaq	_ZL13mrg32k3aM1Seq(%rip), %rsi
	call	__cudaRegisterVar@PLT
	popq	%rcx
	.cfi_def_cfa_offset 24
	popq	%rsi
	.cfi_def_cfa_offset 16
	pushq	$0
	.cfi_def_cfa_offset 24
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC10(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	movq	%rdx, %rcx
	movl	$2304, %r9d
	leaq	_ZL13mrg32k3aM2Seq(%rip), %rsi
	call	__cudaRegisterVar@PLT
	popq	%rdi
	.cfi_def_cfa_offset 24
	popq	%r8
	.cfi_def_cfa_offset 16
	pushq	$0
	.cfi_def_cfa_offset 24
	pushq	$1
	.cfi_def_cfa_offset 32
	leaq	.LC11(%rip), %rdx
	movq	%rbx, %rdi
	xorl	%r8d, %r8d
	movl	$72, %r9d
	movq	%rdx, %rcx
	leaq	_ZL17__cr_lgamma_table(%rip), %rsi
	call	__cudaRegisterVar@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	popq	%r9
	.cfi_def_cfa_offset 24
	popq	%r10
	.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
.LFE2545:
	.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
	.local	_ZL17__cr_lgamma_table
	.comm	_ZL17__cr_lgamma_table,72,32
	.local	_ZL13mrg32k3aM2Seq
	.comm	_ZL13mrg32k3aM2Seq,2304,32
	.local	_ZL13mrg32k3aM1Seq
	.comm	_ZL13mrg32k3aM1Seq,2304,32
	.local	_ZL16mrg32k3aM2SubSeq
	.comm	_ZL16mrg32k3aM2SubSeq,2016,32
	.local	_ZL16mrg32k3aM1SubSeq
	.comm	_ZL16mrg32k3aM1SubSeq,2016,32
	.local	_ZL10mrg32k3aM2
	.comm	_ZL10mrg32k3aM2,2304,32
	.local	_ZL10mrg32k3aM1
	.comm	_ZL10mrg32k3aM1,2304,32
	.local	_ZL28precalc_xorwow_offset_matrix
	.comm	_ZL28precalc_xorwow_offset_matrix,102400,32
	.local	_ZL21precalc_xorwow_matrix
	.comm	_ZL21precalc_xorwow_matrix,102400,32
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC0:
	.long	1048576000
	.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:

// Copyright (c) Meta Platforms, Inc. and its affiliates.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

#ifndef ESP_SENSOR_REDWOODNOISEMODEL_CUH_
#define ESP_SENSOR_REDWOODNOISEMODEL_CUH_

namespace esp {
namespace sensor {
namespace impl {

struct CurandStates;

CurandStates* getCurandStates();

void freeCurandStates(CurandStates* curandStates);

void simulateFromCPU(const int maxThreadsPerBlock,
                     const int warpSize,
                     const float* __restrict__ depth,
                     const int H,
                     const int W,
                     const float* __restrict__ devModel,
                     CurandStates* curandStates,
                     const float noiseMultiplier,
                     float* __restrict__ noisyDepth);

void simulateFromGPU(const int maxThreadsPerBlock,
                     const int warpSize,
                     const float* __restrict__ devDepth,
                     const int H,
                     const int W,
                     const float* __restrict__ devModel,
                     CurandStates* curandStates,
                     const float noiseMultiplier,
                     float* __restrict__ devNoisyDepth);
}  // namespace impl
}  // namespace sensor
}  // namespace esp

#endif  // ESP_SENSOR_REDWOODNOISEMODEL_CUH_

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