#include "kernel_pinhole_split_fixed_pose_fixed_focal_score.h"
#include "memops.cuh"
#include <cooperative_groups.h>
#include <cooperative_groups/details/partitioning.h>
#include <cooperative_groups/memcpy_async.h>
#include <cooperative_groups/reduce.h>
#include <cuda_runtime.h>

namespace cg = cooperative_groups;

namespace caspar {

__global__ void __launch_bounds__(1024, 1)
    PinholeSplitFixedPoseFixedFocalScoreKernel(
        double* sensor_from_rig,
        unsigned int sensor_from_rig_num_alloc,
        double* principal_point,
        unsigned int principal_point_num_alloc,
        SharedIndex* principal_point_indices,
        double* point,
        unsigned int point_num_alloc,
        SharedIndex* point_indices,
        double* pixel,
        unsigned int pixel_num_alloc,
        double* pose,
        unsigned int pose_num_alloc,
        double* focal,
        unsigned int focal_num_alloc,
        double* const out_rTr,
        size_t problem_size) {
  const int global_thread_idx = blockIdx.x * blockDim.x + threadIdx.x;
  __shared__ uint8_t inout_shared[16384];

  __shared__ SharedIndex principal_point_indices_loc[1024];
  principal_point_indices_loc[threadIdx.x] =
      (global_thread_idx < problem_size
           ? principal_point_indices[global_thread_idx]
           : SharedIndex{0xffffffff, 0xffff, 0xffff});
  __shared__ SharedIndex point_indices_loc[1024];
  point_indices_loc[threadIdx.x] =
      (global_thread_idx < problem_size
           ? point_indices[global_thread_idx]
           : SharedIndex{0xffffffff, 0xffff, 0xffff});

  __shared__ double out_rTr_local[1];

  double r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15,
      r16, r17, r18, r19, r20, r21, r22, r23, r24, r25, r26, r27, r28, r29, r30,
      r31, r32, r33, r34, r35, r36, r37, r38, r39, r40, r41, r42, r43, r44;
  LoadShared<2, double, double>(principal_point,
                                0 * principal_point_num_alloc,
                                principal_point_indices_loc,
                                (double*)inout_shared);
  if (global_thread_idx < problem_size) {
    ReadShared2<double>((double*)inout_shared,
                        principal_point_indices_loc[threadIdx.x].target,
                        r0,
                        r1);
  };
  __syncthreads();
  if (global_thread_idx < problem_size) {
    ReadIdx2<1024, double, double, double2>(
        pixel, 0 * pixel_num_alloc, global_thread_idx, r2, r3);
    r4 = -1.00000000000000000e+00;
    r2 = fma(r2, r4, r0);
    ReadIdx2<1024, double, double, double2>(
        focal, 0 * focal_num_alloc, global_thread_idx, r0, r5);
    ReadIdx2<1024, double, double, double2>(sensor_from_rig,
                                            4 * sensor_from_rig_num_alloc,
                                            global_thread_idx,
                                            r6,
                                            r7);
  };
  LoadShared<2, double, double>(
      point, 0 * point_num_alloc, point_indices_loc, (double*)inout_shared);
  if (global_thread_idx < problem_size) {
    ReadShared2<double>(
        (double*)inout_shared, point_indices_loc[threadIdx.x].target, r8, r9);
  };
  __syncthreads();
  if (global_thread_idx < problem_size) {
    r10 = -2.00000000000000000e+00;
    ReadIdx2<1024, double, double, double2>(sensor_from_rig,
                                            2 * sensor_from_rig_num_alloc,
                                            global_thread_idx,
                                            r11,
                                            r12);
    ReadIdx2<1024, double, double, double2>(
        pose, 2 * pose_num_alloc, global_thread_idx, r13, r14);
    ReadIdx2<1024, double, double, double2>(sensor_from_rig,
                                            0 * sensor_from_rig_num_alloc,
                                            global_thread_idx,
                                            r15,
                                            r16);
    ReadIdx2<1024, double, double, double2>(
        pose, 0 * pose_num_alloc, global_thread_idx, r17, r18);
    r19 = fma(r15, r18, r12 * r13);
    r20 = r16 * r17;
    r19 = fma(r4, r20, r19);
    r19 = fma(r11, r14, r19);
    r20 = r19 * r19;
    r20 = r10 * r20;
    r21 = 1.00000000000000000e+00;
    r22 = r15 * r13;
    r22 = fma(r4, r22, r12 * r18);
    r22 = fma(r16, r14, r22);
    r22 = fma(r11, r17, r22);
    r23 = r22 * r22;
    r23 = fma(r10, r23, r21);
    r24 = r20 + r23;
    r24 = fma(r8, r24, r6);
    r6 = 2.00000000000000000e+00;
    r25 = fma(r15, r14, r12 * r17);
    r26 = r11 * r18;
    r25 = fma(r4, r26, r25);
    r25 = fma(r16, r13, r25);
    r26 = r6 * r25;
    r27 = r22 * r26;
    r28 = fma(r16, r18, r15 * r17);
    r28 = fma(r11, r13, r28);
    r28 = fma(r4, r28, r12 * r14);
    r14 = r10 * r28;
    r29 = fma(r19, r14, r27);
  };
  LoadShared<1, double, double>(
      point, 2 * point_num_alloc, point_indices_loc, (double*)inout_shared);
  if (global_thread_idx < problem_size) {
    ReadShared1<double>(
        (double*)inout_shared, point_indices_loc[threadIdx.x].target, r30);
  };
  __syncthreads();
  if (global_thread_idx < problem_size) {
    r31 = r6 * r22;
    r32 = r19 * r26;
    r31 = fma(r28, r31, r32);
    ReadIdx1<1024, double, double, double>(
        pose, 6 * pose_num_alloc, global_thread_idx, r33);
    r34 = r15 * r11;
    r34 = r34 * r6;
    r35 = r16 * r12;
    r36 = fma(r6, r35, r34);
    ReadIdx2<1024, double, double, double2>(
        pose, 4 * pose_num_alloc, global_thread_idx, r37, r38);
    r39 = r11 * r12;
    r40 = r15 * r16;
    r40 = r40 * r6;
    r39 = fma(r10, r39, r40);
    r41 = r16 * r16;
    r41 = r41 * r10;
    r42 = r21 + r41;
    r43 = r11 * r11;
    r43 = r10 * r43;
    r42 = r42 + r43;
    r24 = fma(r9, r29, r24);
    r24 = fma(r30, r31, r24);
    r24 = fma(r33, r36, r24);
    r24 = fma(r38, r39, r24);
    r24 = fma(r37, r42, r24);
    r42 = r0 * r24;
    r39 = 1.00000000000000008e-15;
    ReadIdx1<1024, double, double, double>(
        sensor_from_rig, 6 * sensor_from_rig_num_alloc, global_thread_idx, r36);
    r32 = fma(r22, r14, r32);
    r32 = fma(r8, r32, r36);
    r35 = fma(r10, r35, r34);
    r41 = r21 + r41;
    r34 = r15 * r15;
    r34 = r10 * r34;
    r41 = r41 + r34;
    r36 = r16 * r11;
    r36 = r36 * r6;
    r31 = r15 * r12;
    r31 = fma(r6, r31, r36);
    r29 = r6 * r19;
    r29 = r29 * r22;
    r26 = fma(r28, r26, r29);
    r44 = r25 * r25;
    r44 = r44 * r10;
    r23 = r44 + r23;
    r32 = fma(r37, r35, r32);
    r32 = fma(r33, r41, r32);
    r32 = fma(r38, r31, r32);
    r32 = fma(r9, r26, r32);
    r32 = fma(r30, r23, r32);
    r23 = copysign(1.0, r32);
    r23 = fma(r39, r23, r32);
    r23 = 1.0 / r23;
    r2 = fma(r23, r42, r2);
    r4 = fma(r3, r4, r1);
    r3 = r6 * r19;
    r3 = fma(r28, r3, r27);
    r3 = fma(r8, r3, r7);
    r8 = r11 * r12;
    r8 = fma(r6, r8, r40);
    r43 = r21 + r43;
    r43 = r43 + r34;
    r34 = r15 * r12;
    r34 = fma(r10, r34, r36);
    r14 = fma(r25, r14, r29);
    r20 = r21 + r20;
    r20 = r20 + r44;
    r3 = fma(r37, r8, r3);
    r3 = fma(r38, r43, r3);
    r3 = fma(r33, r34, r3);
    r3 = fma(r30, r14, r3);
    r3 = fma(r9, r20, r3);
    r20 = r5 * r3;
    r4 = fma(r23, r20, r4);
    r4 = fma(r4, r4, r2 * r2);
  };
  SumStore<double>(out_rTr_local,
                   (double*)inout_shared,
                   0,
                   global_thread_idx < problem_size,
                   r4);
  SumFlushFinal<double>(out_rTr_local, out_rTr, 1);
}

void PinholeSplitFixedPoseFixedFocalScore(
    double* sensor_from_rig,
    unsigned int sensor_from_rig_num_alloc,
    double* principal_point,
    unsigned int principal_point_num_alloc,
    SharedIndex* principal_point_indices,
    double* point,
    unsigned int point_num_alloc,
    SharedIndex* point_indices,
    double* pixel,
    unsigned int pixel_num_alloc,
    double* pose,
    unsigned int pose_num_alloc,
    double* focal,
    unsigned int focal_num_alloc,
    double* const out_rTr,
    size_t problem_size) {
  if (problem_size == 0) {
    return;
  }

  const int n_blocks = (problem_size + 1024 - 1) / 1024;
  PinholeSplitFixedPoseFixedFocalScoreKernel<<<n_blocks, 1024>>>(
      sensor_from_rig,
      sensor_from_rig_num_alloc,
      principal_point,
      principal_point_num_alloc,
      principal_point_indices,
      point,
      point_num_alloc,
      point_indices,
      pixel,
      pixel_num_alloc,
      pose,
      pose_num_alloc,
      focal,
      focal_num_alloc,
      out_rTr,
      problem_size);
}

}  // namespace caspar