Monday, October 29, 2007

Counter-intuitive logic minimization

I was wrestling with verilog code that wasn't meeting timing, and my attempt to help one path actually made it worse. It's an interesting example.

The code was like so:

if ((var1 - var2) > 15) then
...

I figured that if I created a shadow version of var1 that was (var1 - 15); we can call it var1_m15), I could rewrite this as:

if (var1_m15 > var2) then
...

BUT, this code actually had worse timing. It turns out that "M > 15" is semi-magical - it's simply (M[n:3] != 0). So if in the first case we have intermed = var1 - var2, and in the second case we have intermed = var1_m15 - var2, then the second case OR's all of the n+1 bits of intermed, and the first case OR's just n-3 bits of intermed. A very minor difference, but interesting/amusing?

No comments: