[fpc-pascal] Byte order modification in fpc on ARM?

Bo Berglund bo.berglund at gmail.com
Mon Oct 19 16:23:06 CEST 2015


On Mon, 19 Oct 2015 15:30:36 +0200 (CEST), Michael Van Canneyt
<michael at freepascal.org> wrote:

>There are compiler defines that give you the endianness.
>{$IFDEF ENDIAN_BIG}
>{$IFDEF ENDIAN_LITTLE}
>
>And
>{$IFDEF FPC_LITTLE_ENDIAN}
>{$IFDEF FPC_BIG_ENDIAN}

Good, that would probably be what I need. Thanks!
Example:

procedure Swap4 (var Source);
{Swap the byte order of a 4-byte variable so it matches the PC i86}
type
    Reverse = packed record
      B1 : Byte;
      I2 : Word;
      B4 : Byte;
    end;
var
    Bsw : Byte;
begin
  {$IFDEF FPC_BIG_ENDIAN}
    Bsw :=  Reverse(Source).B1;
    Reverse(Source).B1 := Reverse(Source).B4;
    Reverse(Source).B4 := Bsw;
    Reverse(Source).I2 := Swap(Reverse(Source).I2);
  {$ENDIF}
end;

Then all functions calling these functions could stay as they are and
it will work on both the PC and Raspberry Pi2.

Just have to make sure I don't have the endians reversed...


-- 
Bo Berglund
Developer in Sweden




More information about the fpc-pascal mailing list