// File: /workspace/github_cpp_makefile_star2_O1/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 $0xd8, %rsp
movq %rsi, %r13
movq %rdi, %r15
movl 0x110(%rsp), %ebp
testl %ebp, %ebp
js 0x6f801
movl $0x1, %eax
movl %ebp, %ecx
shll %cl, %eax
cvtsi2ss %eax, %xmm2
jmp 0x6f81c
movl %ebp, %ecx
negb %cl
movl $0x1, %eax
shll %cl, %eax
cvtsi2ss %eax, %xmm0
movss 0x1a98(%rip), %xmm2     # 0x712b0
divss %xmm0, %xmm2
movl 0x14(%r15), %eax
cmpl %ebp, %eax
jg 0x6fda3
addl 0xc(%r15), %eax
cmpl %ebp, %eax
jle 0x6fda3
movl 0x20(%r15), %ebx
movl %ebx, %edx
movl %ebp, %ecx
sarl %cl, %edx
movl %ebp, %eax
negb %al
movl %eax, %ecx
shll %cl, %ebx
leaq 0x110(%rsp), %rsi
movl 0x24(%r15), %r14d
movl %r14d, %edi
movl %ebp, %ecx
sarl %cl, %edi
movl %eax, %ecx
shll %cl, %r14d
testl %ebp, %ebp
cmovnsl %edx, %ebx
cmovnsl %edi, %r14d
movss 0x10(%rsi), %xmm0
divss %xmm2, %xmm0
movss 0x14(%rsi), %xmm1
movss %xmm1, 0x10(%rsp)
movss 0x1c(%rsi), %xmm1
movss %xmm2, 0x4(%rsp)
divss %xmm2, %xmm1
movss %xmm0, 0x24(%rsp)
cvtss2sd %xmm0, %xmm0
addsd 0x17d4(%rip), %xmm0     # 0x71070
cvttsd2si %xmm0, %r12d
mulss 0x2873f(%rip), %xmm1    # 0x97fe8
movl 0xc(%rsi), %eax
movl %eax, (%rsp)
movss %xmm1, 0x14(%rsp)
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
mulsd 0x17a4(%rip), %xmm0     # 0x71068
callq 0x48f0
movsd 0x179f(%rip), %xmm2     # 0x71070
testl %r12d, %r12d
js 0x6f92a
movl %ebx, %eax
subl %r12d, %eax
movl %eax, 0xc(%rsp)
jle 0x6f92a
movss 0x10(%rsp), %xmm1
divss 0x4(%rsp), %xmm1
movss %xmm1, 0x10(%rsp)
cvtss2sd %xmm1, %xmm1
addsd %xmm2, %xmm1
cvttsd2si %xmm1, %ecx
movq %rcx, 0x18(%rsp)
testl %ecx, %ecx
js 0x6f92a
movl %r14d, %eax
subl 0x18(%rsp), %eax
movl %eax, 0x8(%rsp)
jle 0x6f92a
movl (%rsp), %eax
cmpl 0x18(%r15), %eax
jle 0x6f92a
movl 0x1c(%r15), %eax
addl $-0x2, %eax
cmpl %eax, (%rsp)
jle 0x6f982
movq 0x3d6b7(%rip), %rbx     # 0xacfe8
leaq 0x4d44(%rip), %rsi      # 0x7467c
movl $0x1, %edx
movq %rbx, %rdi
callq 0x45a0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4520
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4050
movq %rax, %rdi
callq 0x4370
xorl %edi, %edi
movl %edi, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, 0x28(%rsp)
cvttsd2si %xmm0, %eax
movl %eax, 0x4(%rsp)
movq %r15, %rdi
movl %ebp, %esi
callq 0x6f402
leaq 0x40(%rsp), %rdi
xorl %r13d, %r13d
movl $0x90, %edx
xorl %esi, %esi
callq 0x4290
movl 0x4(%rsp), %edi
movl %edi, %ecx
negl %ecx
movl $0x1, %edx
subl 0x18(%rsp), %edx
cmpl %ecx, %edx
movl %ecx, %eax
cmovgl %edx, %eax
movl 0x8(%rsp), %esi
addl $-0x2, %esi
cmpl %edi, %esi
cmovgel %edi, %esi
cmpl %esi, %eax
jg 0x6fbb2
movl %esi, %r9d
movl 0x18(%r15), %esi
notl %esi
movl (%rsp), %r11d
addl %esi, %r11d
addl %ebx, %ebx
imull %ebx, %r14d
imull %r14d, %r11d
movl %ebx, %esi
imull 0x18(%rsp), %esi
movl $0x1, %r10d
subl %r12d, %r10d
cmpl %ecx, %r10d
cmovlel %ecx, %r10d
movl 0xc(%rsp), %r8d
addl $-0x2, %r8d
cmpl %edi, %r8d
cmovgel %edi, %r8d
imull %edi, %edi
cvtsi2sd %edi, %xmm2
movslq %r10d, %rdi
cmpl %edx, %ecx
cmovgl %ecx, %edx
movslq %edx, %rbp
movslq %ebx, %rdx
addl %ebp, %r9d
subl %eax, %r9d
movq %rbp, %rax
imulq %rdx, %rax
leaq (,%rdi,8), %rcx
leaq (%rcx,%rax,4), %rax
movslq %r11d, %rcx
leaq (%rax,%rcx,4), %rax
movslq %esi, %rcx
leaq (%rax,%rcx,4), %rax
leal (%r12,%r12), %ecx
leaq (%rax,%rcx,4), %rax
movq 0x30(%r15), %rcx
leaq (%rcx,%rax), %r14
addq $0x4, %r14
movl %r8d, 0xc(%rsp)
movl %r8d, %r15d
subl %edi, %r15d
addsd 0x15f3(%rip), %xmm2     # 0x71070
movss 0x14(%rsp), %xmm0
movaps %xmm0, %xmm1
addss %xmm0, %xmm1
mulss %xmm0, %xmm1
movss %xmm1, 0x14(%rsp)
incl %r9d
movl %r9d, 0x8(%rsp)
shlq $0x2, %rdx
movq %rdx, 0x30(%rsp)
movl %r10d, 0x20(%rsp)
addl %r10d, %r12d
incl %r15d
movsd %xmm2, 0x38(%rsp)
movl 0x20(%rsp), %eax
cmpl 0xc(%rsp), %eax
jg 0x6fba0
movq 0x18(%rsp), %rax
addl %ebp, %eax
xorps %xmm3, %xmm3
cvtsi2ss %eax, %xmm3
subss 0x10(%rsp), %xmm3
mulss %xmm3, %xmm3
xorl %ebx, %ebx
movss %xmm3, (%rsp)
leal (%r12,%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
subss 0x24(%rsp), %xmm0
mulss %xmm0, %xmm0
addss %xmm3, %xmm0
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
ucomisd %xmm2, %xmm1
jae 0x6fb94
divss 0x14(%rsp), %xmm0
ucomiss 0x17ee(%rip), %xmm0     # 0x71308
jb 0x6fd84
movss 0x17f4(%rip), %xmm1     # 0x7131c
ucomiss %xmm0, %xmm1
jb 0x6fd84
xorps 0x1638(%rip), %xmm0     # 0x71170
cvtss2sd %xmm0, %xmm0
callq 0x4980
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x4(%rsp)
movss (%r14,%rbx,8), %xmm0
mulss 0x28497(%rip), %xmm0    # 0x97ff0
cvtss2sd %xmm0, %xmm0
divsd 0x284c3(%rip), %xmm0    # 0x98028
callq 0x48f0
cvttsd2si %xmm0, %eax
movss 0x4(%rsp), %xmm0
mulss -0x4(%r14,%rbx,8), %xmm0
cltq
addss 0x40(%rsp,%rax,4), %xmm0
movss %xmm0, 0x40(%rsp,%rax,4)
movsd 0x38(%rsp), %xmm2
movss (%rsp), %xmm3
incq %rbx
cmpl %ebx, %r15d
jne 0x6fae3
incq %rbp
addq 0x30(%rsp), %r14
cmpl %ebp, 0x8(%rsp)
jne 0x6fab6
movss 0x1726(%rip), %xmm0     # 0x712e0
movsd 0x14ad(%rip), %xmm8     # 0x71070
leaq 0x40(%rsp), %rdx
movss 0x40(%rsp), %xmm1
movss 0xcc(%rsp), %xmm2
xorl %eax, %eax
movaps %xmm2, %xmm3
movss 0x40(%rsp,%rax,4), %xmm2
addss %xmm2, %xmm3
addss 0x44(%rsp,%rax,4), %xmm3
divss %xmm0, %xmm3
movss %xmm3, 0x40(%rsp,%rax,4)
leaq 0x1(%rax), %rcx
movq %rcx, %rax
cmpq $0x23, %rcx
jne 0x6fbd9
addss 0xcc(%rsp), %xmm2
addss %xmm2, %xmm1
divss %xmm0, %xmm1
movss %xmm1, 0xcc(%rsp)
incl %r13d
cmpl $0x6, %r13d
jne 0x6fbc8
movl $0x4, %eax
movq 0x28(%rsp), %r14
movss 0x40(%rsp,%rax), %xmm0
ucomiss (%rdx), %xmm0
jbe 0x6fc43
leaq (%rsp,%rax), %rdx
addq $0x40, %rdx
addq $0x4, %rax
cmpq $0x90, %rax
jne 0x6fc30
xorps %xmm0, %xmm0
cvtss2sd (%rdx), %xmm0
mulsd 0x283b2(%rip), %xmm0    # 0x98010
leaq 0xd4(%rsp), %r9
xorl %r10d, %r10d
movl $0x25, %ecx
movl $0x23, %esi
leaq 0xcc(%rsp), %r11
movabsq $-0x1c71c71c71c71c71, %rbx # imm = 0xE38E38E38E38E38F
movsd 0x2837b(%rip), %xmm1    # 0x98008
movsd 0x28393(%rip), %xmm2    # 0x98028
movsd 0x28393(%rip), %xmm3    # 0x98030
xorl %edi, %edi
movq %rcx, %rax
mulq %rbx
movq %rdx, %r8
movq %rsi, %rax
mulq %rbx
cmpq $0x24, %r10
je 0x6f96e
movss 0x40(%rsp,%r10,4), %xmm4
xorps %xmm5, %xmm5
cvtss2sd %xmm4, %xmm5
ucomisd %xmm0, %xmm5
movb $0x1, %al
jbe 0x6fd66
shrq %rdx
andq $-0x10, %rdx
leaq (%rdx,%rdx,8), %rdx
negq %rdx
movss (%r11,%rdx), %xmm5
ucomiss %xmm5, %xmm4
jbe 0x6fd66
shrq %r8
andq $-0x10, %r8
leaq (%r8,%r8,8), %rdx
negq %rdx
movss (%r9,%rdx), %xmm6
ucomiss %xmm6, %xmm4
jbe 0x6fd66
movaps %xmm6, %xmm7
subss %xmm5, %xmm7
cvtss2sd %xmm7, %xmm7
mulsd %xmm1, %xmm7
addss %xmm6, %xmm5
addss %xmm4, %xmm4
subss %xmm4, %xmm5
xorps %xmm4, %xmm4
cvtss2sd %xmm5, %xmm4
divsd %xmm4, %xmm7
xorps %xmm4, %xmm4
cvtsd2ss %xmm7, %xmm4
xorps %xmm5, %xmm5
cvtsi2ss %r10d, %xmm5
addss %xmm4, %xmm5
xorps %xmm4, %xmm4
cvtss2sd %xmm5, %xmm4
addsd %xmm8, %xmm4
mulsd %xmm2, %xmm4
divsd %xmm3, %xmm4
cvtsd2ss %xmm4, %xmm4
movslq %edi, %rax
incl %edi
movss %xmm4, (%r14,%rax,4)
cmpl $0x4, %edi
setne %al
addq $0x4, %r9
incq %rcx
addq $0x4, %r11
incq %rsi
incq %r10
testb %al, %al
jne 0x6fc9f
jmp 0x6f96e
leaq 0x284b3(%rip), %rdi     # 0x9823e
leaq 0x28433(%rip), %rsi     # 0x981c5
leaq 0x284d6(%rip), %rcx     # 0x9826f
movl $0xdb, %edx
callq 0x4340
leaq 0x28401(%rip), %rdi     # 0x981ab
leaq 0x28414(%rip), %rsi     # 0x981c5
leaq 0x2841d(%rip), %rcx     # 0x981d5
movl $0x59, %edx
callq 0x4340