示例#1
0
def FPDiv(a, b, k, f, kappa, simplex_flag=False):
    """
        Goldschmidt method as presented in Catrina10,
    """
    theta = int(ceil(log(k/3.5) / log(2)))
    alpha = two_power(2*f)
    w = AppRcr(b, k, f, kappa, simplex_flag)
    x = alpha - b * w

    y = a * w
    y = TruncPr(y, 2*k, f, kappa)

    for i in range(theta):
        y = y * (alpha + x)
        x = x * x
        y = TruncPr(y, 2*k, 2*f, kappa)
        x = TruncPr(x, 2*k, 2*f, kappa)

    y = y * (alpha + x)
    y = TruncPr(y, 2*k, 2*f, kappa)
    return y
示例#2
0
def AppRcr(b, k, f, kappa, simplex_flag=False):
    """
        Approximate reciprocal of [b]:
        Given [b], compute [1/b]
    """
    alpha = cint(int(2.9142 * 2**k))
    c, v = Norm(b, k, f, kappa, simplex_flag)
    #v should be 2**{k - m} where m is the length of the bitwise repr of [b]
    d = alpha - 2 * c
    w = d * v
    w = TruncPr(w, 2 * k, 2 * (k - f))
    # now w * 2 ^ {-f} should be an initial approximation of 1/b
    return w
示例#3
0
 def block(i):
     A[i] = TruncPr(A[i - 1] * W[i - 1], 2*k, f, kappa)
     temp = (B[i - 1] * W[i - 1]) >> f
     # no reading and writing to the same variable in a for loop.
     W[i] = two - temp
     B[i] = temp