Пример #1
0
def matrixmultiplication(a, b):

    c1 = fixbv(0, min=-2, max=2, res=2**-15)
    c2 = fixbv(0, min=-2, max=2, res=2**-15)
    c3 = fixbv(0, min=-2, max=2, res=2**-15)
    c4 = fixbv(0, min=-2, max=2, res=2**-15)
    result = list([[c1, c2], [c3, c4]])

    for i in range(len(a)):

        #print ( "iteration of columns a", i )
        for j in range(len(b[0])):
            #print ( "iteration of rows b[0]", j )
            for k in range(len(b)):
                #print ( "iteration of row b", k )
                #print ( "listof", result[ i ][ j ] )
                result[i][j] += a[i][k] * b[k][j]
    print("Result of matrixmultiplication", result)
    for r in result:

        # print ( "Result of matrixmultiplication: Value = {0}, Resolution = {1}".format ( r[0]._fval, r[0].res ) )
        # print ( "Result of matrixmultiplication: Value = {0}, Resolution = {1}".format ( r[ 1 ]._fval, r[ 1 ].res ) )
        #print ( "Matrix multiplication", format (r) )

        print("Matrix multiplication fval", r[0]._fval, r[1]._fval)
    return
Пример #2
0
def test_math():
    x = fixbv(0.5,  format=W(16,0))  
    y = fixbv(0.25, format=W(16,0))  
    z = fixbv(0, format=W(16,0))
    print(type(x), type(y), type(z))
    w = x + y
    print(w, type(w))
    z[:] = x + y
    print(z, type(z), x+y)
    assert float(z) == 0.75

    x = fixbv(3.5,   min=-8, max=8, res=2**-5)  
    y = fixbv(-5.25, min=-8, max=8, res=2**-5)
    iW = x.W + y.W
    print(iW)
    z = fixbv(0, format=iW)
    z[:] = x + y
    assert float(z) == -1.75

    x = fixbv(3.141592, format=W(19,4))  
    y = fixbv(1.618033, format=W(19,4))
    print(float(x), int(x), repr(x))
    print(float(y), int(y), repr(y))

    iW = x.W * y.W
    print(iW)
    z = fixbv(0, format=iW)
    wl,iwl,fwl = z.W.fmt
    print(repr(z), z._max, z._min, z._nrbits, "iwl, fwl", iwl, fwl)
    
    z[:] = x * y
    print(float(z), int(z), repr(z))
    assert z > 5.
Пример #3
0
def test_math():
    x = fixbv(0.5,  format=W(16,0))  
    y = fixbv(0.25, format=W(16,0))  
    z = fixbv(0, format=W(16,0))
    print(type(x), type(y), type(z))
    w = x + y
    print(w, type(w))
    z[:] = x + y
    print(z, type(z), x+y)
    assert float(z) == 0.75

    x = fixbv(3.5,   min=-8, max=8, res=2**-5)  
    y = fixbv(-5.25, min=-8, max=8, res=2**-5)
    iW = x.W + y.W
    print(iW)
    z = fixbv(0, format=iW)
    z[:] = x + y
    assert float(z) == -1.75

    x = fixbv(3.141592, format=W(19,4))  
    y = fixbv(1.618033, format=W(19,4))
    print(float(x), int(x), repr(x))
    print(float(y), int(y), repr(y))

    iW = x.W * y.W
    print(iW)
    z = fixbv(0, format=iW)
    wl,iwl,fwl = z.W.fmt
    print(repr(z), z._max, z._min, z._nrbits, "iwl, fwl", iwl, fwl)
    
    z[:] = x * y
    print(float(z), int(z), repr(z))
    assert z > 5.
Пример #4
0
def testbench(args):
    """
    """

    run = args.simtype
    N = args.N
    Nloops = args.NLoops
    
    clock = Signal(bool(0))
    reset = ResetSignal(0, active=0, async=True)
    xfp = Signal(0.0)
    yfp = Signal(0.0)

    xfx = Signal(fixbv(0, min=-1, max=1, res=2**-15))
    yfx = Signal(fixbv(0, min=-1, max=1, res=2**-30))

    print repr(xfx), repr(yfx)
    tb_dut_fp = sum_of_squares(clk, rst, xfp, yfp, N)
    if run == "trace":
        tb_dut_fx = traceSignals(m_sum_of_squares, clock, reset, xfx, yfx, N)
    else:
        tb_dut_fx = m_sum_of_squares(clock, reset, xfx, yfx, N)

    @always(delay(3))
    def tb_clock():
        clock.next = not clock

    @always(clock.posedge)
    def tb_init():
        f = .9999 #uniform(-1,1)
        xfp.next = f
        xfx.next = xfx.Round(f)

    @instance
    def stimulus():
        yield clk.posedge
        rst.next = True
        yield delay(10)
        rst.next = False
        yield delay(1)

        for ii in range(Nloops): # 
            for jj in range(N):
                yield clk.posedge
                print " FP: %-03.6f [%-03.6f], FX: %-03.6f [%-03.6f] ( %08x, %08x)" % \
                      (yfp, xfp, yfx.fValue, xfx.fValue, yfx, xfx)
                print "Error Squared %f" % ((yfp - yfx.fValue)**2)
        raise StopSimulation
    

    Simulation((tb_clock, tb_init, tb_stim, tb_dut_fp, tb_dut_fx)).run()
Пример #5
0
def testbench(args):
    """
    """

    run = args.simtype
    N = args.N
    Nloops = args.NLoops
    
    clock = Signal(bool(0))
    reset = ResetSignal(0, active=0, isasync=True)
    xfp = Signal(0.0)
    yfp = Signal(0.0)

    xfx = Signal(fixbv(0, min=-1, max=1, res=2**-15))
    yfx = Signal(fixbv(0, min=-1, max=1, res=2**-30))

    # print repr(xfx), repr(yfx)
    tb_dut_fp = sum_of_squares(clock, reset, xfp, yfp, N)
    if run == "trace":
        tb_dut_fx = traceSignals(m_sum_of_squares, clock, reset, xfx, yfx, N)
    else:
        tb_dut_fx = m_sum_of_squares(clock, reset, xfx, yfx, N)

    @always(delay(3))
    def tb_clock():
        clock.next = not clock

    @always(clock.posedge)
    def tb_init():
        f = .9999 #uniform(-1,1)
        xfp.next = f
        xfx.next = xfx._round(f)

    @instance
    def stimulus():
        yield clock.posedge
        reset.next = True
        yield delay(10)
        reset.next = False
        yield delay(1)

        for ii in range(Nloops): # 
            for jj in range(N):
                yield clock.posedge
                print (" FP: %-03.6f [%-03.6f], FX: %-03.6f [%-03.6f] ( %08x, %08x)" )

        raise StopSimulation
    

    Simulation((tb_clock, tb_init, stimulus, tb_dut_fp, tb_dut_fx)).run()
Пример #6
0
def alu_tb():

    clk = Signal(0)

    x1 = Signal(fixbv(0.5, min=-2, max=2, res=2**-15))
    x2 = Signal(fixbv(0.5, min=-2, max=2, res=2**-15))
    output_add = Signal(fixbv(0, min=-2, max=4, res=2**-15))
    output_mul = Signal(fixbv(0, min=-2, max=4, res=2**-15))

    add = adder(x1, x2, output_add)

    mul = multply(x1, x2, output_mul)

    return add, mul
Пример #7
0
def m_sum_of_squares(clock, reset, x, y, N=16):
    """
    This module is the fixbv type
    """
    assert isinstance(x, fixbv)
    assert isinstance(y, fixbv)
    smin,smax,sres = (x.min**2,xmax**2,x.res**2)
    buf = [Signal(fixbv(0, xmin, xmax, xres)) for i in range(N)]
    sqr = [Signal(fixbv(0, smin, smax, sres)) for i in range(N)]

    # Check that N is a power of 2
    ii   = Signal(modbv(0, min=0, max=N))
    
    @always_seq(clock.posedge, reset=reset)
    def rtl_input():
        buf[int(ii)].next = x
        ii.next = ii + 1
                
    @always_comb
    def rtl_square():
        for jj in range(N):
            sqr[jj].next = buf[jj] * buf[jj]

    xsum = fixbv(0, smin*N, smax*N, sres)
        
    # Currently N is limited to a power of two
    divn = int(log(N,2))
    @always_seq(clock.posedge, reset=reset)
    def rtl_sum():
        xsum[:] = sqr[0]
        for jj in range(1,N):
            xsum[:] = xsum + sqr[jj].val

        y.next = xsum >> divn

                
    return instances()
Пример #8
0
def m_sum_of_squares(clock, reset, x, y, N=16):
    """
    This module is the fixbv type
    """
    # assert isinstance(x, fixbv)
    # assert isinstance(y, fixbv)
    smin,smax,sres = (x.min**2,x.max**2,x.res**2)
    buf = [Signal(fixbv(0, x.min, x.max, x.res)) for i in range(N)]
    sqr = [Signal(fixbv(0, smin, smax, sres)) for i in range(N)]

    # Check that N is a power of 2
    ii   = Signal(modbv(0, min=0, max=N))
    
    @always_seq(clock.posedge, reset=reset)
    def rtl_input():
        buf[int(ii)].next = x
        ii.next = ii + 1
                
    @always_comb
    def rtl_square():
        for jj in range(N):
            sqr[jj].next = buf[jj] * buf[jj]

    xsum = fixbv(0, smin*N, smax*N, sres)
        
    # Currently N is limited to a power of two
    divn = int(log(N,2))
    @always_seq(clock.posedge, reset=reset)
    def rtl_sum():
        xsum[:] = sqr[0]
        for jj in range(1,N):
            xsum[:] = xsum + sqr[jj].val

        y.next = xsum >> divn

                
    return instances()
Пример #9
0
def test_basic():
    # Test all exact single bit values for W(16,0,15)
    for f in range(1,16):
        x = fixbv(2**-f, format=W(16,0,15))
        y = fixbv(-2**-f, format=W(16,0,15))
        print(f,x,y)
        assert float(x) == 2**-f, \
               "%s != %s, %04x != %04x" % (2.**-f, x,
                                           hex(x),
                                           hex(0x8000 >> f))
        
        assert bin(x,16) == bin(0x8000 >> f, 16), \
               "%s != %s for f == %d" % (bin(x, 16),
                                         bin(0x8000 >> f, 16), f)
        assert float(y) == -2**-f
        assert bin(y,16) == bin(-0x8000 >> f, 16), \
               "%s" % (bin(y, 16))

    # Test all exact single bit values for W128.0
    for f in range(1,128):
        x = fixbv(2**-f,  min=-1, max=1, res=2**-127)
        y = fixbv(-2**-f, min=-1, max=1, res=2**-127)
        assert float(x) == 2**-f
        assert bin(x,128) == bin(0x80000000000000000000000000000000 >> f, 128)
        assert float(y) == -2**-f
        assert bin(y,128) == bin(-0x80000000000000000000000000000000 >> f, 128)

    assert x > y
    assert y < x
    assert min(x,y) == min(y,x) == y
    assert max(x,y) == max(y,x) == x
    assert x != y

    x = fixbv(3.14159, format=W(18,3))
    y = fixbv(-1.4142 - 1.161802 - 2.71828, format=W(18,3))

    assert x != y
    #assert --x == x
    assert abs(y) > abs(x)
    assert abs(x) < abs(y)
    assert x == x and y == y

    # Create a W8.3 fixed-point object value == 2.5
    x = fixbv(2.5, min=-8, max=8, res=1./32)
    assert float(x) == 2.5
    assert int(x) == 0x50
Пример #10
0
def test_basic():
    # Test all exact single bit values for W(16,0,15)
    for f in range(1,16):
        x = fixbv(2**-f, format=W(16,0,15))
        y = fixbv(-2**-f, format=W(16,0,15))
        print(f,x,y)
        assert float(x) == 2**-f, \
               "%s != %s, %04x != %04x" % (2.**-f, x,
                                           hex(x),
                                           hex(0x8000 >> f))
        
        assert bin(x,16) == bin(0x8000 >> f, 16), \
               "%s != %s for f == %d" % (bin(x, 16),
                                         bin(0x8000 >> f, 16), f)
        assert float(y) == -2**-f
        assert bin(y,16) == bin(-0x8000 >> f, 16), \
               "%s" % (bin(y, 16))

    # Test all exact single bit values for W128.0
    for f in range(1,128):
        x = fixbv(2**-f,  min=-1, max=1, res=2**-127)
        y = fixbv(-2**-f, min=-1, max=1, res=2**-127)
        assert float(x) == 2**-f
        assert bin(x,128) == bin(0x80000000000000000000000000000000 >> f, 128)
        assert float(y) == -2**-f
        assert bin(y,128) == bin(-0x80000000000000000000000000000000 >> f, 128)

    assert x > y
    assert y < x
    assert min(x,y) == min(y,x) == y
    assert max(x,y) == max(y,x) == x
    assert x != y

    x = fixbv(3.14159, format=W(18,3))
    y = fixbv(-1.4142 - 1.161802 - 2.71828, format=W(18,3))

    assert x != y
    #assert --x == x
    assert abs(y) > abs(x)
    assert abs(x) < abs(y)
    assert x == x and y == y

    # Create a W8.3 fixed-point object value == 2.5
    x = fixbv(2.5, min=-8, max=8, res=1./32)
    assert float(x) == 2.5
    assert int(x) == 0x50
Пример #11
0
def test_create():
    """
    This test creates a bunch of different fixed-point objects.
    This test doesn't test correctness only test that creating and many
    of the public functions execute without error.
    """
    print("\n** Create W16.0 fxintbv")
    x = fixbv(0, min=-1, max=1, res=2**-15)
    print(x, hex(x), repr(x))

    print "\n** Create W9.3 fxintbv == 2.5"
    x = fixbv(2.5, min=-8, max=8, res=1./32)
    print(x, hex(x), repr(x))

    print("\n** Create W0.?? fxintbv == 0.0333")
    x = fixbv(0.0333, min=-1, max=1, res=0.0001)
    print(x, hex(x), repr(x))
    
    s = 0.5
    x = 0x4000
    for i in range(16):
        fxp = fixbv(s, min=-1, max=1, res=2**-15)
        s = s / 2
        print(str(fxp), myhdl.bin(fxp, 16), "%04x" % (fxp), type(fxp), repr(fxp))
        #print(hex(fxp & x)  @todo assert hex(fxp & x) == x, "Incorrect fixed-point calculation")
        x = x >> 1

    print("Get fixbv")
    a = fixbv(0, min=-2, max=2, res=2**-4)
    print("Assign to 1")
    a._val = 1
    print(a, repr(a))

    print("[1] Showing range: ")
    print("printing a:", a)
    print("slice operation (Note slice will return intbv)")
    print("  ", str(a), "  slice a[4:] ", hex(a[4:]), " a[16:] ", hex(a[16:]))

    a = fixbv(1.2, min=-2, max=2, res=2**-3)
    print("[2] Showing range: ")
    print("printing a: ", a)


    a = fixbv(0.02, min=-1, max=1, res=2**-8)
    print("[1] Representation a: ", repr(a))

    b = fixbv(0.2, min=-1, max=1, res=2**-8)
    print("[2] Representation b: ", repr(b))

    c = fixbv(0, format=a.W+b.W)
    print("[3] Representation c: ", c.W)

    print("[1] Add: c = a + b")
    c[:] = a + b
    print "c: ", c, type(c), repr(c)

    print("[2] Add: c = 1.25 + 2.0")
    a = fixbv(1.25, min=-4, max=4, res=2**-12)
    b = fixbv(2.0,  min=-4, max=4, res=2**-12)
    c = fixbv(0, format=a.W+b.W)
    print("  a: ", a.W, bin(a,len(a)), " b: ", b.W, bin(b,len(b)))
    c[:] = a + b
    print("c: ", c, c.W, bin(c,len(c)))
Пример #12
0
from fixed_point import fixbv

a = fixbv(min=-100, max=100, res=2**-8)
print(a)
b = fixbv(format=a.format)
print(b)
Пример #13
0
#   loops       = number of iterations to check
#   minmaxPower = maximum power for bounds    (2^minmaxPower)
#   resPower    = maximum power of resolution (2^resPower)

loops = 5000
resPower = 32
minmaxPower = 20

for x in range(0, loops):
    # Generate random fixbv object named x1
    x1_res_power = random.randint(1, resPower)
    x1_res = 2**-x1_res_power
    x1_min = random.randint(-2**minmaxPower, 0)
    x1_max = random.randint(0, 2**minmaxPower)
    x1_val = random.uniform(x1_min, x1_max)
    x1 = fixbv(x1_val, min=x1_min, max=x1_max, res=x1_res)

    # Generate random fixbv object named x2
    x2_res_power = random.randint(1, resPower)
    x2_res = 2**-x2_res_power
    x2_min = random.randint(-2**minmaxPower, 0)
    x2_max = random.randint(0, 2**minmaxPower)
    x2_val = random.uniform(x2_min, x2_max)
    x2 = fixbv(x2_val, min=x2_min, max=x2_max, res=x2_res)

    # Math operations on fixbv objects
    x_mul = x1 * x2
    x_add = x1 + x2
    x_sub = x1 - x2

    # Math on regular numbers (the values input to fixbv contructors)
Пример #14
0
def test_create():
    """
    This test creates a bunch of different fixed-point objects.
    This test doesn't test correctness only test that creating and many
    of the public functions execute without error.
    """
    print("\n** Create W16.0 fxintbv")
    x = fixbv(0, min=-1, max=1, res=2**-15)
    print(x, hex(x), repr(x))

    print("\n** Create W9.3 fxintbv == 2.5")
    x = fixbv(2.5, min=-8, max=8, res=1./32)
    print(x, hex(x), repr(x))

    print("\n** Create W0.?? fxintbv == 0.0333")
    x = fixbv(0.0333, min=-1, max=1, res=0.0001)
    print(x, hex(x), repr(x))
    
    s = 0.5
    x = 0x4000
    for i in range(16):
        fxp = fixbv(s, min=-1, max=1, res=2**-15)
        s = s / 2
        print(str(fxp), myhdl.bin(fxp, 16), "%04x" % (fxp), type(fxp), repr(fxp))
        #print(hex(fxp & x)  @todo assert hex(fxp & x) == x, "Incorrect fixed-point calculation")
        x = x >> 1

    print("Get fixbv")
    a = fixbv(0, min=-2, max=2, res=2**-4)
    print("Assign to 1")
    a._val = 1
    print(a, repr(a))

    print("[1] Showing range: ")
    print("printing a:", a)
    print("slice operation (Note slice will return intbv)")
    print("  ", str(a), "  slice a[4:] ", hex(a[4:]), " a[16:] ", hex(a[16:]))

    a = fixbv(1.2, min=-2, max=2, res=2**-3)
    print("[2] Showing range: ")
    print("printing a: ", a)


    a = fixbv(0.02, min=-1, max=1, res=2**-8)
    print("[1] Representation a: ", repr(a))

    b = fixbv(0.2, min=-1, max=1, res=2**-8)
    print("[2] Representation b: ", repr(b))

    c = fixbv(0, format=a.W+b.W)
    print("[3] Representation c: ", c.W)

    print("[1] Add: c = a + b")
    c[:] = a + b
    print("c: ", c, type(c), repr(c))

    print("[2] Add: c = 1.25 + 2.0")
    a = fixbv(1.25, min=-4, max=4, res=2**-12)
    b = fixbv(2.0,  min=-4, max=4, res=2**-12)
    c = fixbv(0, format=a.W+b.W)
    print("  a: ", a.W, bin(a,len(a)), " b: ", b.W, bin(b,len(b)))
    c[:] = a + b
    print("c: ", c, c.W, bin(c,len(c)))
Пример #15
0
# print(fixNum * fixNum)

# retval = fixbv(0, min=-1, max=1, res=2**-31)
# retval3 = intbv(12)

# fixNum * retval3
# print((retval))
# print(retval._fval)

# A = [[fixNum, fixNum], [fixNum, fixNum]]
# B = [[fixNum, fixNum], [fixNum, fixNum]]
# C = numpy.matmul(A,B)

# ---

x1 = fixbv(0.54, min=-1, max=1, res=2**-10)
x2 = fixbv(0.22, min=-1, max=1, res=2**-15)

# The following works correctly:

print("Testing fixbv math operations for x1 = {0}, x2 = {1}".format(
    x1._fval, x2._fval))
print("x1 resolution = {0}, x2 resolution = {1}".format(x1.res, x2.res))

x_mul = x1 * x2
print("Result of multiplication: Value = {0}, Resolution = {1}".format(
    x_mul._fval, x_mul.res))
print(x_mul)
x_add = x1 + x2
print("Result of addition: Value = {0}, Resolution = {1}".format(
    x_add._fval, x_add.res))