Commit 4d62db9f authored by jan.koester's avatar jan.koester
Browse files

test

parent cd7671cb
Loading
Loading
Loading
Loading
+31 −23
Original line number Diff line number Diff line
@@ -621,20 +621,28 @@ void quic::pmtudTick() {
// ============================================================================

size_t quic::encodeVarInt(uint64_t value, uint8_t* out) {
    if (value <= 63) {
    // Branchless length-class derivation (mirrors decodeVarInt's bit-shift
    // approach below): each comparison is unconditional and summed instead
    // of a short-circuiting if/else-if chain, so classification itself
    // never depends on branch prediction guessing right for whatever
    // distribution of values a given call site happens to produce (e.g.
    // monotonically growing stream offsets crossing a threshold).
    unsigned length_class = (value > 63) + (value > 16383) + (value > 1073741823);
    switch (length_class) {
        case 0:
            out[0] = static_cast<uint8_t>(value);
            return 1;
    } else if (value <= 16383) {
        case 1:
            out[0] = static_cast<uint8_t>(0x40 | (value >> 8));
            out[1] = static_cast<uint8_t>(value & 0xFF);
            return 2;
    } else if (value <= 1073741823) {
        case 2:
            out[0] = static_cast<uint8_t>(0x80 | (value >> 24));
            out[1] = static_cast<uint8_t>((value >> 16) & 0xFF);
            out[2] = static_cast<uint8_t>((value >> 8) & 0xFF);
            out[3] = static_cast<uint8_t>(value & 0xFF);
            return 4;
    } else {
        default:
            out[0] = static_cast<uint8_t>(0xC0 | (value >> 56));
            out[1] = static_cast<uint8_t>((value >> 48) & 0xFF);
            out[2] = static_cast<uint8_t>((value >> 40) & 0xFF);