Module 7: Advanced RTL Design Techniques · 7.2 Arithmetic Circuits in RTL

Signed Arithmetic Pitfalls

Section 7.1 addressed structural modularity. Section 7.2 examines arithmetic precision, signed evaluation rules, and physical pipeline timing.

Consider evaluating the relational expression a > b when a = 4'b1000 and b = 4'b0111. In unsigned representation, 4'b1000 equals decimal 8 and 4'b0111 equals decimal 7. The comparison evaluates to true (1'b1). In signed two’s-complement representation, 4'b1000 equals decimal -8. The comparison evaluates to false (1'b0). The identical bit vector produces opposite logical outcomes based solely on variable type declarations.

Behavioral Effects of the signed Keyword

Declaring a wire or register as signed does not modify its physical bit width or hardware storage. It instructs synthesis compilers and simulation engines how to interpret bit vectors during arithmetic evaluation:

wire signed [7:0] a;   // Interpreted as two's-complement signed vector
wire        [7:0] b;   // Interpreted as standard unsigned binary vector

Signals a and b occupy identical 8-bit physical wires. The difference appears when evaluating relational operators (>, <, >=, <=) and arithmetic right shifts (>>>). Bitwise logic gates (&, |, ^) operate identically regardless of signed declarations.

Pitfall 1: Silent Coercion to Unsigned Evaluation

The IEEE Verilog standard specifies that if any operand in an expression is unsigned, the compiler evaluates the entire expression as unsigned. This rule applies even when all other operands use signed declarations:

wire signed [7:0] a = -8'sd5;   // Two's-complement decimal -5
wire        [7:0] b = 8'd3;     // Unsigned decimal 3
wire result = (a > b);          // Evaluated as UNSIGNED because b is unsigned

A developer expects -5 > 3 to evaluate to false. Because signal b is unsigned, the simulator evaluates operand a (8'b1111_1011) as unsigned decimal 251. The expression 251 > 3 evaluates to true (1'b1). The simulation compiles and executes without generating warnings.

To fix this defect, enforce consistent signed types using explicit type casting with $signed():

wire result = (a > $signed(b));   // Evaluates correctly as signed: -5 > 3 = false

Pitfall 2: Logical Versus Arithmetic Shift Operators

Verilog provides two right-shift operators:

  • >>: Logical right shift; vacates upper bits with logic zeros.
  • >>>: Arithmetic right shift; vacates upper bits with sign bits.

The >>> operator performs arithmetic sign extension only when the target operand is explicitly declared signed. Applying >>> to an unsigned operand executes a standard logical zero-fill shift:

wire [7:0] x = 8'b1000_0000;    // Unsigned vector
wire [7:0] y1 = x >>> 1;         // Evaluates to 8'b0100_0000 (Logical shift)

wire signed [7:0] xs = x;        // Signed vector
wire signed [7:0] y2 = xs >>> 1; // Evaluates to 8'b1100_0000 (Arithmetic shift, decimal -64)

The operator requires an explicit signed declaration to activate sign-extension behavior.

Pitfall 3: Bit Concatenation Discards Signedness

The concatenation operator {...} always treats nested bit vectors as unsigned values. Concatenation operations discard the signed attribute of enclosed operands:

wire signed [3:0] small = -4'sd3;         // Bit vector: 4'b1101
wire signed [7:0] wrong = {4'b0, small};  // Defect: zero-extends to 8'b0000_1101 (+13)
wire signed [7:0] right = {{4{small[3]}}, small}; // Manual sign extension: 8'b1111_1101 (-3)

Direct continuous assignment of a narrower signed signal to a wider signed target performs sign extension automatically:

wire signed [7:0] right2 = small;   // Automatically sign-extends to 8'b1111_1101 (-3)

Assignment statements preserve signed attributes; concatenation expressions do not. When assembling complex vectors inside concatenation braces, replicate sign bits explicitly.

Industry Best Practices

Follow these guidelines to avoid signed arithmetic defects:

  1. Declare all operands, internal wires, and ports as signed when implementing signed datapaths.
  2. Use signed literal notation: write -8'sd5 instead of -8'd5.
  3. Never mix signed and unsigned operands in relational comparisons.
  4. Apply $signed() or $unsigned() type casts at operation sites to make intent explicit.

Many bugs pass basic unit testing because positive integers share identical bit representations in signed and unsigned formats. Defects emerge only when negative values traverse uncast arithmetic comparisons.

Verification Exercise

Analyze the following code fragment:

wire signed [7:0] a = -8'sd20;
wire        [7:0] b = 8'd15;
wire result = (a < b);

Predict the value of result and correct the expression to enforce signed evaluation.

View Solution

Because signal b is unsigned, the compiler evaluates the comparison using unsigned arithmetic. The bit pattern for -20 (8'b1110_1100) evaluates as unsigned decimal 236. The expression 236 < 15 evaluates to false (1'b0).

Correct the expression using $signed():

wire result = (a < $signed(b));   // Evaluates correctly: -20 < 15 = true (1'b1)

Understanding signed evaluation rules prevents subtle arithmetic defects. The next article explores saturating arithmetic and overflow handling.

← A Width-Configurable ALU Saturating Arithmetic →