// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movaps %xmm0, %xmm4
movaps %xmm0, %xmm3
shufps $0x55, %xmm0, %xmm3     # xmm3 = xmm3[1,1],xmm0[1,1]
movaps %xmm3, %xmm5
mulss %xmm3, %xmm5
mulss %xmm0, %xmm0
addss %xmm5, %xmm0
xorps %xmm5, %xmm5
ucomiss %xmm5, %xmm0
movss %xmm2, 0xc(%rsp)
movaps %xmm3, 0x50(%rsp)
movaps %xmm4, 0x40(%rsp)
movss %xmm1, 0x8(%rsp)
jb 0x5b63
xorps %xmm2, %xmm2
sqrtss %xmm0, %xmm2
jmp 0x5b71
callq 0x22c0
movss 0x8(%rsp), %xmm1
movaps %xmm0, %xmm2
movaps %xmm2, 0x10(%rsp)
xorps %xmm0, %xmm0
movss %xmm2, %xmm0            # xmm0 = xmm2[0],xmm0[1,2,3]
leaq 0x30(%rsp), %rdi
movaps %xmm0, 0x60(%rsp)
movq %r14, %rsi
callq 0x5814
testl %eax, %eax
js 0x5d42
movaps 0x10(%rsp), %xmm0
subss 0x3b0(%r14), %xmm0
divss 0x3b8(%r14), %xmm0
movq 0x30(%rsp), %rcx
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
movss 0x8(%rsp), %xmm6
subss 0x3bc(%r14), %xmm6
divss 0x3c4(%r14), %xmm6
movslq 0x38(%rsp), %rdx
xorps %xmm2, %xmm2
cvtsi2ss %edx, %xmm2
subss %xmm1, %xmm0
subss %xmm2, %xmm6
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
movsd 0x24a3(%rip), %xmm2     # 0x8098
movapd %xmm2, %xmm3
subsd %xmm1, %xmm3
xorps %xmm4, %xmm4
cvtss2sd %xmm6, %xmm4
subsd %xmm4, %xmm2
movapd %xmm3, %xmm5
mulsd %xmm2, %xmm5
cvtsd2ss %xmm5, %xmm5
leaq 0x20(%rsp), %rax
mulsd %xmm4, %xmm3
cvtsd2ss %xmm3, %xmm3
movss %xmm5, (%rax)
mulsd %xmm1, %xmm2
xorps %xmm1, %xmm1
cvtsd2ss %xmm2, %xmm1
movss %xmm3, 0x4(%rax)
movss %xmm1, 0x8(%rax)
mulss %xmm0, %xmm6
movss %xmm6, 0xc(%rax)
movslq %ecx, %rcx
shlq $0x3, %rcx
addq 0x1470(%r14), %rcx
leaq (%rdx,%rdx,2), %rdx
leaq 0x8(,%rdx,4), %rdx
xorps %xmm1, %xmm1
xorl %esi, %esi
xorps %xmm4, %xmm4
movq (%rcx,%rsi,8), %rdi
addq %rdx, %rdi
xorl %r8d, %r8d
movaps %xmm4, %xmm0
movss (%rax,%r8,4), %xmm3
movss -0x8(%rdi), %xmm2
mulss %xmm3, %xmm2
addss %xmm2, %xmm4
mulss (%rdi), %xmm3
addss %xmm3, %xmm1
incq %r8
addq $0xc, %rdi
cmpq $0x1, %r8
je 0x5c6f
leaq 0x1(%rsi), %rdi
addq $0x8, %rax
testq %rsi, %rsi
movq %rdi, %rsi
je 0x5c65
addss %xmm0, %xmm2
movaps 0x60(%rsp), %xmm0
movss %xmm2, %xmm0            # xmm0 = xmm2[0],xmm0[1,2,3]
leaq 0x20(%rsp), %rdi
callq 0x3a9c
movaps 0x10(%rsp), %xmm4
xorps %xmm1, %xmm1
cvtss2sd %xmm4, %xmm1
xorps %xmm2, %xmm2
ucomisd 0x2348(%rip), %xmm1     # 0x8020
xorps %xmm1, %xmm1
jbe 0x5d00
movss 0x20(%rsp), %xmm1
movaps 0x40(%rsp), %xmm2
mulss %xmm1, %xmm2
divss %xmm4, %xmm2
movaps 0x50(%rsp), %xmm3
mulss %xmm1, %xmm3
divss %xmm4, %xmm3
movaps %xmm3, %xmm1
movss 0x28(%rsp), %xmm15
movl 0x3d0(%r14), %ecx
cmpl $0x3, %ecx
movl $0x2, %eax
cmovgel %ecx, %eax
addl $-0x2, %eax
imulq $0x4c, %rax, %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x5d4c
movq 0x3d8(%r14), %rdi
incl %edx
ucomiss 0x4c(%rdi,%rsi), %xmm0
leaq 0x4c(%rsi), %rsi
ja 0x5d27
jmp 0x5d4e
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x601b
movl %eax, %edx
movq 0x3d8(%r14), %rcx
movl %edx, %eax
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), %xmm4
movss -0x30(%rsi), %xmm9
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), %xmm8
movss (%r10), %xmm6
movaps %xmm0, %xmm3
subss %xmm4, %xmm3
subss %xmm4, %xmm9
divss %xmm9, %xmm3
subss %xmm5, %xmm7
mulss %xmm3, %xmm7
addss %xmm5, %xmm7
subss %xmm6, %xmm8
mulss %xmm3, %xmm8
addss %xmm6, %xmm8
movss (%rdi), %xmm4
movss (%r9), %xmm5
subss %xmm5, %xmm4
leaq 0x88(%rcx,%rdx), %rsi
leaq 0x74(%rcx,%rdx), %rdi
cmovaq %rsi, %rdi
mulss %xmm3, %xmm4
addss %xmm5, %xmm4
movss (%rdi), %xmm6
leaq 0x3c(%rcx,%rdx), %rsi
leaq 0x28(%rcx,%rdx), %rdi
cmovaq %rsi, %rdi
movss (%rdi), %xmm5
subss %xmm5, %xmm6
mulss %xmm3, %xmm6
leaq 0x8c(%rcx,%rdx), %rsi
leaq 0x78(%rcx,%rdx), %rdi
cmovaq %rsi, %rdi
addss %xmm5, %xmm6
movss (%rdi), %xmm5
leaq 0x40(%rcx,%rdx), %rsi
leaq 0x2c(%rcx,%rdx), %rdi
cmovaq %rsi, %rdi
movss (%rdi), %xmm10
subss %xmm10, %xmm5
mulss %xmm3, %xmm5
leaq 0x5c(%rcx,%rdx), %rsi
leaq 0x50(%rcx,%rdx), %rdi
cmovaq %rsi, %rdi
leaq 0x10(%rcx,%rdx), %rsi
leaq 0x4(%rcx,%rdx), %rcx
cmovaq %rsi, %rcx
addss %xmm10, %xmm5
movss (%rdi), %xmm3
subss (%rcx), %xmm3
divss %xmm9, %xmm3
movss %xmm3, 0x159c(%r14)
movaps %xmm2, %xmm13
mulss %xmm2, %xmm13
movaps %xmm2, %xmm12
mulss %xmm13, %xmm12
movaps %xmm1, %xmm10
mulss %xmm1, %xmm10
movaps %xmm1, %xmm9
mulss %xmm10, %xmm9
mulss %xmm1, %xmm9
mulss %xmm2, %xmm12
movaps %xmm15, %xmm11
mulss %xmm15, %xmm11
movaps %xmm2, %xmm3
mulss %xmm12, %xmm3
movss %xmm1, 0x10(%rsp)
movaps %xmm1, %xmm14
mulss %xmm9, %xmm14
mulss %xmm1, %xmm14
mulss %xmm2, %xmm3
addss %xmm14, %xmm3
movaps %xmm15, %xmm14
mulss %xmm11, %xmm14
mulss %xmm15, %xmm14
movaps %xmm15, %xmm1
mulss %xmm14, %xmm1
mulss %xmm15, %xmm1
addss %xmm3, %xmm1
movaps %xmm12, %xmm3
addss %xmm9, %xmm3
addss %xmm14, %xmm3
mulss %xmm3, %xmm8
addss %xmm7, %xmm8
mulss %xmm1, %xmm4
addss %xmm8, %xmm4
subss %xmm3, %xmm13
subss %xmm3, %xmm10
subss %xmm3, %xmm11
mulss 0x255e(%rip), %xmm6     # 0x84d8
mulss %xmm6, %xmm13
mulss %xmm6, %xmm10
mulss %xmm6, %xmm11
addss %xmm4, %xmm13
addss %xmm4, %xmm10
addss %xmm4, %xmm11
divss %xmm0, %xmm4
movss %xmm4, 0x15a0(%r14)
leaq 0x2574(%rip), %rcx      # 0x8520
mulss 0x2534(%rip), %xmm5     # 0x84e8
subss %xmm1, %xmm12
mulss %xmm5, %xmm12
addss %xmm13, %xmm12
movss (%rcx,%rax,4), %xmm0
mulss %xmm0, %xmm2
mulss %xmm2, %xmm12
movss %xmm12, (%rbx)
subss %xmm1, %xmm9
mulss %xmm5, %xmm9
addss %xmm10, %xmm9
movss 0x10(%rsp), %xmm2
mulss %xmm0, %xmm2
mulss %xmm2, %xmm9
movss %xmm9, 0x4(%rbx)
mulss %xmm15, %xmm0
subss %xmm1, %xmm14
mulss %xmm5, %xmm14
addss %xmm11, %xmm14
mulss %xmm0, %xmm14
movss %xmm14, 0x8(%rbx)
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r14
retq