optimize math (#5287)
[betaflight.git] / src / main / common / encoding.c
blob3823e862a72d3dbd659ac3fd86063ed9ab43cc1a
1 #include "encoding.h"
3 /**
4 * Cast the in-memory representation of the given float directly to an int.
6 * This is useful for printing the hex representation of a float number (which is considerably cheaper
7 * than a full decimal float formatter, in both code size and output length).
8 */
9 uint32_t castFloatBytesToInt(float f)
11 union floatConvert_t {
12 float f;
13 uint32_t u;
14 } floatConvert;
16 floatConvert.f = f;
18 return floatConvert.u;
21 /**
22 * ZigZag encoding maps all values of a signed integer into those of an unsigned integer in such
23 * a way that numbers of small absolute value correspond to small integers in the result.
25 * (Compared to just casting a signed to an unsigned which creates huge resulting numbers for
26 * small negative integers).
28 uint32_t zigzagEncode(int32_t value)
30 return (uint32_t)((value << 1) ^ (value >> 31));