Densely packed decimal


Densely packed decimal is an efficient method for binary encoding decimal digits.
The traditional system of binary encoding for decimal digits, known as binary-coded decimal, uses four bits to encode each digit, resulting in significant wastage of binary data bandwidth, even when using packed BCD. Densely packed decimal is a more efficient code that packs three digits into ten bits using a scheme that allows compression from, or expansion to, BCD with only two or three hardware gate delays.
The densely packed decimal encoding is a refinement of Chen–Ho encoding; it gives the same compression and speed advantages, but the particular arrangement of bits used confers additional advantages:
In 1969, Theodore M. Hertz, and in 1971, Tien Chi Chen with Irving Tze Ho devised lossless prefix codes which packed three decimal digits into ten binary bits using a scheme which allowed compression from or expansion to BCD with only two or three gate delays in hardware. Densely packed decimal is a refinement of this, devised by Mike F. Cowlishaw in 2002, which was incorporated into the IEEE 754-2008 and standards for decimal floating point.

Encoding

Like Chen–Ho encoding, DPD encoding classifies each decimal digit into one of two ranges, depending on the most significant bit of the binary form: "small" digits have values 0 through 7, and "large" digits, 8 through 9. Once it is known or has been indicated that a digit is small, three more bits are still required to specify the value. If a large value has been indicated, only one bit is required to distinguish between the values 8 or 9.
When encoding, the most significant bit of each of the three digits to be encoded select one of eight coding patterns for the remaining bits, according to the following table. The table shows how, on decoding, the ten bits of the coded form in columns b9 through b0 are copied into the three digits d2 through d0, and the remaining bits are filled in with constant zeros or ones.
Bits b7, b4 and b0 are passed through the encoding unchanged, and do not affect the meaning of the other bits. The remaining seven bits can be considered a seven-bit encoding for three base-5 digits.
Bits b8 and b9 are not needed and ignored when decoding DPD groups with three large digits, but are filled with zeros when encoding.
The eight decimal values whose digits are all 8s or 9s have four codings each.
The bits marked x in the table above are ignored on input, but will always be 0 in computed results.

Examples

This table shows some representative decimal numbers and their encodings in BCD, Chen–Ho, and densely packed decimal :
DecimalBCDChen–HoDPD
0050000 0000 0101000 000 0101000 000 0101
0090000 0000 1001110 000 0001000 000 1001
0550000 0101 0101000 010 1101000 101 0101
0790000 0111 1001110 011 1001000 111 1001
0800000 1000 0000101 000 0000000 000 1010
0990000 1001 1001111 000 1001000 101 1111
5550101 0101 0101010 110 1101101 101 0101
9991001 1001 1001111 111 1001001 111 1111