[fpc-devel] wrong results of function "align"

Burkhard Carstens fpc at bcsoft.de
Sun Jul 2 16:03:26 CEST 2006


fpc-2.1.1 r4075
linux-i386

If I understand the purpose of function align 
(rtl/inc/{systemh.inc,generic.inc}) correctly, it gives wrong results 
when used with pointers > $8000000.

Align uses ptrint. Using ptrUint instead works.

I tried using it to align a array to 4k bounds, like this: 
p:=getmem(MyAry,sizeof(MyAry)+4096); p:=align(p,4096);  

IMO align($80000001,$1000) should give $80001000, but gives $80002000.

Attached is a testcase and a patch.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fixalign.patch
Type: text/x-diff
Size: 1889 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20060702/8c4ab42c/attachment.patch>
-------------- next part --------------
program writeroundtest;

{$mode objfpc}{$H+}

var
    ap1,ap2 : pointer;
    

function UINTalign(addr : Pointer;alignment : PtrUInt) : Pointer;
  begin
    if PtrUInt(addr) mod alignment<>0 then
      result:=pointer(addr+(alignment-(PtrUInt(addr) mod alignment)))
    else
      result:=addr;
  end;


begin
  ap1:=pointer($70000001);
  ap2:=pointer($80000001);
  writeln(hexstr(ap1),' --> ',hexstr(align(ap1,$1000)));
  writeln(hexstr(ap2),' --> ',hexstr(align(ap2,$1000)));
  writeln(hexstr(ap2),' --> ',hexstr(UINTalign(ap2,$1000)));
end.



More information about the fpc-devel mailing list