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

Saturating Arithmetic

Standard two’s-complement arithmetic wraps around when numbers exceed representable numerical ranges. In digital audio systems, exceeding positive limits causes values to wrap to negative limits. This abrupt transition produces severe audio distortion and acoustic clicks. In closed-loop motor controllers, numerical wraparound causes feedback oscillations. Saturating arithmetic clamps out-of-range values to the nearest maximum or minimum limit instead of wrapping.

Detecting Signed Arithmetic Overflow

Standard carry-out flags do not detect signed two’s-complement overflow. Signed overflow occurs when the carry into the sign bit differs from the carry out of the sign bit.

An equivalent behavioral rule simplifies hardware detection: Signed overflow occurs only when both inputs share the same sign and the sum produces the opposite sign. Adding numbers with opposite signs cannot produce arithmetic overflow:

wire signed [7:0] a, b;
wire signed [7:0] sum = a + b;
wire overflow = (a[7] == b[7]) && (sum[7] != a[7]);

This expression evaluates the most significant bits directly without requiring extra sign-extension bits.

Implementing a Signed Saturating Adder

When overflow occurs, a multiplexer selects the maximum or minimum boundary constant:

`timescale 1ns / 1ps
`default_nettype none

module sat_adder_signed #(parameter WIDTH = 8) (
    input  wire signed [WIDTH-1:0] a, b,
    output reg  signed [WIDTH-1:0] sum
);
    wire signed [WIDTH-1:0] raw_sum = a + b;
    wire overflow = (a[WIDTH-1] == b[WIDTH-1]) && (raw_sum[WIDTH-1] != a[WIDTH-1]);

    localparam signed [WIDTH-1:0] MAX_VAL = {1'b0, {(WIDTH-1){1'b1}}};   // Maximum positive limit (0111...1)
    localparam signed [WIDTH-1:0] MIN_VAL = {1'b1, {(WIDTH-1){1'b0}}};   // Minimum negative limit (1000...0)

    always @(*) begin
        if (overflow)
            sum = a[WIDTH-1] ? MIN_VAL : MAX_VAL;  // Select limit based on operand sign
        else
            sum = raw_sum;
    end
endmodule

When two negative operands overflow, the mathematical sum is less than MIN_VAL. The circuit clamps to MIN_VAL. When two positive operands overflow, the circuit clamps to MAX_VAL.

Unsigned Saturating Addition

Unsigned arithmetic has only an upper ceiling boundary:

module sat_adder_unsigned #(parameter WIDTH = 8) (
    input  wire [WIDTH-1:0] a, b,
    output reg  [WIDTH-1:0] sum
);
    wire [WIDTH:0] raw_sum = a + b;   // Extra upper bit captures carry-out directly

    always @(*) begin
        if (raw_sum[WIDTH])           // Carry-out indicates numerical overflow
            sum = {WIDTH{1'b1}};      // Clamp output to all-ones
        else
            sum = raw_sum[WIDTH-1:0];
    end
endmodule

For unsigned operands, the carry-out bit directly indicates overflow. The logic clamps the output to the maximum unsigned vector ({WIDTH{1'b1}}).

DSP and Processor Applications

Digital signal processing algorithms rely on saturating arithmetic. Audio mixing consoles sum multiple audio tracks using saturation logic to emulate analog clipping. Video image processing algorithms clamp pixel color intensities to prevent color inversion. Processor instruction sets, such as ARM (QADD, QSUB) and RISC-V DSP extensions, include hardware saturating instructions. These instructions eliminate the software overhead of performing per-sample branch checks.

Verification Exercise

Implement a signed saturating subtractor (diff = a - b). Adjust the overflow detection logic for subtraction. Recall that subtracting $b$ is mathematically equivalent to adding $-b$.

View Solution
`timescale 1ns / 1ps
`default_nettype none

module sat_subtractor_signed #(parameter WIDTH = 8) (
    input  wire signed [WIDTH-1:0] a, b,
    output reg  signed [WIDTH-1:0] diff
);
    wire signed [WIDTH-1:0] raw_diff = a - b;
    // Overflow occurs when operands have different signs
    // and the result sign differs from operand a
    wire overflow = (a[WIDTH-1] != b[WIDTH-1]) && (raw_diff[WIDTH-1] != a[WIDTH-1]);

    localparam signed [WIDTH-1:0] MAX_VAL = {1'b0, {(WIDTH-1){1'b1}}};
    localparam signed [WIDTH-1:0] MIN_VAL = {1'b1, {(WIDTH-1){1'b0}}};

    always @(*) begin
        if (overflow)
            diff = a[WIDTH-1] ? MIN_VAL : MAX_VAL;
        else
            diff = raw_diff;
    end
endmodule

In signed subtraction, overflow occurs when operands have opposite signs and the difference sign fails to match operand a. A positive value minus a negative value can overflow high. A negative value minus a positive value can overflow low.

Saturating arithmetic clamps numerical overflows to valid operating ranges. The next article explores pipelined adder architectures to meet target clock frequencies.

← Signed Arithmetic Pitfalls Pipelining a Wide Adder →