// File: /workspace/github_c_makefile_star2_O3/radforddc[P]icpc_siggen/calc_signal.c
// Function: get_signal
int get_signal(point pt, float *signal_out, MJD_Siggen_Setup *setup) {
  static float *signal, *sum, *tmp;
  static int tsteps = 0;
  float w, x, y;
  char  tmpstr[MAX_LINE];
  int   j, k, l, dt, err, comp_f;

  /* first time -- allocate signal and sum arrays */
  if (tsteps != setup->time_steps_calc) {
    tsteps = setup->time_steps_calc;
    if ((signal = (float *) malloc(tsteps*sizeof(*signal))) == NULL ||
	(tmp    = (float *) malloc(tsteps*sizeof(*tmp))) == NULL ||
	(sum    = (float *) malloc(tsteps*sizeof(*sum))) == NULL) {
      error("malloc failed in get_signal\n");
      return -1;
    }
  }

  for (j = 0; j < tsteps; j++) signal[j] = 0.0;

  if (outside_detector(pt, setup)) {
    TELL_CHATTY("Point %s is outside detector!\n", pt_to_str(tmpstr, MAX_LINE, pt));
    return -1;
  }
  TELL_CHATTY("Calculating signal for %s...\n", pt_to_str(tmpstr, MAX_LINE, pt));

  memset(setup->dpath_e, 0, tsteps*sizeof(point));
  memset(setup->dpath_h, 0, tsteps*sizeof(point));

  err = make_signal(pt, signal, ELECTRON_CHARGE, setup);
  err = make_signal(pt, signal, HOLE_CHARGE, setup);
  /* make_signal returns 1 for success; require hole signal but not electron */

  /* change from current signal to charge signal, i.e.
     each time step contains the summed signals of all previous time steps */
  for (j = 1; j < tsteps; j++) signal[j] += signal[j-1];

  if (signal_out != NULL) {

    if (setup->charge_cloud_size > 0.001 || setup->use_diffusion) {
      /* convolute with a Gaussian to correct for charge cloud size
	 and initial velocity
	 charge_cloud_size = initial FWHM of charge cloud, in mm,
	 NOTE this uses initial velocity of holes only;
	 this may not be quite right if electron signal is strong */
      /* difference in time between center and edge of charge cloud */
      dt = (int) (1.5f + setup->charge_cloud_size /
		  (setup->step_time_calc * setup->initial_vel));
      if (setup->initial_vel < 0.00001f) dt = 0;
      TELL_CHATTY("Initial vel, size, dt = %f mm/ns, %f mm, %d steps\n",
		  setup->initial_vel, setup->charge_cloud_size, dt);
      if (setup->use_diffusion) {
	dt = (int) (1.5f + setup->final_charge_size /
		    (setup->step_time_calc * setup->final_vel));
	TELL_CHATTY("  Final vel, size, dt = %f mm/ns, %f mm, %d steps\n",
		    setup->final_vel, setup->final_charge_size, dt);
      }
      if (dt > 1) {
	/* Gaussian */
	w = ((float) dt) / 2.355;
	l = dt/10;     // use l to speed up convolution of waveform with gaussian;
	if (l < 1) {   // instead of using every 1-ns step, use steps of FWHM/10
	  l = 1;
	} else if (setup->step_time_out > setup->preamp_tau) {
	  if (l > setup->step_time_out/setup->step_time_calc)
	    l = setup->step_time_out/setup->step_time_calc;
	} else {
	  if (l > setup->preamp_tau/setup->step_time_calc)
	    l = setup->preamp_tau/setup->step_time_calc;
	}
	// TELL_CHATTY(">> l: %d\n", l);
	for (j = 0; j < tsteps; j++) {
	  sum[j] = 1.0;
	  tmp[j] = signal[j];
	}
	for (k = l; k < 2*dt; k+=l) {
	  x = ((float) k)/w;
	  y = exp(-x*x/2.0);
	  for (j = 0; j < tsteps - k; j++){
	    sum[j] += y;
	    tmp[j] += signal[j+k] * y;
	    sum[j+k] += y;
	    tmp[j+k] += signal[j] * y;
	  }
	}
        for (j = 0; j < tsteps; j++){
          signal[j] = tmp[j]/sum[j];
        }
      }
    }

    /* now, compress the signal and place it in the signal_out array;
       truncate the signal if time_steps_calc % ntsteps_out != 0 */
    comp_f = setup->time_steps_calc/setup->ntsteps_out;
    for (j = 0; j < setup->ntsteps_out; j++) signal_out[j] = 0;
    for (j = 0; j < setup->ntsteps_out*comp_f; j++)
      signal_out[j/comp_f] += signal[j]/comp_f;

    /* do RC integration for preamp risetime */
    if (setup->preamp_tau/setup->step_time_out >= 0.1f)
      rc_integrate(signal_out, signal_out,
		   setup->preamp_tau/setup->step_time_out, setup->ntsteps_out);
  }

  /* make_signal returns 1 for success; require hole signal but not electron */
  if (err) return -1;
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x238, %rsp            # imm = 0x238
movq %rsi, %r14
movq %rdi, %rbx
movss %xmm1, 0xc(%rsp)
movaps %xmm0, 0x10(%rsp)
movl 0xcbae(%rip), %ecx      # 0xf210
movl 0x390(%rsi), %ebp
cmpl %ebp, %ecx
je 0x26c3
movslq %ebp, %r15
movl %ebp, 0xcb9b(%rip)      # 0xf210
shlq $0x2, %r15
movq %r15, %rdi
callq 0x20d0
movq %rax, 0xcb70(%rip)      # 0xf1f8
testq %rax, %rax
je 0x2b33
movq %r15, %rdi
callq 0x20d0
movq %rax, 0xcb68(%rip)      # 0xf208
testq %rax, %rax
je 0x2b33
movq %r15, %rdi
callq 0x20d0
movq %rax, 0xcb48(%rip)      # 0xf200
movl %ebp, %ecx
testq %rax, %rax
je 0x2b33
testl %ecx, %ecx
jle 0x26db
movq 0xcb2a(%rip), %rdi      # 0xf1f8
movl %ecx, %edx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x2050
movaps 0x10(%rsp), %xmm0
movss 0xc(%rsp), %xmm1
movq %r14, %rdi
callq 0x3a34
movl (%r14), %ecx
testl %eax, %eax
je 0x2733
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $0x2, %ecx
jl 0x2b1f
leaq 0x30(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
movaps 0x10(%rsp), %xmm0
movss 0xc(%rsp), %xmm1
callq 0x5ff7
leaq 0x5ab2(%rip), %rdi      # 0x81d6
movq %rax, %rsi
xorl %eax, %eax
callq 0x25b2
jmp 0x2b1f
cmpl $0x2, %ecx
jl 0x2763
leaq 0x30(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
movaps 0x10(%rsp), %xmm0
movss 0xc(%rsp), %xmm1
callq 0x5ff7
leaq 0x5a9c(%rip), %rdi      # 0x81f5
movq %rax, %rsi
xorl %eax, %eax
callq 0x25b2
movq 0x1580(%r14), %rdi
movslq 0xca9f(%rip), %rax      # 0xf210
shlq $0x2, %rax
leaq (%rax,%rax,2), %r15
xorl %esi, %esi
movq %r15, %rdx
callq 0x2050
movq 0x1588(%r14), %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x2050
movq 0xca5d(%rip), %rdi      # 0xf1f8
movss 0x5861(%rip), %xmm2     # 0x8004
movaps 0x10(%rsp), %xmm0
movss 0xc(%rsp), %xmm1
movq %r14, %rsi
callq 0x2bab
movq 0xca3b(%rip), %rdi      # 0xf1f8
movss 0x5843(%rip), %xmm2     # 0x8008
movaps 0x10(%rsp), %xmm0
movss 0xc(%rsp), %xmm1
movq %r14, %rsi
callq 0x2bab
movl %eax, %ebp
movslq 0xca2f(%rip), %rax      # 0xf210
cmpq $0x2, %rax
jl 0x2809
movq 0xca0a(%rip), %rcx      # 0xf1f8
movss (%rcx), %xmm0
movl $0x1, %edx
addss (%rcx,%rdx,4), %xmm0
movss %xmm0, (%rcx,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0x27f7
testq %rbx, %rbx
je 0x2b16
movss 0x39c(%r14), %xmm2
xorps %xmm1, %xmm1
cvtss2sd %xmm2, %xmm1
ucomisd 0x57f6(%rip), %xmm1     # 0x8020
ja 0x283a
cmpl $0x0, 0x3a0(%r14)
je 0x2a80
movss 0x1594(%r14), %xmm0
movss 0x394(%r14), %xmm3
mulss %xmm0, %xmm3
divss %xmm3, %xmm2
addss 0x57b0(%rip), %xmm2     # 0x800c
cvttss2si %xmm2, %eax
xorl %r15d, %r15d
movss 0x57a5(%rip), %xmm2     # 0x8010
ucomiss %xmm0, %xmm2
cmovbel %eax, %r15d
cmpl $0x2, (%r14)
jl 0x288d
cvtss2sd %xmm0, %xmm0
leaq 0x5990(%rip), %rdi      # 0x8213
movl %r15d, %esi
movb $0x2, %al
callq 0x25b2
cmpl $0x0, 0x3a0(%r14)
je 0x28ea
movsd 0x15a8(%r14), %xmm1
movss 0x1598(%r14), %xmm0
movss 0x394(%r14), %xmm2
mulss %xmm0, %xmm2
cvtss2sd %xmm2, %xmm2
movapd %xmm1, %xmm3
divsd %xmm2, %xmm3
addsd 0x575e(%rip), %xmm3     # 0x8028
cvttsd2si %xmm3, %r15d
cmpl $0x2, (%r14)
jl 0x28ea
cvtss2sd %xmm0, %xmm0
leaq 0x5966(%rip), %rdi      # 0x8246
movl %r15d, %esi
movb $0x2, %al
callq 0x25b2
cmpl $0x2, %r15d
jl 0x2a80
movl $0x1, %ecx
cmpl $0xa, %r15d
jae 0x2b48
movslq 0xc906(%rip), %rax      # 0xf210
testq %rax, %rax
jle 0x2943
movq 0xc8ea(%rip), %rdx      # 0xf200
movq 0xc8db(%rip), %rsi      # 0xf1f8
movq 0xc8e4(%rip), %rdi      # 0xf208
xorl %r8d, %r8d
movl $0x3f800000, (%rdx,%r8,4) # imm = 0x3F800000
movss (%rsi,%r8,4), %xmm0
movss %xmm0, (%rdi,%r8,4)
incq %r8
cmpq %r8, %rax
jne 0x2927
leal (%r15,%r15), %edx
cmpl %edx, %ecx
jge 0x2a4c
movl %ebp, 0x2c(%rsp)
xorps %xmm0, %xmm0
cvtsi2ss %r15d, %xmm0
cvtss2sd %xmm0, %xmm0
divsd 0x56c9(%rip), %xmm0     # 0x8030
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
movslq %ecx, %r12
movl %edx, %eax
movq %rax, 0x10(%rsp)
leaq (,%r12,4), %rbp
movq %rbp, %r13
movq %r12, %r15
xorps %xmm0, %xmm0
cvtsi2ss %r15d, %xmm0
divss 0xc(%rsp), %xmm0
mulss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
mulsd 0x5691(%rip), %xmm0     # 0x8038
callq 0x2070
movslq 0xc85d(%rip), %rax      # 0xf210
movq %rax, %rcx
subq %r15, %rcx
jle 0x2a37
cvtsd2ss %xmm0, %xmm0
movq 0xc83a(%rip), %rdx      # 0xf200
movq 0xc82b(%rip), %rsi      # 0xf1f8
movq 0xc834(%rip), %rdi      # 0xf208
leaq (%rsi,%r13), %r8
leaq (%rdx,%r13), %r9
leaq (%rdi,%r13), %r10
xorl %r11d, %r11d
movss (%rdx,%r11,4), %xmm1
addss %xmm0, %xmm1
movss %xmm1, (%rdx,%r11,4)
movss (%r8,%r11,4), %xmm1
mulss %xmm0, %xmm1
addss (%rdi,%r11,4), %xmm1
movss %xmm1, (%rdi,%r11,4)
movss (%r9,%r11,4), %xmm1
addss %xmm0, %xmm1
movss %xmm1, (%r9,%r11,4)
movss (%rsi,%r11,4), %xmm1
mulss %xmm0, %xmm1
addss (%r10,%r11,4), %xmm1
movss %xmm1, (%r10,%r11,4)
incq %r11
cmpq %rcx, %r11
jl 0x29e3
addq %r12, %r15
addq %rbp, %r13
cmpq 0x10(%rsp), %r15
jl 0x2989
movl 0x2c(%rsp), %ebp
testl %eax, %eax
jle 0x2a80
movq 0xc7b1(%rip), %rcx      # 0xf208
movq 0xc7a2(%rip), %rdx      # 0xf200
movq 0xc793(%rip), %rsi      # 0xf1f8
movl %eax, %eax
xorl %edi, %edi
movss (%rcx,%rdi,4), %xmm0
divss (%rdx,%rdi,4), %xmm0
movss %xmm0, (%rsi,%rdi,4)
incq %rdi
cmpq %rdi, %rax
jne 0x2a69
movl 0x390(%r14), %eax
movslq 0x3ac(%r14), %r15
cltd
idivl %r15d
movl %eax, %r12d
testq %r15, %r15
jle 0x2aac
leaq (,%r15,4), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2050
movl %r12d, %eax
imull %r15d, %eax
testl %eax, %eax
jle 0x2aed
xorps %xmm0, %xmm0
cvtsi2ss %r12d, %xmm0
movq 0xc732(%rip), %rsi      # 0xf1f8
movl %eax, %edi
xorl %ecx, %ecx
movss (%rsi,%rcx,4), %xmm1
divss %xmm0, %xmm1
movl %ecx, %eax
cltd
idivl %r12d
cltq
addss (%rbx,%rax,4), %xmm1
movss %xmm1, (%rbx,%rax,4)
incq %rcx
cmpq %rcx, %rdi
jne 0x2aca
movss 0x38c(%r14), %xmm0
divss 0x398(%r14), %xmm0
ucomiss 0x550e(%rip), %xmm0     # 0x8014
jb 0x2b16
movq %rbx, %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x3719
xorl %ebx, %ebx
negl %ebp
sbbl %ebx, %ebx
orl $0x1, %ebx
movl %ebx, %eax
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x567f(%rip), %rdi      # 0x81b9
xorl %eax, %eax
callq 0x22e0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x2b1f
movl %r15d, %eax
movl $0xcccccccd, %ecx       # imm = 0xCCCCCCCD
imulq %rax, %rcx
shrq $0x23, %rcx
movss 0x398(%r14), %xmm0
movss 0x38c(%r14), %xmm1
ucomiss %xmm1, %xmm0
movss 0x394(%r14), %xmm3
xorps %xmm2, %xmm2
cvtsi2ss %ecx, %xmm2
jbe 0x2b95
divss %xmm3, %xmm0
ucomiss %xmm0, %xmm2
jbe 0x2903
cvttss2si %xmm0, %ecx
jmp 0x2903
divss %xmm3, %xmm1
ucomiss %xmm1, %xmm2
jbe 0x2903
cvttss2si %xmm1, %ecx
jmp 0x2903