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

ByteSwap swap80 assumes real has size 10

Moderators: kris

Posted: 09/22/07 11:29:27

I just noticed that swap80 in ByteSwap? is written to swap exactly 10 bytes, but uses real.sizeof when decrementing the byte count. Also, the endian protocol uses swap80 for reals.

With my current setup (dmd on an AMD64 linux system) I get told reals have size 12 - aren't there going to be problems if endian swapping is used here? Besides which, the spec simply says a real uses the largest size fitting into hardware, with a note saying this is 80 bits on intel CPUs, so even if a real always was 10 bytes on current systems, that could change in the future. (Also surely 32bit CPUs don't support 10byte reals?)

The reason I didn't post a ticket though is because I'm not really certain this is a problem; e.g. if despite reals having size 12 on my system only the first 10 bytes are used.

Author Message

Posted: 09/23/07 19:21:51

swap80 just swaps 80bits. The only correlation with 'real' is applied elsewhere, so there's nothing wrong with swap80 itself. While you'll sometimes find individual instances of 'real' are aligned on a 16 byte boundary, it's likely that real[] requires tight packing? That's something to try. And yes, 'real' is ten bytes on x86

Posted: 09/23/07 19:58:49

Well, there is a combination of facts there - the size of a real (as passed to a function) is 12 bytes on Linux. However, the D spec states that it will be padded for the most significant bytes, such that only 10 are actually used for the value, see function parameters on D's ABI page.

Posted: 09/26/07 19:43:38

No, the swap80 does use the size of real, since it moves real.sizeof bytes between swaps (skipping 2 bytes on 12 byte systems), so really it's specific to reals.

OK, so those routines are valid on current systems. Whether or not that holds in the future partly depends on which part of the spec is held then - that reals are 10-bytes in size (as in the ABI) or that they use the largest size supported by hardware operations (as in the types section).

Thanks for explaining.