P
pallav
Guest
I understand Verilog 2001 added the ">>>" operator for sign extension
right shift. But how does it work? I was implementing a shifter unit
as part of an ALU. I had done the following:
assign #1 rarithmetic = b >>> shiftamount;
where b and shiftamount were both input [31:0] vectors. However, the
sign extension does not occur. I had b = 0xd1200000 and the result was
0x0000d120 instead of 0xffffd120. I know there is also the "signed"
keyword but it didn't seem to have any effect when I tried "input
signed [31:0] ...". What am I doing wrong? Would synthesis results be
different if "signed" keyword is used?
In the end, I just end up coding up a variable 32 bit sign extension
shifter as shown below. Is there a better/more compact way to do the
same thing? I'd like to keep it synthesizable if possible. Thanks for
any help.
Kind regards.
/*
* 32-bit sign extend shifter
*/
module signextshifter32(/*AUTOARG*/
// Outputs
y,
// Inputs
a, shiftamt
);
input [31:0] a; // input
input [4:0] shiftamt; // shift amount
output [31:0] y; // output
assign #1 y = (shiftamt == 5'b00000) ? {a[31:0]} :
(shiftamt == 5'b00001) ? {{1{a[31]}}, a[31:1]} :
(shiftamt == 5'b00010) ? {{2{a[31]}}, a[31:2]} :
(shiftamt == 5'b00011) ? {{3{a[31]}}, a[31:3]} :
(shiftamt == 5'b00100) ? {{4{a[31]}}, a[31:4]} :
(shiftamt == 5'b00101) ? {{5{a[31]}}, a[31:5]} :
(shiftamt == 5'b00110) ? {{6{a[31]}}, a[31:6]} :
(shiftamt == 5'b00111) ? {{7{a[31]}}, a[31:7]} :
(shiftamt == 5'b01000) ? {{8{a[31]}}, a[31:8]} :
(shiftamt == 5'b01001) ? {{9{a[31]}}, a[31:9]} :
(shiftamt == 5'b01010) ? {{10{a[31]}}, a[31:10]} :
(shiftamt == 5'b01011) ? {{11{a[31]}}, a[31:11]} :
(shiftamt == 5'b01100) ? {{12{a[31]}}, a[31:12]} :
(shiftamt == 5'b01101) ? {{13{a[31]}}, a[31:13]} :
(shiftamt == 5'b01110) ? {{14{a[31]}}, a[31:14]} :
(shiftamt == 5'b01111) ? {{15{a[31]}}, a[31:15]} :
(shiftamt == 5'b10000) ? {{16{a[31]}}, a[31:16]} :
(shiftamt == 5'b10001) ? {{17{a[31]}}, a[31:17]} :
(shiftamt == 5'b10010) ? {{18{a[31]}}, a[31:18]} :
(shiftamt == 5'b10011) ? {{19{a[31]}}, a[31:19]} :
(shiftamt == 5'b10100) ? {{20{a[31]}}, a[31:20]} :
(shiftamt == 5'b10101) ? {{21{a[31]}}, a[31:21]} :
(shiftamt == 5'b10110) ? {{22{a[31]}}, a[31:22]} :
(shiftamt == 5'b10111) ? {{23{a[31]}}, a[31:23]} :
(shiftamt == 5'b11000) ? {{24{a[31]}}, a[31:24]} :
(shiftamt == 5'b11001) ? {{25{a[31]}}, a[31:25]} :
(shiftamt == 5'b11010) ? {{26{a[31]}}, a[31:26]} :
(shiftamt == 5'b11011) ? {{27{a[31]}}, a[31:27]} :
(shiftamt == 5'b11100) ? {{28{a[31]}}, a[31:28]} :
(shiftamt == 5'b11101) ? {{29{a[31]}}, a[31:29]} :
(shiftamt == 5'b11110) ? {{30{a[31]}}, a[31:30]} : {31
{a[31]}};
endmodule
right shift. But how does it work? I was implementing a shifter unit
as part of an ALU. I had done the following:
assign #1 rarithmetic = b >>> shiftamount;
where b and shiftamount were both input [31:0] vectors. However, the
sign extension does not occur. I had b = 0xd1200000 and the result was
0x0000d120 instead of 0xffffd120. I know there is also the "signed"
keyword but it didn't seem to have any effect when I tried "input
signed [31:0] ...". What am I doing wrong? Would synthesis results be
different if "signed" keyword is used?
In the end, I just end up coding up a variable 32 bit sign extension
shifter as shown below. Is there a better/more compact way to do the
same thing? I'd like to keep it synthesizable if possible. Thanks for
any help.
Kind regards.
/*
* 32-bit sign extend shifter
*/
module signextshifter32(/*AUTOARG*/
// Outputs
y,
// Inputs
a, shiftamt
);
input [31:0] a; // input
input [4:0] shiftamt; // shift amount
output [31:0] y; // output
assign #1 y = (shiftamt == 5'b00000) ? {a[31:0]} :
(shiftamt == 5'b00001) ? {{1{a[31]}}, a[31:1]} :
(shiftamt == 5'b00010) ? {{2{a[31]}}, a[31:2]} :
(shiftamt == 5'b00011) ? {{3{a[31]}}, a[31:3]} :
(shiftamt == 5'b00100) ? {{4{a[31]}}, a[31:4]} :
(shiftamt == 5'b00101) ? {{5{a[31]}}, a[31:5]} :
(shiftamt == 5'b00110) ? {{6{a[31]}}, a[31:6]} :
(shiftamt == 5'b00111) ? {{7{a[31]}}, a[31:7]} :
(shiftamt == 5'b01000) ? {{8{a[31]}}, a[31:8]} :
(shiftamt == 5'b01001) ? {{9{a[31]}}, a[31:9]} :
(shiftamt == 5'b01010) ? {{10{a[31]}}, a[31:10]} :
(shiftamt == 5'b01011) ? {{11{a[31]}}, a[31:11]} :
(shiftamt == 5'b01100) ? {{12{a[31]}}, a[31:12]} :
(shiftamt == 5'b01101) ? {{13{a[31]}}, a[31:13]} :
(shiftamt == 5'b01110) ? {{14{a[31]}}, a[31:14]} :
(shiftamt == 5'b01111) ? {{15{a[31]}}, a[31:15]} :
(shiftamt == 5'b10000) ? {{16{a[31]}}, a[31:16]} :
(shiftamt == 5'b10001) ? {{17{a[31]}}, a[31:17]} :
(shiftamt == 5'b10010) ? {{18{a[31]}}, a[31:18]} :
(shiftamt == 5'b10011) ? {{19{a[31]}}, a[31:19]} :
(shiftamt == 5'b10100) ? {{20{a[31]}}, a[31:20]} :
(shiftamt == 5'b10101) ? {{21{a[31]}}, a[31:21]} :
(shiftamt == 5'b10110) ? {{22{a[31]}}, a[31:22]} :
(shiftamt == 5'b10111) ? {{23{a[31]}}, a[31:23]} :
(shiftamt == 5'b11000) ? {{24{a[31]}}, a[31:24]} :
(shiftamt == 5'b11001) ? {{25{a[31]}}, a[31:25]} :
(shiftamt == 5'b11010) ? {{26{a[31]}}, a[31:26]} :
(shiftamt == 5'b11011) ? {{27{a[31]}}, a[31:27]} :
(shiftamt == 5'b11100) ? {{28{a[31]}}, a[31:28]} :
(shiftamt == 5'b11101) ? {{29{a[31]}}, a[31:29]} :
(shiftamt == 5'b11110) ? {{30{a[31]}}, a[31:30]} : {31
{a[31]}};
endmodule