Skip to content

pack double and float in a more compact way #1017

@GeorgFritze

Description

@GeorgFritze

As the spec states:

If an object can be represented in multiple possible output formats, serializers SHOULD use the format which represents the data in the smallest number of bytes.

should double and float be packed in the smallest way possible.
Currently double and floats are packed in full size. Even if they store the value 0. The following change would "fix" this issue for double.

template <typename Stream>
inline packer<Stream>& packer<Stream>::pack_double(double d)
{
    if (d == int64_t(d))
    {
        pack_imp_int64(int64_t(d));
        return *this;
    }

    union { double f; uint64_t i; } mem;
    mem.f = d;
    char buf[9];
    buf[0] = static_cast<char>(0xcbu);

#if defined(TARGET_OS_IPHONE)
    // ok
#elif defined(__arm__) && !(__ARM_EABI__) // arm-oabi
    // https://github.com/msgpack/msgpack-perl/pull/1
    mem.i = (mem.i & 0xFFFFFFFFUL) << 32UL | (mem.i >> 32UL);
#endif
    _msgpack_store64(&buf[1], mem.i);
    append_buffer(buf, 9);
    return *this;
}

#1018

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions