def quantize_coefficients(qlp_precision, lp_coefficients, order):
    """returns a (qlp_coeffs, qlp_shift_needed) pair
    where qlp_coeffs is a list of ints
    and qlp_shift_needed is a non-negative integer"""
    from math import log
    l = max(map(abs, lp_coefficients[order - 1]))
    if l > 0:
        qlp_shift_needed = min(qlp_precision - 1 - (int(log(l) / log(2)) - 
            1) - 1, 2 ** 4 - 1)
    else:
        qlp_shift_needed = 0
    if qlp_shift_needed < -2 ** 4:
        raise ValueError('too much negative shift needed')
    qlp_max = 2 ** (qlp_precision - 1) - 1
    qlp_min = -2 ** (qlp_precision - 1)
    error = 0.0
    qlp_coeffs = []
    if qlp_shift_needed >= 0:
        for lp_coeff in lp_coefficients[order - 1]:
            error += lp_coeff * 2 ** qlp_shift_needed
            qlp_coeffs.append(min(max(int(round(error)), qlp_min), qlp_max))
            error -= qlp_coeffs[-1]
        return qlp_coeffs, qlp_shift_needed
    else:
        for lp_coeff in lp_coefficients[order - 1]:
            error += lp_coeff / 2 ** -qlp_shift_needed
            qlp_coeffs.append(min(max(int(round(error)), qlp_min), qlp_max))
            error -= qlp_coeffs[-1]
        return qlp_coeffs, 0
