// File: ingowald[P]closestSurfacePointQueries/src/distanceQueries.cpp
// Function: rtdqComputeClosestPointsfi
void rtdqComputeClosestPointsfi(distance_query_scene scene,
                                  float   *out_closest_point_pos_x,
                                  float   *out_closest_point_pos_y,
                                  float   *out_closest_point_pos_z,
                                  size_t   out_closest_point_pos_stride,
                                  float   *out_closest_point_dist,
                                  size_t   out_closest_point_dist_stride,
                                  int32_t *out_closest_point_primID,
                                  size_t   out_closest_point_primID_stride,
                                  const float *in_query_point_x,
                                  const float *in_query_point_y,
                                  const float *in_query_point_z,
                                  const size_t in_query_point_stride,
                                  const size_t numQueryPoints)
  {
    QueryObject *qo = (QueryObject *)scene;
    if (!qo)
      return;

#if CACHE_LAST_RESULT
    vec3f lastPoint;
#endif
    for (size_t qpi=0;qpi<numQueryPoints;qpi++) {
      const vec3fa queryPoint(in_query_point_x[qpi*in_query_point_stride],
                              in_query_point_y[qpi*in_query_point_stride],
                              in_query_point_z[qpi*in_query_point_stride]);

      QueryResult qr;
      qr.distance = std::numeric_limits<float>::infinity();
      qr.primID   = -1;
#if CACHE_LAST_RESULT
      if (qpi > 0) {
        qr.distance = length(queryPoint-lastPoint)*(((1<<22)+1)/float(1<<22));
      }
#endif
      oneQuery(qr,qo,queryPoint);
#if CACHE_LAST_RESULT
      lastPoint = qr.point;
#endif
      if (out_closest_point_pos_x)
        out_closest_point_pos_x[qpi*out_closest_point_pos_stride] = qr.point.x;
      if (out_closest_point_pos_y)
        out_closest_point_pos_y[qpi*out_closest_point_pos_stride] = qr.point.y;
      if (out_closest_point_pos_z)
        out_closest_point_pos_z[qpi*out_closest_point_pos_stride] = qr.point.z;
      if (out_closest_point_primID)
        out_closest_point_primID[qpi*out_closest_point_primID_stride] = qr.primID;
      if (out_closest_point_dist)
        out_closest_point_dist[qpi*out_closest_point_dist_stride] = qr.distance;
    }
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x8a14
jmp 0x8b39
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq 0x48(%rbp), %rax
jae 0x8b39
movq 0x28(%rbp), %rax
movq -0x40(%rbp), %rcx
imulq 0x40(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movq 0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
imulq 0x40(%rbp), %rcx
movss (%rax,%rcx,4), %xmm1
movq 0x38(%rbp), %rax
movq -0x40(%rbp), %rcx
imulq 0x40(%rbp), %rcx
movss (%rax,%rcx,4), %xmm2
leaq -0x50(%rbp), %rdi
callq 0x9300
leaq -0x68(%rbp), %rdi
callq 0x9340
callq 0x90e0
movss %xmm0, -0x58(%rbp)
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
movq -0x38(%rbp), %rsi
leaq -0x68(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x8430
cmpq $0x0, -0x10(%rbp)
je 0x8ab2
movss -0x68(%rbp), %xmm0
movq -0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
imulq -0x28(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
cmpq $0x0, -0x18(%rbp)
je 0x8ad0
movss -0x64(%rbp), %xmm0
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
imulq -0x28(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
cmpq $0x0, -0x20(%rbp)
je 0x8aee
movss -0x60(%rbp), %xmm0
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
imulq -0x28(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
cmpq $0x0, 0x18(%rbp)
je 0x8b08
movl -0x54(%rbp), %edx
movq 0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
imulq 0x20(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
cmpq $0x0, -0x30(%rbp)
je 0x8b26
movss -0x58(%rbp), %xmm0
movq -0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
imulq 0x10(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
jmp 0x8b28
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x8a1c
addq $0x70, %rsp
popq %rbp
retq
nop