Get the Most Significant Bit

wire [15:0] data;
wire [3:0] msb;

wire or_15_14 = |data[15:14];
wire or_13_12 = |data[13:12];
wire or_11_10 = |data[11:10];
wire or_9_8 = |data[9:8];
wire or_7_6 = |data[7:6];
wire or_5_4 = |data[5:4];
wire or_3_2 = |data[3:2];
wire or_1_0 = |data[1:0];

wire or_15_14_13_12 = or_15_14 | or_13_12;
wire or_11_10_9_8 = or_11_10 | or_9_8;
wire or_7_6_5_4 = or_7_6 | or_5_4;
wire or_3_2_1_0 = or_3_2 | or_1_0;

wire or_15_14_13_12_11_10_9_8 = or_15_14_13_12 | or_11_10_9_8;
wire or_7_6_5_4_3_2_1_0 = or_7_6_5_4 | or_3_2_1_0;

assign msb[3] = or_15_14_13_12_11_10_9_8;

assign msb[2] = msb[3] ? or_15_14_13_12 : or_7_6_5_4;

assign msb[1] = msb[3:2] == 2'b11 ? or_15_14 : msb[3:2] == 2'b10 ? or_11_10 : msb[3:2] == 2'b01 ? or_7_6 : or_3_2;

assign msb[0] = msb[3:1] == 3'b111 ? data[15] : msb[3:1] == 3'b110 ? data[13] : msb[3:1] == 3'b101 ? data[11] : msb[3:1] == 3'b100 ? data[9] : msb[3:1] == 3'b011 ? data[7] : msb[3:1] == 3'b010 ? data[5] : msb[3:1] == 3'b001 ? data[3] : data[1];

0 Comments: