Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 37 ยท Data Types and Signals

Numeric Arithmetic with numeric_std

Do arithmetic in VHDL the standard way with numeric_std: unsigned and signed types, to_unsigned, to_integer, resize, overflow, comparisons and shifting.

A std_logic_vector is just a bag of bits: you cannot add two of them. To do arithmetic you tell the compiler how to interpret the bits, as an unsigned number or as a two's-complement signed number. The IEEE package numeric_std provides the types unsigned and signed, which are arrays of std_logic with arithmetic defined on them.

VHDL
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

unsigned and signed

VHDL
signal a, b : unsigned(7 downto 0);   -- 0 .. 255
signal s    : signed(7 downto 0);     -- -128 .. 127
signal sum  : unsigned(7 downto 0);

sum <= a + b;                          -- 8-bit result, wraps on overflow

The result of + has the width of the larger operand, so carry-out is lost silently. To keep the carry, widen the operands first:

VHDL
signal wide : unsigned(8 downto 0);
wide <= ('0' & a) + ('0' & b);         -- 9 bits: top bit is the carry
-- or:  wide <= resize(a, 9) + resize(b, 9);

Conversions you will use all the time

From โ†’ To Code
std_logic_vector โ†’ unsigned unsigned(slv) (cast)
unsigned โ†’ std_logic_vector std_logic_vector(u) (cast)
integer โ†’ unsigned to_unsigned(n, width)
unsigned โ†’ integer to_integer(u)
integer โ†’ signed to_signed(n, width)
change width resize(u, new_width)

resize for unsigned pads with zeros (or truncates the top bits); for signed it sign-extends. A typical idiom: addr <= to_integer(unsigned(addr_slv)) to index an array.

VHDL
signal slv  : std_logic_vector(7 downto 0);
signal u    : unsigned(7 downto 0);
signal idx  : integer range 0 to 255;

u   <= unsigned(slv) + 1;
slv <= std_logic_vector(u);
idx <= to_integer(unsigned(slv));

Comparison, mixing and literals

Relational operators (=, <, >= ...) work between two unsigneds, between two signeds, and between an unsigned and a natural:

VHDL
if u > 100 then ... end if;           -- unsigned vs integer: allowed
if count = 9 then ... end if;

You may add an integer to an unsigned directly (u + 1), but you cannot add an unsigned and a signed. Convert one of them first. Never compare a std_logic_vector with an integer; cast it.

Overflow and sign

Wrapping is the natural behavior of fixed-width hardware. In the two's-complement view signed(7 downto 0) the pattern x"80" is -128 and x"7F" is 127; adding 1 to 127 wraps to -128. The stand-in below shows unsigned and signed wrap-around for 8 bits and demonstrates resize (zero versus sign extension).

C Stand-in (C): 8-bit wrap and resize

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Keeping the carry

This stand-in shows why widening matters: an 8-bit unsigned sum can need 9 bits, and a multiplication of two 8-bit values needs 16 bits (numeric_std returns a result as wide as both operands together for *).

C Stand-in (C): widths of results

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Shifts and division

shift_left(u, n) and shift_right(u, n) are the functions for shifting unsigned and signed (for signed, shift_right is arithmetic). In VHDL-2008 the operators sll and srl also work on them. Shifting by a constant costs no logic, only wiring. Division by a power of two is a shift; general division (/) and mod may not synthesize efficiently, or at all, unless the divisor is constant. Multiplication usually maps onto dedicated DSP blocks in FPGAs.

VHDL-2008 adds ieee.numeric_std_unsigned, which treats std_logic_vector as unsigned so slv + 1 works. It is convenient but hides the interpretation, so many teams prefer explicit unsigned signals.

Writing u <= u + 1; with u declared as unsigned(3 downto 0) is fine and wraps from 15 to 0. But writing idx <= idx + 1; with idx : integer range 0 to 15 stops the simulation with a range error at 16. Choose the type that matches the behavior you want.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How do you turn the integer 5 into an 8-bit unsigned?

    to_unsigned takes the number and the result width.
  2. 2. a, b are unsigned(7 downto 0) and s <= a + b; has s : unsigned(7 downto 0). a = 200, b = 100. What is s?

    300 mod 256 = 44; the sum wraps in 8 bits without any error.
  3. 3. How does resize treat a signed value when widening?

    For signed values the sign bit is replicated, preserving the numeric value.
  4. 4. Which package should be used with unsigned and signed?

    numeric_std is the standard package that defines unsigned, signed and their arithmetic.

Summary

  • Use unsigned and signed from numeric_std for arithmetic; std_logic_vector is only bits.
  • Cast between related array types; use to_unsigned, to_integer and resize for numbers and widths.
  • Results wrap; widen operands first if you need the carry.
  • resize zero-extends unsigned and sign-extends signed.
  • Constant shifts are free; multiplication and division have hardware costs.

Next: Subtypes, Enumerations and Records