// File: /workspace/github_c_makefile_star2_O2/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x60, %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, 0x30(%rsp)
mulss %xmm0, %xmm0
addss %xmm2, %xmm0
sqrtss %xmm0, %xmm2
xorps %xmm0, %xmm0
movaps %xmm2, 0x50(%rsp)
movss %xmm2, %xmm0            # xmm0 = xmm2[0],xmm0[1,2,3]
leaq 0x20(%rsp), %rdi
movaps %xmm0, 0x40(%rsp)
movss %xmm1, 0x8(%rsp)
callq 0x5215
testl %eax, %eax
js 0x59ae
movq 0x20(%rsp), %r12
movslq 0x28(%rsp), %r13
leaq 0x10(%rsp), %r15
movaps 0x40(%rsp), %xmm0
movss 0x8(%rsp), %xmm1
movq %r12, %rdi
movl %r13d, %esi
movq %r15, %rdx
movq %r14, %rcx
callq 0x5546
movslq %r12d, %rax
shlq $0x3, %rax
addq 0x1470(%r14), %rax
imulq $0xc, %r13, %rcx
addq $0x8, %rcx
xorps %xmm1, %xmm1
xorl %edx, %edx
xorps %xmm0, %xmm0
cmpq $0x2, %rdx
je 0x56a9
movq (%rax,%rdx,8), %rsi
addq %rcx, %rsi
xorl %edi, %edi
cmpq $0x2, %rdi
je 0x56a0
movss (%r15,%rdi,4), %xmm3
movss -0x8(%rsi), %xmm2
mulss %xmm3, %xmm2
mulss (%rsi), %xmm3
addss %xmm2, %xmm0
addss %xmm3, %xmm1
incq %rdi
addq $0xc, %rsi
jmp 0x5676
incq %rdx
addq $0x8, %r15
jmp 0x5667
xorps %xmm2, %xmm2
unpcklps %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
leaq 0x10(%rsp), %rdi
callq 0x383c
xorps %xmm15, %xmm15
movaps 0x50(%rsp), %xmm2
cvtss2sd %xmm2, %xmm1
ucomisd 0x2952(%rip), %xmm1     # 0x8020
jbe 0x56e9
movaps 0x10(%rsp), %xmm15
shufps $0x0, %xmm15, %xmm15    # xmm15 = xmm15[0,0,0,0]
mulps 0x30(%rsp), %xmm15
shufps $0x0, %xmm2, %xmm2      # xmm2 = xmm2[0,0,0,0]
divps %xmm2, %xmm15
movss 0x18(%rsp), %xmm1
movl 0x3d0(%r14), %ecx
cmpl $0x3, %ecx
pushq $0x2
popq %rax
cmovgel %ecx, %eax
addl $-0x2, %eax
movq 0x3d8(%r14), %rcx
leaq 0x4c(%rcx), %rsi
pushq $0x1
popq %rdx
leaq (%rax,%rdx), %rdi
cmpq $0x1, %rdi
je 0x572c
decq %rdx
ucomiss (%rsi), %xmm0
leaq 0x4c(%rsi), %rsi
ja 0x5710
negq %rdx
movq %rdx, %rax
imulq $0x4c, %rax, %rdx
xorps %xmm2, %xmm2
xorl %eax, %eax
movss 0xc(%rsp), %xmm3
ucomiss %xmm3, %xmm2
seta %al
leaq (%rcx,%rdx), %rsi
addq $0x7c, %rsi
ucomiss %xmm2, %xmm3
leaq 0x68(%rcx,%rdx), %rdi
cmovaq %rsi, %rdi
leaq 0x30(%rcx,%rdx), %r8
leaq 0x1c(%rcx,%rdx), %r9
cmovaq %r8, %r9
movss -0x7c(%rsi), %xmm2
movss -0x30(%rsi), %xmm3
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), %xmm6
movss (%r9), %xmm4
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), %xmm9
movss (%r10), %xmm5
movaps %xmm0, %xmm7
subss %xmm2, %xmm7
subss %xmm2, %xmm3
divss %xmm3, %xmm7
subss %xmm4, %xmm6
mulss %xmm7, %xmm6
addss %xmm4, %xmm6
subss %xmm5, %xmm9
mulss %xmm7, %xmm9
addss %xmm5, %xmm9
movss (%rdi), %xmm2
movss (%r9), %xmm4
subss %xmm4, %xmm2
leaq 0x88(%rcx,%rdx), %rsi
leaq 0x74(%rcx,%rdx), %rdi
cmovaq %rsi, %rdi
mulss %xmm7, %xmm2
addss %xmm4, %xmm2
movss (%rdi), %xmm4
leaq 0x3c(%rcx,%rdx), %rsi
leaq 0x28(%rcx,%rdx), %rdi
cmovaq %rsi, %rdi
movss (%rdi), %xmm5
subss %xmm5, %xmm4
mulss %xmm7, %xmm4
leaq 0x8c(%rcx,%rdx), %rsi
leaq 0x78(%rcx,%rdx), %rdi
cmovaq %rsi, %rdi
addss %xmm5, %xmm4
movss (%rdi), %xmm5
leaq 0x40(%rcx,%rdx), %rsi
leaq 0x2c(%rcx,%rdx), %rdi
cmovaq %rsi, %rdi
movss (%rdi), %xmm8
subss %xmm8, %xmm5
mulss %xmm7, %xmm5
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 %xmm8, %xmm5
movaps %xmm15, %xmm10
mulps %xmm15, %xmm10
movaps %xmm1, %xmm8
mulss %xmm1, %xmm8
movaps %xmm1, %xmm7
mulss %xmm8, %xmm7
mulss %xmm1, %xmm7
unpcklps %xmm0, %xmm3            # xmm3 = xmm3[0],xmm0[0],xmm3[1],xmm0[1]
movaps %xmm15, %xmm11
mulps %xmm10, %xmm11
movaps %xmm15, %xmm12
shufps $0x55, %xmm15, %xmm12   # xmm12 = xmm12[1,1],xmm15[1,1]
movaps %xmm15, %xmm0
mulps %xmm11, %xmm0
movaps %xmm0, %xmm13
shufps $0x55, %xmm0, %xmm13    # xmm13 = xmm13[1,1],xmm0[1,1]
mulss %xmm15, %xmm11
addss %xmm13, %xmm11
mulss %xmm12, %xmm13
mulss %xmm12, %xmm13
movaps %xmm15, %xmm14
mulss %xmm0, %xmm14
mulss %xmm15, %xmm14
addss %xmm13, %xmm14
movaps %xmm1, %xmm12
mulss %xmm7, %xmm12
mulss %xmm1, %xmm12
addss %xmm14, %xmm12
addss %xmm7, %xmm11
mulss %xmm11, %xmm9
addss %xmm6, %xmm9
movss (%rsi), %xmm6
subss (%rcx), %xmm6
mulss %xmm12, %xmm2
addss %xmm9, %xmm2
leaq 0x2c42(%rip), %rcx      # 0x8568
unpcklps %xmm2, %xmm6            # xmm6 = xmm6[0],xmm2[0],xmm6[1],xmm2[1]
divps %xmm3, %xmm6
movss (%rcx,%rax,4), %xmm3
movlps %xmm6, 0x159c(%r14)
movaps %xmm3, %xmm6
unpcklps %xmm3, %xmm6            # xmm6 = xmm6[0],xmm3[0],xmm6[1],xmm3[1]
mulps %xmm15, %xmm6
subss %xmm11, %xmm8
shufps $0x0, %xmm11, %xmm11    # xmm11 = xmm11[0,0,0,0]
subps %xmm11, %xmm10
mulss 0x2b7f(%rip), %xmm4     # 0x84d8
mulss %xmm4, %xmm8
unpcklps %xmm4, %xmm4            # xmm4 = xmm4[0,0,1,1]
mulps %xmm10, %xmm4
addss %xmm2, %xmm8
unpcklps %xmm2, %xmm2            # xmm2 = xmm2[0,0,1,1]
addps %xmm4, %xmm2
subss %xmm12, %xmm7
unpcklps %xmm12, %xmm12          # xmm12 = xmm12[0,0,1,1]
subps %xmm12, %xmm0
mulss 0x2b5f(%rip), %xmm5     # 0x84e4
mulss %xmm5, %xmm7
unpcklps %xmm5, %xmm5            # xmm5 = xmm5[0,0,1,1]
mulps %xmm0, %xmm5
addps %xmm2, %xmm5
mulps %xmm6, %xmm5
movlps %xmm5, (%rbx)
mulss %xmm1, %xmm3
addss %xmm8, %xmm7
mulss %xmm3, %xmm7
movss %xmm7, 0x8(%rbx)
xorl %eax, %eax
jmp 0x59b1
pushq $-0x1
popq %rax
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq