cr88192
9/15/2014 11:19:00 AM
On 9/14/2014 4:05 PM, Ben Bacarisse wrote:
> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:
>
>> On Sun, 14 Sep 2014 15:31:31 -0400, Bill Cunningham wrote:
>>
>>> I just write in C. With some
>>> machines you have to re-write code because of the machine's endianess. That
>>> might only be with assembley and old assemblers I don't know.
>>
>> That depends on how the code is written. With C chances are high, because
>> it does not offer any means to handle endianness nor to declare types in a
>> machine-independent way. But I guess people like C mostly for its
>> deficiencies.
>
> What means do other languages offer to handle endianness?
>
in my scripting language, it is possible to use modifiers which declare
explicit endianess, which effect storage of the datatypes in some cases,
but the VM can ignore it if it will have no effect (typically in
function arguments and local variables).
I have wanted similar in C a few times.
my custom C frontend adds a few modifiers, but isn't really usable at
present.
otherwise, in C one needs to use wrapper functions, which work but are
often less convenient. a wrapper can though be made to work with
different compilers.
a "safer" way to glue it onto C proper would probably be via compiler
intrinsics.
how does one do it in functions?
load/save in terms of bytes and shifts, which is simple/portable but not
particularly efficient;
rely on machine-specific load/store behavior in cases where it is
applicable (such as x86 and newer ARM having unaligned little-endian
loads/stores);
use inline ASM, where one can utilize specific CPU instructions to
handle the endianess efficiently (for example, on x86 one can use
"BSWAP", ...).
though, granted, being able to be like:
i=*(__bigendian __unaligned __int32 *)cs;
would be convinient, even if:
i=bgbbtj_gets32be(cs);
is both more portable and more compact in this case.
more often the modifiers would be used in struct declarations (where the
struct can be declared in a way which makes it, theoretically,
machine-independent).