Пример #1
0
def test_basic():
    # Test all exact single bit values for W(16,0,15)
    for f in range(1, 16):
        x = fixed(2**-f, format=W(16, 0, 15))
        y = fixed(-2**-f, format=W(16, 0, 15))
        #print(f,x,y)
        assert float(x) == 2**-f, \
               "%f != %f" % (float(x),2**-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, \
               "%f != %f" % (float(x),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 = fixed(2**-f, min=-1, max=1, res=2**-127)
        y = fixed(-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 = fixed(3.14159, format=W(18, 3))
    y = fixed(-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 = fixed(2.5, min=-8, max=8, res=1. / 32)
    assert float(x) == 2.5
    assert int(x) == 0x50
Пример #2
0
def test_basic():
    # Test all exact single bit values for W(16,0,15)
    for f in range(1,16):
        x = fixed(2**-f, format=W(16,0,15))
        y = fixed(-2**-f, format=W(16,0,15))
        #print(f,x,y)
        assert float(x) == 2**-f, \
               "%f != %f" % (float(x),2**-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, \
               "%f != %f" % (float(x),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 = fixed(2**-f,  min=-1, max=1, res=2**-127)
        y = fixed(-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 = fixed(3.14159, format=W(18,3))
    y = fixed(-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 = fixed(2.5, min=-8, max=8, res=1./32)
    assert float(x) == 2.5
    assert int(x) == 0x50    
Пример #3
0
import numpy as np
import matplotlib.pyplot as plt

from fixed_point import fixed,W
from fixed_point import ROUND_MODES

format1 = W(7,3,3)
format2 = W(4,3,0)
for rm in ROUND_MODES:
    xf = np.arange(-10000,10000)/1000.
    xq1,xq2,xe = ([],[],[])
    for ff in xf:
        fx1 = float(fixed(ff, format=format1, round_mode=rm))
        fx2 = float(fixed(fx1, format=format2, round_mode=rm))
        xq1.append(fx1)
        xq2.append(fx2)
        xe.append(fx1-fx2) # also plot error

    # plot the quantized values
    fig,ax = plt.subplots(1)
    tax = ax.twinx()
    ax.plot(xf,xf, label='real number')
    ax.plot(xf,xq1, label='quantized W(7,3,3)')
    ax.plot(xf,xq2, label='quantized W(7,3,3) -> W(4,3,0)')
    ax.grid(True)
    ax.set_xlim(-10,10)
    ax.set_ylim(-10,10)
    ax.set_title("Quantization\nfixed(format=(4,0,3),"
              "round_mode='%s'"
              "overflow_mode='saturate')"%(rm))
Пример #4
0
import numpy as np
import matplotlib.pyplot as plt

from fixed_point import fixed,W
from fixed_point import ROUND_MODES

format = W(4,0,3)
for rm in ROUND_MODES:
    xf = np.arange(-1200,1200)/1000.
    xq,xe = ([],[])
    for ff in xf:
        fx = float(fixed(ff, format=format, round_mode=rm))
        xq.append(fx)    # back to float for plotting, preserves value
        xe.append(ff-fx) # also plot error

    # plot the quantized values
    fig,ax = plt.subplots(1)
    tax = ax.twinx()
    ax.plot(xf,xf, label='real number value')
    ax.plot(xf,xq, label='quantized value')
    ax.grid(True)
    ax.set_xlim(-1.2,1.2)
    ax.set_ylim(-1.2,1.2)
    ax.set_title("Quantization\nfixed(format=W%s,"
              "round_mode='%s'"
              "overflow_mode='saturate')"%(rm,W.format))
    ax.set_ylabel('')
    ax.set_xlabel('')

    tax.plot(xf,xe, 'm:', alpha=.6, label='error')    
Пример #5
0
def _round_modes1(ival, tdict):
    for rm, rval in tdict.items():
        print('  rnd[%10s]: %f' % (rm, ival))
        x = fixed(ival, min=-2, max=2, res=.5, round_mode=rm)
        assert float(x) == rval, "%s expected %f got %f" % (rm, rval, float(x))
Пример #6
0
def test_math():
    x = fixed(0.5, format=W(16, 0))
    y = fixed(0.25, format=W(16, 0))
    w = x + y  # typical modeling
    print(repr(w), repr(x), repr(y))

    # pre-declared
    z = fixed(0, format=x + y)
    z[:] = x + y  # hardware transfer (type is declared)
    print(repr(z), repr(x), repr(y))

    assert float(w) == 0.75
    assert float(z) == 0.75

    # test non-aligned additions
    x = fixed(0.5, min=-4, max=4, res=.125)
    y = fixed(0.25, min=-16, max=16, res=.0625)
    print(repr(z), repr(x), repr(y))
    z = x + y
    print(repr(z), repr(x), repr(y))
    assert float(z) == 0.75

    # test non-aligned additions and overflow
    x = fixed(0.5, min=-4, max=4, res=2**-32)
    y = fixed(1.5, min=-16, max=16, res=2**-32)
    z = x + y
    print(repr(z), repr(x), repr(y))
    assert float(z) == 2.0

    # @todo: negative iwl and fwl

    # test subtraction
    x = fixed(0.5, format=W(16, 0))
    y = fixed(0.25, format=W(16, 0))
    z = x - y
    print('S', repr(z), repr(x), repr(y))
    assert float(z) == 0.25

    x = fixed(0.5, min=-4, max=4, res=.125)
    y = fixed(0.25, min=-16, max=16, res=.0625)
    z = x - y
    print('S', repr(z), repr(x), repr(y))
    assert float(z) == 0.25

    z = y - x
    print('S', repr(z), repr(x), repr(y))
    assert float(z) == -0.25

    x = fixed(10.33, min=-16, max=16, res=.125)
    y = fixed(10.33, min=-16, max=16, res=.125)
    z = x - y
    print('S', repr(z), repr(x), repr(y))
    assert float(z) == 0.

    # The resolution of x is .125, it means 0.33 will be
    # rounded to .25 or .375 (it should be .375).  For
    # y the resolution is 2**-8 and the actual value should
    # be .328125 or .33203125 (.32 is closer?)
    x = fixed(0.33, min=-1, max=1, res=.125)
    y = fixed(0.33, min=-1, max=1, res=2**-8)
    z = x + y
    print('A', repr(z), repr(x), repr(y))
    assert float(z) == 0.375 + 0.328125

    x = fixed(0.33, min=-1, max=1, res=.125)
    y = fixed(0.33, min=-1, max=1, res=2**-8)
    z = x - y
    print('S', repr(z), repr(x), repr(y))
    assert float(z) == 0.375 - 0.328125

    x = fixed(-1, format=W(8, 0, 7)) + .5
    assert float(x) == -.5
Пример #7
0
import numpy as np
import matplotlib.pyplot as plt

from fixed_point import fixed, W
from fixed_point import ROUND_MODES

format = W(4, 0, 3)
for rm in ROUND_MODES:
    xf = np.arange(-1200, 1200) / 1000.
    xq, xe = ([], [])
    for ff in xf:
        fx = float(fixed(ff, format=format, round_mode=rm))
        xq.append(fx)  # back to float for plotting, preserves value
        xe.append(ff - fx)  # also plot error

    # plot the quantized values
    fig, ax = plt.subplots(1)
    tax = ax.twinx()
    ax.plot(xf, xf, label='real number value')
    ax.plot(xf, xq, label='quantized value')
    ax.grid(True)
    ax.set_xlim(-1.2, 1.2)
    ax.set_ylim(-1.2, 1.2)
    ax.set_title("Quantization\nfixed(format=W%s,"
                 "round_mode='%s'"
                 "overflow_mode='saturate')" % (rm, W.format))
    ax.set_ylabel('')
    ax.set_xlabel('')

    tax.plot(xf, xe, 'm:', alpha=.6, label='error')
    xf = np.array(xf)
Пример #8
0
def _round_modes1(ival,tdict):
     for rm,rval in tdict.items():
        print('  rnd[%10s]: %f'%(rm,ival))
        x = fixed(ival, min=-2,max=2, res=.5, round_mode=rm)
        assert float(x) == rval, "%s expected %f got %f"%(rm,rval,float(x))
Пример #9
0
def test_math():
    x = fixed(0.5, format=W(16,0))
    y = fixed(0.25, format=W(16,0))
    w = x + y       # typical modeling
    print(repr(w),repr(x),repr(y))

    # pre-declared
    z = fixed(0, format=x+y)
    z[:] = x + y    # hardware transfer (type is declared)
    print(repr(z),repr(x),repr(y))
    
    assert float(w) == 0.75
    assert float(z) == 0.75

    # test non-aligned additions
    x = fixed(0.5, min=-4, max=4, res=.125)
    y = fixed(0.25, min=-16, max=16, res=.0625)
    print(repr(z),repr(x),repr(y))    
    z = x+y
    print(repr(z),repr(x),repr(y))
    assert float(z) == 0.75

    # test non-aligned additions and overflow
    x = fixed(0.5, min=-4, max=4, res=2**-32)
    y = fixed(1.5, min=-16, max=16, res=2**-32)
    z = x+y
    print(repr(z),repr(x),repr(y))
    assert float(z) == 2.0

    # @todo: negative iwl and fwl

    # test subtraction
    x = fixed(0.5, format=W(16,0))
    y = fixed(0.25, format=W(16,0))
    z = x - y  
    print('S',repr(z),repr(x),repr(y))
    assert float(z) == 0.25

    x = fixed(0.5, min=-4, max=4, res=.125)
    y = fixed(0.25, min=-16, max=16, res=.0625)
    z = x-y
    print('S',repr(z),repr(x),repr(y))
    assert float(z) == 0.25

    z = y-x
    print('S',repr(z),repr(x),repr(y))
    assert float(z) == -0.25

    x = fixed(10.33, min=-16, max=16, res=.125)
    y = fixed(10.33, min=-16, max=16, res=.125)
    z = x-y
    print('S',repr(z),repr(x),repr(y))
    assert float(z) == 0.

    # The resolution of x is .125, it means 0.33 will be
    # rounded to .25 or .375 (it should be .375).  For
    # y the resolution is 2**-8 and the actual value should
    # be .328125 or .33203125 (.32 is closer?)
    x = fixed(0.33, min=-1, max=1, res=.125) 
    y = fixed(0.33, min=-1, max=1, res=2**-8)
    z = x+y
    print('A',repr(z),repr(x),repr(y))
    assert float(z) == 0.375+0.328125

    x = fixed(0.33, min=-1, max=1, res=.125)
    y = fixed(0.33, min=-1, max=1, res=2**-8)
    z = x-y
    print('S',repr(z),repr(x),repr(y))
    assert float(z) == 0.375-0.328125

    
    x = fixed(-1, format=W(8,0,7)) + .5
    assert float(x) == -.5
Пример #10
0
import numpy as np
import matplotlib.pyplot as plt

from fixed_point import fixed,W
from fixed_point import ROUND_MODES

format1 = W(7,3,3)
format2 = W(4,3,0)
for rm in ROUND_MODES:
    xf = np.arange(-10000,10000)/1000.
    xq1,xq2,xe = ([],[],[])
    for ff in xf:
        fx1 = float(fixed(ff, format=format1, round_mode=rm))
        fx2 = float(fixed(fx1, format=format2, round_mode=rm))
        xq1.append(fx1)
        xq2.append(fx2)
        xe.append(fx1-fx2) # also plot error

    # plot the quantized values
    fig,ax = plt.subplots(1)
    tax = ax.twinx()
    ax.plot(xf,xf, label='real number')
    ax.plot(xf,xq1, label='quantized W(7,3,3)')
    ax.plot(xf,xq2, label='quantized W(7,3,3) -> W(4,3,0)')
    ax.grid(True)
    ax.set_xlim(-10,10)
    ax.set_ylim(-10,10)
    ax.set_title("Quantization\nfixed(format=(4,0,3),"
              "round_mode='%s'"
              "overflow_mode='saturate')"%(rm))