// File: /workspace/github_c_makefile_star2_O2/zpole[P]IICV/vl/sift.c
// Function: vl_sift_calc_keypoint_descriptor
VL_EXPORT
void
vl_sift_calc_keypoint_descriptor (VlSiftFilt *f,
                                  vl_sift_pix *descr,
                                  VlSiftKeypoint const* k,
                                  double angle0)
{
  /*
     The SIFT descriptor is a three dimensional histogram of the
     position and orientation of the gradient.  There are NBP bins for
     each spatial dimension and NBO bins for the orientation dimension,
     for a total of NBP x NBP x NBO bins.

     The support of each spatial bin has an extension of SBP = 3sigma
     pixels, where sigma is the scale of the keypoint.  Thus all the
     bins together have a support SBP x NBP pixels wide. Since
     weighting and interpolation of pixel is used, the support extends
     by another half bin. Therefore, the support is a square window of
     SBP x (NBP + 1) pixels. Finally, since the patch can be
     arbitrarily rotated, we need to consider a window 2W += sqrt(2) x
     SBP x (NBP + 1) pixels wide.
  */

  double const magnif      = f-> magnif ;

  double       xper        = pow (2.0, f->o_cur) ;

  int          w           = f-> octave_width ;
  int          h           = f-> octave_height ;
  int const    xo          = 2 ;         /* x-stride */
  int const    yo          = 2 * w ;     /* y-stride */
  int const    so          = 2 * w * h ; /* s-stride */
  double       x           = k-> x     / xper ;
  double       y           = k-> y     / xper ;
  double       sigma       = k-> sigma / xper ;

  int          xi          = (int) (x + 0.5) ;
  int          yi          = (int) (y + 0.5) ;
  int          si          = k-> is ;

  double const st0         = sin (angle0) ;
  double const ct0         = cos (angle0) ;
  double const SBP         = magnif * sigma + VL_EPSILON_D ;
  int    const W           = floor
    (sqrt(2.0) * SBP * (NBP + 1) / 2.0 + 0.5) ;

  int const binto = 1 ;          /* bin theta-stride */
  int const binyo = NBO * NBP ;  /* bin y-stride */
  int const binxo = NBO ;        /* bin x-stride */

  int bin, dxi, dyi ;
  vl_sift_pix const *pt ;
  vl_sift_pix       *dpt ;

  /* check bounds */
  if(k->o  != f->o_cur        ||
     xi    <  0               ||
     xi    >= w               ||
     yi    <  0               ||
     yi    >= h -    1        ||
     si    <  f->s_min + 1    ||
     si    >  f->s_max - 2     )
    return ;

  /* synchronize gradient buffer */
  update_gradient (f) ;

  /* VL_PRINTF("W = %d ; magnif = %g ; SBP = %g\n", W,magnif,SBP) ; */

  /* clear descriptor */
  memset (descr, 0, sizeof(vl_sift_pix) * NBO*NBP*NBP) ;

  /* Center the scale space and the descriptor on the current keypoint.
   * Note that dpt is pointing to the bin of center (SBP/2,SBP/2,0).
   */
  pt  = f->grad + xi*xo + yi*yo + (si - f->s_min - 1)*so ;
  dpt = descr + (NBP/2) * binyo + (NBP/2) * binxo ;

#undef atd
#define atd(dbinx,dbiny,dbint) *(dpt + (dbint)*binto + (dbiny)*binyo + (dbinx)*binxo)

  /*
   * Process pixels in the intersection of the image rectangle
   * (1,1)-(M-1,N-1) and the keypoint bounding box.
   */
  for(dyi =  VL_MAX (- W, 1 - yi    ) ;
      dyi <= VL_MIN (+ W, h - yi - 2) ; ++ dyi) {

    for(dxi =  VL_MAX (- W, 1 - xi    ) ;
        dxi <= VL_MIN (+ W, w - xi - 2) ; ++ dxi) {

      /* retrieve */
      vl_sift_pix mod   = *( pt + dxi*xo + dyi*yo + 0 ) ;
      vl_sift_pix angle = *( pt + dxi*xo + dyi*yo + 1 ) ;
      vl_sift_pix theta = vl_mod_2pi_f (angle - angle0) ;

      /* fractional displacement */
      vl_sift_pix dx = xi + dxi - x;
      vl_sift_pix dy = yi + dyi - y;

      /* get the displacement normalized w.r.t. the keypoint
         orientation and extension */
      vl_sift_pix nx = ( ct0 * dx + st0 * dy) / SBP ;
      vl_sift_pix ny = (-st0 * dx + ct0 * dy) / SBP ;
      vl_sift_pix nt = NBO * theta / (2 * VL_PI) ;

      /* Get the Gaussian weight of the sample. The Gaussian window
       * has a standard deviation equal to NBP/2. Note that dx and dy
       * are in the normalized frame, so that -NBP/2 <= dx <=
       * NBP/2. */
      vl_sift_pix const wsigma = f->windowSize ;
      vl_sift_pix win = fast_expn
        ((nx*nx + ny*ny)/(2.0 * wsigma * wsigma)) ;

      /* The sample will be distributed in 8 adjacent bins.
         We start from the ``lower-left'' bin. */
      int         binx = (int)vl_floor_f (nx - 0.5) ;
      int         biny = (int)vl_floor_f (ny - 0.5) ;
      int         bint = (int)vl_floor_f (nt) ;
      vl_sift_pix rbinx = nx - (binx + 0.5) ;
      vl_sift_pix rbiny = ny - (biny + 0.5) ;
      vl_sift_pix rbint = nt - bint ;
      int         dbinx ;
      int         dbiny ;
      int         dbint ;

      /* Distribute the current sample into the 8 adjacent bins*/
      for(dbinx = 0 ; dbinx < 2 ; ++dbinx) {
        for(dbiny = 0 ; dbiny < 2 ; ++dbiny) {
          for(dbint = 0 ; dbint < 2 ; ++dbint) {

            if (binx + dbinx >= - (NBP/2) &&
                binx + dbinx <    (NBP/2) &&
                biny + dbiny >= - (NBP/2) &&
                biny + dbiny <    (NBP/2) ) {
              vl_sift_pix weight = win
                * mod
                * vl_abs_f (1 - dbinx - rbinx)
                * vl_abs_f (1 - dbiny - rbiny)
                * vl_abs_f (1 - dbint - rbint) ;

              atd(binx+dbinx, biny+dbiny, (bint+dbint) % NBO) += weight ;
            }
          }
        }
      }
    }
  }

  /* Standard SIFT descriptors are normalized, truncated and normalized again */
  if(1) {

    /* Normalize the histogram to L2 unit length. */
    vl_sift_pix norm = normalize_histogram (descr, descr + NBO*NBP*NBP) ;

    /* Set the descriptor to zero if it is lower than our norm_threshold */
    if(f-> norm_thresh && norm < f-> norm_thresh) {
        for(bin = 0; bin < NBO*NBP*NBP ; ++ bin)
            descr [bin] = 0;
    }
    else {

      /* Truncate at 0.2. */
      for(bin = 0; bin < NBO*NBP*NBP ; ++ bin) {
        if (descr [bin] > 0.2) descr [bin] = 0.2;
      }

      /* Normalize again. */
      normalize_histogram (descr, descr + NBO*NBP*NBP) ;
    }
  }

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movsd %xmm0, 0x30(%rsp)
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %r12
movsd 0xa0(%rdi), %xmm0
movsd %xmm0, 0x50(%rsp)
movl 0x3c(%rdi), %edi
movsd 0x426f(%rip), %xmm0     # 0x2d238
callq 0x90c0
movapd %xmm0, %xmm1
movsd %xmm0, 0x8(%rsp)
movslq 0x58(%r12), %rbp
movl 0x5c(%r12), %r14d
cvtss2sd 0x10(%r15), %xmm0
movsd %xmm0, 0x28(%rsp)
cvtss2sd 0x14(%r15), %xmm0
movsd %xmm0, 0x18(%rsp)
cvtss2sd 0x1c(%r15), %xmm0
divsd %xmm1, %xmm0
mulsd 0x50(%rsp), %xmm0
movapd %xmm0, 0x40(%rsp)
movl 0xc(%r15), %ebx
movsd 0x30(%rsp), %xmm0
callq 0x98d0
movaps %xmm0, 0x60(%rsp)
movsd 0x30(%rsp), %xmm0
callq 0x9470
movaps %xmm0, 0x50(%rsp)
movapd 0x40(%rsp), %xmm1
addsd 0x4b56(%rip), %xmm1     # 0x2db98
movsd 0x52f6(%rip), %xmm0     # 0x2e340
movapd %xmm1, 0x40(%rsp)
mulsd %xmm1, %xmm0
mulsd 0x41e4(%rip), %xmm0     # 0x2d240
movsd 0x415c(%rip), %xmm1     # 0x2d1c0
mulsd %xmm1, %xmm0
addsd %xmm1, %xmm0
callq 0x98f0
movsd 0x8(%rsp), %xmm2
movl (%r15), %eax
movq %r12, (%rsp)
cmpl 0x3c(%r12), %eax
jne 0x290fa
movsd 0x28(%rsp), %xmm1
divsd %xmm2, %xmm1
movsd %xmm1, 0x28(%rsp)
addsd 0x4123(%rip), %xmm1     # 0x2d1c0
cvttsd2si %xmm1, %ecx
movq %rcx, 0x20(%rsp)
testl %ecx, %ecx
js 0x290fa
movl %ebp, %r15d
subl 0x20(%rsp), %r15d
jle 0x290fa
movsd 0x18(%rsp), %xmm1
divsd %xmm2, %xmm1
movsd %xmm1, 0x18(%rsp)
addsd 0x40f4(%rip), %xmm1     # 0x2d1c0
cvttsd2si %xmm1, %ecx
movq %rcx, 0x10(%rsp)
testl %ecx, %ecx
js 0x290fa
leal -0x1(%r14), %eax
cmpl 0x10(%rsp), %eax
jle 0x290fa
movq (%rsp), %rax
cmpl 0x34(%rax), %ebx
jle 0x290fa
movq (%rsp), %rax
movl 0x38(%rax), %eax
addl $-0x2, %eax
cmpl %eax, %ebx
jle 0x2910c
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq %rbp, %rbp
movl %ebp, %eax
imull %r14d, %eax
movl %eax, 0x8(%rsp)
cvttsd2si %xmm0, %eax
movl %eax, 0x70(%rsp)
movq (%rsp), %r12
movq %r12, %rdi
callq 0x2826b
movl $0x200, %edx            # imm = 0x200
movq %r13, %rdi
xorl %esi, %esi
callq 0x9420
movq 0x20(%rsp), %rsi
leal (%rsi,%rsi), %eax
shlq $0x2, %rax
addq 0xb0(%r12), %rax
movq %rbp, 0x98(%rsp)
movl %ebp, %ecx
movq 0x10(%rsp), %rdx
imull %edx, %ecx
movslq %ecx, %rcx
leaq (%rax,%rcx,4), %rax
movl 0x34(%r12), %ecx
notl %ecx
addl %ecx, %ebx
imull 0x8(%rsp), %ebx
movslq %ebx, %rcx
leaq (%rax,%rcx,4), %rax
movq %rax, 0x90(%rsp)
movl 0x70(%rsp), %edi
movl %edi, %eax
negl %eax
pushq $0x1
popq %rbx
movl %ebx, %ecx
subl %edx, %ecx
subl %edx, %r14d
addl $-0x2, %r14d
cmpl %edi, %r14d
cmovgel %edi, %r14d
movq %r13, 0x80(%rsp)
leaq 0x140(%r13), %rdx
movq %rdx, 0x8(%rsp)
movl %ebx, %edx
subl %esi, %edx
addl $-0x2, %r15d
cmpl %edi, %r15d
cmovgel %edi, %r15d
movaps 0x50(%rsp), %xmm2
movaps %xmm2, %xmm0
movaps 0x60(%rsp), %xmm1
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movaps %xmm0, 0xc0(%rsp)
xorps 0x3ebb(%rip), %xmm1     # 0x2d0a0
cmpl %edx, %eax
cmovgl %eax, %edx
movslq %edx, %rdx
movq %rdx, 0x88(%rsp)
movslq %r15d, %rdx
movq %rdx, 0xb8(%rsp)
cmpl %ecx, %eax
cmovgl %eax, %ecx
movslq %ecx, %rdx
movslq %r14d, %rax
movq %rax, 0xa0(%rsp)
movapd 0x40(%rsp), %xmm0
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movapd %xmm0, 0x40(%rsp)
movlhps %xmm2, %xmm1            # xmm1 = xmm1[0],xmm2[0]
movaps %xmm1, 0x60(%rsp)
pushq $0x8
popq %r13
cmpq 0xa0(%rsp), %rdx
jg 0x29531
movq %rdx, %rax
imulq 0x98(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rdx, 0xa8(%rsp)
addl %edx, %ecx
cvtsi2sd %ecx, %xmm0
movq 0x90(%rsp), %rcx
leaq (%rcx,%rax,4), %rax
movq %rax, 0xb0(%rsp)
subsd 0x18(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
movlhps %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd 0xc0(%rsp), %xmm0
movapd %xmm0, 0xd0(%rsp)
movq 0x88(%rsp), %rcx
cmpq 0xb8(%rsp), %rcx
jg 0x29521
movq 0xb0(%rsp), %rax
cvtss2sd 0x4(%rax,%rcx,8), %xmm0
movss (%rax,%rcx,8), %xmm1
movss %xmm1, 0x3c(%rsp)
subsd 0x30(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movq %rcx, %r14
callq 0x28f11
movq 0x20(%rsp), %rax
movq %r14, 0x50(%rsp)
addl %r14d, %eax
cvtsi2sd %eax, %xmm1
subsd 0x28(%rsp), %xmm1
cvtsd2ss %xmm1, %xmm1
mulss 0x5079(%rip), %xmm0     # 0x2e370
cvtss2sd %xmm0, %xmm0
divsd 0x407d(%rip), %xmm0     # 0x2d380
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movq (%rsp), %rax
cvtsd2ss 0xa8(%rax), %xmm0
cvtss2sd %xmm1, %xmm1
cvtss2sd %xmm0, %xmm0
movaps %xmm0, %xmm2
addsd %xmm0, %xmm2
mulsd %xmm0, %xmm2
movlhps %xmm1, %xmm1            # xmm1 = xmm1[0,0]
mulpd 0x60(%rsp), %xmm1
addpd 0xd0(%rsp), %xmm1
divpd 0x40(%rsp), %xmm1
cvtpd2ps %xmm1, %xmm1
movapd %xmm1, 0xe0(%rsp)
movapd %xmm1, %xmm0
mulss %xmm1, %xmm0
movaps %xmm1, %xmm3
shufps $0x55, %xmm1, %xmm3     # xmm3 = xmm3[1,1],xmm1[1,1]
movaps %xmm3, 0xf0(%rsp)
movaps %xmm3, %xmm1
mulss %xmm3, %xmm1
addss %xmm0, %xmm1
cvtss2sd %xmm1, %xmm0
divsd %xmm2, %xmm0
callq 0x288a4
movaps 0x3dca(%rip), %xmm7     # 0x2d150
cvtsd2ss %xmm0, %xmm0
movaps 0xe0(%rsp), %xmm5
movaps %xmm5, %xmm1
addps 0x4fc4(%rip), %xmm1     # 0x2e360
movaps %xmm1, %xmm2
shufps $0x55, %xmm1, %xmm2     # xmm2 = xmm2[1,1],xmm1[1,1]
cvttss2si %xmm2, %rax
cvtsi2ss %rax, %xmm3
mulss 0x3c(%rsp), %xmm0
xorps %xmm4, %xmm4
ucomiss %xmm3, %xmm2
cmpnleps %xmm1, %xmm4
setp %cl
setne %dl
orb %cl, %dl
unpcklps %xmm4, %xmm4            # xmm4 = xmm4[0,0,1,1]
movmskpd %xmm4, %esi
movl %esi, %ecx
shrb %cl
andb %dl, %cl
movzbl %cl, %edx
cvttss2si %xmm1, %rcx
subq %rdx, %rax
cvtsi2ss %rcx, %xmm2
ucomiss %xmm2, %xmm1
setp %dl
setne %dil
orb %dl, %dil
andb %sil, %dil
movaps 0x70(%rsp), %xmm6
cvttss2si %xmm6, %rsi
cvtss2sd 0xf0(%rsp), %xmm1
cvtsi2ss %rsi, %xmm3
cvtss2sd %xmm5, %xmm2
movzbl %dil, %edx
cvtsi2sd %eax, %xmm4
movsd 0x3da2(%rip), %xmm5     # 0x2d1c0
addsd %xmm5, %xmm4
subsd %xmm4, %xmm1
cvtsd2ss %xmm1, %xmm1
subq %rdx, %rcx
cvtsi2sd %ecx, %xmm4
addsd %xmm5, %xmm4
subsd %xmm4, %xmm2
cvtsd2ss %xmm2, %xmm2
cmpneqps %xmm6, %xmm3
xorpd %xmm4, %xmm4
cmpnleps %xmm6, %xmm4
andps %xmm3, %xmm4
movd %xmm4, %edx
addl %edx, %esi
cvtsi2ss %esi, %xmm3
subss %xmm6, %xmm3
movslq %ecx, %rdi
movslq %eax, %r8
xorl %r9d, %r9d
cmpq $0x2, %r9
je 0x29514
movl %ebx, %eax
subl %r9d, %eax
cvtsi2ss %eax, %xmm4
leaq (%r9,%r8), %r10
leal -0x2(%r10), %r11d
subss %xmm1, %xmm4
andps %xmm7, %xmm4
mulss %xmm0, %xmm4
shlq $0x5, %r10
addq 0x8(%rsp), %r10
xorl %r12d, %r12d
cmpq $0x2, %r12
je 0x2950c
cmpl $-0x4, %r11d
setb %al
leaq (%r12,%rdi), %r14
leal -0x2(%r14), %ecx
cmpl $-0x4, %ecx
setb %r15b
movl %ebx, %ecx
subl %r12d, %ecx
cvtsi2ss %ecx, %xmm5
subss %xmm2, %xmm5
andps %xmm7, %xmm5
mulss %xmm4, %xmm5
shlq $0x7, %r14
addq %r10, %r14
orb %al, %r15b
movl %esi, %ecx
movl %ebx, %ebp
cmpl $-0x1, %ebp
je 0x29507
testb %r15b, %r15b
jne 0x29501
cvtsi2ss %ebp, %xmm6
addss %xmm3, %xmm6
andps %xmm7, %xmm6
mulss %xmm5, %xmm6
movl %ecx, %eax
cltd
idivl %r13d
movslq %edx, %rax
addss (%r14,%rax,4), %xmm6
movss %xmm6, (%r14,%rax,4)
decl %ebp
incl %ecx
jmp 0x294d3
incq %r12
jmp 0x29495
incq %r9
jmp 0x29463
movq 0x50(%rsp), %rcx
incq %rcx
jmp 0x2929b
movq 0xa8(%rsp), %rdx
incq %rdx
jmp 0x2922f
movq 0x80(%rsp), %rbx
leaq 0x200(%rbx), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x28f3b
movq (%rsp), %rax
movsd 0x98(%rax), %xmm1
xorps %xmm2, %xmm2
ucomisd %xmm2, %xmm1
jne 0x29562
jnp 0x29583
cvtss2sd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x29583
xorl %eax, %eax
cmpq $0x80, %rax
je 0x290fa
andl $0x0, (%rbx,%rax,4)
incq %rax
jmp 0x2956e
xorl %eax, %eax
movsd 0x43eb(%rip), %xmm0     # 0x2d978
cmpq $0x80, %rax
je 0x295ac
cvtss2sd (%rbx,%rax,4), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x295a7
movl $0x3e4ccccd, (%rbx,%rax,4) # imm = 0x3E4CCCCD
incq %rax
jmp 0x2958d
movq %rbx, %rdi
movq %r15, %rsi
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x28f3b