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;  
   audio1 : tmmxword;  
   i: smallint;  
   helpdata1 : tmmxword = ($c000,$c000,$c000,$c000);  
   helpdata2 : tmmxword = ($8000,$8000,$8000,$8000);  
   { audio1 contains four 16 bit audio samples }  
   { convert it to $8000 is defined as zero, multiply data with 0.75 }  
   { avoid overflows (all values>$ffff becomes $ffff) }  
   { now mupltily with 2 and change to integer }  
   for i:=0 to 3 do  
     audio1[i] := audio1[i] shl 1;