| 68 | // (modulo 2^n where n is the number of bits used to represent the unsigned type) |
| 69 | template <typename Unsigned, typename Signed = std::make_signed_t<Unsigned>> |
| 70 | Signed ModularCast(Unsigned value) |
| 71 | { |
| 72 | static_assert(std::is_unsigned<Unsigned>::value, "T should be an unsigned type"); |
| 73 | |
| 74 | if (value <= static_cast<Unsigned>(std::numeric_limits<Signed>::max())) |
| 75 | return static_cast<Signed>(value); |
| 76 | |
| 77 | auto constexpr minSignedT = std::numeric_limits<Signed>::min(); |
| 78 | return static_cast<Signed>(value - static_cast<Unsigned>(minSignedT)) + minSignedT; |
| 79 | } |
| 80 | |
| 81 | // Encodes current as delta compared with prev. |
| 82 | template <typename T, typename UnsignedT = std::make_unsigned_t<T>> |
no outgoing calls