// File: /workspace/github_cpp_makefile_star2_O2/manikandan5[P]Image-Matching-Stitching-Warping/siftpp/sift.cpp
// Function: VL::Sift::computeKeypointDescriptor(float*, VL::Sift::Keypoint, float)
void
Sift::computeKeypointDescriptor
(VL::float_t* descr_pt,
 Keypoint keypoint, 
 VL::float_t angle0)
{

  /* The SIFT descriptor is a  three dimensional histogram of the position
   * and orientation of the gradient.  There are NBP bins for each spatial
   * dimesions and NBO  bins for the orientation dimesion,  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, another  half bin is  needed at both
   * ends of  the extension. Therefore, we  need a square window  of SBP x
   * (NBP + 1) pixels. Finally, since the patch can be arbitrarly rotated,
   * we need to consider  a window 2W += sqrt(2) x SBP  x (NBP + 1) pixels
   * wide.
   */      

  // octave
  int o = keypoint.o ;
  VL::float_t xperiod = getOctaveSamplingPeriod(o) ;

  // offsets to move in Gaussian scale space octave
  const int ow = getOctaveWidth(o) ;
  const int oh = getOctaveHeight(o) ;
  const int xo = 2 ;
  const int yo = xo * ow ;
  const int so = yo * oh ;

  // keypoint fractional geometry
  VL::float_t x     = keypoint.x / xperiod;
  VL::float_t y     = keypoint.y / xperiod ;
  VL::float_t sigma = keypoint.sigma / xperiod ;

  VL::float_t st0   = sinf( angle0 ) ;
  VL::float_t ct0   = cosf( angle0 ) ;
  
  // shall we use keypoints.ix,iy,is here?
  int xi = ((int) (x+0.5)) ; 
  int yi = ((int) (y+0.5)) ;
  int si = keypoint.is ;

  // const VL::float_t magnif = 3.0f ;
  const int NBO = 8 ;
  const int NBP = 4 ;
  const VL::float_t SBP = magnif * sigma ;
  const int   W = (int) floor (sqrt(2.0) * SBP * (NBP + 1) / 2.0 + 0.5) ;
  
  /* Offsets to move in the descriptor. */
  /* Use Lowe's convention. */
  const int binto = 1 ;
  const int binyo = NBO * NBP ;
  const int binxo = NBO ;
  // const int bino  = NBO * NBP * NBP ;
  
  int bin ;
  
  // check bounds
  if(o  < omin   ||
     o  >=omin+O ||
     xi < 0      || 
     xi > ow-1   || 
     yi < 0      || 
     yi > oh-1   ||
     si < smin+1 ||
     si > smax-2 )
        return ;
  
  // make sure gradient buffer is up-to-date
  prepareGrad(o) ;

  std::fill( descr_pt, descr_pt + NBO*NBP*NBP, 0 ) ;

  /* 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).
   */
  pixel_t const * pt = temp + xi*xo + yi*yo + (si - smin - 1)*so ;
  VL::float_t *  dpt = descr_pt + (NBP/2) * binyo + (NBP/2) * binxo ;
     
#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(int dyi = std::max(-W, 1-yi) ; dyi <= std::min(+W, oh-2-yi) ; ++dyi) {
    for(int dxi = std::max(-W, 1-xi) ; dxi <= std::min(+W, ow-2-xi) ; ++dxi) {
      
      // retrieve 
      VL::float_t mod   = *( pt + dxi*xo + dyi*yo + 0 ) ;
      VL::float_t angle = *( pt + dxi*xo + dyi*yo + 1 ) ;
      VL::float_t theta = fast_mod_2pi(-angle + angle0) ; // lowe compatible ?
      
      // fractional displacement
      VL::float_t dx = xi + dxi - x;
      VL::float_t dy = yi + dyi - y;
      
      // get the displacement normalized w.r.t. the keypoint
      // orientation and extension.
      VL::float_t nx = ( ct0 * dx + st0 * dy) / SBP ;
      VL::float_t ny = (-st0 * dx + ct0 * dy) / SBP ; 
      VL::float_t nt = NBO * theta / (2*M_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::float_t const wsigma = NBP/2 ;
      VL::float_t win = VL::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 = fast_floor( nx - 0.5 ) ;
      int biny = fast_floor( ny - 0.5 ) ;
      int bint = fast_floor( nt ) ;
      VL::float_t rbinx = nx - (binx+0.5) ;
      VL::float_t rbiny = ny - (biny+0.5) ;
      VL::float_t 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::float_t weight = win 
                * mod 
                * fast_abs (1 - dbinx - rbinx)
                * fast_abs (1 - dbiny - rbiny)
                * fast_abs (1 - dbint - rbint) ;
              
              atd(binx+dbinx, biny+dbiny, (bint+dbint) % NBO) += weight ;
            }
          }            
        }
      }
    }  
  }

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

    /* Normalize the histogram to L2 unit length. */        
    Detail::normalize_histogram(descr_pt, descr_pt + NBO*NBP*NBP) ;
    
    /* Truncate at 0.2. */
    for(bin = 0; bin < NBO*NBP*NBP ; ++bin) {
      if (descr_pt[bin] > 0.2) descr_pt[bin] = 0.2;
    }
    
    /* Normalize again. */
    Detail::normalize_histogram(descr_pt, descr_pt + NBO*NBP*NBP) ;
  }

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movss %xmm0, 0x8(%rsp)
movq %rsi, 0x48(%rsp)
movq %rdi, %r14
leaq 0x150(%rsp), %rbx
movl (%rbx), %r13d
pushq $0x1
popq %r12
testl %r13d, %r13d
js 0x52aea
pushq $0x1
popq %rax
movl %r13d, %ecx
shll %cl, %eax
cvtsi2ss %eax, %xmm0
movaps %xmm0, 0x30(%rsp)
jmp 0x52b09
movl %r13d, %ecx
negb %cl
movl %r12d, %eax
shll %cl, %eax
cvtsi2ss %eax, %xmm0
movss 0x1720(%rip), %xmm1     # 0x54220
divss %xmm0, %xmm1
movaps %xmm1, 0x30(%rsp)
movq %r14, %rdi
movl %r13d, %esi
callq 0x532d0
movl %eax, %r15d
movq %r14, %rdi
movl %r13d, %esi
callq 0x53310
movl %eax, %ebp
movss 0x1c(%rbx), %xmm0
divss 0x30(%rsp), %xmm0
movss %xmm0, 0x4(%rsp)
movss 0x8(%rsp), %xmm0
callq 0x4450
movaps %xmm0, 0xc0(%rsp)
movss 0x8(%rsp), %xmm0
callq 0x43a0
movss %xmm0, 0xc(%rsp)
movsd 0x10(%rbx), %xmm0
movaps %xmm0, 0x60(%rsp)
movss 0x4(%rsp), %xmm0
mulss 0x28(%r14), %xmm0
movss %xmm0, 0x4(%rsp)
cvtss2sd %xmm0, %xmm0
mulsd 0x269af(%rip), %xmm0    # 0x79530
mulsd 0x148f(%rip), %xmm0     # 0x54018
movl 0xc(%rbx), %ebx
movsd 0x14d4(%rip), %xmm1     # 0x54068
mulsd %xmm1, %xmm0
addsd %xmm1, %xmm0
callq 0x4900
movl 0x14(%r14), %eax
cmpl %eax, %r13d
jge 0x52bbc
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addl 0xc(%r14), %eax
cmpl %eax, %r13d
jge 0x52baa
movaps 0x30(%rsp), %xmm2
shufps $0x0, %xmm2, %xmm2      # xmm2 = xmm2[0,0,0,0]
movaps 0x60(%rsp), %xmm1
divps %xmm2, %xmm1
movaps %xmm1, 0x60(%rsp)
cvtps2pd %xmm1, %xmm1
addpd 0x2695a(%rip), %xmm1    # 0x79540
cvttpd2dq %xmm1, %xmm1
movd %xmm1, %eax
movq %rax, 0x58(%rsp)
testl %eax, %eax
js 0x52baa
movd %ebp, %xmm2
movd %r15d, %xmm3
punpckldq %xmm2, %xmm3    # xmm3 = xmm3[0],xmm2[0],xmm3[1],xmm2[1]
pshufd $0x50, %xmm3, %xmm2     # xmm2 = xmm3[0,0,1,1]
pshufd $0x50, %xmm1, %xmm3     # xmm3 = xmm1[0,0,1,1]
pcmpgtd %xmm3, %xmm2
movmskpd %xmm2, %eax
testb $0x1, %al
je 0x52baa
pshufd $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
movd %xmm1, %ecx
movq %rcx, 0x50(%rsp)
testl %ecx, %ecx
js 0x52baa
shrb %al
je 0x52baa
cmpl 0x18(%r14), %ebx
jle 0x52baa
movl 0x1c(%r14), %eax
addl $-0x2, %eax
cmpl %eax, %ebx
jg 0x52baa
leal (%r15,%r15), %ecx
movl %ebp, %eax
movl %ecx, 0x18(%rsp)
movl %ecx, %ebp
movl %eax, (%rsp)
imull %eax, %ebp
movq %r14, %rdi
cvttsd2si %xmm0, %r14d
movq %rdi, 0x40(%rsp)
movl %r13d, %esi
callq 0x5230e
xorl %eax, %eax
movq 0x48(%rsp), %rdx
cmpq $0x200, %rax            # imm = 0x200
je 0x52c91
andl $0x0, (%rdx,%rax)
addq $0x4, %rax
jmp 0x52c7f
movq 0x58(%rsp), %rdi
leal (%rdi,%rdi), %eax
shlq $0x2, %rax
movq 0x40(%rsp), %rsi
addq 0x30(%rsi), %rax
movl 0x18(%rsp), %r9d
movl %r9d, %ecx
movq 0x50(%rsp), %r8
imull %r8d, %ecx
movslq %ecx, %rcx
leaq (%rax,%rcx,4), %rax
movl 0x18(%rsi), %ecx
notl %ecx
addl %ecx, %ebx
imull %ebx, %ebp
movslq %ebp, %rcx
leaq (%rax,%rcx,4), %rax
movq %rax, 0x90(%rsp)
leaq 0x140(%rdx), %rax
movq %rax, 0x30(%rsp)
movl %r14d, %eax
negl %eax
movl %r12d, %ecx
subl %r8d, %ecx
movl (%rsp), %esi
subl %r8d, %esi
addl $-0x2, %esi
cmpl %r14d, %esi
cmovgel %r14d, %esi
movl %r12d, %edx
subl %edi, %edx
subl %edi, %r15d
addl $-0x2, %r15d
cmpl %r14d, %r15d
cmovgel %r14d, %r15d
movaps 0x26858(%rip), %xmm0    # 0x79570
xorps 0xc0(%rsp), %xmm0
movaps %xmm0, 0xe0(%rsp)
cmpl %edx, %eax
cmovgl %eax, %edx
movslq %edx, %rdx
movq %rdx, 0x88(%rsp)
cmpl %ecx, %eax
cmovgl %eax, %ecx
movslq %r15d, %rax
movq %rax, 0xb0(%rsp)
movslq %ecx, %rdx
movslq %esi, %rax
movq %rax, 0x98(%rsp)
movslq %r9d, %rax
movq %rax, 0x80(%rsp)
movaps 0x60(%rsp), %xmm0
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movaps %xmm0, 0xd0(%rsp)
pushq $0x8
popq %r14
cmpq 0x98(%rsp), %rdx
jg 0x53053
movq %rdx, %rax
imulq 0x80(%rsp), %rax
movq 0x90(%rsp), %rcx
leaq (%rcx,%rax,4), %rax
movq %rax, 0xa8(%rsp)
movq 0x50(%rsp), %rax
movq %rdx, 0xa0(%rsp)
addl %edx, %eax
cvtsi2ss %eax, %xmm0
subss 0xd0(%rsp), %xmm0
movaps 0xc0(%rsp), %xmm1
mulss %xmm0, %xmm1
movaps %xmm1, 0xf0(%rsp)
mulss 0xc(%rsp), %xmm0
movss %xmm0, 0x24(%rsp)
movq 0x88(%rsp), %rbx
cmpq 0xb0(%rsp), %rbx
jg 0x53043
movq 0xa8(%rsp), %rax
movss (%rax,%rbx,8), %xmm0
movss %xmm0, 0x2c(%rsp)
movss 0x8(%rsp), %xmm0
subss 0x4(%rax,%rbx,8), %xmm0
callq 0x53448
movq 0x58(%rsp), %rax
movq %rbx, 0x18(%rsp)
addl %ebx, %eax
cvtsi2ss %eax, %xmm2
subss 0x60(%rsp), %xmm2
movss 0xc(%rsp), %xmm3
mulss %xmm2, %xmm3
addss 0xf0(%rsp), %xmm3
movss 0x4(%rsp), %xmm1
divss %xmm1, %xmm3
movss %xmm3, 0x10(%rsp)
mulss 0xe0(%rsp), %xmm2
addss 0x24(%rsp), %xmm2
divss %xmm1, %xmm2
movaps %xmm2, %xmm1
movss %xmm2, 0x70(%rsp)
mulss 0x26671(%rip), %xmm0    # 0x794e8
cvtss2sd %xmm0, %xmm0
divsd 0x2669d(%rip), %xmm0    # 0x79520
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%rsp)
mulss %xmm2, %xmm1
movaps %xmm3, %xmm0
mulss %xmm3, %xmm0
addss %xmm1, %xmm0
mulss 0x26649(%rip), %xmm0    # 0x794ec
callq 0x53487
movss %xmm0, 0x28(%rsp)
movss 0x10(%rsp), %xmm1
cvtss2sd %xmm1, %xmm0
movsd %xmm0, 0xb8(%rsp)
movss 0x26627(%rip), %xmm0    # 0x794f0
addss %xmm0, %xmm1
movaps %xmm1, %xmm0
callq 0x41b0
movaps %xmm0, 0x100(%rsp)
cvttss2si %xmm0, %ebx
movss 0x70(%rsp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
addss 0x265f7(%rip), %xmm0    # 0x794f0
callq 0x41b0
movaps %xmm0, 0x70(%rsp)
cvttss2si %xmm0, %ebp
movss (%rsp), %xmm0
callq 0x41b0
movss 0x28(%rsp), %xmm7
movaps 0x1202(%rip), %xmm6     # 0x54120
cvttss2si %xmm0, %esi
cvttps2dq 0x100(%rsp), %xmm1
cvtdq2pd %xmm1, %xmm1
movsd 0x1131(%rip), %xmm3     # 0x54068
addsd %xmm3, %xmm1
movsd 0xb8(%rsp), %xmm2
subsd %xmm1, %xmm2
cvtsd2ss %xmm2, %xmm1
cvttps2dq 0x70(%rsp), %xmm2
cvtdq2pd %xmm2, %xmm2
addsd %xmm3, %xmm2
movsd 0x10(%rsp), %xmm3
subsd %xmm2, %xmm3
cvtsd2ss %xmm3, %xmm2
cvttps2dq %xmm0, %xmm0
cvtdq2ps %xmm0, %xmm0
mulss 0x2c(%rsp), %xmm7
subss (%rsp), %xmm0
movslq %ebp, %rdi
movslq %ebx, %r8
xorl %r9d, %r9d
cmpq $0x2, %r9
je 0x53036
movl %r12d, %eax
subl %r9d, %eax
cvtsi2ss %eax, %xmm3
leaq (%r9,%r8), %r10
leal -0x2(%r10), %r11d
subss %xmm1, %xmm3
andps %xmm6, %xmm3
mulss %xmm7, %xmm3
shlq $0x5, %r10
addq 0x30(%rsp), %r10
xorl %r15d, %r15d
cmpq $0x2, %r15
je 0x5302e
cmpl $-0x4, %r11d
setb %al
leaq (%r15,%rdi), %rbp
leal -0x2(%rbp), %ecx
cmpl $-0x4, %ecx
setb %bl
movl %r12d, %ecx
subl %r15d, %ecx
cvtsi2ss %ecx, %xmm4
subss %xmm2, %xmm4
andps %xmm6, %xmm4
mulss %xmm3, %xmm4
shlq $0x7, %rbp
addq %r10, %rbp
orb %al, %bl
movl %esi, %ecx
movl %r12d, %r13d
cmpl $-0x1, %r13d
je 0x53029
testb %bl, %bl
jne 0x53022
cvtsi2ss %r13d, %xmm5
addss %xmm0, %xmm5
andps %xmm6, %xmm5
mulss %xmm4, %xmm5
movl %ecx, %eax
cltd
idivl %r14d
movslq %edx, %rax
addss (%rbp,%rax,4), %xmm5
movss %xmm5, (%rbp,%rax,4)
decl %r13d
incl %ecx
jmp 0x52ff3
incq %r15
jmp 0x52fb6
incq %r9
jmp 0x52f83
movq 0x18(%rsp), %rbx
incq %rbx
jmp 0x52de8
movq 0xa0(%rsp), %rdx
incq %rdx
jmp 0x52d76
movq 0x40(%rsp), %rax
cmpb $0x0, 0x2c(%rax)
je 0x52baa
movq 0x48(%rsp), %rbx
leaq 0x200(%rbx), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x52a40
xorl %eax, %eax
movsd 0x264b5(%rip), %xmm0    # 0x79538
cmpq $0x80, %rax
je 0x530a2
cvtss2sd (%rbx,%rax,4), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x5309d
movl $0x3e4ccccd, (%rbx,%rax,4) # imm = 0x3E4CCCCD
incq %rax
jmp 0x53083
movq %rbx, %rdi
movq %r14, %rsi
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x52a40