From: dlopez on
Hi,
I'm trying to calculate the absolute value of a signed number (two's
complement).

Right now, I sign extend the input, and when msb=1, inverse all bits and
add 1. The sign extend is to take care of the most negative number.

Is there a better way in terms of hardware utilization?


Here is my verilog code:

wire signed [w-1:0] a;
wire signed [w:0] b, c;

assign b = $signed(a); //sign exted input
assign c = b[w] ? (~b+1'b1) : b; //inverse all bits and add 1 if msb=1

Thanks,
Diego

---------------------------------------
Posted through http://www.FPGARelated.com
 | 
Pages: 1
Prev: Quartus II under Windows7?
Next: Helping tools