def _div_mod(dividend, divisor, target_precision=64):
    """Perform long division up to the desired target precision.

    Requires that the target ratio (dividend / divisor) falls within
    the interval [1; 2).

    Returns a (quotient, remainder) tuple, where the number of bits in
    the quotient is the requested target precision.
    """
    # Precondition:
    assert dividend > 0
    assert divisor > 0
    assert target_precision >= 1
    assert divisor <= dividend < 2 * divisor

    quotient = 1
    remainder = dividend - divisor
    precision = 1

    def assert_invariant():
        assert quotient >= 1
        assert 0 <= remainder < divisor
        assert 1 <= precision <= target_precision
        prec = precision - 1
        assert (1 << prec) <= quotient < (1 << precision)
        assert quotient * divisor + remainder == dividend << prec
        assert divmod(dividend << prec, divisor) == (quotient, remainder)
        return

    while precision < target_precision:
        assert_invariant()
        quotient <<= 1
        remainder <<= 1
        if remainder >= divisor:
            quotient |= 1
            remainder -= divisor
        precision += 1
    assert_invariant()
    assert precision == target_precision
    return quotient, remainder