diff --git a/src/core/math_func.cpp b/src/core/math_func.cpp index 2590087809..3a62fcd303 100644 --- a/src/core/math_func.cpp +++ b/src/core/math_func.cpp @@ -162,3 +162,23 @@ uint32 IntCbrt(uint64 num) return ((uint32) r1); /* floor(cbrt(x)); */ } + +/** + * Compress unsigned integer into 16 bits, in a way that increases dynamic range, at the expense of precision for large values + */ +uint16 RXCompressUint(uint32 num) +{ + if (num <= 0x100) return num; + if (num <= 0x7900) return 0x100 + ((num - 0x100) >> 3); + return std::min(UINT16_MAX, 0x1000 + ((num - 0x7900) >> 6)); +} + +/** + * Inverse of RXCompressUint + */ +uint32 RXDecompressUint(uint16 num) +{ + if (num > 0x1000) return ((num - 0x1000) << 6) + 0x7900; + if (num > 0x100) return ((num - 0x100) << 3) + 0x100; + return num; +} diff --git a/src/core/math_func.hpp b/src/core/math_func.hpp index 6408876d95..7d5bfa008b 100644 --- a/src/core/math_func.hpp +++ b/src/core/math_func.hpp @@ -376,4 +376,7 @@ uint32 IntSqrt(uint32 num); uint32 IntSqrt64(uint64 num); uint32 IntCbrt(uint64 num); +uint16 RXCompressUint(uint32 num); +uint32 RXDecompressUint(uint16 num); + #endif /* MATH_FUNC_HPP */