Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Endiannes in tango

Posted: 01/28/10 20:43:02

I am looking at tango\io\stream\Data.d and I find statement like "version (BigEndian?) flip = e is Little;". There was similar thing in tango.protocol. If I understand the code correctly it determines if it needs to flip the data. But I don't see how version(BigEndian?) makes sense. If I compile tango with -version=BigEndian? and then run it on a machine that is little endian and pass little endian data then there is actually no need to flip. Or am I wrong? Shouldn't version(BigEndian?) be replaced with a runtime check?

Author Message

Posted: 01/28/10 21:11:08

BigEndian is one of those version identifiers set by the compiler (or is supposed to be), and identifies the architecture of the platform being targeted by the compilation. That info is used by Data.d to determine how to translate multi-byte values, given a desired external representation (set via the endian() method).

There may be a bug there, but I believe it operates correctly?

Posted: 01/29/10 07:18:20

Well I'm not into this stuff so I'm not sure. It is the fist time I write code that cares about endiannes :) I thought how bytes are arranged in memory depends on the CPU? So I thought if you compile it for one CPU and run it on another the code may fail and it is better to check how the cpu is operating in runtime. Or does all cpus that can run, say Linux, use the same endiannes? Or it depends not on the CPU but on something else, like the OS? My point, if I am right, is that you can not write portable code with endiannes determined at compile time - there is need for runtime check.

Posted: 01/29/10 08:54:07 -- Modified: 01/29/10 08:58:42 by
kris

How would you execute a binary compiled for (say) big-endian on (say) a little-endian target? I suspect you'd have to recompile it? I'm aware that some CPU architectures can switch their endian-ness, but that's a whole different matter from trying to run (say) an Intel binary on a Motorola CPU ...

Tango currently asserts that an endian platform-change requires a recompile. If that's inappropriate then please let us know?

(having read the post again I should add that endian-ness is specific to a CPU architecture. For example: x86 is little-endian, whereas Motorola CPUs [and Vax etc] are generally big-endian)

Posted: 01/29/10 10:46:30

I see. Forgive my lack of knowledge on the matter. Explained in this way it seems perfectly fine. Thanks.