// File: /workspace/github_c_makefile_star2_O1/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 0x390(%rsi), %eax
cmpl %eax, 0xcba8(%rip)      # 0xf210
je 0x26c0
movslq %eax, %r15
movl %r15d, 0xcb9c(%rip)     # 0xf210
shlq $0x2, %r15
movq %r15, %rdi
callq 0x20d0
movq %rax, 0xcb71(%rip)      # 0xf1f8
testq %rax, %rax
je 0x2b39
movq %r15, %rdi
callq 0x20d0
movq %rax, 0xcb69(%rip)      # 0xf208
testq %rax, %rax
je 0x2b39
movq %r15, %rdi
callq 0x20d0
movq %rax, 0xcb49(%rip)      # 0xf200
testq %rax, %rax
je 0x2b39
movslq 0xcb49(%rip), %rdx      # 0xf210
testq %rdx, %rdx
jle 0x26de
movq 0xcb25(%rip), %rdi      # 0xf1f8
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x2050
movaps 0x10(%rsp), %xmm0
movss 0xc(%rsp), %xmm1
movq %r14, %rdi
callq 0x3b04
movl (%r14), %ecx
testl %eax, %eax
je 0x2736
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $0x2, %ecx
jl 0x2b25
leaq 0x30(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
movaps 0x10(%rsp), %xmm0
movss 0xc(%rsp), %xmm1
callq 0x6378
leaq 0x5aaf(%rip), %rdi      # 0x81d6
movq %rax, %rsi
xorl %eax, %eax
callq 0x25b2
jmp 0x2b25
cmpl $0x2, %ecx
jl 0x2766
leaq 0x30(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
movaps 0x10(%rsp), %xmm0
movss 0xc(%rsp), %xmm1
callq 0x6378
leaq 0x5a99(%rip), %rdi      # 0x81f5
movq %rax, %rsi
xorl %eax, %eax
callq 0x25b2
movq 0x1580(%r14), %rdi
movslq 0xca9c(%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 0xca5a(%rip), %rdi      # 0xf1f8
movss 0x585e(%rip), %xmm2     # 0x8004
movaps 0x10(%rsp), %xmm0
movss 0xc(%rsp), %xmm1
movq %r14, %rsi
callq 0x2bb1
movq 0xca38(%rip), %rdi      # 0xf1f8
movss 0x5840(%rip), %xmm2     # 0x8008
movaps 0x10(%rsp), %xmm0
movss 0xc(%rsp), %xmm1
movq %r14, %rsi
callq 0x2bb1
movl %eax, %ebp
movslq 0xca2c(%rip), %rax      # 0xf210
cmpq $0x2, %rax
jl 0x280c
movq 0xca07(%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 0x27fa
testq %rbx, %rbx
je 0x2b1c
movss 0x39c(%r14), %xmm2
xorps %xmm1, %xmm1
cvtss2sd %xmm2, %xmm1
ucomisd 0x57f3(%rip), %xmm1     # 0x8020
ja 0x283d
cmpl $0x0, 0x3a0(%r14)
je 0x2a86
movss 0x1594(%r14), %xmm0
movss 0x394(%r14), %xmm3
mulss %xmm0, %xmm3
divss %xmm3, %xmm2
addss 0x57ad(%rip), %xmm2     # 0x800c
cvttss2si %xmm2, %eax
xorl %r15d, %r15d
movss 0x57a2(%rip), %xmm2     # 0x8010
ucomiss %xmm0, %xmm2
cmovbel %eax, %r15d
cmpl $0x2, (%r14)
jl 0x2890
cvtss2sd %xmm0, %xmm0
leaq 0x598d(%rip), %rdi      # 0x8213
movl %r15d, %esi
movb $0x2, %al
callq 0x25b2
cmpl $0x0, 0x3a0(%r14)
je 0x28ed
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 0x575b(%rip), %xmm3     # 0x8028
cvttsd2si %xmm3, %r15d
cmpl $0x2, (%r14)
jl 0x28ed
cvtss2sd %xmm0, %xmm0
leaq 0x5963(%rip), %rdi      # 0x8246
movl %r15d, %esi
movb $0x2, %al
callq 0x25b2
cmpl $0x2, %r15d
jl 0x2a86
movl $0x1, %eax
cmpl $0xa, %r15d
jae 0x2b4e
movl %ebp, 0x2c(%rsp)
movslq 0xc8ff(%rip), %rcx      # 0xf210
testq %rcx, %rcx
jle 0x294a
movq 0xc8e3(%rip), %rdx      # 0xf200
movq 0xc8d4(%rip), %rsi      # 0xf1f8
movq 0xc8dd(%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, %rcx
jne 0x292e
leal (%r15,%r15), %ecx
cmpl %ecx, %eax
jge 0x2a48
xorps %xmm0, %xmm0
cvtsi2ss %r15d, %xmm0
cvtss2sd %xmm0, %xmm0
divsd 0x56c6(%rip), %xmm0     # 0x8030
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
movslq %eax, %r12
movl %ecx, %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 0x568e(%rip), %xmm0     # 0x8038
callq 0x2070
movslq 0xc85a(%rip), %rax      # 0xf210
subq %r15, %rax
jle 0x2a37
cvtsd2ss %xmm0, %xmm0
movq 0xc83a(%rip), %rcx      # 0xf200
movq 0xc82b(%rip), %rdx      # 0xf1f8
movq 0xc834(%rip), %rsi      # 0xf208
leaq (%rdx,%r13), %rdi
leaq (%rcx,%r13), %r8
leaq (%rsi,%r13), %r9
xorl %r10d, %r10d
movss (%rcx,%r10,4), %xmm1
addss %xmm0, %xmm1
movss %xmm1, (%rcx,%r10,4)
movss (%rdi,%r10,4), %xmm1
mulss %xmm0, %xmm1
addss (%rsi,%r10,4), %xmm1
movss %xmm1, (%rsi,%r10,4)
movss (%r8,%r10,4), %xmm1
addss %xmm0, %xmm1
movss %xmm1, (%r8,%r10,4)
movss (%rdx,%r10,4), %xmm1
mulss %xmm0, %xmm1
addss (%r9,%r10,4), %xmm1
movss %xmm1, (%r9,%r10,4)
incq %r10
cmpq %rax, %r10
jl 0x29e3
addq %r12, %r15
addq %rbp, %r13
cmpq 0x10(%rsp), %r15
jl 0x298c
movslq 0xc7c1(%rip), %rax      # 0xf210
testq %rax, %rax
movl 0x2c(%rsp), %ebp
jle 0x2a86
movq 0xc7a9(%rip), %rcx      # 0xf208
movq 0xc79a(%rip), %rdx      # 0xf200
movq 0xc78b(%rip), %rsi      # 0xf1f8
xorl %edi, %edi
movss (%rcx,%rdi,4), %xmm0
divss (%rdx,%rdi,4), %xmm0
movss %xmm0, (%rsi,%rdi,4)
incq %rdi
cmpq %rdi, %rax
jne 0x2a6f
movl 0x390(%r14), %eax
movslq 0x3ac(%r14), %r15
cltd
idivl %r15d
movl %eax, %r12d
testq %r15, %r15
jle 0x2ab2
leaq (,%r15,4), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2050
movl %r12d, %eax
imull %r15d, %eax
testl %eax, %eax
jle 0x2af3
xorps %xmm0, %xmm0
cvtsi2ss %r12d, %xmm0
movq 0xc72c(%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 0x2ad0
movss 0x38c(%r14), %xmm0
divss 0x398(%r14), %xmm0
ucomiss 0x5508(%rip), %xmm0     # 0x8014
jb 0x2b1c
movq %rbx, %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x3758
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 0x5679(%rip), %rdi      # 0x81b9
xorl %eax, %eax
callq 0x22e0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x2b25
movl %r15d, %ecx
movl $0xcccccccd, %eax       # imm = 0xCCCCCCCD
imulq %rcx, %rax
shrq $0x23, %rax
movss 0x398(%r14), %xmm0
movss 0x38c(%r14), %xmm1
ucomiss %xmm1, %xmm0
movss 0x394(%r14), %xmm3
xorps %xmm2, %xmm2
cvtsi2ss %eax, %xmm2
jbe 0x2b9b
divss %xmm3, %xmm0
ucomiss %xmm0, %xmm2
jbe 0x2906
cvttss2si %xmm0, %eax
jmp 0x2906
divss %xmm3, %xmm1
ucomiss %xmm1, %xmm2
jbe 0x2906
cvttss2si %xmm1, %eax
jmp 0x2906