// File: /workspace/github_c_makefile_star2_O3/radforddc[P]icpc_siggen/fields.c
// Function: drift_velocity
int drift_velocity(point pt, float q, vector *velo, MJD_Siggen_Setup *setup){
  point  cart_en;
  cyl_pt e, en, cyl;
  cyl_int_pt ipt;
  int   i, sign;
  float abse, absv, f, a, b, c;
  float bp, cp, en4, en6;
  struct velocity_lookup *v_lookup1, *v_lookup2;

  /*  DCR: replaced this with faster code below, saves calls to atan and tan
  cyl = cart_to_cyl(pt);
  if (nearest_field_grid_index(cyl, &ipt, setup) < 0) return -1;
  e = efield(cyl, ipt, setup);
  abse = vector_norm_cyl(e, &en);
  en.phi = cyl.phi;
  cart_en = cyl_to_cart(en);
  */
  cyl.r = sqrt(pt.x*pt.x + pt.y*pt.y);
  cyl.z = pt.z;
  cyl.phi = 0;
  if (nearest_field_grid_index(cyl, &ipt, setup) < 0) return -1;
  e = efield(cyl, ipt, setup);
  abse = vector_norm_cyl(e, &en);
  if (cyl.r > 0.001) {
    cart_en.x = en.r * pt.x/cyl.r;
    cart_en.y = en.r * pt.y/cyl.r;
  } else {
    cart_en.x = cart_en.y = 0;
  }
  cart_en.z = en.z;

  /* find location in table to interpolate from */
  for (i = 0; i < setup->v_lookup_len - 2 && abse > setup->v_lookup[i+1].e; i++);
  v_lookup1 = setup->v_lookup + i;
  v_lookup2 = setup->v_lookup + i+1;
  f = (abse - v_lookup1->e)/(v_lookup2->e - v_lookup1->e);
  if (q > 0){
    a = (v_lookup2->ha - v_lookup1->ha)*f+v_lookup1->ha;
    b = (v_lookup2->hb- v_lookup1->hb)*f+v_lookup1->hb;
    c = (v_lookup2->hc - v_lookup1->hc)*f+v_lookup1->hc;
    bp = (v_lookup2->hbp- v_lookup1->hbp)*f+v_lookup1->hbp;
    cp = (v_lookup2->hcp - v_lookup1->hcp)*f+v_lookup1->hcp;
    setup->dv_dE = (v_lookup2->h100 - v_lookup1->h100)/(v_lookup2->e - v_lookup1->e);
  }else{
    a = (v_lookup2->ea - v_lookup1->ea)*f+v_lookup1->ea;
    b = (v_lookup2->eb- v_lookup1->eb)*f+v_lookup1->eb;
    c = (v_lookup2->ec - v_lookup1->ec)*f+v_lookup1->ec;
    bp = (v_lookup2->ebp- v_lookup1->ebp)*f+v_lookup1->ebp;
    cp = (v_lookup2->ecp - v_lookup1->ecp)*f+v_lookup1->ecp;
    setup->dv_dE = (v_lookup2->e100 - v_lookup1->e100)/(v_lookup2->e - v_lookup1->e);
  }
  /* velocity can vary from the direction of the el. field
     due to effect of crystal axes */
#define POW4(x) ((x)*(x)*(x)*(x))
#define POW6(x) ((x)*(x)*(x)*(x)*(x)*(x))
  en4 = POW4(cart_en.x) + POW4(cart_en.y) + POW4(cart_en.z);
  en6 = POW6(cart_en.x) + POW6(cart_en.y) + POW6(cart_en.z);
  absv = a + b*en4 + c*en6;
  sign = (q < 0 ? -1 : 1);
  setup->v_over_E = absv / abse;
  velo->x = sign*cart_en.x*(absv+bp*4*(cart_en.x*cart_en.x - en4)
			    + cp*6*(POW4(cart_en.x) - en6));
  velo->y = sign*cart_en.y*(absv+bp*4*(cart_en.y*cart_en.y - en4)
			    + cp*6*(POW4(cart_en.y) - en6));
  velo->z = sign*cart_en.z*(absv+bp*4*(cart_en.z*cart_en.z - en4)
			    + cp*6*(POW4(cart_en.z) - en6));
#undef POW4
#undef POW6
  return 0;
}

// Assembly:
pushq %r14
pushq %rbx
subq $0x68, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movss %xmm2, 0xc(%rsp)
movaps %xmm0, %xmm2
mulps %xmm0, %xmm2
shufps $0x55, %xmm2, %xmm2     # xmm2 = xmm2[1,1,1,1]
movaps %xmm0, 0x40(%rsp)
mulss %xmm0, %xmm0
addss %xmm2, %xmm0
xorps %xmm2, %xmm2
sqrtss %xmm0, %xmm2
xorps %xmm0, %xmm0
movaps %xmm2, 0x30(%rsp)
movss %xmm2, %xmm0            # xmm0 = xmm2[0],xmm0[1,2,3]
leaq 0x20(%rsp), %rdi
movaps %xmm0, 0x50(%rsp)
movss %xmm1, 0x8(%rsp)
callq 0x551b
testl %eax, %eax
js 0x5d02
movq 0x20(%rsp), %rax
movslq 0x28(%rsp), %rdx
movaps 0x30(%rsp), %xmm0
subss 0x3b0(%r14), %xmm0
divss 0x3b8(%r14), %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
subss %xmm1, %xmm0
movss 0x8(%rsp), %xmm6
subss 0x3bc(%r14), %xmm6
divss 0x3c4(%r14), %xmm6
xorps %xmm1, %xmm1
cvtsi2ss %edx, %xmm1
subss %xmm1, %xmm6
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
movsd 0x276e(%rip), %xmm2     # 0x8098
movapd %xmm2, %xmm3
subsd %xmm1, %xmm3
cvtss2sd %xmm6, %xmm4
subsd %xmm4, %xmm2
unpcklpd %xmm3, %xmm3            # xmm3 = xmm3[0,0]
movapd %xmm2, %xmm5
unpcklpd %xmm4, %xmm5            # xmm5 = xmm5[0],xmm4[0]
mulpd %xmm3, %xmm5
cvtpd2ps %xmm5, %xmm3
movlpd %xmm3, 0x10(%rsp)
mulsd %xmm1, %xmm2
xorps %xmm1, %xmm1
cvtsd2ss %xmm2, %xmm1
movss %xmm1, 0x18(%rsp)
mulss %xmm0, %xmm6
movss %xmm6, 0x1c(%rsp)
cltq
shlq $0x3, %rax
addq 0x1470(%r14), %rax
xorps %xmm1, %xmm1
movb $0x1, %cl
xorl %edi, %edi
leaq (%rdx,%rdx,2), %rdx
shlq $0x2, %rdx
xorps %xmm4, %xmm4
movq (%rax,%rdi,8), %rsi
addq %rdx, %rsi
leaq (%rsp,%rdi,8), %rdi
addq $0x10, %rdi
movb $0x1, %r8b
xorl %r9d, %r9d
movaps %xmm4, %xmm0
leaq (%r9,%r9,2), %r10
movss (%rdi,%r9,4), %xmm3
movss (%rsi,%r10,4), %xmm2
mulss %xmm3, %xmm2
addss %xmm2, %xmm4
mulss 0x8(%rsi,%r10,4), %xmm3
addss %xmm3, %xmm1
movl $0x1, %r9d
testb $0x1, %r8b
movl $0x0, %r8d
jne 0x59a3
movl $0x1, %edi
testb $0x1, %cl
movl $0x0, %ecx
jne 0x598e
addss %xmm0, %xmm2
movaps 0x50(%rsp), %xmm0
movss %xmm2, %xmm0            # xmm0 = xmm2[0],xmm0[1,2,3]
leaq 0x10(%rsp), %rdi
callq 0x39f9
movaps 0x30(%rsp), %xmm3
xorps %xmm1, %xmm1
cvtss2sd %xmm3, %xmm1
xorps %xmm2, %xmm2
ucomisd 0x2608(%rip), %xmm1     # 0x8020
jbe 0x5a2f
movaps 0x10(%rsp), %xmm2
shufps $0x0, %xmm2, %xmm2      # xmm2 = xmm2[0,0,0,0]
mulps 0x40(%rsp), %xmm2
shufps $0x0, %xmm3, %xmm3      # xmm3 = xmm3[0,0,0,0]
divps %xmm3, %xmm2
movss 0x18(%rsp), %xmm1
movl 0x3d0(%r14), %ecx
cmpl $0x3, %ecx
movl $0x2, %eax
cmovgel %ecx, %eax
addl $-0x2, %eax
movq 0x3d8(%r14), %rcx
leaq 0x4c(%rcx), %rsi
movl $0x1, %edx
leaq (%rax,%rdx), %rdi
cmpq $0x1, %rdi
je 0x5a76
decq %rdx
ucomiss (%rsi), %xmm0
leaq 0x4c(%rsi), %rsi
ja 0x5a5a
negq %rdx
movq %rdx, %rax
imulq $0x4c, %rax, %rdx
xorps %xmm3, %xmm3
xorl %eax, %eax
movss 0xc(%rsp), %xmm4
ucomiss %xmm4, %xmm3
seta %al
leaq (%rcx,%rdx), %rsi
addq $0x7c, %rsi
ucomiss %xmm3, %xmm4
leaq 0x68(%rcx,%rdx), %rdi
cmovaq %rsi, %rdi
leaq 0x30(%rcx,%rdx), %r8
leaq 0x1c(%rcx,%rdx), %r9
cmovaq %r8, %r9
movss -0x7c(%rsi), %xmm3
movss -0x30(%rsi), %xmm4
leaq 0x80(%rcx,%rdx), %rsi
leaq 0x6c(%rcx,%rdx), %r8
cmovaq %rsi, %r8
leaq 0x34(%rcx,%rdx), %rsi
leaq 0x20(%rcx,%rdx), %r10
cmovaq %rsi, %r10
movss (%rdi), %xmm7
movss (%r9), %xmm5
leaq 0x84(%rcx,%rdx), %rsi
leaq 0x70(%rcx,%rdx), %rdi
cmovaq %rsi, %rdi
leaq 0x38(%rcx,%rdx), %rsi
leaq 0x24(%rcx,%rdx), %r9
cmovaq %rsi, %r9
movss (%r8), %xmm10
movss (%r10), %xmm6
movaps %xmm0, %xmm8
subss %xmm3, %xmm8
subss %xmm3, %xmm4
divss %xmm4, %xmm8
subss %xmm5, %xmm7
mulss %xmm8, %xmm7
addss %xmm5, %xmm7
subss %xmm6, %xmm10
mulss %xmm8, %xmm10
addss %xmm6, %xmm10
movss (%rdi), %xmm3
movss (%r9), %xmm5
subss %xmm5, %xmm3
leaq 0x88(%rcx,%rdx), %rsi
leaq 0x74(%rcx,%rdx), %rdi
cmovaq %rsi, %rdi
mulss %xmm8, %xmm3
addss %xmm5, %xmm3
movss (%rdi), %xmm5
leaq 0x3c(%rcx,%rdx), %rsi
leaq 0x28(%rcx,%rdx), %rdi
cmovaq %rsi, %rdi
movss (%rdi), %xmm6
subss %xmm6, %xmm5
mulss %xmm8, %xmm5
leaq 0x8c(%rcx,%rdx), %rsi
leaq 0x78(%rcx,%rdx), %rdi
cmovaq %rsi, %rdi
addss %xmm6, %xmm5
movss (%rdi), %xmm6
leaq 0x40(%rcx,%rdx), %rsi
leaq 0x2c(%rcx,%rdx), %rdi
cmovaq %rsi, %rdi
movss (%rdi), %xmm9
subss %xmm9, %xmm6
mulss %xmm8, %xmm6
leaq 0x5c(%rcx,%rdx), %rdi
leaq 0x50(%rcx,%rdx), %rsi
cmovaq %rdi, %rsi
leaq 0x10(%rcx,%rdx), %rdi
leaq 0x4(%rcx,%rdx), %rcx
cmovaq %rdi, %rcx
addss %xmm9, %xmm6
movaps %xmm2, %xmm11
mulps %xmm2, %xmm11
movaps %xmm1, %xmm9
mulss %xmm1, %xmm9
movaps %xmm1, %xmm8
mulss %xmm9, %xmm8
mulss %xmm1, %xmm8
unpcklps %xmm0, %xmm4            # xmm4 = xmm4[0],xmm0[0],xmm4[1],xmm0[1]
movaps %xmm2, %xmm12
mulps %xmm11, %xmm12
movaps %xmm2, %xmm13
shufps $0x55, %xmm2, %xmm13    # xmm13 = xmm13[1,1],xmm2[1,1]
movaps %xmm2, %xmm0
mulps %xmm12, %xmm0
movaps %xmm0, %xmm14
shufps $0x55, %xmm0, %xmm14    # xmm14 = xmm14[1,1],xmm0[1,1]
mulss %xmm2, %xmm12
addss %xmm14, %xmm12
mulss %xmm13, %xmm14
mulss %xmm13, %xmm14
movaps %xmm2, %xmm15
mulss %xmm0, %xmm15
mulss %xmm2, %xmm15
addss %xmm14, %xmm15
movaps %xmm1, %xmm13
mulss %xmm8, %xmm13
mulss %xmm1, %xmm13
addss %xmm15, %xmm13
addss %xmm8, %xmm12
mulss %xmm12, %xmm10
addss %xmm7, %xmm10
movss (%rsi), %xmm7
subss (%rcx), %xmm7
mulss %xmm13, %xmm3
addss %xmm10, %xmm3
leaq 0x28f0(%rip), %rcx      # 0x8568
unpcklps %xmm3, %xmm7            # xmm7 = xmm7[0],xmm3[0],xmm7[1],xmm3[1]
divps %xmm4, %xmm7
movss (%rcx,%rax,4), %xmm4
movlps %xmm7, 0x159c(%r14)
movaps %xmm4, %xmm7
unpcklps %xmm4, %xmm7            # xmm7 = xmm7[0],xmm4[0],xmm7[1],xmm4[1]
mulps %xmm2, %xmm7
subss %xmm12, %xmm9
shufps $0x0, %xmm12, %xmm12    # xmm12 = xmm12[0,0,0,0]
subps %xmm12, %xmm11
mulss 0x282e(%rip), %xmm5     # 0x84d8
mulss %xmm5, %xmm9
unpcklps %xmm5, %xmm5            # xmm5 = xmm5[0,0,1,1]
mulps %xmm11, %xmm5
addss %xmm3, %xmm9
unpcklps %xmm3, %xmm3            # xmm3 = xmm3[0,0,1,1]
addps %xmm5, %xmm3
subss %xmm13, %xmm8
unpcklps %xmm13, %xmm13          # xmm13 = xmm13[0,0,1,1]
subps %xmm13, %xmm0
mulss 0x280e(%rip), %xmm6     # 0x84e4
mulss %xmm6, %xmm8
unpcklps %xmm6, %xmm6            # xmm6 = xmm6[0,0,1,1]
mulps %xmm0, %xmm6
addps %xmm3, %xmm6
mulps %xmm7, %xmm6
movlps %xmm6, (%rbx)
mulss %xmm1, %xmm4
addss %xmm9, %xmm8
mulss %xmm4, %xmm8
movss %xmm8, 0x8(%rbx)
xorl %eax, %eax
jmp 0x5d07
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x68, %rsp
popq %rbx
popq %r14
retq