FIDO-Raw
view release on metacpan or search on metacpan
deps/libcbor/src/cbor/encoding.c view on Meta::CPAN
size_t cbor_encode_half(float value, unsigned char *buffer,
size_t buffer_size) {
/* Assuming value is normalized */
uint32_t val = ((union _cbor_float_helper){.as_float = value}).as_uint;
uint16_t res;
uint8_t exp = (uint8_t)((val & 0x7F800000u) >>
23u); /* 0b0111_1111_1000_0000_0000_0000_0000_0000 */
uint32_t mant =
val & 0x7FFFFFu; /* 0b0000_0000_0111_1111_1111_1111_1111_1111 */
if (exp == 0xFF) { /* Infinity or NaNs */
if (value != value) {
res = (uint16_t)0x007e00; /* Not IEEE semantics - required by CBOR
[s. 3.9] */
} else {
res = (uint16_t)((val & 0x80000000u) >> 16u | 0x7C00u |
(mant ? 1u : 0u) << 15u);
}
} else if (exp == 0x00) { /* Zeroes or subnorms */
res = (uint16_t)((val & 0x80000000u) >> 16u | mant >> 13u);
} else { /* Normal numbers */
deps/libcbor/src/cbor/encoding.h view on Meta::CPAN
size_t cbor_encode_undef(unsigned char *, size_t);
/** Encodes a half-precision float
*
* Since there is no native representation or semantics for half floats
* in the language, we use single-precision floats, as every value that
* can be expressed as a half-float can also be expressed as a float.
*
* This however means that not all floats passed to this function can be
* unambiguously encoded. The behavior is as follows:
* - Infinity, NaN are preserved
* - Zero is preserved
* - Denormalized numbers keep their sign bit and 10 most significant bit of
* the significand
* - All other numbers
* - If the logical value of the exponent is < -24, the output is zero
* - If the logical value of the exponent is between -23 and -14, the output
* is cut off to represent the 'magnitude' of the input, by which we
* mean (-1)^{signbit} x 1.0e{exponent}. The value in the significand is
* lost.
* - In all other cases, the sign bit, the exponent, and 10 most significant
s++; if (s == send || (*s != 'F' && *s != 'f')) return 0;
s++; if (s < send && (*s == 'I' || *s == 'i')) {
s++; if (s == send || (*s != 'N' && *s != 'n')) return 0;
s++; if (s == send || (*s != 'I' && *s != 'i')) return 0;
s++; if (s == send || (*s != 'T' && *s != 't')) return 0;
s++; if (s == send || (*s != 'Y' && *s != 'y')) return 0;
s++;
}
sawinf = 1;
} else if (*s == 'N' || *s == 'n') {
/* XXX TODO: There are signaling NaNs and quiet NaNs. */
s++; if (s == send || (*s != 'A' && *s != 'a')) return 0;
s++; if (s == send || (*s != 'N' && *s != 'n')) return 0;
s++;
sawnan = 1;
} else
return 0;
if (sawinf) {
numtype &= IS_NUMBER_NEG; /* Keep track of sign */
numtype |= IS_NUMBER_INFINITY | IS_NUMBER_NOT_INT;
( run in 0.294 second using v1.01-cache-2.11-cpan-4d50c553e7e )