Browse Source

* optimized align() so it no longer contains any branches which
are undecidable at compile time
+ basic test for align() function

git-svn-id: trunk@9674 -

Jonas Maebe 17 years ago
parent
commit
bf9b021749
3 changed files with 32 additions and 8 deletions
  1. 1 0
      .gitattributes
  2. 8 8
      rtl/inc/generic.inc
  3. 23 0
      tests/test/units/system/talign.pp

+ 1 - 0
.gitattributes

@@ -7355,6 +7355,7 @@ tests/test/units/strings/tstrcopy.pp svneol=native#text/plain
 tests/test/units/strings/tstrings1.pp svneol=native#text/plain
 tests/test/units/system/interlocked1.pp svneol=native#text/plain
 tests/test/units/system/tabs.pp svneol=native#text/plain
+tests/test/units/system/talign.pp svneol=native#text/plain
 tests/test/units/system/targs.pp svneol=native#text/plain
 tests/test/units/system/tassert1.pp svneol=native#text/plain
 tests/test/units/system/tassert2.pp svneol=native#text/plain

+ 8 - 8
rtl/inc/generic.inc

@@ -1507,20 +1507,20 @@ procedure inclocked(var l:int64);
 
 
 function align(addr : PtrUInt;alignment : PtrUInt) : PtrUInt;{$ifdef SYSTEMINLINE}inline;{$endif}
+  var
+    tmp: PtrUInt;
   begin
-    if addr mod alignment<>0 then
-      result:=addr+(alignment-(addr mod alignment))
-    else
-      result:=addr;
+    tmp:=addr+alignment-1;
+    result:=tmp-(tmp mod alignment)
   end;
 
 
 function align(addr : Pointer;alignment : PtrUInt) : Pointer;{$ifdef SYSTEMINLINE}inline;{$endif}
+  var
+    tmp: PtrUInt;
   begin
-    if PtrUInt(addr) mod alignment<>0 then
-      result:=pointer(addr+(alignment-(PtrUInt(addr) mod alignment)))
-    else
-      result:=addr;
+    tmp:=PtrUInt(addr)+alignment-1;
+    result:=pointer(tmp-(tmp mod alignment));
   end;
 
 

+ 23 - 0
tests/test/units/system/talign.pp

@@ -0,0 +1,23 @@
+var
+  p: pointer;
+  u: ptruint;
+  i: cardinal;
+begin
+  p:=pointer(1);
+  for i:=0 to 15 do
+    if align(p+i,16)<>pointer(16) then
+      halt(1);
+  p:=pointer(41);
+  for i:=0 to 39 do
+    if align(p+i,40)<>pointer(80) then
+      halt(2);
+
+  u:=1;
+  for i:=0 to 15 do
+    if align(u+i,16)<>16 then
+      halt(3);
+  u:=41;
+  for i:=0 to 39 do
+    if align(u+i,40)<>80 then
+      halt(4);
+end.