Skip to content

Instantly share code, notes, and snippets.

@mwlon
Created September 1, 2023 02:17
Show Gist options
  • Select an option

  • Save mwlon/b82fd1001d061d45565b2be22c88cfd5 to your computer and use it in GitHub Desktop.

Select an option

Save mwlon/b82fd1001d061d45565b2be22c88cfd5 to your computer and use it in GitHub Desktop.
tANS decoding from a bit stream (x86_64 assembly)
LBB226_3:
movq 1072(%rdi), %r11 load state_idx
movq 1088(%rdi), %rbx load ptr to ANS nodes
movl %eax, %ecx (copy bits_past_byte to do arithmetic later)
shrl $3, %ecx bytes_to_advance = bits_past_byte / 8
addq %rcx, %r8 byte_idx += bytes_to_advance
andl $7, %eax bit_idx = bit_idx % 8
movq (%r9,%r8), %r14 word = bytes[byte_idx]
movl %eax, %ecx (copy bit_idx to do arithmetic later)
shrq %cl, %r14 word >>= bit_idx
shlq $4, %r11 (multiply node index due to struct size)
movl 12(%rbx,%r11), %ecx bits_to_read = node[state_idx].bits_to_read
addl %ecx, %eax bits_past_byte = bit_idx + bits_to_read
negb %cl excess_bits = word_size -
shlq %cl, %r14 word <<= excess_bits (with next line, masking out msb at end of buffer)
shrq %cl, %r14 word >>= excess_bits
addq (%rbx,%r11), %r14 state_idx = node[state_idx].next_base_state_idx + word
movq %r14, 1072(%rdi) decoder.state_idx = state_idx
movl 8(%rbx,%r11), %ecx token = tokens[token_idx]
movl %ecx, 40(%rdi,%r10,4) dst[i] = token
leaq 1(%r10), %rcx i += 1
movq %rcx, %r10 (copy i for next iteration; honestly idk why rcx was used here)
cmpq %rcx, %rdx check if i != n...
jne LBB226_3 ... and loop again if so
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment