// File: /workspace/github_cpp_makefile_star2_O2/jkingan[P]ladyheather/heathmsc.cpp
// Function: add_pps_adev_point(double, int)
void add_pps_adev_point(double val, int phase)
{
   if(pps_adev_period <= 0.0) return;
   if(adev_q_allocated == 0) {
      alloc_adev();
   }

if(rcvr_type == BRANDY_RCVR) phase = 1;  // receiver outputs phase instead of time for the pps_offset
if(rcvr_type == RFTG_RCVR) phase = 1;
   if(phase) pps_phase = val;
   else      pps_phase += val;  // convert time interval to phase

   if(pps_adev_q_count == 0) {
      pps_resid = 0.0;
      pps_adev_jd0 = jd_utc;
   }
   else pps_resid = (pps_phase - last_pps_resid);
   last_pps_resid = pps_phase;

   calc_resids(PPS, pps_phase, pps_adev_period);

//sprintf(debug_text, "phase:%.12f  last:%.12f  diff:%.12f %.12f", pps_phase,last_pps_phase, (pps_phase-last_pps_phase), (pps_phase-last_pps_phase)/1.0E9);

   if((have_pps_base == 0) && (subtract_base_value == 1) && (pps_adev_q_count <= 1)) {  // aaaaa
      pps_base_value = pps_phase;
      have_pps_base = 1;
   }

   if(pps_adev_q) {
      pps_adev_q[pps_adev_q_in] = (OFS_SIZE) (pps_phase - pps_base_value);
   }

   if(++pps_adev_q_in >= adev_q_size) {  // queue has wrapped
      pps_adev_q_in = 0;
   }

   if(pps_adev_q_in == pps_adev_q_out) {  // queue is full
      ++pps_adev_q_out;           // drop oldest entry from the queue
      pps_adev_q_overflow += 1.0;
      incr_pps_overflow();        // tweek counts in the adev bins

      // Once the adev queue fills up,  the adev results begin to get stale
      // because the incremental adevs are based upon all the points seen in
      // the past.  To keep the adev numbers fresh, we preiodically reset
      // the incremental adev bins.  This causes the adevs to be
      // recalculated from just the values stored in the queue.
      if(pps_adev_q_overflow >= adev_q_size) {  // !!!! wrap >= was >0
         if(keep_adevs_fresh) {
            reset_pps_bins();              
            adev_freshened = 1;
         }
      }
   }
   else ++pps_adev_q_count;   // keep count of number of entries in the adev queue
   if(pps_adev_q_out >= adev_q_size) pps_adev_q_out = 0;

   // incrementally update the adev bin values with the new data point
   do_incr_pps_adevs();    // recalculate all the adevs from the queued data

   if(adev_freshened) {
      show_adev_info(1);
      adev_freshened = 0;
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movapd %xmm0, %xmm1
leaq 0x13ca06(%rip), %r15    # 0x226d00
xorpd %xmm0, %xmm0
ucomisd (%r15), %xmm0
jae 0xea4d9
movl %edi, %ebx
leaq 0x13c9ce(%rip), %rax    # 0x226ce0
cmpl $0x0, (%rax)
jne 0xea326
movsd %xmm1, -0x20(%rbp)
callq 0x10118
movsd -0x20(%rbp), %xmm1
leaq 0x1185a3(%rip), %rax    # 0x2028d0
movq (%rax), %rax
leaq 0x20c4c1(%rip), %r14    # 0x2f67f8
cmpq $0x40000, %rax          # imm = 0x40000
jne 0xea345
movapd %xmm1, %xmm0
jmp 0xea34e
movsd (%r14), %xmm0
addsd %xmm1, %xmm0
cmpq $0x400000, %rax         # imm = 0x400000
jne 0xea35a
movapd %xmm1, %xmm0
testl %ebx, %ebx
je 0xea362
movapd %xmm1, %xmm0
movsd %xmm0, (%r14)
leaq 0x13c7ca(%rip), %rbx    # 0x226b38
cmpq $0x0, (%rbx)
je 0xea385
leaq 0x13c7e5(%rip), %rax    # 0x226b60
movapd %xmm0, %xmm1
subsd (%rax), %xmm1
jmp 0xea39f
leaq 0x20d1a4(%rip), %rax    # 0x2f7530
movsd (%rax), %xmm1
leaq 0x13c901(%rip), %rax    # 0x226c98
movsd %xmm1, (%rax)
xorpd %xmm1, %xmm1
leaq 0x13c7b2(%rip), %rax    # 0x226b58
movsd %xmm1, (%rax)
leaq 0x13c7af(%rip), %rax    # 0x226b60
movsd %xmm0, (%rax)
movsd (%r15), %xmm1
xorl %edi, %edi
callq 0xea263
leaq 0x123108(%rip), %rcx    # 0x20d4d0
cmpl $0x0, (%rcx)
sete %al
leaq 0x12310b(%rip), %rdx    # 0x20d4e0
cmpb $0x1, (%rdx)
sete %dl
andb %al, %dl
movq (%rbx), %rax
cmpq $0x2, %rax
setl %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0xea407
movsd (%r14), %xmm0
leaq 0x1230b3(%rip), %rdx    # 0x20d4b0
movsd %xmm0, (%rdx)
movl $0x1, (%rcx)
leaq 0x13c712(%rip), %rcx    # 0x226b20
movq (%rcx), %rcx
leaq 0x13c710(%rip), %rdx    # 0x226b28
movq (%rdx), %rsi
testq %rcx, %rcx
je 0xea435
movsd (%r14), %xmm0
leaq 0x123084(%rip), %rdi    # 0x20d4b0
subsd (%rdi), %xmm0
movsd %xmm0, (%rcx,%rsi,8)
incq %rsi
leaq 0x13c899(%rip), %r15    # 0x226cd8
movq (%r15), %rcx
xorl %edi, %edi
cmpq %rcx, %rsi
cmovlq %rsi, %rdi
movq %rdi, (%rdx)
leaq 0x13c6db(%rip), %r14    # 0x226b30
cmpq (%r14), %rdi
jne 0xea4b1
incq %rdi
movq %rdi, (%r14)
leaq 0x13c6d9(%rip), %rbx    # 0x226b40
movsd (%rbx), %xmm0
addsd 0x8bfd(%rip), %xmm0     # 0xf3070
movsd %xmm0, (%rbx)
callq 0xea06f
movsd (%rbx), %xmm0
movq (%r15), %rcx
cvtsi2sd %rcx, %xmm1
ucomisd %xmm1, %xmm0
jb 0xea4b7
leaq 0x13c8a7(%rip), %rax    # 0x226d3c
cmpb $0x0, (%rax)
je 0xea4b7
callq 0xe8b01
leaq 0x13c7d2(%rip), %rax    # 0x226c78
movl $0x1, (%rax)
movq (%r15), %rcx
jmp 0xea4b7
incq %rax
movq %rax, (%rbx)
cmpq %rcx, (%r14)
jl 0xea4c0
andq $0x0, (%r14)
callq 0xe95ae
leaq 0x13c7ac(%rip), %rbx    # 0x226c78
cmpl $0x0, (%rbx)
je 0xea4d9
callq 0xea4e4
andl $0x0, (%rbx)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq