Perf opt for the c conversion. Use downto instead to avoid repeated function calls in the loop
authornemo
Sat, 04 Aug 2012 22:51:41 -0400
changeset 7492 3188794b9d87
parent 7489 43a998fbacfe
child 7495 2592240b7b62
Perf opt for the c conversion. Use downto instead to avoid repeated function calls in the loop
hedgewars/uGearsUtils.pas
hedgewars/uLandGraphics.pas
--- a/hedgewars/uGearsUtils.pas	Sat Aug 04 22:26:51 2012 -0400
+++ b/hedgewars/uGearsUtils.pas	Sat Aug 04 22:51:41 2012 -0400
@@ -510,7 +510,7 @@
     count: LongInt = 0;
 begin
     if (y and LAND_HEIGHT_MASK) = 0 then
-        for i:= max(x - r, 0) to min(x + r, LAND_WIDTH - 4) do
+        for i:= min(x + r, LAND_WIDTH - 4) downto max(x - r, 0) do 
             if Land[y, i] and mask <> 0 then
             begin
                 inc(count);
--- a/hedgewars/uLandGraphics.pas	Sat Aug 04 22:26:51 2012 -0400
+++ b/hedgewars/uLandGraphics.pas	Sat Aug 04 22:51:41 2012 -0400
@@ -59,7 +59,7 @@
         begin
         addBgColor:= NewColor;
         exit
-        end; 
+        end;
     // Get colors
     oRed   := (OldColor shr RShift);
     oGreen := (OldColor shr GShift);
@@ -72,7 +72,7 @@
     // Mix colors
     nRed   := min(255,((nRed*nAlpha) div 255) + ((oRed*oAlpha*byte(255-nAlpha)) div 65025));
     nGreen := min(255,((nGreen*nAlpha) div 255) + ((oGreen*oAlpha*byte(255-nAlpha)) div 65025));
-    nBlue  := min(255,((nBlue*nAlpha) div 255) + ((oBlue*oAlpha*byte(255-nAlpha)) div 65025)); 
+    nBlue  := min(255,((nBlue*nAlpha) div 255) + ((oBlue*oAlpha*byte(255-nAlpha)) div 65025));
     nAlpha := min(255, oAlpha + nAlpha);
 
     addBgColor := (nAlpha shl AShift) or (nRed shl RShift) or (nGreen shl GShift) or (nBlue shl BShift);
@@ -82,19 +82,19 @@
 var i: LongInt;
 begin
 if ((y + dy) and LAND_HEIGHT_MASK) = 0 then
-    for i:= Max(x - dx, 0) to Min(x + dx, LAND_WIDTH - 1) do
+    for i:= Min(x + dx, LAND_WIDTH - 1) downto Max(x - dx, 0) do
         if (Land[y + dy, i] and lfIndestructible) = 0 then
             Land[y + dy, i]:= Value;
 if ((y - dy) and LAND_HEIGHT_MASK) = 0 then
-    for i:= Max(x - dx, 0) to Min(x + dx, LAND_WIDTH - 1) do
+    for i:= Min(x + dx, LAND_WIDTH - 1) downto Max(x - dx, 0) do
         if (Land[y - dy, i] and lfIndestructible) = 0 then
             Land[y - dy, i]:= Value;
 if ((y + dx) and LAND_HEIGHT_MASK) = 0 then
-    for i:= Max(x - dy, 0) to Min(x + dy, LAND_WIDTH - 1) do
+    for i:= Min(x + dy, LAND_WIDTH - 1) downto Max(x - dy, 0) do
         if (Land[y + dx, i] and lfIndestructible) = 0 then
             Land[y + dx, i]:= Value;
 if ((y - dx) and LAND_HEIGHT_MASK) = 0 then
-    for i:= Max(x - dy, 0) to Min(x + dy, LAND_WIDTH - 1) do
+    for i:= Min(x + dy, LAND_WIDTH - 1) downto Max(x - dy, 0) do
         if (Land[y - dx, i] and lfIndestructible) = 0 then
             Land[y - dx, i]:= Value;
 end;
@@ -105,26 +105,26 @@
 if not doSet then
     begin
     if ((y + dy) and LAND_HEIGHT_MASK) = 0 then
-        for i:= Max(x - dx, 0) to Min(x + dx, LAND_WIDTH - 1) do
-            if isCurrent then 
+        for i:= Min(x + dx, LAND_WIDTH - 1) downto Max(x - dx, 0) do
+            if isCurrent then
                 Land[y + dy, i]:= Land[y + dy, i] and $FF7F
             else if Land[y + dy, i] and $007F > 0 then
                 Land[y + dy, i]:= (Land[y + dy, i] and $FF80) or ((Land[y + dy, i] and $7F) - 1);
     if ((y - dy) and LAND_HEIGHT_MASK) = 0 then
-        for i:= Max(x - dx, 0) to Min(x + dx, LAND_WIDTH - 1) do
-            if isCurrent then 
+        for i:= Min(x + dx, LAND_WIDTH - 1) downto Max(x - dx, 0) do
+            if isCurrent then
                 Land[y - dy, i]:= Land[y - dy, i] and $FF7F
             else if Land[y - dy, i] and $007F > 0 then
                 Land[y - dy, i]:= (Land[y - dy, i] and $FF80) or ((Land[y - dy, i] and $7F) - 1);
     if ((y + dx) and LAND_HEIGHT_MASK) = 0 then
-        for i:= Max(x - dy, 0) to Min(x + dy, LAND_WIDTH - 1) do
-            if isCurrent then 
+        for i:= Min(x + dy, LAND_WIDTH - 1) downto Max(x - dy, 0) do
+            if isCurrent then
                 Land[y + dx, i]:= Land[y + dx, i] and $FF7F
             else if Land[y + dx, i] and $007F > 0 then
                 Land[y + dx, i]:= (Land[y + dx, i] and $FF80) or ((Land[y + dx, i] and $7F) - 1);
     if ((y - dx) and LAND_HEIGHT_MASK) = 0 then
-        for i:= Max(x - dy, 0) to Min(x + dy, LAND_WIDTH - 1) do
-            if isCurrent then 
+        for i:= Min(x + dy, LAND_WIDTH - 1) downto Max(x - dy, 0) do
+            if isCurrent then
                 Land[y - dx, i]:= Land[y - dx, i] and $FF7F
             else if Land[y - dx, i] and $007F > 0 then
                 Land[y - dx, i]:= (Land[y - dx, i] and $FF80) or ((Land[y - dx, i] and $7F) - 1)
@@ -132,28 +132,28 @@
 else
     begin
     if ((y + dy) and LAND_HEIGHT_MASK) = 0 then
-        for i:= Max(x - dx, 0) to Min(x + dx, LAND_WIDTH - 1) do
-            if isCurrent then 
+        for i:= Min(x + dx, LAND_WIDTH - 1) downto Max(x - dx, 0) do
+            if isCurrent then
                 Land[y + dy, i]:= Land[y + dy, i] or $80
             else if Land[y + dy, i] and $007F < 127 then
                 Land[y + dy, i]:= (Land[y + dy, i] and $FF80) or ((Land[y + dy, i] and $7F) + 1);
-    if ((y - dy) and LAND_HEIGHT_MASK) = 0 then                                                   
-        for i:= Max(x - dx, 0) to Min(x + dx, LAND_WIDTH - 1) do                                  
-            if isCurrent then                                                                     
-                Land[y - dy, i]:= Land[y - dy, i] or $80                                          
-            else if Land[y - dy, i] and $007F < 127 then                                          
+    if ((y - dy) and LAND_HEIGHT_MASK) = 0 then
+        for i:= Min(x + dx, LAND_WIDTH - 1) downto Max(x - dx, 0) do
+            if isCurrent then
+                Land[y - dy, i]:= Land[y - dy, i] or $80
+            else if Land[y - dy, i] and $007F < 127 then
                 Land[y - dy, i]:= (Land[y - dy, i] and $FF80) or ((Land[y - dy, i] and $7F) + 1);
-    if ((y + dx) and LAND_HEIGHT_MASK) = 0 then                                                   
-        for i:= Max(x - dy, 0) to Min(x + dy, LAND_WIDTH - 1) do                                  
-            if isCurrent then                                                                     
-                Land[y + dx, i]:= Land[y + dx, i] or $80                                          
-            else if Land[y + dx, i] and $007F < 127 then                                          
+    if ((y + dx) and LAND_HEIGHT_MASK) = 0 then
+        for i:= Min(x + dy, LAND_WIDTH - 1) downto Max(x - dy, 0) do
+            if isCurrent then
+                Land[y + dx, i]:= Land[y + dx, i] or $80
+            else if Land[y + dx, i] and $007F < 127 then
                 Land[y + dx, i]:= (Land[y + dx, i] and $FF80) or ((Land[y + dx, i] and $7F) + 1);
-    if ((y - dx) and LAND_HEIGHT_MASK) = 0 then                                                   
-        for i:= Max(x - dy, 0) to Min(x + dy, LAND_WIDTH - 1) do                                  
-            if isCurrent then                                                                     
-                Land[y - dx, i]:= Land[y - dx, i] or $80                                          
-            else if Land[y - dx, i] and $007F < 127 then                                          
+    if ((y - dx) and LAND_HEIGHT_MASK) = 0 then
+        for i:= Min(x + dy, LAND_WIDTH - 1) downto Max(x - dy, 0) do 
+            if isCurrent then
+                Land[y - dx, i]:= Land[y - dx, i] or $80
+            else if Land[y - dx, i] and $007F < 127 then
                 Land[y - dx, i]:= (Land[y - dx, i] and $FF80) or ((Land[y - dx, i] and $7F) + 1)
     end
 end;
@@ -207,7 +207,7 @@
 begin
 t:= y + dy;
 if (t and LAND_HEIGHT_MASK) = 0 then
-    for i:= Max(x - dx, 0) to Min(x + dx, LAND_WIDTH - 1) do
+    for i:= Min(x + dx, LAND_WIDTH - 1) downto Max(x - dx, 0) do 
         if ((Land[t, i] and lfIndestructible) = 0) and (not disableLandBack or (Land[t, i] > 255))  then
             if (cReducedQuality and rqBlurryLand) = 0 then
                 LandPixels[t, i]:= 0
@@ -216,7 +216,7 @@
 
 t:= y - dy;
 if (t and LAND_HEIGHT_MASK) = 0 then
-    for i:= Max(x - dx, 0) to Min(x + dx, LAND_WIDTH - 1) do
+    for i:= Min(x + dx, LAND_WIDTH - 1) downto Max(x - dx, 0) do 
         if ((Land[t, i] and lfIndestructible) = 0) and (not disableLandBack or (Land[t, i] > 255))  then
             if (cReducedQuality and rqBlurryLand) = 0 then
                 LandPixels[t, i]:= 0
@@ -225,7 +225,7 @@
 
 t:= y + dx;
 if (t and LAND_HEIGHT_MASK) = 0 then
-    for i:= Max(x - dy, 0) to Min(x + dy, LAND_WIDTH - 1) do
+    for i:= Min(x + dy, LAND_WIDTH - 1) downto Max(x - dy, 0) do 
         if ((Land[t, i] and lfIndestructible) = 0) and (not disableLandBack or (Land[t, i] > 255))  then
             if (cReducedQuality and rqBlurryLand) = 0 then
                 LandPixels[t, i]:= 0
@@ -234,7 +234,7 @@
 
 t:= y - dx;
 if (t and LAND_HEIGHT_MASK) = 0 then
-    for i:= Max(x - dy, 0) to Min(x + dy, LAND_WIDTH - 1) do
+    for i:= Min(x + dy, LAND_WIDTH - 1) downto Max(x - dy, 0) do 
         if ((Land[t, i] and lfIndestructible) = 0) and (not disableLandBack or (Land[t, i] > 255))  then
             if (cReducedQuality and rqBlurryLand) = 0 then
                 LandPixels[t, i]:= 0
@@ -250,7 +250,7 @@
 cnt:= 0;
 t:= y + dy;
 if (t and LAND_HEIGHT_MASK) = 0 then
-    for i:= Max(x - dx, 0) to Min(x + dx, LAND_WIDTH - 1) do
+    for i:= Min(x + dx, LAND_WIDTH - 1) downto Max(x - dx, 0) do 
         if (Land[t, i] and lfIndestructible) = 0 then
             begin
             if (cReducedQuality and rqBlurryLand) = 0 then
@@ -266,13 +266,13 @@
                 inc(cnt);
                 LandPixels[by, bx]:= LandBackPixel(i, t)
                 end
-            else if ((Land[t, i] and lfObject) <> 0) or (((LandPixels[by,bx] and AMask) shr AShift) < 255) then 
+            else if ((Land[t, i] and lfObject) <> 0) or (((LandPixels[by,bx] and AMask) shr AShift) < 255) then
                 LandPixels[by, bx]:= 0
             end;
 
 t:= y - dy;
 if (t and LAND_HEIGHT_MASK) = 0 then
-    for i:= Max(x - dx, 0) to Min(x + dx, LAND_WIDTH - 1) do
+    for i:= Min(x + dx, LAND_WIDTH - 1) downto Max(x - dx, 0) do 
         if (Land[t, i] and lfIndestructible) = 0 then
             begin
             if (cReducedQuality and rqBlurryLand) = 0 then
@@ -288,13 +288,13 @@
                 inc(cnt);
                 LandPixels[by, bx]:= LandBackPixel(i, t)
                 end
-            else if ((Land[t, i] and lfObject) <> 0) or (((LandPixels[by,bx] and AMask) shr AShift) < 255) then 
+            else if ((Land[t, i] and lfObject) <> 0) or (((LandPixels[by,bx] and AMask) shr AShift) < 255) then
                 LandPixels[by, bx]:= 0
             end;
 
 t:= y + dx;
 if (t and LAND_HEIGHT_MASK) = 0 then
-    for i:= Max(x - dy, 0) to Min(x + dy, LAND_WIDTH - 1) do
+    for i:= Min(x + dy, LAND_WIDTH - 1) downto Max(x - dy, 0) do 
         if (Land[t, i] and lfIndestructible) = 0 then
             begin
             if (cReducedQuality and rqBlurryLand) = 0 then
@@ -310,12 +310,12 @@
                 inc(cnt);
                 LandPixels[by, bx]:= LandBackPixel(i, t)
                 end
-            else if ((Land[t, i] and lfObject) <> 0) or (((LandPixels[by,bx] and AMask) shr AShift) < 255) then 
+            else if ((Land[t, i] and lfObject) <> 0) or (((LandPixels[by,bx] and AMask) shr AShift) < 255) then
                 LandPixels[by, bx]:= 0
             end;
 t:= y - dx;
 if (t and LAND_HEIGHT_MASK) = 0 then
-    for i:= Max(x - dy, 0) to Min(x + dy, LAND_WIDTH - 1) do
+    for i:= Min(x + dy, LAND_WIDTH - 1) downto Max(x - dy, 0) do 
         if (Land[t, i] and lfIndestructible) = 0 then
             begin
             if (cReducedQuality and rqBlurryLand) = 0 then
@@ -331,7 +331,7 @@
                 inc(cnt);
                 LandPixels[by, bx]:= LandBackPixel(i, t)
                 end
-            else if ((Land[t, i] and lfObject) <> 0) or (((LandPixels[by,bx] and AMask) shr AShift) < 255) then 
+            else if ((Land[t, i] and lfObject) <> 0) or (((LandPixels[by,bx] and AMask) shr AShift) < 255) then
                 LandPixels[by, bx]:= 0
             end;
 FillLandCircleLinesBG:= cnt;
@@ -342,7 +342,7 @@
 begin
 t:= y + dy;
 if (t and LAND_HEIGHT_MASK) = 0 then
-    for i:= Max(x - dx, 0) to Min(x + dx, LAND_WIDTH - 1) do
+    for i:= Min(x + dx, LAND_WIDTH - 1) downto Max(x - dx, 0) do 
         if ((Land[t, i] and lfBasic) <> 0) or ((Land[t, i] and lfObject) <> 0) then
             begin
             if (cReducedQuality and rqBlurryLand) = 0 then
@@ -357,7 +357,7 @@
 
 t:= y - dy;
 if (t and LAND_HEIGHT_MASK) = 0 then
-    for i:= Max(x - dx, 0) to Min(x + dx, LAND_WIDTH - 1) do
+    for i:= Min(x + dx, LAND_WIDTH - 1) downto Max(x - dx, 0) do 
         if ((Land[t, i] and lfBasic) <> 0) or ((Land[t, i] and lfObject) <> 0) then
             begin
             if (cReducedQuality and rqBlurryLand) = 0 then
@@ -371,7 +371,7 @@
 
 t:= y + dx;
 if (t and LAND_HEIGHT_MASK) = 0 then
-    for i:= Max(x - dy, 0) to Min(x + dy, LAND_WIDTH - 1) do
+    for i:= Min(x + dy, LAND_WIDTH - 1) downto Max(x - dy, 0) do 
         if ((Land[t, i] and lfBasic) <> 0) or ((Land[t, i] and lfObject) <> 0) then
             begin
             if (cReducedQuality and rqBlurryLand) = 0 then
@@ -386,7 +386,7 @@
 
 t:= y - dx;
 if (t and LAND_HEIGHT_MASK) = 0 then
-    for i:= Max(x - dy, 0) to Min(x + dy, LAND_WIDTH - 1) do
+    for i:= Min(x + dy, LAND_WIDTH - 1) downto Max(x - dy, 0) do 
         if ((Land[t, i] and lfBasic) <> 0) or ((Land[t, i] and lfObject) <> 0) then
             begin
             if (cReducedQuality and rqBlurryLand) = 0 then
@@ -489,8 +489,8 @@
 begin
 for i:= 0 to Pred(Count) do
     begin
-    for ty:= Max(y - Radius, 0) to Min(y + Radius, LAND_HEIGHT) do
-        for tx:= Max(0, ar^[i].Left - Radius) to Min(LAND_WIDTH, ar^[i].Right + Radius) do
+    for ty:= Min(y + Radius, LAND_HEIGHT) downto Max(y - Radius, 0) do 
+        for tx:= Min(LAND_WIDTH, ar^[i].Right + Radius) downto Max(0, ar^[i].Left - Radius) do 
             begin
             if (Land[ty, tx] and lfIndestructible) = 0 then
                 begin
@@ -504,7 +504,7 @@
                     end;
                 if ((Land[ty, tx] and lfBasic) <> 0) and (((LandPixels[by,bx] and AMask) shr AShift) = 255) and (not disableLandBack) then
                     LandPixels[by, bx]:= LandBackPixel(tx, ty)
-                else if ((Land[ty, tx] and lfObject) <> 0) or (((LandPixels[by,bx] and AMask) shr AShift) < 255) then 
+                else if ((Land[ty, tx] and lfObject) <> 0) or (((LandPixels[by,bx] and AMask) shr AShift) < 255) then
                     LandPixels[by, bx]:= 0
                 end
             end;
@@ -516,8 +516,8 @@
 
 for i:= 0 to Pred(Count) do
     begin
-    for ty:= Max(y - Radius, 0) to Min(y + Radius, LAND_HEIGHT) do
-        for tx:= Max(0, ar^[i].Left - Radius) to Min(LAND_WIDTH, ar^[i].Right + Radius) do
+    for ty:= Min(y + Radius, LAND_HEIGHT) downto Max(y - Radius, 0) do 
+        for tx:= Min(LAND_WIDTH, ar^[i].Right + Radius) downto Max(0, ar^[i].Left - Radius) do 
             if ((Land[ty, tx] and lfBasic) <> 0) or ((Land[ty, tx] and lfObject) <> 0) then
                 begin
                  if (cReducedQuality and rqBlurryLand) = 0 then
@@ -567,7 +567,7 @@
     and ((tx and LAND_WIDTH_MASK) = 0)
     and (((Land[ty, tx] and lfBasic) <> 0) or ((Land[ty, tx] and lfObject) <> 0)) then
         begin
-        if despeckle then 
+        if despeckle then
             begin
             Land[ty, tx]:= Land[ty, tx] or lfDamaged;
             LandDirty[ty div 32, tx div 32]:= 1
@@ -837,7 +837,7 @@
 procedure Smooth(X, Y: LongInt);
 begin
 // a bit of AA for explosions
-if (Land[Y, X] = 0) and (Y > LongInt(topY) + 1) and 
+if (Land[Y, X] = 0) and (Y > LongInt(topY) + 1) and
     (Y < LAND_HEIGHT-2) and (X > LongInt(leftX) + 1) and (X < LongInt(rightX) - 1) then
     begin
     if ((((Land[y, x-1] and lfDamaged) <> 0) and (((Land[y+1,x] and lfDamaged) <> 0)) or ((Land[y-1,x] and lfDamaged) <> 0))