13.5 Saturation Arithmetic and Wrap-Around Mode


13.5 Saturation Arithmetic and Wrap-Around Mode

The MMX instruction set supports saturating arithmetic (see Chapter 2 for details). When manipulating standard integer values and an overflow occurs, the standard integer instructions maintain the correct L.O. bits of the value in the integer while truncating any overflow.[2] This form of arithmetic is known as wrap-around mode because the L.O. bits wrap back around to zero. For example, if you add the two 8-bit values $02 and $FF you wind up with a carry and the result $01. The actual sum is $101, but the operation truncates the ninth bit and the L.O. byte wraps around to $01.

In saturation mode, results of an operation that overflow or underflow are clipped (saturated) to some maximum or minimum value depending on the size of the object and whether it is signed or unsigned. The result of an operation that exceeds the range of a data type saturates to the maximum value of the range. A result that is less than the range of a data type saturates to the minimum value of the range.

For example, as you can see in Table 13-1, when the result exceeds the data range limit for signed bytes, it is saturated to $7f; if a value is less than the data range limit, it is saturated to $80 for signed bytes. If a value exceeds the range for unsigned bytes, it is saturated to $ff or $00.

Table 13-1: Ranges for Decimal and Hexidecimal Values

Data Type

Decimal

Hexadecimal

Lower Limit

Upper Limit

Lower Limit

Upper Limit

Signed Byte

-128

+127

$80

$7f

Unsigned Byte

0

255

$00

$ff

Signed Word

-32768

+32767

$8000

$7fff

Unsigned Word

0

65535

0

$ffff

This saturation effect is very useful for audio and video data. For example, if you are amplifying an audio signal by multiplying the words in the CD-quality 44.1 kHz audio stream by 1.5, then clipping the value at +32767 (while introducing distortion) sounds far better than allowing the waveform to wrap around to -32768. Similarly, if you are mixing colors in a 24-bit graphic or video image, saturating to white produces much more meaningful results than wrap-around.

Because Intel created the MMX architecture to support audio, graphics, and video, it should come as no surprise that the MMX instruction set supports saturating arithmetic. For those applications that require saturating arithmetic, having the CPU automatically handle this process (rather than having to explicitly check after each calculation) is another way the MMX architecture speeds up multimedia applications.

[2]Typically you would merge in a fourth byte of zero and then store the resulting double word every three bytes in memory to overwrite the zeros.




The Art of Assembly Language
The Art of Assembly Language
ISBN: 1593272073
EAN: 2147483647
Year: 2005
Pages: 246
Authors: Randall Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net