// File: /workspace/github_c_makefile_star2_O2/zpole[P]IICV/vl/sift.c
// Function: vl_sift_calc_keypoint_orientations
VL_EXPORT
int
vl_sift_calc_keypoint_orientations (VlSiftFilt *f,
                                    double angles [4],
                                    VlSiftKeypoint const *k)
{
  double const winf   = 1.5 ;
  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 sigmaw = winf * sigma ;
  int          W      = VL_MAX(floor (3.0 * sigmaw), 1) ;

  int          nangles= 0 ;

  enum {nbins = 36} ;

  double hist [nbins], maxh ;
  vl_sift_pix const * pt ;
  int xs, ys, iter, i ;

  /* skip if the keypoint octave is not current */
  if(k->o != f->o_cur)
    return 0 ;

  /* skip the keypoint if it is out of bounds */
  if(xi < 0            ||
     xi > w - 1        ||
     yi < 0            ||
     yi > h - 1        ||
     si < f->s_min + 1 ||
     si > f->s_max - 2  ) {
    return 0 ;
  }

  /* make gradient up to date */
  update_gradient (f) ;

  /* clear histogram */
  memset (hist, 0, sizeof(double) * nbins) ;

  /* compute orientation histogram */
  pt = f-> grad + xo*xi + yo*yi + so*(si - f->s_min - 1) ;

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

  for(ys  =  VL_MAX (- W,       - yi) ;
      ys <=  VL_MIN (+ W, h - 1 - yi) ; ++ys) {

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


      double dx = (double)(xi + xs) - x;
      double dy = (double)(yi + ys) - y;
      double r2 = dx*dx + dy*dy ;
      double wgt, mod, ang, fbin ;

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

      wgt  = fast_expn (r2 / (2*sigmaw*sigmaw)) ;
      mod  = *(pt + xs*xo + ys*yo    ) ;
      ang  = *(pt + xs*xo + ys*yo + 1) ;
      fbin = nbins * ang / (2 * VL_PI) ;

#if defined(VL_SIFT_BILINEAR_ORIENTATIONS)
      {
        int bin = (int) vl_floor_d (fbin - 0.5) ;
        double rbin = fbin - bin - 0.5 ;
        hist [(bin + nbins) % nbins] += (1 - rbin) * mod * wgt ;
        hist [(bin + 1    ) % nbins] += (    rbin) * mod * wgt ;
      }
#else
      {
        int    bin  = vl_floor_d (fbin) ;
        bin = vl_floor_d (nbins * ang / (2*VL_PI)) ;
        hist [(bin) % nbins] += mod * wgt ;
      }
#endif

    } /* for xs */
  } /* for ys */

  /* smooth histogram */
  for (iter = 0; iter < 6; iter ++) {
    double prev  = hist [nbins - 1] ;
    double first = hist [0] ;
    int i ;
    for (i = 0; i < nbins - 1; i++) {
      double newh = (prev + hist[i] + hist[(i+1) % nbins]) / 3.0;
      prev = hist[i] ;
      hist[i] = newh ;
    }
    hist[i] = (prev + hist[i] + first) / 3.0 ;
  }

  /* find the histogram maximum */
  maxh = 0 ;
  for (i = 0 ; i < nbins ; ++i)
    maxh = VL_MAX (maxh, hist [i]) ;

  /* find peaks within 80% from max */
  nangles = 0 ;
  for(i = 0 ; i < nbins ; ++i) {
    double h0 = hist [i] ;
    double hm = hist [(i - 1 + nbins) % nbins] ;
    double hp = hist [(i + 1 + nbins) % nbins] ;

    /* is this a peak? */
    if (h0 > 0.8*maxh && h0 > hm && h0 > hp) {

      /* quadratic interpolation */
      double di = - 0.5 * (hp - hm) / (hp + hm - 2 * h0) ;
      double th = 2 * VL_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 $0x1a8, %rsp            # imm = 0x1A8
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r14
movl 0x3c(%rdi), %edi
movsd 0x54ef(%rip), %xmm0     # 0x2d238
callq 0x90c0
cvtss2sd 0x1c(%r15), %xmm1
movapd %xmm0, 0x20(%rsp)
divsd %xmm0, %xmm1
movq 0x58(%r14), %rbx
cvtps2pd 0x10(%r15), %xmm0
movaps %xmm0, 0x40(%rsp)
movl 0xc(%r15), %ebp
mulsd 0x5460(%rip), %xmm1     # 0x2d1d8
movsd 0x5468(%rip), %xmm0     # 0x2d1e8
movsd %xmm1, 0x10(%rsp)
mulsd %xmm1, %xmm0
callq 0x98f0
movl (%r15), %eax
xorl %ecx, %ecx
cmpl 0x3c(%r14), %eax
jne 0x27e14
movapd 0x20(%rsp), %xmm1
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
movapd 0x40(%rsp), %xmm4
divpd %xmm1, %xmm4
movapd 0x52fa(%rip), %xmm1     # 0x2d0b0
addpd %xmm4, %xmm1
cvttpd2dq %xmm1, %xmm2
movd %xmm2, %eax
testl %eax, %eax
js 0x27e14
movq %rax, %r13
movq %rbx, %xmm1
movdqa %xmm1, %xmm3
pcmpgtd %xmm2, %xmm3
pshufd $0x50, %xmm3, %xmm3     # xmm3 = xmm3[0,0,1,1]
movmskpd %xmm3, %eax
movl %eax, %edx
shrb %dl
pshufd $0x55, %xmm2, %xmm2     # xmm2 = xmm2[1,1,1,1]
movd %xmm2, %esi
movq %rsi, 0x38(%rsp)
testl %esi, %esi
setns %sil
andb %al, %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x27e14
cmpl 0x34(%r14), %ebp
jle 0x27e14
movl 0x38(%r14), %eax
addl $-0x2, %eax
cmpl %eax, %ebp
jle 0x27e28
movl %ecx, %eax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, 0x50(%rsp)
pshufd $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
movd %xmm1, %ecx
movl %ecx, 0x18(%rsp)
leal (%rbx,%rbx), %r15d
movl %r15d, (%rsp)
imull %ecx, %r15d
maxsd 0x53ea(%rip), %xmm0     # 0x2d238
cvttsd2si %xmm0, %r12d
movq %r14, %rdi
movapd %xmm4, 0x40(%rsp)
callq 0x2826b
leaq 0x80(%rsp), %rdi
movl $0x120, %edx            # imm = 0x120
xorl %esi, %esi
callq 0x9420
movapd 0x40(%rsp), %xmm4
movq 0xb0(%r14), %rax
movq %rax, 0x20(%rsp)
movq %r13, %rax
leal (%r13,%r13), %ecx
movq %rcx, 0x8(%rsp)
movl (%rsp), %ecx
movl %ecx, %edx
movq 0x38(%rsp), %rdi
imull %edi, %edx
movslq %edx, %rdx
movl 0x34(%r14), %esi
notl %esi
addl %esi, %ebp
imull %r15d, %ebp
movslq %ebp, %rsi
cmpl %edi, %r12d
movl %edi, %r8d
cmovll %r12d, %r8d
negl %r8d
movl %edi, %r9d
notl %r9d
addl 0x18(%rsp), %r9d
cmpl %r12d, %r9d
cmovgel %r12d, %r9d
cmpl %eax, %r12d
movl %eax, %edi
cmovll %r12d, %edi
movl %edi, %r10d
negl %r10d
movl %eax, %r11d
notl %r11d
addl %ebx, %r11d
cmpl %r12d, %r11d
cmovgel %r12d, %r11d
imull %r12d, %r12d
cvtsi2sd %r12d, %xmm1
addsd 0x52ae(%rip), %xmm1     # 0x2d1b0
movsd 0x10(%rsp), %xmm2
movapd %xmm2, %xmm0
addsd %xmm2, %xmm0
mulsd %xmm2, %xmm0
movsd %xmm0, 0x10(%rsp)
movslq %r10d, %r10
movslq %r11d, %r13
movslq %r8d, %rbp
movslq %r9d, %r8
movq %r8, 0x68(%rsp)
movslq %ecx, %r11
movapd %xmm4, %xmm0
unpckhpd %xmm4, %xmm0            # xmm0 = xmm0[1],xmm4[1]
movapd %xmm0, 0x70(%rsp)
movq %rbp, %r8
imulq %r11, %r8
movq %r10, 0x60(%rsp)
leaq (,%r10,8), %r9
leaq (%r9,%r8,4), %r8
leaq (%r8,%rsi,4), %rsi
leaq (%rsi,%rdx,4), %rdx
movq 0x8(%rsp), %rcx
leaq (%rdx,%rcx,4), %rcx
movq 0x20(%rsp), %rdx
leaq (%rdx,%rcx), %r12
addq $0x4, %r12
shlq $0x2, %r11
movq %r11, 0x58(%rsp)
subl %edi, %eax
movq %rax, (%rsp)
pushq $0x24
popq %rbx
movsd %xmm1, 0x18(%rsp)
cmpq 0x68(%rsp), %rbp
jg 0x280d2
movq 0x38(%rsp), %rax
addl %ebp, %eax
cvtsi2sd %eax, %xmm2
subsd 0x70(%rsp), %xmm2
mulsd %xmm2, %xmm2
movq (%rsp), %rax
movl %eax, %r14d
movq %r12, 0x8(%rsp)
movq 0x60(%rsp), %r15
movsd %xmm2, 0x20(%rsp)
cmpq %r13, %r15
jg 0x280c0
cvtsi2sd %r14d, %xmm0
subsd %xmm4, %xmm0
mulsd %xmm0, %xmm0
addsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jae 0x280b1
divsd 0x10(%rsp), %xmm0
callq 0x288a4
cvtss2sd -0x4(%r12), %xmm2
cvtss2sd (%r12), %xmm1
mulsd 0x6323(%rip), %xmm1     # 0x2e328
divsd 0x5373(%rip), %xmm1     # 0x2d380
movapd %xmm1, %xmm3
movsd 0x51d7(%rip), %xmm6     # 0x2d1f0
addsd %xmm6, %xmm3
cvttsd2si %xmm3, %rcx
cvtsi2sd %rcx, %xmm4
cmpneqpd %xmm3, %xmm4
xorpd %xmm5, %xmm5
cmpnlepd %xmm3, %xmm5
andpd %xmm4, %xmm5
movapd 0x40(%rsp), %xmm4
movd %xmm5, %esi
leal (%rcx,%rsi), %eax
cvtsi2sd %eax, %xmm3
subsd %xmm3, %xmm1
addsd %xmm6, %xmm1
movsd 0x51de(%rip), %xmm3     # 0x2d238
subsd %xmm1, %xmm3
mulsd %xmm2, %xmm3
addl %ebx, %eax
cltd
idivl %ebx
movslq %edx, %rax
mulsd %xmm0, %xmm3
addsd 0x80(%rsp,%rax,8), %xmm3
movsd %xmm3, 0x80(%rsp,%rax,8)
mulsd %xmm2, %xmm1
movsd 0x20(%rsp), %xmm2
leal (%rcx,%rsi), %eax
incl %eax
cltd
idivl %ebx
movslq %edx, %rax
mulsd %xmm0, %xmm1
addsd 0x80(%rsp,%rax,8), %xmm1
movsd %xmm1, 0x80(%rsp,%rax,8)
movsd 0x18(%rsp), %xmm1
incq %r15
addq $0x8, %r12
incl %r14d
jmp 0x27fc1
incq %rbp
movq 0x8(%rsp), %r12
addq 0x58(%rsp), %r12
jmp 0x27f8a
xorl %eax, %eax
pushq $0x1
popq %rcx
movsd 0x5109(%rip), %xmm0     # 0x2d1e8
cmpl $0x6, %eax
je 0x28149
movsd 0x80(%rsp), %xmm1
movsd 0x198(%rsp), %xmm2
movq %rcx, %rdx
movapd %xmm1, %xmm4
cmpq $0x24, %rdx
je 0x2812b
movapd %xmm4, %xmm3
addsd %xmm4, %xmm2
movsd 0x80(%rsp,%rdx,8), %xmm4
addsd %xmm4, %xmm2
divsd %xmm0, %xmm2
movsd %xmm2, 0x78(%rsp,%rdx,8)
incq %rdx
movapd %xmm3, %xmm2
jmp 0x280fd
addsd 0x198(%rsp), %xmm2
addsd %xmm2, %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x198(%rsp)
incl %eax
jmp 0x280df
xorpd %xmm0, %xmm0
xorl %eax, %eax
cmpq $0x24, %rax
je 0x28163
maxsd 0x80(%rsp,%rax,8), %xmm0
incq %rax
jmp 0x2814f
mulsd 0x50c5(%rip), %xmm0     # 0x2d230
xorl %edx, %edx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movb $0x24, %dil
pushq $0x23
popq %r8
movsd 0x503f(%rip), %xmm1     # 0x2d1c0
pushq $0x4
popq %r9
xorl %ecx, %ecx
leal 0x25(%rdx), %r11d
leaq (%rdx,%rsi), %rbx
leaq (%rsp,%rdx,8), %r14
addq $0x80, %r14
movl %r11d, %ebp
xorl %r10d, %r10d
movzbl %bpl, %eax
divb %dil
leaq (%rdx,%r10), %r13
cmpq $0x24, %r13
je 0x27e14
movsd (%r14,%r10,8), %xmm2
movl %r10d, %r15d
leal (%rbx,%r15), %r12d
testq %r13, %r13
cmoveq %r8, %r12
incq %r10
incb %bpl
ucomisd %xmm0, %xmm2
jbe 0x281a0
movsd 0x80(%rsp,%r12,8), %xmm3
ucomisd %xmm3, %xmm2
jbe 0x281a0
movzbl %al, %eax
imull $0x24, %eax, %eax
movl %r11d, %r12d
subb %al, %r12b
movzbl %r12b, %eax
addl %r15d, %eax
movzbl %al, %eax
movsd 0x80(%rsp,%rax,8), %xmm4
ucomisd %xmm4, %xmm2
jbe 0x281a0
leal (%rdx,%r10), %eax
decl %eax
movapd %xmm4, %xmm5
subsd %xmm3, %xmm5
mulsd %xmm1, %xmm5
addsd %xmm4, %xmm3
addsd %xmm2, %xmm2
subsd %xmm2, %xmm3
divsd %xmm3, %xmm5
cvtsi2sd %eax, %xmm2
subsd %xmm5, %xmm2
addsd %xmm1, %xmm2
mulsd 0x5140(%rip), %xmm2     # 0x2d380
divsd 0x60e0(%rip), %xmm2     # 0x2e328
movl %ecx, %eax
incl %ecx
movq 0x50(%rsp), %r11
movsd %xmm2, (%r11,%rax,8)
addq %r10, %rdx
cmpl $0x4, %ecx
jne 0x28187
movl %r9d, %ecx
jmp 0x27e14