// File: /workspace/github_cpp_makefile_star2_O3/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 $0x118, %rsp            # imm = 0x118
movq %rsi, %r13
movq %rdi, %r15
movl 0x150(%rsp), %ebp
testl %ebp, %ebp
js 0x6c695
movl $0x1, %eax
movl %ebp, %ecx
shll %cl, %eax
cvtsi2ss %eax, %xmm0
jmp 0x6c6b0
movl %ebp, %ecx
negb %cl
movl $0x1, %eax
shll %cl, %eax
cvtsi2ss %eax, %xmm1
movss 0x1c04(%rip), %xmm0     # 0x6e2b0
divss %xmm1, %xmm0
movl 0x14(%r15), %eax
cmpl %ebp, %eax
jg 0x6ccc1
addl 0xc(%r15), %eax
cmpl %ebp, %eax
jle 0x6ccc1
leaq 0x150(%rsp), %rax
movss 0x1c(%rax), %xmm2
divss %xmm0, %xmm2
movsd 0x10(%rax), %xmm1
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
divps %xmm0, %xmm1
movaps %xmm1, 0x60(%rsp)
cvtps2pd %xmm1, %xmm0
addpd 0x26d9b(%rip), %xmm0    # 0x93490
cvttpd2dq %xmm0, %xmm0
movq 0x20(%r15), %xmm1
movdqa %xmm1, 0x10(%rsp)
movapd %xmm0, 0x20(%rsp)
movd %xmm0, %r12d
mulss 0x26d0c(%rip), %xmm2    # 0x93424
movl 0xc(%rax), %ebx
movss %xmm2, 0x4(%rsp)
xorps %xmm0, %xmm0
cvtss2sd %xmm2, %xmm0
mulsd 0x1930(%rip), %xmm0     # 0x6e060
callq 0x4900
testl %r12d, %r12d
js 0x6c7c3
movl %ebp, %eax
negl %eax
movd %ebp, %xmm1
movdqa 0x10(%rsp), %xmm4
movdqa %xmm4, %xmm2
psrad %xmm1, %xmm2
movd %eax, %xmm1
pslld %xmm1, %xmm4
movl %ebp, %eax
shrl $0x1f, %eax
movd %eax, %xmm1
pshufd $0x50, %xmm1, %xmm3     # xmm3 = xmm1[0,0,1,1]
pslld $0x1f, %xmm3
psrad $0x1f, %xmm3
pand %xmm3, %xmm4
pandn %xmm2, %xmm3
por %xmm4, %xmm3
movdqa %xmm3, %xmm1
movdqa 0x20(%rsp), %xmm2
pcmpgtd %xmm2, %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
movmskpd %xmm1, %eax
movl %eax, %ecx
shrb %cl
pshufd $0x55, %xmm2, %xmm1     # xmm1 = xmm2[1,1,1,1]
movd %xmm1, %esi
testl %esi, %esi
setns %dl
andb %al, %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x6c7c3
cmpl 0x18(%r15), %ebx
jle 0x6c7c3
movl 0x1c(%r15), %eax
addl $-0x2, %eax
cmpl %eax, %ebx
jle 0x6c819
movq 0x3b81e(%rip), %rbx     # 0xa7fe8
leaq 0x33db(%rip), %rsi      # 0x6fbac
movl $0x1, %edx
movq %rbx, %rdi
callq 0x45b0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4530
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4050
movq %rax, %rdi
callq 0x4380
xorl %eax, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebx, 0x20(%rsp)
pshufd $0x55, %xmm3, %xmm1     # xmm1 = xmm3[1,1,1,1]
movd %xmm1, %r14d
movq %r13, 0x38(%rsp)
movq %rsi, %r13
cvttsd2si %xmm0, %ebx
movq %r15, %rdi
movl %ebp, %esi
movdqa %xmm3, 0x10(%rsp)
callq 0x6c2ac
leaq 0x80(%rsp), %rdi
xorl %ebp, %ebp
movl $0x90, %edx
xorl %esi, %esi
callq 0x4290
movl %ebx, %eax
negl %eax
movl $0x1, %r8d
movl $0x1, %edx
subl %r13d, %edx
cmpl %eax, %edx
movl %eax, %ecx
cmovgl %edx, %ecx
movl %r14d, %r9d
movq %r13, 0x40(%rsp)
subl %r13d, %r9d
movq 0x38(%rsp), %r13
addl $-0x2, %r9d
cmpl %ebx, %r9d
cmovgel %ebx, %r9d
xorpd %xmm0, %xmm0
cmpl %r9d, %ecx
jg 0x6caa6
movdqa 0x10(%rsp), %xmm0
movd %xmm0, %r10d
movl 0x18(%r15), %esi
notl %esi
movl 0x20(%rsp), %r11d
addl %esi, %r11d
leal (%r10,%r10), %esi
imull %esi, %r14d
imull %r14d, %r11d
movslq %esi, %r14
movl %esi, %edi
imull 0x40(%rsp), %edi
subl %r12d, %r8d
cmpl %eax, %r8d
cmovlel %eax, %r8d
subl %r12d, %r10d
addl $-0x2, %r10d
cmpl %ebx, %r10d
cmovgel %ebx, %r10d
imull %ebx, %ebx
cvtsi2sd %ebx, %xmm3
movslq %r8d, %rsi
cmpl %edx, %eax
cmovgl %eax, %edx
movslq %edx, %r13
addl %r13d, %r9d
subl %ecx, %r9d
movq %r13, %rax
imulq %r14, %rax
leaq (,%rsi,8), %rcx
leaq (%rcx,%rax,4), %rax
movslq %r11d, %rcx
leaq (%rax,%rcx,4), %rax
movslq %edi, %rcx
leaq (%rax,%rcx,4), %rax
leal (%r12,%r12), %ecx
leaq (%rax,%rcx,4), %rax
movq 0x30(%r15), %rcx
leaq (%rcx,%rax), %r15
addq $0x4, %r15
movq %r10, 0x50(%rsp)
movl %r10d, %ebx
subl %esi, %ebx
addsd 0x1728(%rip), %xmm3     # 0x6e068
movss 0x4(%rsp), %xmm0
movaps %xmm0, %xmm1
addss %xmm0, %xmm1
mulss %xmm0, %xmm1
movss %xmm1, 0x4(%rsp)
incl %r9d
movaps 0x60(%rsp), %xmm2
movaps %xmm2, %xmm0
shufps $0x55, %xmm2, %xmm0     # xmm0 = xmm0[1,1],xmm2[1,1]
movaps %xmm0, 0x70(%rsp)
shlq $0x2, %r14
movq %r14, 0x48(%rsp)
addl %r8d, %r12d
incl %ebx
movl %r8d, 0xc(%rsp)
movl %r9d, 0x8(%rsp)
movsd %xmm3, 0x58(%rsp)
cmpl 0x50(%rsp), %r8d
jg 0x6ca7d
movq 0x40(%rsp), %rax
addl %r13d, %eax
xorps %xmm4, %xmm4
cvtsi2ss %eax, %xmm4
subss 0x70(%rsp), %xmm4
mulss %xmm4, %xmm4
xorl %r14d, %r14d
movss %xmm4, 0x10(%rsp)
leal (%r12,%r14), %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
subss %xmm2, %xmm0
mulss %xmm0, %xmm0
addss %xmm4, %xmm0
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
ucomisd %xmm3, %xmm1
jae 0x6ca71
divss 0x4(%rsp), %xmm0
ucomiss 0x191d(%rip), %xmm0     # 0x6e308
jb 0x6cca2
movss 0x1923(%rip), %xmm1     # 0x6e31c
ucomiss %xmm0, %xmm1
jb 0x6cca2
xorps 0x1777(%rip), %xmm0     # 0x6e180
cvtss2sd %xmm0, %xmm0
callq 0x4990
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x20(%rsp)
movss (%r15,%r14,8), %xmm0
mulss 0x26a02(%rip), %xmm0    # 0x9342c
cvtss2sd %xmm0, %xmm0
divsd 0x26a3a(%rip), %xmm0    # 0x93470
callq 0x4900
cvttsd2si %xmm0, %eax
movss 0x20(%rsp), %xmm0
mulss -0x4(%r15,%r14,8), %xmm0
cltq
addss 0x80(%rsp,%rax,4), %xmm0
movss %xmm0, 0x80(%rsp,%rax,4)
movaps 0x60(%rsp), %xmm2
movsd 0x58(%rsp), %xmm3
movss 0x10(%rsp), %xmm4
incq %r14
cmpl %r14d, %ebx
jne 0x6c9b6
incq %r13
addq 0x48(%rsp), %r15
movl 0x8(%rsp), %r9d
cmpl %r13d, %r9d
movl 0xc(%rsp), %r8d
jne 0x6c989
movss 0x10c(%rsp), %xmm0
movq 0x38(%rsp), %r13
movss 0x1832(%rip), %xmm1     # 0x6e2e0
leaq 0x80(%rsp), %rdx
movl $0x1, %eax
movss 0x80(%rsp), %xmm2
movaps %xmm2, %xmm3
movapd %xmm0, %xmm4
movaps %xmm3, %xmm0
addss %xmm3, %xmm4
movss 0x80(%rsp,%rax,4), %xmm3
addss %xmm3, %xmm4
divss %xmm1, %xmm4
movss %xmm4, 0x7c(%rsp,%rax,4)
incq %rax
cmpq $0x24, %rax
jne 0x6cac7
addss 0x10c(%rsp), %xmm0
addss %xmm0, %xmm2
divss %xmm1, %xmm2
movss %xmm2, 0x10c(%rsp)
incl %ebp
movaps %xmm2, %xmm0
cmpl $0x6, %ebp
jne 0x6cab6
movl $0x4, %eax
movss 0x80(%rsp), %xmm0
leaq (%rsp,%rax), %rcx
addq $0x80, %rcx
movss (%rcx), %xmm1
ucomiss %xmm0, %xmm1
cmovaq %rcx, %rdx
maxss %xmm0, %xmm1
addq $0x4, %rax
movaps %xmm1, %xmm0
cmpq $0x90, %rax
jne 0x6cb24
xorps %xmm0, %xmm0
cvtss2sd (%rdx), %xmm0
mulsd 0x268ec(%rip), %xmm0    # 0x93448
xorl %ecx, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x23, %esi
movsd 0x268d0(%rip), %xmm1    # 0x93440
movsd 0x14f0(%rip), %xmm2     # 0x6e068
movsd 0x268f0(%rip), %xmm3    # 0x93470
movsd 0x268f0(%rip), %xmm4    # 0x93478
xorl %edi, %edi
leal 0x25(%rcx), %r8d
leaq (%rcx,%rdx), %r9
leaq (%rsp,%rcx,4), %r10
addq $0x80, %r10
movl %r8d, %r11d
xorl %eax, %eax
movl %r11d, %r14d
leaq (%rcx,%rax), %r11
cmpq $0x24, %r11
je 0x6cc9b
movss (%r10,%rax,4), %xmm5
movl %eax, %ebx
leal (%r9,%rbx), %r15d
testq %r11, %r11
cmoveq %rsi, %r15
xorps %xmm6, %xmm6
cvtss2sd %xmm5, %xmm6
incq %rax
leal 0x1(%r14), %r11d
ucomisd %xmm0, %xmm6
jbe 0x6cba2
movss 0x80(%rsp,%r15,4), %xmm6
ucomiss %xmm6, %xmm5
jbe 0x6cba2
movzbl %r14b, %ebp
imull $0x39, %ebp, %r14d
shrl $0x9, %r14d
andl $-0x4, %r14d
leal (%r14,%r14,8), %ebp
movl %r8d, %r14d
subb %bpl, %r14b
movzbl %r14b, %r14d
addl %ebx, %r14d
movzbl %r14b, %ebx
movss 0x80(%rsp,%rbx,4), %xmm7
ucomiss %xmm7, %xmm5
jbe 0x6cba2
leal (%rcx,%rax), %r8d
decl %r8d
movaps %xmm7, %xmm8
subss %xmm6, %xmm8
cvtss2sd %xmm8, %xmm8
mulsd %xmm1, %xmm8
addss %xmm7, %xmm6
addss %xmm5, %xmm5
subss %xmm5, %xmm6
xorps %xmm5, %xmm5
cvtss2sd %xmm6, %xmm5
divsd %xmm5, %xmm8
xorps %xmm5, %xmm5
cvtsd2ss %xmm8, %xmm5
xorps %xmm6, %xmm6
cvtsi2ss %r8d, %xmm6
addss %xmm5, %xmm6
xorps %xmm5, %xmm5
cvtss2sd %xmm6, %xmm5
addsd %xmm2, %xmm5
mulsd %xmm3, %xmm5
divsd %xmm4, %xmm5
cvtsd2ss %xmm5, %xmm5
movl %edi, %r8d
incl %edi
movss %xmm5, (%r13,%r8,4)
addq %rax, %rcx
movl $0x4, %eax
cmpl $0x4, %edi
jne 0x6cb8a
jmp 0x6c807
movl %edi, %eax
jmp 0x6c807
leaq 0x26985(%rip), %rdi     # 0x9362e
leaq 0x26905(%rip), %rsi     # 0x935b5
leaq 0x269a8(%rip), %rcx     # 0x9365f
movl $0xdb, %edx
callq 0x4340
leaq 0x268d3(%rip), %rdi     # 0x9359b
leaq 0x268e6(%rip), %rsi     # 0x935b5
leaq 0x268ef(%rip), %rcx     # 0x935c5
movl $0x59, %edx
callq 0x4340