All pastes #137976 Raw Edit

Something

public text v1 · immutable
#137976 ·published 2006-08-18 21:03 UTC
rendered paste body
;; u8 *huffCompress(u32 *dst, u8 *src, u8 *end, u32 *tbl)

huffCompress:
    mov     r10,rcx            ; table with huffman codes/lengths
    mov     r8b,-32            ; bits to go
    

.loop:
    movzx   r11,byte [rsi]     ; load byte
    mov     ecx, [r10+4*r11]
   ; load huffman code/length
    add	    r8b,cl
             ; add code legth into 'r8b'
    jl	    .nostore
           ; enough space in the register?


    sub     cl,r8b
             ; no, put in what fits in and adjust 'cl'
    sub     r8b,32

    shld    eax,ecx,cl

    add     cl,r8b
    bswap   eax

    mov     [rdi],eax

    add     rdi,4



.nostore:
                      ; put the code into 'eax'
    shld    eax,ecx,cl
	
    add     rsi,1
              ; advance source by one byte
    cmp     rsi,rdx
            ; loop...
    jnz     .loop



    cmp     r8b,-32
            ; extra bits to flush?
    jle    .noextra

    mov     cl,r8b

    neg     cl

    shl     eax,cl
    bswap   eax

    mov     [rdi],eax

    add     rdi,4


.noextra:
    mov     rax,rdi            ; how many dwords did we encode?
    retn