Stefano Tommesani

  • Increase font size
  • Default font size
  • Decrease font size
Home SIMD MMX Examples

MMX Examples


This section describes example uses of the MMX instruction set to implement basic coding structures.

Conditional Select

Operating on multiple data operands using a single instruction presents an interesting issue: what happens when a computation is only done if the operand value passes some conditional check? For example, in an absolute value calculation, only if the number is already negative a 2’s complement is performed on it:

for i = 1 to 100
     if a[i] < 0 
       then b[i] = - a[i] 
       else b[i] = a[i]

There are different approaches possible, and some are simpler than others. Using a branch approach does not work well for two reasons: a branch-based solution is slower because of the inherent branch misprediction penalty, and because of the need to convert packed data types to scalars. Direct conditional execution support does not work well for the x86 IA since it requires three independent operands (source, source/destination, and predicate vector).
The MMX technology adopts a simpler design: a conditional execution is converted into a conditional assignment. MMX compare operations result in a bit mask corresponding to the length of the operands: for example, a compare operation operating on packed byte operands produce byte-wide masks. These masks then can be used in conjunction with logical operations to achieve conditional assignment. Consider the following example:

If True
    then Ra := Rb 
    else Ra := Rc

Assuming that register Rx contains all 1’s if the condition is true and all 0’s if the condition is false, Ra can be computed with the following logical expression:

Ra = (Rb AND Rx) OR (Rc ANDNOT Rx)

This approach works for operations with a register as the destination. Conditional assignment to memory can be implemented as a sequence of load, conditional assignment, and store. 
The Chroma Keying example demonstrates how conditional selection using the MMX instruction set removes branches, in addition to performing multiple selection operations in parallel. Text overlay on a pix/video background, and sprite overlays in games are some of the other operations that would benefit from this technique.
Most have seen the television weather man overlaid on the image of a weather map. In this example a blue screen is used to overlay an image of a woman on a background picture.
PCMPEQ (packed compare for equality) is performed on the weathercaster and blue-screen images, yielding a bitmask that traces the outline of the weathercaster.

This bitmask image is PANDNed (packed and not) with the weathercaster image, yielding the first intermediate image: now the weathercaster has no background behind her.

The same bitmask image is PANDed (packed and) with the weather map image, yielding the second intermediate image.

The two intermediate images are PORed (packed or) together, resulting in final composite of the weathercaster over weather map


Vector Dot Product

The vector dot product is one of the most basic algorithms used in signal-processing of multimedia data such as images, audio, video and sound. The following example shows how the PMADD instruction helps speed up algorithms using vector dot products. 
The PMADD instruction handles four multiplies and two additions at a time: it starts from a 16-bit, packed data type and generates a 32-bit packed, data type result, then it multiplies all the corresponding elements generating four 32-bit results, and adds the two products on the left together for one result and the two products on the right together for the other result. To complete a multiply-accumulate operation, the results would then be added to another register which is used as the accumulator.
Assuming that the precision supported by the PMADD instruction is sufficient, this dot-product example on eight-element vectors can be completed using eight MMX instructions: 2 PMADDs, 2 more PADDs, 2 shifts (if needed to fix the precision after the multiply operation), and 2 memory moves to load one of the vectors (the other vector is loaded by the PMADD instruction which can have one of its operands come from memory).

Comparing instruction counts with and without MMX technology for this operation yields that only one third of the number of instructions is needed with MMX.


Latest Articles

Easily upload videos of security cameras to YouTube
In this example, we will import video from a Yi security camera into YouTube. The same process, with eventual adjustment to the naming of directories in the SD card used by the camera to record videos, will also apply to other
A software to stand out 27 January 2018, 14.35 Web
A software to stand out
Standing out of the pack starts by being visible, and being noticed by the right group of professionals. No matter how good your profile is, it is lost in a sea of similar profiles, so you need to show up and start attracting
Web page scraping, the easy way 07 January 2018, 00.46 Web
Web page scraping, the easy way
There are many ways to extract data elements from web pages, almost all of them prettier and cooler than the method proposed here, but as we are in an hurry, let's get that data quickly, ok? Suppose we have to extract the
Scraping dynamic page content 06 January 2018, 23.57 Web
Scraping dynamic page content
One of the most common roadblocks when scraping the content of web sites is getting the full contents of the page, including JS-generated data elements (probably, the ones you are looking for). So, when using CEFSharp to scrape
Unit-testing file I/O 26 November 2017, 12.09 Testing
Unit-testing file I/O
Two good news: file I/O is unit-testable, and it is surprisingly easy to do. Let's see how it works! A software no-one asked for First, we need a piece of software that deals with files and that has to be unit-tested. The