5#ifndef BITCOIN_SERIALIZE_INTCODE_H
6#define BITCOIN_SERIALIZE_INTCODE_H
25 0x0100'0000'0000'0000,
47template <
typename Stream>
void WriteIntcode(Stream &os, uint64_t value) {
55 const uint64_t numBits = std::bit_width(value);
57 uint64_t numBytes = (numBits - 1) / 7;
64 const uint64_t header = (0xff << (8 - numBytes)) & 0xff;
66 value |= header << (8 * numBytes);
70 value <<= 8 * (8 - numBytes);
74 for (
size_t i = 0; i < numBytes; ++i) {
95 const uint64_t leadingOnes = 8 - std::bit_width(uint8_t(~header));
98 const uint8_t mask = 0xff >> leadingOnes;
99 uint64_t result = header & mask;
102 for (
size_t i = 0; i < leadingOnes; ++i) {
109 "non-canonical ReadMitraInt(): 0x%016x out of range for %d "
111 result, leadingOnes));
uint8_t ser_readdata8(Stream &s)
void ser_writedata8(Stream &s, uint8_t obj)
Lowest-level serialization and conversion.
const uint64_t VALID_RANGE[]
uint64_t ReadIntcode(Stream &is)
Read a 64-bit integer as intcode, see WriteIntcode.
void WriteIntcode(Stream &os, uint64_t value)
Write a 64-bit integer in intcode encoding: