1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-25 13:41:56 +03:00

CBOR support for half-precision floats

This commit is contained in:
Niels Lohmann
2016-12-07 21:43:59 +01:00
parent 17c9b17a7e
commit b7e0c12966
3 changed files with 62 additions and 10 deletions

View File

@ -34,7 +34,7 @@ SOFTWARE.
#include <cassert> // assert
#include <cctype> // isdigit
#include <ciso646> // and, not, or
#include <cmath> // isfinite, signbit
#include <cmath> // isfinite, ldexp, signbit
#include <cstddef> // nullptr_t, ptrdiff_t, size_t
#include <cstdint> // int64_t, uint64_t
#include <cstdlib> // strtod, strtof, strtold, strtoul
@ -7169,6 +7169,29 @@ class basic_json
{
return value_t::null;
}
else if (v[current_idx] == 0xf9) // Half-Precision Float
{
idx += 2; // skip two content bytes
// code from RFC 7049, Appendix D
const int half = (v[current_idx + 1] << 8) + v[current_idx + 2];
const int exp = (half >> 10) & 0x1f;
const int mant = half & 0x3ff;
double val;
if (exp == 0)
{
val = std::ldexp(mant, -24);
}
else if (exp != 31)
{
val = std::ldexp(mant + 1024, exp - 25);
}
else
{
val = mant == 0 ? INFINITY : NAN;
}
return half & 0x8000 ? -val : val;
}
else if (v[current_idx] == 0xfa) // Single-Precision Float
{
// copy bytes in reverse order into the float variable