[myhdl-list] fixed-point thoughts : part one
Brought to you by:
jandecaluwe
From: Christopher F. <chr...@gm...> - 2013-08-06 01:58:41
|
Introduction ============= A couple years ago I adopted Tom Dillon’s Python fixed-point module [1] and used the /intbv/ as a base class, hence convertible. I would like to start taking the steps to get the /fixbv/ type into the MyHDL package (target 0.9). The following is a conversation on a proposed MyHDL fixed-point type, /fixbv/ (a MEP will be created post any conversations). The brief fixed-point primer: a fixed-point bit-vector represents a signed rational fractional value. The *point* will be logically assigned at some position in the bit-vector. The bits to the left of the *point* are the -well known- integer values and the bits to the right are the fractional values. Example: s : sign bit i : integer bits f : fractional bits siii.ffff The above example is an 8-bit bit-vector with three integer bits and four fractional bits. The fractional values are combinations of negative powers of two, analogous, integers are combinations of positive powers of two. In [8]: [2**(-1*ii) for ii in range(1,9)] Out[8]: [0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625, 0.0078125, 0.00390625] fixbv class ============ Creating a /fixbv/ object should be straightforward. I followed Jan Decaluwe’s lead and designed the /fixbv/ instantiation similar to /intbv/. Meaning, the natural method to declare a /fixbv/ is to define the: initial value, minimum, maximum, and resolution. Example: x = fixbv(0.333, min=-1, max=1, res=0.1) The resolution is the smallest quantity representable. In many cases the requested /res/ cannot be encoded exactly with a reasonable number of bits. As mentioned, the resolution is limited to negative powers of two. In the above example a resolution of 0.1 is defined and the resulting resolution will be 0.0625. This resolution is better than the 0.1 but will have the downside that multiples of the requested resolution cannot be represented exactly. If the goal is to encode: 0.1, 0.2, 0.3, 0.4, etc., it cannot with a finite number of bits. The basic idea: if one of the denominators prime multiples (e.g. 2 and 5 for 10) is not a multiple of the base then the rational fraction cannot be represented exactly (something to that effect). The above definition (instantiation) should cover most of the use cases. Except, some designers have the habit of defining the bits explicitly. Like the /intbv/ the proposed /fixbv/ would allow the definition of the bits contained. To define the bits the word-length (wl), integer word-length (iwl), and fractional word-length (fwl) are set, example: fixbv(0)[wl,iwl,fwl] The word-lengths have a simple relation: wl = iwl + fwl + 1 There are a couple more arguments that are needed to complete the constructor: rounding_mode : instructs how to round the initial value overflow_mode : instructs how to handle overflow in the initial value There are six different round modes and two overflow modes supported by /fixbv/ type. These will be discussed again in the operators discussions (part two). The complete /fixbv/ constructor is: fixbv(val, [, min=-1] [, max=1] [, res=None] [, round_mode=’convergent’] [, overflow_mode=’saturate’]) This was part one of the conversations on fixed-point. This post covered the fixed-point representation and constructing a /fixbv/ object. The next post will cover fixed-point operations. Regards, Chris [1] http://www.dilloneng.com/documents/downloads/demodel/ |