Integer arithmetic in hardware descriptions

John Nagle nagle at animats.com
Sat Mar 14 14:50:13 EDT 2009


Jan Decaluwe wrote:
> I am the author of MyHDL, a Python package that turns Python
> into a hardware description language (HDL).
> 
> Integer arithmetic is very important in hardware design,
> but with traditional HDLs such as Verilog and VHDL it is
> complicated and confusing. MyHDL has a better solution,
> inspired by Python's native integer type, int.
> 
> I have written an essay that explores these issues in
> detail:
> 
> http://www.jandecaluwe.com/hdldesign/counting.html
> 
    I went through this exercise many years ago, from a program
verification perspective, and somewhere I have an article entitled
"Type Integer Considered Harmful".

    The position I took was:

	Integer results should be the same on all platforms.
	
	The basic integer type should be the "range", with an
	explicit upper and lower bound.  (Pascal had something
	like this, called a "subrange", but it it wasn't done quite right.)

	Violating the range of a variable is an error.

	It is the job of the compiler to ensure that intermediate values
	in integer expressions can handle all possible values given the
	declared ranges of the user variables.

The last item has to do with expressions like (in Pascal-like syntax)

	x,a,b,c: range[0..2^32-1];
	x = (a * b) / c;

The compiler has to work out that "(a * b)" needs to be bigger than x,a,b,c.

CPython uses a "bignum" approach to escape this problem, but that has costs.
It's also not an option when compiling to hardware, as with VHDL.

I used to do proof of correctness work.  See this manual, from the early 1980s.

	http://www.animats.com/papers/verifier/verifiermanual.pdf

I gave up on this when C came in; the C crowd was so casual about integer
overflow that nobody cared about this level of correctness.  Today, of course,
"buffer overflows" are a way of life.

This is really off topic for the group.

				John Nagle



More information about the Python-list mailing list