Ejemplo n.º 1
0
def test_fpf_mul():
    W1 = fpf(8,4)  # W8.4.3
    W2 = fpf(8,4)  # W8.4.3
    Wa = W1*W2
    wl,iwl,fwl = Wa[:]
    assert iwl == 9, '%s+%s==%s'  % (W1,W2,Wa)
    assert fwl == 6, '%s+%s==%s'  % (W1,W2,Wa)
    assert wl == 16, '%s+%s==%s'  % (W1,W2,Wa)
Ejemplo n.º 2
0
def test_fpf_inst():
    ewl,eiwl,efwl = (8,4,3)
    W1 = fpf(8,4,3)
    W2 = fpf(8,4)
    W3 = fpf(wl=8,iwl=4,fwl=3)
    W4 = fpf(*(8,4,3,))

    for ww in (W1,W2,W3,W4):
        wl,iwl,fwl = ww[:]
        assert wl == ewl
        assert iwl == eiwl
        assert fwl == efwl
Ejemplo n.º 3
0
def test_fpf_sub():
    # some basic adds
    W1 = fpf(8,4)  # W8.4.3
    W2 = fpf(8,4)  # W8.4.3
    Wa = W1-W2
    wl,iwl,fwl = Wa[:]
    assert iwl == 5, '%s+%s==%s'  % (W1,W2,Wa)
    assert fwl == 3, '%s+%s==%s'  % (W1,W2,Wa)
    assert wl == 9, '%s+%s==%s'  % (W1,W2,Wa)

    W1 = fpf(8,4)  # W8.4.3
    W2 = fpf(8,3)  # W8.3.4
    Wa = W1-W2
    wl,iwl,fwl = Wa[:]
    assert iwl == 5, '%s+%s==%s'  % (W1,W2,Wa)
    assert fwl == 4, '%s+%s==%s'  % (W1,W2,Wa)
    assert wl == 10, '%s+%s==%s'  % (W1,W2,Wa)

    W1 = fpf(16,0)  # W16.0.15
    W2 = fpf(16,0)  # W16.0.15
    Wa = W1-W2
    wl,iwl,fwl = Wa[:]
    assert iwl == 1, '%s+%s==%s'  % (W1,W2,Wa)
    assert fwl == 15, '%s+%s==%s'  % (W1,W2,Wa)
    assert wl == 17, '%s+%s==%s'  % (W1,W2,Wa)    
Ejemplo n.º 4
0
def test_fpf_add():
    W1 = fpf(8,4)
    W2 = fpf(8,4)
    Wa = W1+W2
    wl,iwl,fwl = Wa[:]
    assert iwl == 5, '%s+%s==%s'  % (W1,W2,Wa)
    assert fwl == 3, '%s+%s==%s'  % (W1,W2,Wa)
    assert wl == 9, '%s+%s==%s'  % (W1,W2,Wa)

    W1 = fpf(8,4)  # W8.4.3
    W2 = fpf(8,3)  # W8.3.4
    Wa = W1+W2
    wl,iwl,fwl = Wa[:]
    assert iwl == 5, '%s+%s==%s'  % (W1,W2,Wa)
    assert fwl == 4, '%s+%s==%s'  % (W1,W2,Wa)
    assert wl == 10, '%s+%s==%s'  % (W1,W2,Wa)
    
    W1 = fpf(16,0)  # W16.0.15
    W2 = fpf(16,0)  # W16.0.15
    Wa = W1+W2
    wl,iwl,fwl = Wa[:]
    assert iwl == 1, '%s+%s==%s'  % (W1,W2,Wa)
    assert fwl == 15, '%s+%s==%s'  % (W1,W2,Wa)
    assert wl == 17, '%s+%s==%s'  % (W1,W2,Wa)

    W1 = fpf(16,1)  # W16.1.14
    W2 = fpf(16,3)  # W16.3.12
    Wa = W1+W2
    wl,iwl,fwl = Wa[:]
    assert iwl == 4, '%s+%s==%s'  % (W1,W2,Wa)
    assert fwl == 14, '%s+%s==%s'  % (W1,W2,Wa)
    assert wl == 19, '%s+%s==%s'  % (W1,W2,Wa)

    # negative integer widths, fwl larger than the iwl
    # wl(4) = iwl(-4) + fwl + 1
    # fwl = wl(4) - iwl(-4) -1 
    # fwl = 7
    # when adding the total word-length (wl) should increase
    # the number of fractions should increase (until iwl >= 0)
    # and the iwl should increase.
    W1 = fpf(4,-4)
    W2 = fpf(4,-4)
    Wa = W1+W2
    wl,iwl,fwl = Wa[:]
    assert iwl == -3, '%s+%s==%s'  % (W1,W2,afmt)
    assert fwl == 7, '%s+%s==%s'  % (W1,W2,Wa)
    assert wl == 5, '%s+%s==%s'  % (W1,W2,Wa)

    # the following is a little odd, each /add/ has
    # to make the assumption that an overflow can
    # occur (require one extra bit), but for a /sum/
    # the number of required bits is log(N,2) where N
    # is the number of sums, when adding four operands
    # only two additional bits are needed but the following
    # will generate three, the operations would need a 
    # method to look at the complete operation versus 
    # each individual add
    W1 = fpf(16,randint(3,16))  
    W2 = fpf(16,randint(3,16))
    W3 = fpf(8,randint(3,8))
    W4 = fpf(8,randint(3,8))    
    miwl = max([W1[1],W2[1],W3[1],W4[1]])
    Wa = W1+W2+W3+W4
    wl,iwl,fwl = Wa[:]
    assert iwl == miwl+3, '%s+%s+%s+%s==%s'  % (W1,W2,W3,W4,Wa)

    # cross-over points when negative iwl becomes
    # positive and vise-versa
    W1 = fpf(8, -3)
    W2 = fpf(8, -4)
    niwl = -2
    for ii in range(6):
        W1 = W1 + W2
        assert W1._iwl == niwl
        niwl += 1

    # negative fraction widths
    W1 = fpf(4, 8)
    assert W1._fwl == -5

    # cross-over points when negative fwl becomes
    # positive and vise-versa
    W1 = fpf(4, 8)
    W2 = fpf(10, 8, 1)
    nfwl = -5
    for ii in range(6):
        W1 = W1 * W2
        nfwl += 1
        assert W1._fwl == nfwl