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

   if(phase) chd_phase = val;
   else      chd_phase += val;  // convert time interval to phase

   if(chd_adev_q_count == 0) {
      chd_resid = 0.0;
      chd_adev_jd0 = jd_utc;
   }
   else chd_resid = (float) ((val-last_chd_resid));
   last_chd_resid = val;

   calc_resids(CHD, chd_phase, chd_adev_period);

   if((have_chd_base == 0) && (subtract_base_value == 1) && (chd_adev_q_count <= 1)) {  // aaaaa
      chd_base_value = chd_phase;
      have_chd_base = 1;
   }

   if(chd_adev_q) chd_adev_q[chd_adev_q_in] = (OFS_SIZE) (chd_phase - chd_base_value);

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

   if(chd_adev_q_in == chd_adev_q_out) {  // queue is full
      ++chd_adev_q_out;               // drop oldest entry from the queue
      chd_adev_q_overflow += 1.0;
      incr_chd_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(chd_adev_q_overflow >= adev_q_size) {  // !!!! wrap >= was >0 
         if(keep_adevs_fresh) {
            reset_chd_bins();              
            adev_freshened = 1;
         }
      }
   }
   else ++chd_adev_q_count;   // keep count of number of entries in the adev queue
   if(chd_adev_q_out >= adev_q_size) chd_adev_q_out = 0;

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

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x10, %rsp
movsd %xmm0, -0x8(%rbp)
movl %edi, -0xc(%rbp)
xorps %xmm0, %xmm0
leaq 0x139d2e(%rip), %rax    # 0x29bef8
ucomisd (%rax), %xmm0
jb 0x1621d5
jmp 0x16244c
leaq 0x139ce4(%rip), %rax    # 0x29bec0
cmpl $0x0, (%rax)
jne 0x1621e6
callq 0x14040
cmpl $0x0, -0xc(%rbp)
je 0x1621fe
movsd -0x8(%rbp), %xmm0
leaq 0x209808(%rip), %rax    # 0x36ba00
movsd %xmm0, (%rax)
jmp 0x162219
movsd -0x8(%rbp), %xmm0
leaq 0x2097f6(%rip), %rax    # 0x36ba00
addsd (%rax), %xmm0
leaq 0x2097eb(%rip), %rax    # 0x36ba00
movsd %xmm0, (%rax)
leaq 0x139be8(%rip), %rax    # 0x29be08
cmpq $0x0, (%rax)
jne 0x16224c
leaq 0x139c03(%rip), %rax    # 0x29be30
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
leaq 0x20a4d5(%rip), %rax    # 0x36c710
movsd (%rax), %xmm0
leaq 0x139c4a(%rip), %rax    # 0x29be90
movsd %xmm0, (%rax)
jmp 0x16226f
movsd -0x8(%rbp), %xmm0
leaq 0x139be0(%rip), %rax    # 0x29be38
subsd (%rax), %xmm0
cvtsd2ss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0x139bc5(%rip), %rax    # 0x29be30
movsd %xmm0, (%rax)
movsd -0x8(%rbp), %xmm0
leaq 0x139bbd(%rip), %rax    # 0x29be38
movsd %xmm0, (%rax)
leaq 0x20977a(%rip), %rax    # 0x36ba00
movsd (%rax), %xmm0
leaq 0x139c67(%rip), %rax    # 0x29bef8
movsd (%rax), %xmm1
movl $0xb, %edi
callq 0x1613f0
leaq 0x120416(%rip), %rax    # 0x2826bc
cmpl $0x0, (%rax)
jne 0x1622ea
leaq 0x12040e(%rip), %rax    # 0x2826c0
movzbl (%rax), %eax
cmpl $0x1, %eax
jne 0x1622ea
leaq 0x139b47(%rip), %rax    # 0x29be08
cmpq $0x1, (%rax)
jg 0x1622ea
leaq 0x209732(%rip), %rax    # 0x36ba00
movsd (%rax), %xmm0
leaq 0x1203cf(%rip), %rax    # 0x2826a8
movsd %xmm0, (%rax)
leaq 0x1203d8(%rip), %rax    # 0x2826bc
movl $0x1, (%rax)
leaq 0x139aff(%rip), %rax    # 0x29bdf0
cmpq $0x0, (%rax)
je 0x162326
leaq 0x209702(%rip), %rax    # 0x36ba00
movsd (%rax), %xmm0
leaq 0x12039f(%rip), %rax    # 0x2826a8
subsd (%rax), %xmm0
leaq 0x139adc(%rip), %rax    # 0x29bdf0
movq (%rax), %rax
leaq 0x139ada(%rip), %rcx    # 0x29bdf8
movq (%rcx), %rcx
movsd %xmm0, (%rax,%rcx,8)
leaq 0x139acb(%rip), %rax    # 0x29bdf8
movq (%rax), %rax
addq $0x1, %rax
leaq 0x139abd(%rip), %rcx    # 0x29bdf8
movq %rax, (%rcx)
leaq 0x139b73(%rip), %rcx    # 0x29beb8
cmpq (%rcx), %rax
jl 0x162358
leaq 0x139aa7(%rip), %rax    # 0x29bdf8
movq $0x0, (%rax)
leaq 0x139a99(%rip), %rax    # 0x29bdf8
movq (%rax), %rax
leaq 0x139a97(%rip), %rcx    # 0x29be00
cmpq (%rcx), %rax
jne 0x1623e8
leaq 0x139a8b(%rip), %rax    # 0x29be00
movq (%rax), %rcx
addq $0x1, %rcx
leaq 0x139a7d(%rip), %rax    # 0x29be00
movq %rcx, (%rax)
movsd 0xccda(%rip), %xmm0     # 0x16f068
leaq 0x139a7b(%rip), %rax    # 0x29be10
addsd (%rax), %xmm0
leaq 0x139a70(%rip), %rax    # 0x29be10
movsd %xmm0, (%rax)
callq 0x1612c0
leaq 0x139a60(%rip), %rax    # 0x29be10
movsd (%rax), %xmm0
leaq 0x139afd(%rip), %rax    # 0x29beb8
cvtsi2sdq (%rax), %xmm1
ucomisd %xmm1, %xmm0
jb 0x1623e6
leaq 0x139b4f(%rip), %rax    # 0x29bf1c
cmpb $0x0, (%rax)
je 0x1623e4
callq 0x15edd0
leaq 0x139a7a(%rip), %rax    # 0x29be58
movl $0x1, (%rax)
jmp 0x1623e6
jmp 0x162400
leaq 0x139a19(%rip), %rax    # 0x29be08
movq (%rax), %rcx
addq $0x1, %rcx
leaq 0x139a0b(%rip), %rax    # 0x29be08
movq %rcx, (%rax)
leaq 0x1399f9(%rip), %rax    # 0x29be00
movq (%rax), %rax
leaq 0x139aa7(%rip), %rcx    # 0x29beb8
cmpq (%rcx), %rax
jl 0x162424
leaq 0x1399e3(%rip), %rax    # 0x29be00
movq $0x0, (%rax)
callq 0x160e90
leaq 0x139a28(%rip), %rax    # 0x29be58
cmpl $0x0, (%rax)
je 0x16244c
movl $0x4, %edi
callq 0x161820
leaq 0x139a12(%rip), %rax    # 0x29be58
movl $0x0, (%rax)
addq $0x10, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)