def delta16(v1, v2):
    """Return the delta (difference) between two increasing 16-bit counters,
    accounting for the wraparound from 65535 back to 0"""
    diff = v2 - v1
    if diff < 0:
        diff += (1<<16)
    return diff