5.2 Saturation support

One important point of MMX is the support of saturated operations. If an operation would cause an overflow, the value stays at the highest or lowest possible value for the data type: If you use byte values you get normally 250+12=6. This is very annoying when doing color manipulations or changing audio samples, when you have to do a word add and check if the value is greater than 255. The solution is saturation: 250+12 gives 255. Saturated operations are supported by the MMX unit. If you want to use them, you have simple turn the switch saturation on: $saturation+

Here is an example:

Program SaturationDemo;  
{  
  example for saturation, scales data (for example audio)  
  with 1.5 with rounding to negative infinity  
}  
uses mmx;  
 
var  
   audio1 : tmmxword;  
   i: smallint;  
 
const  
   helpdata1 : tmmxword = ($c000,$c000,$c000,$c000);  
   helpdata2 : tmmxword = ($8000,$8000,$8000,$8000);  
 
begin  
   { audio1 contains four 16 bit audio samples }  
{$mmx+}  
   { convert it to $8000 is defined as zero, multiply data with 0.75 }  
   audio1:=(audio1+helpdata2)*(helpdata1);  
{$saturation+}  
   { avoid overflows (all values>$ffff becomes $ffff) }  
   audio1:=(audio1+helpdata2)-helpdata2;  
{$saturation-}  
   { now mupltily with 2 and change to integer }  
   for i:=0 to 3 do  
     audio1[i] := audio1[i] shl 1;  
   audio1:=audio1-helpdata2;  
{$mmx-}  
end.