Skip to content

Commit

Permalink
Use common.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Teddy-van-Jerry committed Nov 1, 2023
1 parent 669df0e commit 58c047d
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 31 deletions.
8 changes: 8 additions & 0 deletions docs/api/common.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Common
========

.. currentmodule:: rtlsim

.. autoclass:: FxpProps
:members:
:undoc-members:
1 change: 1 addition & 0 deletions docs/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ API Documentation
.. toctree::
:maxdepth: 2

common
Quantize
40 changes: 9 additions & 31 deletions rtlsim/Quantize.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
from .common import *

def shift_left_(x, n):
return x * (2 ** n)
Expand All @@ -15,22 +16,6 @@ def clip_(x, dwt):
return np.clip(x, min_v, max_v)

class Quantize:
class Overflow:
"""Overflow behavior
"""
WRAP = 0
"""Wrap around (default behavior)"""
SATURATE = 1

class Rounding:
"""Rounding behavior
"""
TRUNCATE = 0
"""Truncate the fractional part (default behavior)"""
AROUND = 1
FLOOR = 2
CEIL = 3
FIX = 4

"""Fixed-point quantization
"""
Expand All @@ -43,25 +28,18 @@ def __init__(self, S, W, F, overflow: str = 'wrap', rounding: str = 'truncate')
:type W: positive integer
:param F: Fractional bit width
:type F: integer
:param overflow: :class:`~rtlsim.Quantize.Overflow` behavior, defaults to :code:`'wrap'`
:param overflow: :class:`~rtlsim.FxpProps.Overflow` behavior, defaults to :code:`'wrap'`
:type overflow: str, optional
:param rounding: :class:`~rtlsim.Quantize.Rounding` behavior, defaults to :code:`'truncate'`
:param rounding: :class:`~rtlsim.FxpProps.Rounding` behavior, defaults to :code:`'truncate'`
:type rounding: str, optional
"""
assert S in [True, False], 'Sign bit must be boolean.'
assert W > 0, 'Word bit width must be positive.'
assert overflow in FxpProps.overflow_dict.keys(), 'Overflow behavior must be one of ' + str(FxpProps.overflow_dict.keys())
assert rounding in FxpProps.rounding_dict.keys(), 'Rounding behavior must be one of ' + str(FxpProps.rounding_dict.keys())
self.S = S
self.W = W
self.F = F
overflow_dict = {
'wrap': self.Overflow.WRAP,
'saturate': self.Overflow.SATURATE,
}
rounding_dict = {
'truncate': self.Rounding.TRUNCATE,
'around': self.Rounding.AROUND,
'floor': self.Rounding.FLOOR,
'ceil': self.Rounding.CEIL,
'fix': self.Rounding.FIX,
}
self.overflow: self.Overflow = overflow_dict[overflow.lower()]
self.rounding: self.Rounding = rounding_dict[rounding.lower()]
self.overflow: FxpProps.Overflow = FxpProps.overflow_dict[overflow.lower()]
self.rounding: FxpProps.Rounding = FxpProps.rounding_dict[rounding.lower()]
pass
1 change: 1 addition & 0 deletions rtlsim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

__author__ = "Teddy van Jerry (Wuqiong Zhao)"

from .common import FxpProps
from .Quantize import Quantize

114 changes: 114 additions & 0 deletions rtlsim/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
"""Common definitions for the RTL Sim
"""

class FxpProps:
"""Fixed-point properties.
"""
class Overflow:
"""Overflow behavior
"""
WRAP = 0
"""Wrap around (default behavior)"""
SATURATE = 1

class Rounding:
"""Rounding behavior
"""
TRUNCATE = 0
"""Truncate the fractional part (default behavior)"""
AROUND = 1
FLOOR = 2
CEIL = 3
FIX = 4

overflow_dict = {
'wrap': Overflow.WRAP,
'saturate': Overflow.SATURATE,
}
rounding_dict = {
'truncate': Rounding.TRUNCATE,
'around': Rounding.AROUND,
'floor': Rounding.FLOOR,
'ceil': Rounding.CEIL,
'fix': Rounding.FIX,
}

def __init__(self) -> None:
pass

def __repr__(self) -> str:
return f'FxpProps()'

def vMinInt(self, S, W):
"""Minimum value of the integer number (disregarding :code:`F`).
:param S: Sign bit (:code:`True` for signed, :code:`False` for unsigned)
:type S: boolean
:param W: Word bit width
:type W: positive integer
"""
return -(1 << (W - 1)) if S else 0

def vMin(self, S, W, F):
"""Minimum value of the fixed-point number.
:param S: Sign bit (:code:`True` for signed, :code:`False` for unsigned)
:type S: boolean
:param W: Word bit width
:type W: positive integer
:param F: Fractional bit width
:type F: integer
"""
return FxpProps.v_min_int(S, W) / (2 ** F)

def vMaxInt(self, S, W):
"""Maximum value of the integer number (disregarding :code:`F`).
:param S: Sign bit (:code:`True` for signed, :code:`False` for unsigned)
:type S: boolean
:param W: Word bit width
:type W: positive integer
"""
return (1 << (W - 1)) - 1 if S else (1 << W) - 1

def vMax(self, S, W, F):
"""Maximum value of the fixed-point number.
:param S: Sign bit (:code:`True` for signed, :code:`False` for unsigned)
:type S: boolean
:param W: Word bit width
:type W: positive integer
:param F: Fractional bit width
:type F: integer
"""
return FxpProps.v_max_int(S, W) / (2 ** F)

def vRangeInt(self, S, W):
"""Range of the integer number (disregarding :code:`F`).
:param S: Sign bit (:code:`True` for signed, :code:`False` for unsigned)
:type S: boolean
:param W: Word bit width
:type W: positive integer
"""
return FxpProps.v_max_int(S, W) - FxpProps.v_min_int(S, W)

def vRange(self, S, W, F):
"""Range of the fixed-point number.
:param S: Sign bit (:code:`True` for signed, :code:`False` for unsigned)
:type S: boolean
:param W: Word bit width
:type W: positive integer
:param F: Fractional bit width
:type F: integer
"""
return FxpProps.v_max(S, W, F) - FxpProps.v_min(S, W, F)

def vPrevision(self, F):
"""Prevision of the fixed-point number (:math:`2^{-F}`).
:param F: Fractional bit width
:type F: integer
"""
return 1 / (2 ** F)
3 changes: 3 additions & 0 deletions scripts/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

python3 test_rtlsim.py
2 changes: 2 additions & 0 deletions test_rtlsim.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import rtlsim

quantize_u_8_4 = rtlsim.Quantize(False, 8, 4)

0 comments on commit 58c047d

Please sign in to comment.