I would like to design a 16 to 32-bit sign extension unit. I would
like to preserve the number's sign (positive/negative). So I guess
after I append digits to the MSB then I would need to take care of the
sign's, how could I do this in verilog?

On Jan 27, 4:23*pm, rekz <[email protected]> wrote:
> I would like to design a 16 to 32-bit sign extension unit. I would
> like to preserve the number's sign (positive/negative). So I guess
> after I append digits to the MSB then I would need to take care of the
> sign's, how could I do this in verilog?

> On Jan 27, 4:23*pm, rekz <[email protected]> wrote:
>> I would like to design a 16 to 32-bit sign extension unit. I would
>> like to preserve the number's sign (positive/negative). So I guess
>> after I append digits to the MSB then I would need to take care of the
>> sign's, how could I do this in verilog?
>
> is it just as simple as this:
>
> module SignExtension(a, result);
>
> input [15:0] a; // 16-bit input
> output [31:0] result; // 32-bit output
>
> assign result = { 16{a[31]}, a };

This should be assign result = { 16{a[15]}, a };

Basically, you just take the MSB of the input and repeat it to fill to
the left.

On Jan 27, 5:07*pm, David Rogoff <[email protected]> wrote:
> On 2010-01-27 15:30:27 -0800, rekz said:
>
> > On Jan 27, 4:23*pm, rekz <[email protected]> wrote:
> >> I would like to design a 16 to 32-bit sign extension unit. I would
> >> like to preserve the number's sign (positive/negative). So I guess
> >> after I append digits to the MSB then I would need to take care of the
> >> sign's, how could I do this in verilog?
>
> > is it just as simple as this:
>
> > module SignExtension(a, result);
>
> > input [15:0] a; // 16-bit input
> > output [31:0] result; // 32-bit output
>
> > assign result = { 16{a[31]}, a };
>
> This should be assign result = { 16{a[15]}, a };
>
> Basically, you just take the MSB of the input and repeat it to fill to
> the left.
>
>
>
> > endmodule
>
>

And doing this will preserve the sign of the numbers?? Why?

David Rogoff wrote:
> On 2010-01-27 15:30:27 -0800, rekz said:
>
>> On Jan 27, 4:23 pm, rekz <[email protected]> wrote:
>> I would like to design a 16 to 32-bit sign extension unit. I would
>> like to preserve the number's sign (positive/negative). So I guess
>> after I append digits to the MSB then I would need to take care of the
>> sign's, how could I do this in verilog?
>>
>> is it just as simple as this:
>>
>> module SignExtension(a, result);
>> input [15:0] a; // 16-bit input
>> output [31:0] result; // 32-bit output
>>
>> assign result = { 16{a[31]}, a };
>>
>> endmodule
>
> This should be assign result = { 16{a[15]}, a };
>
> Basically, you just take the MSB of the input and repeat it to fill to
> the left.

Or use a signed right expression and the tools should take care of it
for you. There can be some subtlety with this, but it does work when you
understand the rules.

rekz wrote:
> On Jan 27, 5:07 pm, David Rogoff <[email protected]> wrote:
>> On 2010-01-27 15:30:27 -0800, rekz said:
>>
>>> On Jan 27, 4:23 pm, rekz <[email protected]> wrote:
>>>> I would like to design a 16 to 32-bit sign extension unit. I would
>>>> like to preserve the number's sign (positive/negative). So I guess
>>>> after I append digits to the MSB then I would need to take care of the
>>>> sign's, how could I do this in verilog?
>>> is it just as simple as this:
>>> module SignExtension(a, result);
>>> input [15:0] a; // 16-bit input
>>> output [31:0] result; // 32-bit output
>>> assign result = { 16{a[31]}, a };
>> This should be assign result = { 16{a[15]}, a };
>>
>> Basically, you just take the MSB of the input and repeat it to fill to
>> the left.
>>
>>
>>
>>> endmodule
>>
>
> And doing this will preserve the sign of the numbers?? Why?

On Wed, 27 Jan 2010 16:44:28 -0800, "Cary R." wrote:

>> Basically, you just take the MSB of the input and
>> repeat it to fill to the left.
>
>Or use a signed right expression and the tools should take
>care of it for you. There can be some subtlety with this,

Nominated for Understatement Of The Year.

> but it does work when you understand the rules.

And spectacularly and mysteriously fails to work when you
don't, or when you do understand but you forget one of
the many Gotchas.

I would very strongly urge beginners to steer clear of
Verilog signed arithmetic - especially beginners like
the OP whose grasp of twos-complement seems shaky :-)
--
Jonathan Bromley

Jonathan Bromley wrote:
> On Wed, 27 Jan 2010 16:44:28 -0800, "Cary R." wrote:
>
>>> Basically, you just take the MSB of the input and
>>> repeat it to fill to the left.
>> Or use a signed right expression and the tools should take
>> care of it for you. There can be some subtlety with this,
>
> Nominated for Understatement Of The Year.
>
>> but it does work when you understand the rules.
>
> And spectacularly and mysteriously fails to work when you
> don't, or when you do understand but you forget one of
> the many Gotchas.
>
> I would very strongly urge beginners to steer clear of
> Verilog signed arithmetic - especially beginners like
> the OP whose grasp of twos-complement seems shaky :-)

Correct advice, but now those poor beginners must be
really confused :-) They might intuitively think that
handling negative integers is a common task that should
be simple.

My advice to them: think outside the box for a while,
before attempting to become a Verilog guru. Perhaps your
intuition is right, and it's the HDL language designers
that have it all wrong. Perhaps there is a better way.

> And spectacularly and mysteriously fails to work when you
> don't, or when you do understand but you forget one of
> the many Gotchas.

Been there seen that, but sometime not using it is even more painful.
For example in my current design I have a signed signal that represents
a correction value as 0.2dB per step. This needs to be expanded to
0.15dB per step with the correct rounding. Then because of a mistake in
the polarity of the analog system I need to negate the result. With
signed registers and arithmetic this isn't too bad to look at and
understand. Well the rounding of negative values is a bit complicated,
but that's completely independent and I added ample comments. ;-)

> I would very strongly urge beginners to steer clear of
> Verilog signed arithmetic - especially beginners like
> the OP whose grasp of twos-complement seems shaky :-)

I won't disagree with this, but they need to understand that
alternatives exist. Otherwise they won't understand perfectly valid and
working code.