From 3f3f389b110c345f20579bfbb8a0cc03cd526724 Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Wed, 14 Oct 2020 14:47:58 +0200 Subject: [PATCH] added float encoding --- include/gp/enveloppe/cbor.hpp | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/include/gp/enveloppe/cbor.hpp b/include/gp/enveloppe/cbor.hpp index 41df107..f370e35 100644 --- a/include/gp/enveloppe/cbor.hpp +++ b/include/gp/enveloppe/cbor.hpp @@ -70,7 +70,14 @@ namespace gp { {} }; + struct ieee754_hf final { + uint16_t sign : 1; + uint16_t exponent : 5; + uint16_t mantissa : 10; + }; + using cbor_floating_point = gp::fixed_variant< + ieee754_hf, float, double >; @@ -152,6 +159,30 @@ namespace gp { return gp::vector>{alloc}; } + static auto encode_float(buffer dest, cbor_floating_point& value) { + switch(value.type()) { + case cbor_floating_point::alt():{ + if(dest.size() < 3) return dest.begin(); + dest[0] = std::byte(((uint8_t)cbor_type::oths << 5u) + (uint8_t)cbor_oths::word); + (dest.slice_start(3).slice_end(2).cast>())[0] = value.value(); + return dest.begin()+3; + } + case cbor_floating_point::alt():{ + if(dest.size() < 5) return dest.begin(); + dest[0] = std::byte(((uint8_t)cbor_type::oths << 5u) + (uint8_t)cbor_oths::dword); + (dest.slice_start(5).slice_end(4).cast>())[0] = value.value(); + return dest.begin()+5; + } + case cbor_floating_point::alt():{ + if(dest.size() < 9) return dest.begin(); + dest[0] = std::byte(((uint8_t)cbor_type::oths << 5u) + (uint8_t)cbor_oths::qword); + (dest.slice_start(9).slice_end(8).cast>())[0] = value.value(); + return dest.begin()+9; + } + default: return dest.begin(); + } + } + static auto encode_length(buffer dest, cbor_type major, uint64_t value) { auto num = value; if(value <= 23) { @@ -230,7 +261,7 @@ namespace gp { return dest.begin()+1; } case cbor_composite::alt(): { - + return encode_float(dest, contents.value()); } } }