// File: /workspace/github_cpp_makefile_star2_O2/manikandan5[P]Image-Matching-Stitching-Warping/siftpp/sift.cpp
// Function: VL::Sift::computeKeypointOrientations(float*, VL::Sift::Keypoint)
int
Sift::computeKeypointOrientations(VL::float_t angles [4], Keypoint keypoint)
{
  int const   nbins = 36 ;
  VL::float_t const winFactor = 1.5 ;
  VL::float_t hist [nbins] ;

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

  // offsets to move in the 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 ;
  
  // shall we use keypoints.ix,iy,is here?
  int xi = ((int) (x+0.5)) ; 
  int yi = ((int) (y+0.5)) ;
  int si = keypoint.is ;
  
  VL::float_t const sigmaw = winFactor * sigma ;
  int W = (int) floor(3.0 * sigmaw) ;
  
  // skip the keypoint if it is out of bounds
  if(o  < omin   ||
     o  >=omin+O ||
     xi < 0      || 
     xi > ow-1   || 
     yi < 0      || 
     yi > oh-1   || 
     si < smin+1 || 
     si > smax-2 ) {
    std::cerr<<"!"<<std::endl ;
    return 0 ;
  }
  
  // make sure that the gradient buffer is filled with octave o
  prepareGrad(o) ;

  // clear the SIFT histogram
  std::fill(hist, hist + nbins, 0) ;

  // fill the SIFT histogram
  pixel_t* pt = temp + xi * xo + yi * yo + (si - smin -1) * so ;

#undef at
#define at(dx,dy) (*(pt + (dx)*xo + (dy)*yo))

  for(int ys = std::max(-W, 1-yi) ; ys <= std::min(+W, oh -2 -yi) ; ++ys) {
    for(int xs = std::max(-W, 1-xi) ; xs <= std::min(+W, ow -2 -xi) ; ++xs) {
      
      VL::float_t dx = xi + xs - x;
      VL::float_t dy = yi + ys - y;
      VL::float_t r2 = dx*dx + dy*dy ;

      // limit to a circular window
      if(r2 >= W*W+0.5) continue ;

      VL::float_t wgt = VL::fast_expn( r2 / (2*sigmaw*sigmaw) ) ;
      VL::float_t mod = *(pt + xs*xo + ys*yo) ;
      VL::float_t ang = *(pt + xs*xo + ys*yo + 1) ;

      //      int bin = (int) floor( nbins * ang / (2*M_PI) ) ;
      int bin = (int) floor( nbins * ang / (2*M_PI) ) ;
      hist[bin] += mod * wgt ;        
    }
  }
  
  // smooth the histogram
#if defined VL_LOWE_STRICT
  // Lowe's version apparently has a little issue with orientations
  // around + or - pi, which we reproduce here for compatibility
  for (int iter = 0; iter < 6; iter++) {
    VL::float_t prev  = hist[nbins/2] ;
    for (int i = nbins/2-1; i >= -nbins/2 ; --i) {
      int const j  = (i     + nbins) % nbins ;
      int const jp = (i - 1 + nbins) % nbins ;
      VL::float_t newh = (prev + hist[j] + hist[jp]) / 3.0;
      prev = hist[j] ;
      hist[j] = newh ;
    }
  }
#else
  // this is slightly more correct
  for (int iter = 0; iter < 6; iter++) {
    VL::float_t prev  = hist[nbins-1] ;
    VL::float_t first = hist[0] ;
    int i ;
    for (i = 0; i < nbins - 1; i++) {
      VL::float_t newh = (prev + hist[i] + hist[(i+1) % nbins]) / 3.0;
      prev = hist[i] ;
      hist[i] = newh ;
    }
    hist[i] = (prev + hist[i] + first)/3.0 ;
  }
#endif
  
  // find the histogram maximum
  VL::float_t maxh = * std::max_element(hist, hist + nbins) ;

  // find peaks within 80% from max
  int nangles = 0 ;
  for(int i = 0 ; i < nbins ; ++i) {
    VL::float_t h0 = hist [i] ;
    VL::float_t hm = hist [(i-1+nbins) % nbins] ;
    VL::float_t hp = hist [(i+1+nbins) % nbins] ;
    
    // is this a peak?
    if( h0 > 0.8*maxh && h0 > hm && h0 > hp ) {
      
      // quadratic interpolation
      //      VL::float_t di = -0.5 * (hp - hm) / (hp+hm-2*h0) ; 
      VL::float_t di = -0.5 * (hp - hm) / (hp+hm-2*h0) ; 
      VL::float_t th = 2*M_PI * (i+di+0.5) / nbins ;      
      angles [ nangles++ ] = th ;
      if( nangles == 4 )
        goto enough_angles ;
    }
  }
 enough_angles:
  return nangles ;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rsi, 0x50(%rsp)
movq %rdi, %r15
leaq 0x140(%rsp), %r13
movl (%r13), %r12d
pushq $0x1
popq %rbx
testl %r12d, %r12d
js 0x52563
movl %ebx, %eax
movl %r12d, %ecx
shll %cl, %eax
cvtsi2ss %eax, %xmm0
movaps %xmm0, 0x10(%rsp)
jmp 0x52581
movl %r12d, %ecx
negb %cl
movl %ebx, %eax
shll %cl, %eax
cvtsi2ss %eax, %xmm0
movss 0x1ca8(%rip), %xmm1     # 0x54220
divss %xmm0, %xmm1
movaps %xmm1, 0x10(%rsp)
movq %r15, %rdi
movl %r12d, %esi
callq 0x532d0
movl %eax, %r14d
movq %r15, %rdi
movl %r12d, %esi
callq 0x53310
movl %eax, %ebp
movss 0x1c(%r13), %xmm1
divss 0x10(%rsp), %xmm1
movsd 0x10(%r13), %xmm0
movaps %xmm0, 0x30(%rsp)
mulss 0x26f1d(%rip), %xmm1    # 0x794d8
movl 0xc(%r13), %r13d
movss %xmm1, 0xc(%rsp)
cvtss2sd %xmm1, %xmm0
mulsd 0x1a8f(%rip), %xmm0     # 0x54060
callq 0x4900
movl 0x14(%r15), %eax
cmpl %eax, %r12d
jge 0x52610
movq 0x3da0a(%rip), %rdi     # 0x8fff0
leaq 0x353f(%rip), %rsi      # 0x55b2c
callq 0x4520
movq %rax, %rdi
callq 0x4200
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addl 0xc(%r15), %eax
cmpl %eax, %r12d
jge 0x525df
movaps 0x10(%rsp), %xmm2
shufps $0x0, %xmm2, %xmm2      # xmm2 = xmm2[0,0,0,0]
movaps 0x30(%rsp), %xmm1
divps %xmm2, %xmm1
movaps %xmm1, 0x30(%rsp)
cvtps2pd %xmm1, %xmm1
addpd 0x26f06(%rip), %xmm1    # 0x79540
cvttpd2dq %xmm1, %xmm1
movd %xmm1, %edx
testl %edx, %edx
js 0x525df
movd %ebp, %xmm2
movd %r14d, %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 0x525df
pshufd $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
movd %xmm1, %ecx
movq %rcx, 0x28(%rsp)
testl %ecx, %ecx
js 0x525df
shrb %al
je 0x525df
cmpl 0x18(%r15), %r13d
jle 0x525df
movl 0x1c(%r15), %eax
addl $-0x2, %eax
cmpl %eax, %r13d
jg 0x525df
leal (%r14,%r14), %eax
movl %eax, 0x10(%rsp)
imull %ebp, %eax
movl %eax, 0x8(%rsp)
cvttsd2si %xmm0, %eax
movl %eax, 0x20(%rsp)
movq %r15, %rdi
movl %r12d, %esi
movq %rdx, %r12
callq 0x5230e
movq %r12, %r10
xorl %eax, %eax
cmpq $0x90, %rax
je 0x526e2
andl $0x0, 0x70(%rsp,%rax)
addq $0x4, %rax
jmp 0x526cf
movq 0x30(%r15), %rax
leal (%r10,%r10), %ecx
movl 0x10(%rsp), %r11d
movl %r11d, %edx
movq 0x28(%rsp), %r9
imull %r9d, %edx
movslq %edx, %rdx
movl 0x18(%r15), %esi
notl %esi
addl %esi, %r13d
movl 0x8(%rsp), %esi
imull %r13d, %esi
movl 0x20(%rsp), %r15d
movl %r15d, %edi
negl %edi
movl %ebx, %r8d
subl %r9d, %r8d
subl %r9d, %ebp
addl $-0x2, %ebp
cmpl %r15d, %ebp
cmovgel %r15d, %ebp
movslq %esi, %rsi
subl %r10d, %ebx
subl %r10d, %r14d
addl $-0x2, %r14d
cmpl %r15d, %r14d
cmovgel %r15d, %r14d
imull %r15d, %r15d
cvtsi2sd %r15d, %xmm2
addsd 0x1917(%rip), %xmm2     # 0x54068
movss 0xc(%rsp), %xmm0
movaps %xmm0, %xmm1
addss %xmm0, %xmm1
mulss %xmm0, %xmm1
movss %xmm1, 0x8(%rsp)
cmpl %ebx, %edi
cmovgl %edi, %ebx
movslq %ebx, %r9
movslq %r14d, %r14
cmpl %r8d, %edi
cmovgl %edi, %r8d
movslq %r8d, %r15
movslq %ebp, %rdi
movq %rdi, 0x58(%rsp)
movslq %r11d, %r11
movaps 0x30(%rsp), %xmm0
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movaps %xmm0, 0x60(%rsp)
movq %r15, %rdi
imulq %r11, %rdi
movq %r9, 0x48(%rsp)
leaq (,%r9,8), %r8
leaq (%r8,%rdi,4), %rdi
leaq (%rdi,%rsi,4), %rsi
leaq (%rsi,%rdx,4), %rdx
leaq (%rdx,%rcx,4), %rcx
leaq (%rax,%rcx), %r12
addq $0x4, %r12
shlq $0x2, %r11
movq %r11, 0x40(%rsp)
movl %r10d, %ebx
movsd %xmm2, 0x20(%rsp)
cmpq 0x58(%rsp), %r15
jg 0x5289d
movq 0x28(%rsp), %rax
addl %r15d, %eax
cvtsi2ss %eax, %xmm3
subss 0x60(%rsp), %xmm3
mulss %xmm3, %xmm3
movq %r12, %rbp
movq 0x48(%rsp), %r13
movss %xmm3, 0xc(%rsp)
cmpq %r14, %r13
jg 0x52890
leal (%rbx,%r13), %eax
cvtsi2ss %eax, %xmm0
subss 0x30(%rsp), %xmm0
mulss %xmm0, %xmm0
addss %xmm3, %xmm0
cvtss2sd %xmm0, %xmm1
ucomisd %xmm2, %xmm1
jae 0x52884
divss 0x8(%rsp), %xmm0
callq 0x53487
movss %xmm0, 0x10(%rsp)
movss (%rbp), %xmm0
mulss 0x26c96(%rip), %xmm0    # 0x794e0
cvtss2sd %xmm0, %xmm0
divsd 0x26cca(%rip), %xmm0    # 0x79520
callq 0x4900
movss 0xc(%rsp), %xmm3
movsd 0x20(%rsp), %xmm2
cvttsd2si %xmm0, %eax
movss 0x10(%rsp), %xmm0
mulss -0x4(%rbp), %xmm0
cltq
addss 0x70(%rsp,%rax,4), %xmm0
movss %xmm0, 0x70(%rsp,%rax,4)
incq %r13
addq $0x8, %rbp
jmp 0x52803
incq %r15
addq 0x40(%rsp), %r12
jmp 0x527d4
xorl %eax, %eax
pushq $0x1
popq %rcx
movss 0x19a2(%rip), %xmm0     # 0x5424c
cmpl $0x6, %eax
je 0x5290b
movss 0x70(%rsp), %xmm1
movss 0xfc(%rsp), %xmm2
movq %rcx, %rdx
movaps %xmm1, %xmm4
cmpq $0x24, %rdx
je 0x528ed
movaps %xmm4, %xmm3
addss %xmm4, %xmm2
movss 0x70(%rsp,%rdx,4), %xmm4
addss %xmm4, %xmm2
divss %xmm0, %xmm2
movss %xmm2, 0x6c(%rsp,%rdx,4)
incq %rdx
movaps %xmm3, %xmm2
jmp 0x528c4
addss 0xfc(%rsp), %xmm2
addss %xmm2, %xmm1
divss %xmm0, %xmm1
movss %xmm1, 0xfc(%rsp)
incl %eax
jmp 0x528aa
leaq 0x100(%rsp), %rsi
leaq 0x70(%rsp), %rdi
callq 0x5376d
cvtss2sd (%rax), %xmm0
mulsd 0x26bd7(%rip), %xmm0    # 0x79500
xorl %edx, %edx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movb $0x24, %dil
pushq $0x23
popq %r8
movsd 0x26bb9(%rip), %xmm1    # 0x794f8
movsd 0x1721(%rip), %xmm2     # 0x54068
movsd 0x26bd9(%rip), %xmm3    # 0x79528
pushq $0x4
popq %r9
xorl %ecx, %ecx
leal 0x25(%rdx), %r11d
leaq (%rdx,%rsi), %rbx
leaq (%rsp,%rdx,4), %r14
addq $0x70, %r14
movl %r11d, %ebp
xorl %r10d, %r10d
movzbl %bpl, %eax
divb %dil
leaq (%rdx,%r10), %r13
cmpq $0x24, %r13
je 0x525fc
movss (%r14,%r10,4), %xmm4
movl %r10d, %r15d
leal (%rbx,%r15), %r12d
testq %r13, %r13
cmoveq %r8, %r12
cvtss2sd %xmm4, %xmm5
incq %r10
incb %bpl
ucomisd %xmm0, %xmm5
jbe 0x5296b
movss 0x70(%rsp,%r12,4), %xmm5
ucomiss %xmm5, %xmm4
jbe 0x5296b
movzbl %al, %eax
imull $0x24, %eax, %eax
movl %r11d, %r12d
subb %al, %r12b
movzbl %r12b, %eax
addl %r15d, %eax
movzbl %al, %eax
movss 0x70(%rsp,%rax,4), %xmm6
ucomiss %xmm6, %xmm4
jbe 0x5296b
movaps %xmm6, %xmm7
subss %xmm5, %xmm7
cvtss2sd %xmm7, %xmm7
mulsd %xmm1, %xmm7
addss %xmm6, %xmm5
addss %xmm4, %xmm4
subss %xmm4, %xmm5
cvtss2sd %xmm5, %xmm4
divsd %xmm4, %xmm7
cvtsd2ss %xmm7, %xmm4
leal -0x1(%rdx,%r10), %eax
cvtsi2ss %eax, %xmm5
addss %xmm4, %xmm5
cvtss2sd %xmm5, %xmm4
addsd %xmm2, %xmm4
mulsd 0x26b0b(%rip), %xmm4    # 0x79520
divsd %xmm3, %xmm4
cvtsd2ss %xmm4, %xmm4
movl %ecx, %eax
incl %ecx
movq 0x50(%rsp), %r11
movss %xmm4, (%r11,%rax,4)
addq %r10, %rdx
cmpl $0x4, %ecx
jne 0x52955
movl %r9d, %ecx
jmp 0x525fc