[fpc-pascal] Re: Endian issues when porting FPC programs from x86 to other platforms

Thomas Schatzl tom_at_work at gmx.at
Wed Nov 3 10:50:10 CET 2010


Hi,

On Tue, 02 Nov 2010 23:49:03 +0100, Bo Berglund <bo.berglund at gmail.com>
wrote:
> On Tue, 02 Nov 2010 22:53:21 +0100, Vinzent Höfler
> <JeLlyFish.software at gmx.net> wrote:
> 
>>On Tue, 02 Nov 2010 21:39:31 +0100, Felipe Monteiro de Carvalho  
>><felipemonteiro.carvalho at gmail.com> wrote:
>>
>>> On Tue, Nov 2, 2010 at 7:04 PM, Bo Berglund <bo.berglund at gmail.com>  
>>> wrote:
>>>> Thanks, that helps a lot! Are there also overloaded BEtoN functions
>>>> for floating point values?
>>>
>>> I think that single and double have always the same binary layout.
>>
>>Mostly, I'd guess. But I wouldn't count on that.
>>
> I know for sure that on the Motorola system the floats are 4 bytes
> (single precision) and have to be swapped in order to be used on
> Windows. I have done it on Delphi already so I know this is a fact.
> So now I need to find an endian corrector working with floating point
> values as well...

Coming back to the suggestion from jonas:

function BEtoN(const AValue : single) : single;
type
  TData = packed record
    case integer of
      0 : (s : single);
      1 : (l : longint);
  end;
var
  d : TData;
begin
  d.s := AValue;
  d.l := system.BEtoN(d.l);
  result := d.s;
end;

A BEtoN function for doubles is similar, just replace the single type by
double, and longint by int64.

Hth,
  Thomas




More information about the fpc-pascal mailing list