Non-SIMD subtract w/saturation...

Quote:

> I'm looking for code to subtract 4, 8-bit, numbers in a way such that if

> a value wraps around (ie: 100 - 200), it does not interfere with the other

> bytes.

> If sign matters, then I'm looking for an unsigned solution.

I use this code in FBlend:

For a better description, see:

http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/fblend/fble...

/* Compute saturation of difference */

/* color1 and color2 are 32-bit RGBA colors */

temp1 = (color2 & 0x80808080) ^ 0x80808080;

temp2 = color1 & 0x80808080;

color1 = (color2 | 0x80808080) - (color1 & 0x7F7F7F7F);

color2 = temp1;

temp1 = temp1 | temp2;

temp2 = color2 & temp2;

color2 = temp1 & (color1 ^ 0x80808080);

temp2 = (temp2 | color2) >> 7; /* Make sure it's unsigned shift */

temp2 = ((temp2 + 0x7F7F7F7F) ^ 0x80808080);

color1 = temp2 & (temp1 ^ color1);

/* color1 has the saturated difference */

You can put 0x80808080 in memory to save code space. It can also dual as

0x7F7F7F7F if you 'not' it.