Parcourir la source

added float encoding

cbor
Ludovic 'Archivist' Lagouardette il y a 4 ans
Parent
révision
3f3f389b11
1 fichiers modifiés avec 32 ajouts et 1 suppressions
  1. +32
    -1
      include/gp/enveloppe/cbor.hpp

+ 32
- 1
include/gp/enveloppe/cbor.hpp Voir le fichier

@ -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<gp::pair<cbor_value, cbor_value>>{alloc};
}
static auto encode_float(buffer<std::byte> dest, cbor_floating_point& value) {
switch(value.type()) {
case cbor_floating_point::alt<ieee754_hf>():{
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<gp::endian_wrapper<ieee754_hf, gp::endian::big>>())[0] = value.value<ieee754_hf>();
return dest.begin()+3;
}
case cbor_floating_point::alt<float>():{
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<gp::endian_wrapper<float, gp::endian::big>>())[0] = value.value<float>();
return dest.begin()+5;
}
case cbor_floating_point::alt<double>():{
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<gp::endian_wrapper<double, gp::endian::big>>())[0] = value.value<double>();
return dest.begin()+9;
}
default: return dest.begin();
}
}
static auto encode_length(buffer<std::byte> 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<cbor_value>::alt<cbor_floating_point>(): {
return encode_float(dest, contents.value<cbor_floating_point>());
}
}
}

Chargement…
Annuler
Enregistrer