// File: ke6jjj[P]r-dat/NRZIEQSyncDeframer.cc
// Function: NRZIEQSyncDeframer::ReceiveSample(float)
void
NRZIEQSyncDeframer::ReceiveSample(float sample)
{
  bool bit;
  size_t bit_count;
  
  //
  // Find the slopes of the sides of the triangle formed by
  // the previous two samples and this bit.
  //
  float slope_a = mLast2 - mLast1;
  float slope_b = sample - mLast2;

  bool sign_a = slope_a > 0.0;
  bool sign_b = slope_b > 0.0;

  //
  // NRZI decode the bit.
  //
  bit = sign_a != sign_b;

  //
  // Accumulate a new bit into the large shift register.
  //
  mFrame  &= 0x1ff;
  mFrame <<= 1;
  mFrame  |= bit;
  
  //
  // Are we currently searching for frame sync and have we received
  // a sync word?
  //
  if ((mFrame & 0x1ff) == 0x111) {
    //
    // We've found a sync pattern. Enter sync state.
    //
    mSyncBitCount = 0;
    mState = STATE_SYNCED;

    //
    // Notify the upstream frame receiver of the sync word.
    //
    mReceiver->ReceiveWord(mFrame);
  } else if (mState == STATE_SYNCED) {
    //
    // We're in a synchronized state. We should keep accepting bits into
    // the accumulator until a full frame is built up. Has a frame been
    // built up?
    //
    mSyncBitCount += 1;
    if (mSyncBitCount == 10) {
      //
      // There's a full frame built up.
      //
      mSyncBitCount = 0;
      
      //
      // Deliver it upstream.
      //
      mReceiver->ReceiveWord(mFrame);
    }
  }

  //
  // Update the sample history.
  //
  mLast1 = mLast2;
  mLast2 = sample;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movss %xmm0, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
movss 0x10(%rax), %xmm0
subss 0xc(%rax), %xmm0
movss %xmm0, -0x1c(%rbp)
movss -0xc(%rbp), %xmm0
subss 0x10(%rax), %xmm0
movss %xmm0, -0x20(%rbp)
movss -0x1c(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
seta %cl
andb $0x1, %cl
movb %cl, -0x21(%rbp)
movss -0x20(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
seta %cl
andb $0x1, %cl
movb %cl, -0x22(%rbp)
movb -0x21(%rbp), %cl
andb $0x1, %cl
movzbl %cl, %ecx
movb -0x22(%rbp), %dl
andb $0x1, %dl
movzbl %dl, %edx
cmpl %edx, %ecx
setne %cl
andb $0x1, %cl
movb %cl, -0xd(%rbp)
movl 0x8(%rax), %ecx
andl $0x1ff, %ecx            # imm = 0x1FF
movl %ecx, 0x8(%rax)
movl 0x8(%rax), %ecx
shll %ecx
movl %ecx, 0x8(%rax)
movb -0xd(%rbp), %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl 0x8(%rax), %ecx
movl %ecx, 0x8(%rax)
movl 0x8(%rax), %eax
andl $0x1ff, %eax            # imm = 0x1FF
cmpl $0x111, %eax            # imm = 0x111
jne 0x5219
movq -0x30(%rbp), %rax
movq $0x0, 0x18(%rax)
movl $0x1, 0x20(%rax)
movq 0x28(%rax), %rdi
movl 0x8(%rax), %esi
callq 0x3560
jmp 0x5256
movq -0x30(%rbp), %rax
cmpl $0x1, 0x20(%rax)
jne 0x5254
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x18(%rax)
cmpq $0xa, 0x18(%rax)
jne 0x5252
movq -0x30(%rbp), %rax
movq $0x0, 0x18(%rax)
movq 0x28(%rax), %rdi
movl 0x8(%rax), %esi
callq 0x3560
jmp 0x5254
jmp 0x5256
movq -0x30(%rbp), %rax
movss 0x10(%rax), %xmm0
movss %xmm0, 0xc(%rax)
movss -0xc(%rbp), %xmm0
movss %xmm0, 0x10(%rax)
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop