# Namespace math

This namespace is used to provide support functions for math operations.

## Functions

`template<pint Worig, Wnew> function sign_extend (int<Worig> x) : int<Wnew>;`

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<pint Worig, Wnew> function zero_extend (int<Worig> x) : int<Wnew>;`

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.`

## 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<pint A,B> function add (int<A+B> x, y) : int<A+B>;`

This returns the sum of two Q(A,B) numbers `x` and `y`.

`template<pint A,B> function sub (int<A+B> x, y) : int<A+B>;`

This returns the difference of two Q(A,B) numbers `x` and `y`.

`template<pint A,B> function multu (int<A+B> x, y) : int<A+B>;`

This returns the unsigned product of two Q(A,B) numbers `x` and `y`.

`template<pint A,B> function divu (int<A+B> x, y) : int<A+B>;`

This returns the unsigned division result `x/y` of two Q(A,B) numbers `x` and `y`.

`template<pint A,B> function mults (int<A+B> x, y) : int<A+B>;`

This returns the signed product of two Q(A,B) numbers `x` and `y`.

`template<pint A,B> function divs (int<A+B> x, y) : int<A+B>;`

This returns the signed division result `x/y` of two Q(A,B) numbers `x` and `y`.

`template<pint A,B> function uminus (int<A+B> x) : int<A+B>;`

This returns the negated value of a signed Q(A,B) number `x`.

`template<pint A,B> function positive (int<A+B> x) : bool;`

This is true if the fixed point number is positive (greater than zero), and false otherwise.

`template<pint A,B> function negative (int<A+B> x) : bool;`

This is true if the fixed point number is negative, and false otherwise

`template<pint A,B> function le(int<A+B> 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.