Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Parallel

High-Speed Finite-State Machines


Dr. Dobb's Journal November 1997: A High-Speed Static Huffman Decoder

Dr. Dobb's Journal November 1997

A High-Speed Static Huffman Decoder


Huffman compression uses short codes for frequently appearing byte values and much longer codes for less common values. A simple Huffman decoder uses a binary tree. It reads one input bit at each step, using it to select the left (0) or right (1) path until a leaf node is reached. The decoder then outputs the symbol associated with the leaf node, and starts over from the root.

Stepping through the tree one node (and one bit) at a time is expensive. A table-driven decoder instead reads the next eight bits of input into a register and uses this register as an index into a decoding table. The table lookup classifies the input according to bit pattern. Each entry specifies how many bits are used to form the symbol, and the symbol value to be emitted. For example, if the three bits 1012 encode a byte value of 27, you would store 3 (number of bits) and 27 (output value) in each table location matching 101xxxxx2 (values 101000002 to 101111112).

If a symbol requires more than eight bits, the entry in the table for the first eight bits instructs the decoder to use a secondary table to continue interpreting the remainder of the code. For example, the code 100110100102 would be decoded by matching 100110102 in the first table, and then matching 010xxxxx2 in an additional table.

To implement these ideas, I've defined the actions USE1, USE2, USE3, USE4, USE5, USE6, USE7, USE8 and USEGT8. Each action describes how many bits to consume. Actions USE1..USE8 emit a symbol, while USEGT8 chains to a lower-order table. Figure 5 illustrates how this works.

Decoders optimized to use table lookup spend most of their time arranging the input stream so that the next eight bits are presented as a byte for the table lookup. This cost can be slashed by cloning the decoder so that each possible bit alignment is handled by a tailored code page. The finite-state machine models its state space in two dimensions -- one state for the decode table, one for the input bit alignment.

On a Pentium, the optimized assembly-language decoder consumes just 14 cycles per decoded byte, not including the time to prepare the decoding tables. My implementation of this decoder (available electronically, see "Availability," page 3) uses Perl scripts to construct C or assembly source code for the decoder state machine.

-- B.K.


Copyright © 1997, Dr. Dobb's Journal


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.