MCPcopy Create free account
hub / github.com/OpenStickCommunity/GP2040-CE / pb_encode_float_as_double

Function pb_encode_float_as_double

lib/nanopb/pb_encode.c:954–999  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

952
953#ifdef PB_CONVERT_DOUBLE_FLOAT
954bool pb_encode_float_as_double(pb_ostream_t *stream, float value)
955{
956 union { float f; uint32_t i; } in;
957 uint_least8_t sign;
958 int exponent;
959 uint64_t mantissa;
960
961 in.f = value;
962
963 /* Decompose input value */
964 sign = (uint_least8_t)((in.i >> 31) & 1);
965 exponent = (int)((in.i >> 23) & 0xFF) - 127;
966 mantissa = in.i & 0x7FFFFF;
967
968 if (exponent == 128)
969 {
970 /* Special value (NaN etc.) */
971 exponent = 1024;
972 }
973 else if (exponent == -127)
974 {
975 if (!mantissa)
976 {
977 /* Zero */
978 exponent = -1023;
979 }
980 else
981 {
982 /* Denormalized */
983 mantissa <<= 1;
984 while (!(mantissa & 0x800000))
985 {
986 mantissa <<= 1;
987 exponent--;
988 }
989 mantissa &= 0x7FFFFF;
990 }
991 }
992
993 /* Combine fields */
994 mantissa <<= 29;
995 mantissa |= (uint64_t)(exponent + 1023) << 52;
996 mantissa |= (uint64_t)sign << 63;
997
998 return pb_encode_fixed64(stream, &mantissa);
999}
1000#endif

Callers 5

pb_enc_fixedFunction · 0.85
write_doubleFunction · 0.85
write_repeated_doubleFunction · 0.85
write_doubleFunction · 0.85
write_repeated_doubleFunction · 0.85

Calls 1

pb_encode_fixed64Function · 0.85

Tested by 4

write_doubleFunction · 0.68
write_repeated_doubleFunction · 0.68
write_doubleFunction · 0.68
write_repeated_doubleFunction · 0.68