// File: 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
subq $0x10, %rsp
movsd %xmm0, -0x8(%rbp)
movl %edi, -0xc(%rbp)
xorps %xmm0, %xmm0
leaq 0x13a986(%rip), %rax    # 0x29bee0
ucomisd (%rax), %xmm0
jb 0x161565
jmp 0x16180b
leaq 0x13a954(%rip), %rax    # 0x29bec0
cmpl $0x0, (%rax)
jne 0x161576
callq 0x14040
leaq 0x116533(%rip), %rax    # 0x277ab0
cmpq $0x40000, (%rax)        # imm = 0x40000
jne 0x16158d
movl $0x1, -0xc(%rbp)
leaq 0x11651c(%rip), %rax    # 0x277ab0
cmpq $0x400000, (%rax)       # imm = 0x400000
jne 0x1615a4
movl $0x1, -0xc(%rbp)
cmpl $0x0, -0xc(%rbp)
je 0x1615bc
movsd -0x8(%rbp), %xmm0
leaq 0x20a422(%rip), %rax    # 0x36b9d8
movsd %xmm0, (%rax)
jmp 0x1615d7
movsd -0x8(%rbp), %xmm0
leaq 0x20a410(%rip), %rax    # 0x36b9d8
addsd (%rax), %xmm0
leaq 0x20a405(%rip), %rax    # 0x36b9d8
movsd %xmm0, (%rax)
leaq 0x13a73a(%rip), %rax    # 0x29bd18
cmpq $0x0, (%rax)
jne 0x16160a
leaq 0x13a74d(%rip), %rax    # 0x29bd38
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
leaq 0x20b117(%rip), %rax    # 0x36c710
movsd (%rax), %xmm0
leaq 0x13a874(%rip), %rax    # 0x29be78
movsd %xmm0, (%rax)
jmp 0x16162b
leaq 0x20a3c7(%rip), %rax    # 0x36b9d8
movsd (%rax), %xmm0
leaq 0x13a724(%rip), %rax    # 0x29bd40
subsd (%rax), %xmm0
leaq 0x13a711(%rip), %rax    # 0x29bd38
movsd %xmm0, (%rax)
leaq 0x20a3a6(%rip), %rax    # 0x36b9d8
movsd (%rax), %xmm0
leaq 0x13a703(%rip), %rax    # 0x29bd40
movsd %xmm0, (%rax)
leaq 0x20a390(%rip), %rax    # 0x36b9d8
movsd (%rax), %xmm0
leaq 0x13a88d(%rip), %rax    # 0x29bee0
movsd (%rax), %xmm1
xorl %edi, %edi
callq 0x1613f0
leaq 0x12104b(%rip), %rax    # 0x2826b0
cmpl $0x0, (%rax)
jne 0x1616a9
leaq 0x12104f(%rip), %rax    # 0x2826c0
movzbl (%rax), %eax
cmpl $0x1, %eax
jne 0x1616a9
leaq 0x13a698(%rip), %rax    # 0x29bd18
cmpq $0x1, (%rax)
jg 0x1616a9
leaq 0x20a34b(%rip), %rax    # 0x36b9d8
movsd (%rax), %xmm0
leaq 0x120ff8(%rip), %rax    # 0x282690
movsd %xmm0, (%rax)
leaq 0x12100d(%rip), %rax    # 0x2826b0
movl $0x1, (%rax)
leaq 0x13a650(%rip), %rax    # 0x29bd00
cmpq $0x0, (%rax)
je 0x1616e5
leaq 0x20a31b(%rip), %rax    # 0x36b9d8
movsd (%rax), %xmm0
leaq 0x120fc8(%rip), %rax    # 0x282690
subsd (%rax), %xmm0
leaq 0x13a62d(%rip), %rax    # 0x29bd00
movq (%rax), %rax
leaq 0x13a62b(%rip), %rcx    # 0x29bd08
movq (%rcx), %rcx
movsd %xmm0, (%rax,%rcx,8)
leaq 0x13a61c(%rip), %rax    # 0x29bd08
movq (%rax), %rax
addq $0x1, %rax
leaq 0x13a60e(%rip), %rcx    # 0x29bd08
movq %rax, (%rcx)
leaq 0x13a7b4(%rip), %rcx    # 0x29beb8
cmpq (%rcx), %rax
jl 0x161717
leaq 0x13a5f8(%rip), %rax    # 0x29bd08
movq $0x0, (%rax)
leaq 0x13a5ea(%rip), %rax    # 0x29bd08
movq (%rax), %rax
leaq 0x13a5e8(%rip), %rcx    # 0x29bd10
cmpq (%rcx), %rax
jne 0x1617a7
leaq 0x13a5dc(%rip), %rax    # 0x29bd10
movq (%rax), %rcx
addq $0x1, %rcx
leaq 0x13a5ce(%rip), %rax    # 0x29bd10
movq %rcx, (%rax)
movsd 0xd91b(%rip), %xmm0     # 0x16f068
leaq 0x13a5cc(%rip), %rax    # 0x29bd20
addsd (%rax), %xmm0
leaq 0x13a5c1(%rip), %rax    # 0x29bd20
movsd %xmm0, (%rax)
callq 0x160f30
leaq 0x13a5b1(%rip), %rax    # 0x29bd20
movsd (%rax), %xmm0
leaq 0x13a73e(%rip), %rax    # 0x29beb8
cvtsi2sdq (%rax), %xmm1
ucomisd %xmm1, %xmm0
jb 0x1617a5
leaq 0x13a790(%rip), %rax    # 0x29bf1c
cmpb $0x0, (%rax)
je 0x1617a3
callq 0x15ec80
leaq 0x13a6bb(%rip), %rax    # 0x29be58
movl $0x1, (%rax)
jmp 0x1617a5
jmp 0x1617bf
leaq 0x13a56a(%rip), %rax    # 0x29bd18
movq (%rax), %rcx
addq $0x1, %rcx
leaq 0x13a55c(%rip), %rax    # 0x29bd18
movq %rcx, (%rax)
leaq 0x13a54a(%rip), %rax    # 0x29bd10
movq (%rax), %rax
leaq 0x13a6e8(%rip), %rcx    # 0x29beb8
cmpq (%rcx), %rax
jl 0x1617e3
leaq 0x13a534(%rip), %rax    # 0x29bd10
movq $0x0, (%rax)
callq 0x15ff00
leaq 0x13a669(%rip), %rax    # 0x29be58
cmpl $0x0, (%rax)
je 0x16180b
movl $0x1, %edi
callq 0x161820
leaq 0x13a653(%rip), %rax    # 0x29be58
movl $0x0, (%rax)
addq $0x10, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)