Re: [Algorithms] Byte Swapping
Brought to you by:
vexxed72
|
From: Jens A. <jAy...@ne...> - 2000-07-16 07:14:20
|
Tom Hubina:
> Jens Ayton:
>> Tom Hubina:
>>>
>>> I can do the obvious method, but I was wondering if anyone
>>> have any cool/fast code snippets for byte swapping integers
>>> and floats?
>>
>> Depends on the platform. PowerPCs have "load word byte-
>> reversed indexed" (lwbrx) and "load halfword byte-reversed
>> indexed" (lhbrx) instructions, for instance.
>
> Since that happens to be my platform, perhaps you would have
> some routines handy that work in Codewarrior for the Mac that
> make those calls to reverse the bytes I'm loading?
These instructions are available as intrinsics. If you have an aligned
array theLittleEndianArrayOfLongs of wrong-endian longs, this:
long theLong = __lwbrx(theLittleEndianArrayOfLongs, n);
is equivalent to
long theLong = theBigEndianArrayOfLongs[n];
for right-endian data. Similarly, you can use
short theShort = __lhbrx(theLittleEndianArrayOfShorts, n);
^
Note the "w" vs. "h" as marked.
--
Jens Ayton
|