Skip to content

Commit

Permalink
huffman set_bits_r not sure if it works
Browse files Browse the repository at this point in the history
  • Loading branch information
ChillerDragon committed Aug 2, 2024
1 parent 0549cac commit 05e6edc
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/huffman/construct_tree.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; ; m_aNodes array of CNODE structs
; huff_nodes resb HUFF_CNODE_SIZE * HUFFMAN_MAX_NODES
;
; ; m_apDecodeLut array of CNODE structs
; huff_decode_lut resb HUFFMAN_LUTSIZE * 8
;
; ; pointer to start node
; huff_start_node resb 8
;
; ; integer with amount of nodes
; huff_num_nodes resb 4

_huff_construct_tree:
ret

111 changes: 111 additions & 0 deletions src/huffman/set_bits_r.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
; ; m_aNodes array of CNODE structs
; huff_nodes resb HUFF_CNODE_SIZE * HUFFMAN_MAX_NODES
;
; ; m_apDecodeLut array of CNODE structs
; huff_decode_lut resb HUFFMAN_LUTSIZE * 8
;
; ; pointer to start node
; huff_start_node resb 8
;
; ; integer with amount of nodes
; huff_num_nodes resb 4

_huff_setbits_r:
; _huff_setbits_r [rax] [rdi] [rsi]
; rax = *pNode
; rdi = int Bits
; rsi = unsigned Depth
push_registers

; leaf1
push rdi
push rsi

mov rcx, 0
mov word cx, [rax + HUFF_CNODE_LEAF_1_OFFSET]
cmp cx, 0xffff
je ._huff_setbits_r_no_leaf1_recursion

; &m_aNodes[pNode->m_aLeafs[1]]
lea rax, [huff_nodes + rcx]

; bits | (1<<Depth)
mov r9, 1
shift_left r9, rsi
xor rdi, r9
shl edx,cl

; depth + 1
inc rsi
call _huff_setbits_r

pop rsi
pop rdi

._huff_setbits_r_no_leaf1_recursion:

; leaf0
mov word cx, [rax + HUFF_CNODE_LEAF_0_OFFSET]
cmp cx, 0xffff
je ._huff_setbits_r_no_leaf0_recursion

; &m_aNodes[pNode->m_aLeafs[0]]
lea rax, [huff_nodes + rcx]

; bits are still in rdi

; depth + 1
inc rsi
call _huff_setbits_r

._huff_setbits_r_no_leaf0_recursion:

; num bits
mov dword ecx, [rax + HUFF_CNODE_LEAF_0_OFFSET]
je ._huff_setbits_r_got_num_bits
jmp ._huff_setbits_r_end

._huff_setbits_r_got_num_bits:
; node.bits = bits
lea rcx, [rax + HUFF_CNODE_BITS_OFFSET]
mov dword [rcx], edi

; node.num_bits = depth
lea rcx, [rax + HUFF_CNODE_NUM_BITS_OFFSET]
mov dword [rcx], esi

._huff_setbits_r_end:

pop_registers
ret

%macro huff_setbits_r 3
; huff_setbits_r [*pNode] [int Bits] [unsigned Depth]
push rax
push rdi
push rsi

mov rbp, rsp

; *pNode = 8 byte
; int Bits = 4 byte
; unsigned Depth = 4 byte
; 8 + 4 + 4 = 16
sub rsp, 16

mov qword [rbp-16], %1
mov dword [rbp-8], %2
mov dword [rbp-4], %3

mov rax, [rbp-16]
mov rdi, [rbp-8]
mov rsi, [rbp-4]
call _huff_setbits_r

mov rsp, rbp

pop rsi
pop rdi
pop rax
%endmacro

Empty file removed src/huffman_decompress.asm
Empty file.
40 changes: 40 additions & 0 deletions src/macros.asm
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,43 @@
pop rcx
pop rbx
%endmacro

%macro shift_left 2
; shift_left [register] [amount]
;
; I saw with objdump that `shl edx, cl` is possible.
; But nasm refuses to compile it
;
; So this macro polyfills it
; shift left n bits
mov rbp, rsp
sub rsp, 8

push rcx
push r12

; register
mov [rbp-8], %1

; amount
mov r12, %2

mov rcx, 0
%%shift_left_loop:
inc rcx
cmp rcx, r12
jge %%shift_left_loop_end

shl qword [rbp-8], 1

jmp %%shift_left_loop
%%shift_left_loop_end:

pop r12
pop rcx

mov %1, [rbp-8]

mov rsp, rbp
%endmacro

3 changes: 2 additions & 1 deletion src/teeworlds_asmr.asm
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ section .text
%include "src/on_game.asm"
%include "src/packet_packer.asm"
%include "src/pack_int.asm"
%include "src/huffman_decompress.asm"
%include "src/huffman/set_bits_r.asm"
%include "src/huffman/construct_tree.asm"

print_udp:
print s_got_udp
Expand Down
3 changes: 2 additions & 1 deletion tests/assert.asm
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ section .text
%include "src/on_game.asm"
%include "src/packet_packer.asm"
%include "src/pack_int.asm"
%include "src/huffman_decompress.asm"
%include "src/huffman/set_bits_r.asm"
%include "src/huffman/construct_tree.asm"

assert_ok:
push_registers
Expand Down

0 comments on commit 05e6edc

Please sign in to comment.