[fpc-devel] Fix for SwapEndian
Joost van der Sluis
joost at cnoc.nl
Tue Apr 29 15:41:05 CEST 2008
Hi all,
I've a problem with BeToN with smallints on a x86 system. When you use
BeToN on smallint's with values like $8000 and $8200 it will return
values like $FF80 and $FF82, instead or $0080 and $0082 respectively.
The patch below fixes this, but I'm not sure if it's a correct fix, or
that it's a bug in the compiler. (ie: is this behaviour of shr/shl
normal on signed smallints?)
If this patch is correct, how about the other signed integer-types?
Patch:
--- rtl/inc/generic.inc (revision 10836)
+++ rtl/inc/generic.inc (working copy)
@@ -1707,7 +1707,7 @@
{$ifndef FPC_SYSTEM_HAS_SWAPENDIAN}
function SwapEndian(const AValue: SmallInt): SmallInt;
begin
- Result := (AValue shr 8) or (AValue shl 8);
+ Result := (word(AValue) shr 8) or (word(AValue) shl 8);
end;
test-program:
program swap;
uses sysutils;
var si1,si2 : smallint;
begin
si1 := $8000;
si2 := $0080;
si1 := beton(si1);
if si1<>si2 then
begin
writeln('Not good:');
writeln(IntToHex(si1,2),' should be ', IntToHex(si2,2));
end;
end.
More information about the fpc-devel
mailing list