[Verse-dev] Network packing of real64s (doubles)

Emil Brink verse-dev@blender.org
Fri, 19 Mar 2004 14:07:17 +0100 (MET)


Hello.

This is mainly directed at Eskil, but I wrote it to the list since
it's possible that someone out there has already started to depend in
one way or another on the Verse 2 codebase and network formats.

Background: I'm working on writing a decent Verse specification, and
am currently talking about the network encoding of various data
types. I'm not happy with the way 64-bit floating points (called
"real64" in Verse, generally implemented as doubles) are packed.

Eskil's current code looks like this:

size_t vnp_raw_pack_double(void *buffer, double data)
{
        uint32 size;
        void *p = &data;

        size = vnp_raw_pack_uint32(buffer, ((uint32 *)p)[0]);
        size += vnp_raw_pack_uint32(&((uint8 *)buffer)[4], ((uint32 *)p)[1]);
        return size;
}

The problem is not obvious, at least not to me. A 64-bit quantity is
packed by splitting it into two 32-bit quantities, and then packing
those as uint32's. Sounds fine, right? Well...

The problem is that this does not do anything to control the byte
ordering of the number. Unlike pack_uint32(), which keeps the number
in a register and arithmetically splits it into bytes that can be
emitted in a known order, this code depends on the *in-memory* format
for doubles on the hardware where it runs.

For example, on my Intel laptop, what we get after packing is this:

>  1 packs as: 00 00 00 00 3F F0 00 00
> -1 packs as: 00 00 00 00 BF F0 00 00

Careful inspection shows that the only change is that the high bit of
a single byte is set in the negative case; this is of course the sign
bit. The sign bit *should be* the most significant bit of the number,
so the expected big-endian packing would be 3F F0 00 00 00 00 00 00
and BF F0 00 00 00 00 00 00, respectively.

Instead, we get this bastardized format of two big-endian 32-bit
words, which appear in little-endian order. To me, this feels a
rather great deal icky.

If run on the SPARC-based Solaris machine I have access to at KTH, we
get this:

 1 packs as: 3F F0 00 00 00 00 00 00
-1 packs as: BF F0 00 00 00 00 00 00

Somebody sound the alarm, please.

Regards,

/Emil