P
Paul Solomon
Guest
Hi guys,
I am trying to work out a piece of code that will allow me to obtain the
number of leading zero's in a number.
I have tried to do this with the following case statement but it does not
work.
I need this to work for positive and negative numbers, so the number of
leading 0's or the number of leading 1's
Can anyone give me pointers on how I can make this work??
Regards,
Paul Solomon
parameter WIDTH = 14;
input signed [WIDTH-1:0] x;
reg [3:0] scale_x;
// Prescaler
always @(posedge clk, posedge reset) begin
case (x)
{14'b00000000000001,{(WIDTH-14){1'bx}}} : scale_x <= 12;
{14'b11111111111111,{(WIDTH-14){1'bx}}} : scale_x <= 12;
{13'b0000000000001,{(WIDTH-13){1'bx}}} : scale_x <= 11;
{13'b1111111111111,{(WIDTH-13){1'bx}}} : scale_x <= 11;
{12'b000000000001,{(WIDTH-12){1'bx}}} : scale_x <= 10;
{12'b111111111111,{(WIDTH-12){1'bx}}} : scale_x <= 10;
{11'b00000000001,{(WIDTH-11){1'bx}}} : scale_x <= 9;
{11'b11111111111,{(WIDTH-11){1'bx}}} : scale_x <= 9;
{10'b0000000001,{(WIDTH-10){1'bx}}} : scale_x <= 8;
{10'b1111111111,{(WIDTH-10){1'bx}}} : scale_x <= 8;
{9'b000000001,{(WIDTH-9){1'bx}}} : scale_x <= 7;
{9'b111111111,{(WIDTH-9){1'bx}}} : scale_x <= 7;
{8'b00000001,{(WIDTH-8){1'bx}}} : scale_x <= 6;
{8'b11111111,{(WIDTH-8){1'bx}}} : scale_x <= 6;
{7'b0000001,{(WIDTH-7){1'bx}}} : scale_x <= 5;
{7'b1111111,{(WIDTH-7){1'bx}}} : scale_x <= 5;
{6'b000001,{(WIDTH-6){1'bx}}} : scale_x <= 4;
{6'b111111,{(WIDTH-6){1'bx}}} : scale_x <= 4;
{5'b00001,{(WIDTH-5){1'bx}}} : scale_x <= 3;
{5'b11111,{(WIDTH-5){1'bx}}} : scale_x <= 3;
{4'b0001,{(WIDTH-4){1'bx}}} : scale_x <= 2;
{4'b1111,{(WIDTH-4){1'bx}}} : scale_x <= 2;
{3'b001,{(WIDTH-3){1'bx}}} : scale_x <= 1;
{3'b111,{(WIDTH-3){1'bx}}} : scale_x <= 1;
default : scale_x <= 0;
endcase
end
I am trying to work out a piece of code that will allow me to obtain the
number of leading zero's in a number.
I have tried to do this with the following case statement but it does not
work.
I need this to work for positive and negative numbers, so the number of
leading 0's or the number of leading 1's
Can anyone give me pointers on how I can make this work??
Regards,
Paul Solomon
parameter WIDTH = 14;
input signed [WIDTH-1:0] x;
reg [3:0] scale_x;
// Prescaler
always @(posedge clk, posedge reset) begin
case (x)
{14'b00000000000001,{(WIDTH-14){1'bx}}} : scale_x <= 12;
{14'b11111111111111,{(WIDTH-14){1'bx}}} : scale_x <= 12;
{13'b0000000000001,{(WIDTH-13){1'bx}}} : scale_x <= 11;
{13'b1111111111111,{(WIDTH-13){1'bx}}} : scale_x <= 11;
{12'b000000000001,{(WIDTH-12){1'bx}}} : scale_x <= 10;
{12'b111111111111,{(WIDTH-12){1'bx}}} : scale_x <= 10;
{11'b00000000001,{(WIDTH-11){1'bx}}} : scale_x <= 9;
{11'b11111111111,{(WIDTH-11){1'bx}}} : scale_x <= 9;
{10'b0000000001,{(WIDTH-10){1'bx}}} : scale_x <= 8;
{10'b1111111111,{(WIDTH-10){1'bx}}} : scale_x <= 8;
{9'b000000001,{(WIDTH-9){1'bx}}} : scale_x <= 7;
{9'b111111111,{(WIDTH-9){1'bx}}} : scale_x <= 7;
{8'b00000001,{(WIDTH-8){1'bx}}} : scale_x <= 6;
{8'b11111111,{(WIDTH-8){1'bx}}} : scale_x <= 6;
{7'b0000001,{(WIDTH-7){1'bx}}} : scale_x <= 5;
{7'b1111111,{(WIDTH-7){1'bx}}} : scale_x <= 5;
{6'b000001,{(WIDTH-6){1'bx}}} : scale_x <= 4;
{6'b111111,{(WIDTH-6){1'bx}}} : scale_x <= 4;
{5'b00001,{(WIDTH-5){1'bx}}} : scale_x <= 3;
{5'b11111,{(WIDTH-5){1'bx}}} : scale_x <= 3;
{4'b0001,{(WIDTH-4){1'bx}}} : scale_x <= 2;
{4'b1111,{(WIDTH-4){1'bx}}} : scale_x <= 2;
{3'b001,{(WIDTH-3){1'bx}}} : scale_x <= 1;
{3'b111,{(WIDTH-3){1'bx}}} : scale_x <= 1;
default : scale_x <= 0;
endcase
end