====== Namespace math ======
This namespace is used to provide support functions for math operations.
===== Functions =====
template function sign_extend (int x) : int;
This function takes an integer ''x'' of width ''Worig'', and sign extends its most significant bit to return a result that has width ''Wnew''. It assumes that ''Wnew'' is at least ''Worig.''
template function zero_extend (int x) : int;
This function takes an integer ''x'' of width ''Worig'', and zero extends it to return a result that has width ''Wnew''. It assumes that ''Wnew'' is at least ''Worig.''
===== Fixed point data type =====
The standard math library has a fixed point data type.
export template deftype fixpoint(int x);
This datatype uses the standard Q(A,B) fixed point format, where A bits are used for the integer part and B bits are used for the fractional part. This datatype defines operators for addition, subtraction, multiplication, division, as well as comparison operators.
The following code fragment initializes the fixed point value ''x'' to 5.25. ''x.const(5.25)'' returns a parameterized integer corresponding to translating the real number 5.25 to the fixed point representation used by ''x''. Note that ''x.const()'' returns a ''pint'', so it is limited to 64 bits. ''x.set()'' is a macro that takes an integer of bitwidth (A+B) and sets ''x'' to the specified fixed point number.
math::fixpoint<8,3> x;
...
chp {
x.set(x.const(5.25));
...
}
Other ways to initialize ''x'' to the fixed point number corresponding to the integer representation ''0x0f'' (which corresponds to 1.875 for Q(8,3)) are:
...
chp {
x.x := 0x0f;
...
x.set(0x0f);
...
}
For debugging purposes, two macros are also provided for displaying the value of a fixed point number. The ''log()'' macro just displays the number using the ''log()'' statement, while the ''log_p()'' displays it without a new line.
chp {
...
x.log();
...
}
===== Namespace math::fxp =====
==== Functions ====
The ''math::fxp'' namespace contains simple definitions for fixed-point arithmetic functions. Functions take two parameters ''A'' and ''B'', and integers use the standard Q-format where the representation Q(A,B) corresponds to A digits for the integer part and B digits for the fractional part. The representation uses 2's complement for negative
numbers, so addition and subtraction are the same for signed and unsigned arithmetic.
template function add (int x, y) : int;
This returns the sum of two Q(A,B) numbers ''x'' and ''y''.
template function sub (int x, y) : int;
This returns the difference of two Q(A,B) numbers ''x'' and ''y''.
template function multu (int x, y) : int;
This returns the unsigned product of two Q(A,B) numbers ''x'' and ''y''.
template function divu (int x, y) : int;
This returns the unsigned division result ''x/y'' of two Q(A,B) numbers ''x'' and ''y''.
template function mults (int x, y) : int;
This returns the signed product of two Q(A,B) numbers ''x'' and ''y''.
template function divs (int x, y) : int;
This returns the signed division result ''x/y'' of two Q(A,B) numbers ''x'' and ''y''.
template function uminus (int x) : int;
This returns the negated value of a signed Q(A,B) number ''x''.
template function positive (int x) : bool;
This is true if the fixed point number is positive (greater than zero), and false otherwise.
template function negative (int x) : bool;
This is true if the fixed point number is negative, and false otherwise
template function le(int x, y) : bool;
This tests if the value in ''x'' is less than or equal to ''y''.
function conv_to_fxp(pint A, B; preal v) : pint;
This can be used to convert a real constant value into its fixed point representation. Note that since ''pint'' values are 64-bit wide (max), this only works when ''A+B'' is at most 64.
These functions are used by the fixed point data type.