Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
16 years ago

signed/unsigned multipliers

I can't seem to find a way to instantiate a multiplier that will dynamically select between signed and unsigned multiplication. This is on a Cyclone II, but applies to other parts as well.

Reading the Cyclone II documentation it is clear that signa, signb inputs does what I want, but none of the multipliers created with the Wizzard exports that as an input signal. Trying to infer it with the code below doesn't work (bug in Quartus II?); it complains that no logic depends on input_sign. However looking at the RTL with Tools/Netlist Viewers/RTL Viewer clearly shows it being used, however the technology map viewer shows input_sign as disconnected and signa and signb hardwired to ground.

This looks like a bug to me, but I could be doing something wrong.


module mult(input clock,
input        input_sign,
input  input_a,
input  input_b,
output reg  output_c);
reg  a, b;
reg sign;
always @(posedge clock) begin
  sign <= input_sign;
  a <= input_a;
  b <= input_b;
  
  if (sign)
        output_c <= $signed(a) * $signed(b);
  else
        output_c <= $unsigned(a) * $unsigned(b);
  end
 endmodule
 

42 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    It makes sense. The way I make sense is this:

    [invert => +1] naturally reverses by [-1 => invert]

    but if we also reverse the order of operations then :

    [-1 => invert] becomes equivalent to [invert => +1]
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    It is always true if we allow for correct width. The issue of 8 bit width representing numbers from 0~127 and -1 to -128 is separate.

    The puzzle still stands:

    - => + should reverse + => - naturally

    hence for - => + subtract 1 then invert (by definition)

    but how come the same rule apply to both conversions as well.

    i.e. [invert add 1] in either case.

    --- Quote End ---

    If you search Wikipedia for "two's complement" there is an explanation, but rather complex.

    For me, when you start with a positive number and complement the objective is to find the distance from zero in the negative direction so when you complement 0 "adding 1" has the effect of reducing the magnitude so that 0 is still zero, but the magnitude of all negative numbers appears to be offset toward 0. When you complement the negative number you have to add 1 to adjust the magnitude to get the correct distance from 0 in the positive direction. Therefore the maximum negative number 1.000 ... 0 is farther away from 0 than the maximum positive number and cannot be converted.