0

I'm trying to implement as follows to multiplying by 15.

module mul15( 
output [10:0] result, 
input [3:0] a
 ); 
assign result =   a*15;
 endmodule

But is there any improve way to multiplying to a by 15?

I think there are 2 ways like this

1.result = a<<4 -1;

2.result = {a,3'b1111_1111};

Ans I think the best way is 2. but I'm not sure also with aspect to synthesis.

update:

What if I am multiplying 0 at {a,3'b1111_1111}? This is 255 not 0.

Does anyone know the best way?

Update How about this way?

Case1

result = {a,8'b0}+ {a,7'b0}+ {a,6'b0}+ {a,5'b0}+ {a,4'b0}+ {a,7'b0}+ {a,3'b0}+ {a,2'b0}+ {a,1'b0}+ a; But it looks 8 adder used.

Case2

result = a<<8 -1

I'm not sure what is the best way else.

Community
  • 1
  • 1
Carter
  • 274
  • 2
  • 13
  • you don't make the least sense; a triple left shift (1.) would be a multiplication with 8; `a*15` is always < `a*16`, so `output` should have only four bits more length than `a`, not seven. – Marcus Müller Feb 10 '15 at 22:55
  • @MarcusMüller Thanks sir, I have just changed it – Carter Feb 10 '15 at 23:00
  • @Morgan Thanks, but your are using multiplier. My method does not use multiplier also fast and low power. – Carter Feb 11 '15 at 00:08
  • Not sure what your trying to do by concatenating `{}` with a number, pretty sure that is never going to work out as a multiply by any number, unless your just zero padding for a shift, which is multiply by power of 2. – Morgan Feb 11 '15 at 00:14
  • @e19293001: I'm not the one to downvote, but: a << 4 - 1 = a*16 -1 != a*15; kaji simply doesn't do the elementary math. – Marcus Müller Feb 11 '15 at 08:25

2 Answers2

4

There is always a*16 - a. Static multiplications of power of 2 are basically free in hardware; it is just hard-coded 0s to the LSB. So you just need one 11-bit full-subtracter, which is a full adder and some inverters.

other forms:

result = a<<4 - a;
result = {a,4'b0} - a; // unsigned full-subtractor
result = {a,4'b0} + ~a + 1'b1; // unsigned full-adder w/ carry in, 2's complement
result = {{3{a[3]}},a,4'b0} + ~{ {7{a[3]}}, a} + 1'b1; // signed full-adder w/ carry in, 2's complement
Greg
  • 18,111
  • 5
  • 46
  • 68
  • I'm not sure, what if I have to synthesis with your code, which one is less resource used. – Carter Feb 11 '15 at 00:51
  • @kaji you should try them all! Once your synthesis is setup just swap out the implementations. – Morgan Feb 11 '15 at 00:58
  • Thanks you Greg, Sir. I can conclude that a<<4-a is best way. – Carter Feb 11 '15 at 01:03
  • 1
    @kaji: Morgan is right, just let your sythesizer handle this. basic numeric operations have been automatically optimized since forever. In fact, it's perfectly possible that using `a*15` is faster than any explicit bit-pushery, because whatever -- you might have free DSP slices. – Marcus Müller Feb 11 '15 at 08:28
2

The cleanest RTL version is as you have stated in the question:

module mul15( 
  input      [3:0] a
  output reg [7:0] result, 
); 
  always @* begin
    result = a * 4'd15;
  end
endmodule

The Multiplicand 15 in binary is 4'b1111; That is 8 + 4 + 2 + 1.

Instead of a multiplier it could be broken down into the sum of these powers of 2. Powers of 2 are just barrel shifts. This is how a shift and add multiplier would work.

module mul15( 
  input      [3:0] a
  output reg [7:0] result, 
); 
  always @* begin
    //        8        4        2       1 =>15
    result = (a<<3) + (a<<2) + (a<<1) + a;
  end
endmodule

To minimise the number of adders required a CSD could be used. making 15 out of 16-1:

module mul15( 
  input      [3:0] a
  output reg [7:0] result, 
); 
  always @* begin
    //        16    - 1 =>15
    result = (a<<4) - a;
  end
endmodule

With a modern synthesis tool these should all result in same the thing. Therefore having more readable code which gives a clear instruction to the tool as to what you intended gives it the free rein to optimise as required.

Morgan
  • 19,934
  • 8
  • 58
  • 84
  • Thanks Sir, I have some query about your answer.the first answer's resource is adder 3 shifter 3 used it. Second is used subtractor 1, shifter 1. Then which one is more less resource used? – Carter Feb 11 '15 at 00:46
  • As I know,the barrel shifter is not good performance at timing. – Carter Feb 11 '15 at 00:52
  • subtractors is an adder with input inverted, and the carry tied high (to create twos complement invert) inverters are cheaper then buffers. so a subtractor is often cheaper. – Morgan Feb 11 '15 at 00:53
  • Barrel shifters are usually free for timing and area, it is just clever muxing. – Morgan Feb 11 '15 at 00:54
  • @kaji Remember that the synthesis tool can build the logic out of what ever it wants as long as it is functionally correct. Some times RTL helps steer it down a better path. All of these should give you the same thing. – Morgan Feb 11 '15 at 00:56
  • Yes We can concluded that (a<<4)-1 is the best method. Thanks you very much Morgan – Carter Feb 11 '15 at 01:00
  • kaji: I don't know, you're a hardware designer. How comes you think a*16 -1 == a*15? It's not, unless a == 1! – Marcus Müller Feb 11 '15 at 08:27
  • @MarcusMüller the `-1` is meant to be `-a`. – Morgan Feb 11 '15 at 12:03