Algorithmcore
Bus-invert coding
Also called bus invert code, Stan-Burleson coding.
Stan and Burleson's code that adds one line to an n-bit bus and sends a word inverted when that toggles fewer lines, so at most ceil(n/2) of the n + 1 lines toggle per transfer.
- Time
- O(T (1 + n / w)) worst
- Space
- O(1 + n / w) extra
- Where
- T = the number of words sent; n = the number of data lines; w = the machine word size in bits
- Model
- Word RAM
- Properties
- deterministicexactonlineiterative
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Mircea R. Stan, Wayne P. Burleson, 1995
Bus-invert coding is a code for a parallel bus that lowers the number of lines that change value per transfer. It adds one line, INV, to an -bit bus. Before each transfer the encoder counts how many of the lines would toggle if the word went out as it is with INV = 0. If that count is more than , it sends the complement of the word with INV = 1, which toggles lines. The receiver complements the bus again when INV = 1. So at most of the lines toggle per transfer, against without the code, and for random data the mean also falls, from to toggles for . Stan and Burleson (1995) made it for off-chip buses, where each line drives a large capacitance and the dynamic power of the lines is a large part of the chip power.
In plain words
Each wire of a bus costs energy when its value changes. Before you send the next word, compare it with what the wires show now. If more than half of the wires would change, send the opposite of every bit instead, so that fewer than half change. Then set one extra wire to say "this word is upside down", and the receiver flips it back.
The extra wire also counts, because it can change too. With it counted, no transfer ever changes more than about half of the wires.
The problem
A bus has data lines and one line INV. Its state is a pair with the values on the data lines and the value on INV. A stream of words must go over the bus, one word per transfer, so that the receiver recovers each from the state alone. The cost of a transfer from to is the number of lines that toggle, the Hamming distance
where is the Hamming weight. Each state that bus-invert coding may send for is either or , with the bitwise complement. The goal is to choose one of the two for each word so that the total cost is least, and so that the cost of every single transfer is bounded.
The link to power uses the convention of the dynamic power page, with the number of rising (0 to 1) transitions of a line per cycle. On one line, rising and falling transitions alternate, so over a long run is half the number of toggles per cycle. With equal capacitance per line, the bus power is . The model counts each line alone. It ignores the coupling capacitance between neighbor lines, which the Pitfalls come back to. The toggle counts and bounds of this page are exact and have no fitted parameter. Only the line capacitance , extracted from the layout or measured for an off-chip bus, turns them into power.
The algorithm
BUS-INVERT-ENCODE(D_1, ..., D_T)- 1
(b, x) ← (0, 0), or the state that the bus has now - 2
for t ← 1 to T do - 3
h ← |b ⊕ D_t| + x ▷ toggles if (D_t, 0) is sent - 4
if 2h > n then (b, x) ← (not D_t, 1) ▷ this toggles n + 1 − h lines - 5
else (b, x) ← (D_t, 0) - 6
send (b, x) BUS-INVERT-DECODE(b, x)- 7
return b ⊕ (x, x, ..., x)
Line L3 is the whole decision. It counts INV too: if INV is 1 now and the plain word is sent, INV falls, which is one more toggle. In hardware, line L3 is XOR gates and a majority voter over bits, and the choice of lines L4 and L5 is XOR gates driven by the voter. The decoder is XOR gates.
Example
Take , the start state , and the words below.
| (line L3) | sent | INV | toggles | toggles without the code | ||
|---|---|---|---|---|---|---|
| 1 | 11111110 | 7 | 00000001 | 1 | 2 | 7 |
| 2 | 11110000 | 5 + 1 = 6 | 00001111 | 1 | 3 | 3 |
| 3 | 00001111 | 0 + 1 = 1 | 00001111 | 0 | 1 | 8 |
| 4 | 00001110 | 1 + 0 = 1 | 00001110 | 0 | 1 | 1 |
At the plain word would toggle 5 data lines and drop INV, 6 toggles, so the encoder sends the complement, which toggles . At the data lines already show the word, and the only toggle is INV falling. The total is 7 toggles with the code and 19 without it, and the peak is 3 against 8. The decoder gets back each word: , and so on.
Correctness
Two facts carry the proofs, which are in the proof file.
Complement. From any state , the two candidates for a word toggle and lines. Lines L4 and L5 pick the smaller of the two, so each transfer costs .
Bound. So each transfer toggles at most lines. For even the bound is , and it is met when . For odd it is , and it is met when , where the two candidates tie. INV is counted in both cases. A rule that counts only the data lines, and inverts when , can toggle lines, which is more than for even : with INV at 1 and it keeps the plain word, and INV falls too.
Whole sequences. The greedy choice is also optimal over a whole stream, among all choices of the INV bits. The cost of transfer depends only on and on , so the total is a sum of terms that each depend on one free bit, and lines L4 and L5 choose each of these bits at its best value.
Decoding is right because line L7 undoes the complement of line L4 and leaves the word of line L5 as it is.
Proof: Bus-invert coding decodes right, toggles at most ceil(n/2) lines per transfer, and is optimal over its codewordsfull
Let . A bus state is a pair with and . Write for the all-ones word, for the complement, and for the number of ones of . The cost of a transfer from to is , the number of the lines that toggle. The candidates for a word are and .
Lemma 1 (decoding). Line L7 returns for both candidates of .
Proof. For it returns . For it returns .
Lemma 2 (complement). From a state , let . Then costs and costs .
Proof. The first cost is . For the second, has ones, and . The sum is .
Lemma 3 (each transfer is the cheaper candidate). The state sent at line L4 or L5 costs .
Proof. Line L4 sends when , that is when . By Lemma 2 it costs . Since is an integer, gives , so . Line L5 sends when , at cost , and then .
Theorem 1 (peak bound). Each transfer toggles at most of the lines, and some transfer from some state reaches this bound.
Proof. By Lemma 3 the cost is , and the least of two integers that sum to is at most . For even this is . For odd it is . In both cases it is .
The bound is reached. Take and a word with . Then and , so the cost is . For odd the two candidates tie at .
Remark (a rule that ignores INV). Suppose that the encoder inverts when , without the term . Take and . The rule sends , which costs . For even this is , so the bound of Theorem 1 fails. For odd it is , and in fact the two rules then differ only where the candidates tie: with they disagree only at , where both candidates cost , and with they agree.
Theorem 2 (optimal over whole sequences). Fix a start state and words . Among all sequences of choices , with state , the encoder of lines L1 to L6 gives the least total cost .
Proof. Put , so that has the same form as the other states. Let for . Then
So the cost of transfer depends only on . The map from to is a bijection of , since . So the least total is , reached by choosing each alone.
The encoder reaches this sum. At step the state is , and , where . The candidate has , and the candidate has , so the two candidates are the two values of , with the two costs above. By Lemma 3 the encoder takes the cheaper one, so its cost at step is for every .
Corollary (mean for random words). If the words are independent and uniform on , then the mean cost of each transfer is with binomial with parameters and .
Proof. Given the state , the word is uniform and independent of it, so is binomial with parameters and . By the proof of Theorem 2 the cost is with this .
For the sum is . The tests check Lemma 3 and Theorem 1 on every state and word for , Theorem 2 against all choices on random streams, and the corollary by exhaustive averaging for .
Complexity
Each word takes one XOR, one population count and one compare on bits, so time on a word RAM with -bit words and a constant-time population count, and for the stream. The state is one word of bits and one bit. The code is online: it needs only the current state and the next word. In hardware, the cost is the majority voter, whose depth grows as , plus one extra line and XOR gates on the two sides.
Properties
- Peak bound, proved. At most of the lines toggle per transfer, for every input, and the bound is tight.
- Mean for random data, computed exactly. If the words are independent and uniform, then is binomial with parameters and whatever the state is, and the mean cost is with . This gives 3.27 toggles for (4 without the code, 18.3% less), 6.83 for (8, 14.6% less), and 14.19 for (16, 11.3% less). Stan and Burleson report the same trend. The gain falls as grows, because the binomial gets narrow around .
- Optimal among its codewords. No other choice of INV bits for the same words toggles fewer lines in total.
- Online and deterministic. Each output depends only on the state and the next word.
- Not a gain for every stream. On data with few toggles, such as a counter on the address bus, the code saves little for its extra line. The carries of a counter often make , so it inverts often, but an 8-bit counter over 1024 transfers toggles 1.82 lines per transfer with the code against 1.99 without it, about 9% less.
Variants and improvements
- Partitioned bus. Split a wide bus into groups, each with its own INV line. For random data, a 32-bit bus with 4 groups of 8 has a mean of toggles, against 13.66 with 2 groups of 16, 14.19 with one INV line and 16 without a code. Each group costs one more line and one more voter.
- Codes for correlated data. Address buses carry mostly consecutive values. A Gray code on the address toggles one line per increment. Other address codes add a line that tells the receiver to increment its own copy, so that consecutive addresses toggle nothing.
- Limited-weight codes. Stan and Burleson also studied codes in which every codeword, or every transition, has a bounded Hamming weight. Bus-invert is the simplest such code with one extra line.
- Coupling-aware codes. On-chip wires in deep submicron processes have a coupling capacitance between neighbors that is larger than the capacitance to ground. A cost function that counts opposite toggles of neighbor lines gives other inversion rules, with the same structure of a choice between candidate codewords.
When to use it
Use it on wide buses with a large load per line and data close to random: off-chip data buses, long on-chip global buses and wide memory interfaces. It also bounds the simultaneous switching current, since at most drivers switch at once, which lowers ground bounce and the peak current that the power grid must deliver. Do not use it where the energy of the encoder, the voter and the XOR gates is close to the energy saved, as on short local buses, or on streams with few toggles, where a code for correlated data fits better.
Pitfalls
- Forgetting INV in the count. A rule that compares only the data lines with can toggle lines, as shown above. For even this breaks the bound and wastes one toggle. For odd the two rules differ only on ties, so the slip stays hidden until the bus width changes.
- Ties. For odd , makes the two candidates equal. Either choice is right, and by the argument for whole sequences it does not change the cost of later transfers either. A test that compares with the wrong half, such as , is not right: it misses transfers where the complement is cheaper.
- The encoder sits on the critical path. The voter and the XOR gates add delay before the drivers. Pipelines put the encoder in the stage before the bus.
- Counting toggles, not energy. The bound is on toggles. With coupling between neighbors, a transfer with few toggles can still cost much, for example when two neighbors toggle in opposite directions. Check the real cost model of the wires.
- Rising transitions. The power formula of the dynamic power page counts rising transitions. A toggle count is twice that over a long run, not in each transfer.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/bus-invert-coding/. The authors are listed in prov/7HE5BF.jsonl.
"""Bus-invert coding (Stan and Burleson 1995).
An n-bit bus gets one extra line, INV. Before each transfer the encoder counts
H, the number of the n + 1 lines that would toggle if the word D went out as it
is with INV = 0. If H > n / 2 it sends the complement of D with INV = 1, which
toggles n + 1 - H lines. The receiver XORs the bus with INV.
Words are Python ints in [0, 2^n).
"""
from __future__ import annotations
from typing import Iterable, List, Tuple
def popcount(v: int) -> int:
return bin(v).count("1")
def encode(words: Iterable[int], n: int, bus: int = 0, inv: int = 0) -> List[Tuple[int, int]]:
"""The sequence of (bus value, INV) for the words, from the state (bus, inv)."""
mask = (1 << n) - 1
out = [] # @line L1: the state (bus, inv) is (0, 0) unless the caller gives one
for d in words: # @line L2
h = popcount(bus ^ d) + inv # @line L3: toggles of (d, 0) against (bus, inv)
if 2 * h > n: # @line L4
bus, inv = d ^ mask, 1
else: # @line L5
bus, inv = d, 0
out.append((bus, inv)) # @line L6
return out
def decode(bus: int, inv: int, n: int) -> int:
"""The data word: the bus, inverted when INV = 1."""
return bus ^ (((1 << n) - 1) if inv else 0) # @line L7
def toggles(states: List[Tuple[int, int]], start: Tuple[int, int] = (0, 0)) -> List[int]:
"""Toggles on all n + 1 lines for each transfer."""
out, prev = [], start
for s in states:
out.append(popcount(prev[0] ^ s[0]) + (prev[1] ^ s[1]))
prev = s
return out
def encode_data_only(words: Iterable[int], n: int, bus: int = 0, inv: int = 0) -> List[Tuple[int, int]]:
"""A flawed variant that counts only the n data lines when it decides."""
mask = (1 << n) - 1
out = []
for d in words:
if 2 * popcount(bus ^ d) > n:
bus, inv = d ^ mask, 1
else:
bus, inv = d, 0
out.append((bus, inv))
return out
test_bus_invert_coding.py 89 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/bus-invert-coding/. The authors are listed in prov/7HE5BF.jsonl.
import itertools
import random
from fractions import Fraction
from math import comb
from bus_invert_coding import decode, encode, encode_data_only, popcount, toggles
def test_decode_inverts_encode():
rng = random.Random(1)
for n in range(1, 9):
words = [rng.randrange(1 << n) for _ in range(50)]
states = encode(words, n)
assert [decode(b, i, n) for b, i in states] == words
def test_each_transfer_takes_the_cheaper_codeword_exhaustive():
# every state (bus, inv) and every word, for n <= 6
for n in range(1, 7):
mask = (1 << n) - 1
for bus, inv, d in itertools.product(range(1 << n), (0, 1), range(1 << n)):
(s,) = encode([d], n, bus, inv)
cost = toggles([s], (bus, inv))[0]
plain = popcount(bus ^ d) + inv
inverted = popcount(bus ^ d ^ mask) + (1 - inv)
assert plain + inverted == n + 1
assert cost == min(plain, inverted)
assert cost <= (n + 1) // 2 # = ceil(n / 2)
def test_bound_is_reached():
for n in range(1, 9):
worst = max(
toggles(encode([d], n, bus, inv), (bus, inv))[0]
for bus in range(1 << n)
for inv in (0, 1)
for d in range(1 << n)
)
assert worst == (n + 1) // 2
def test_data_only_rule_can_toggle_one_more_line():
for n in range(2, 7):
worst = max(
toggles(encode_data_only([d], n, bus, inv), (bus, inv))[0]
for bus in range(1 << n)
for inv in (0, 1)
for d in range(1 << n)
)
assert worst == n // 2 + 1
def test_greedy_is_optimal_over_whole_sequences():
# Against all 2^T choices of the INV bits for the same word sequence.
rng = random.Random(2)
for _ in range(60):
n = rng.randint(1, 6)
t = rng.randint(1, 7)
mask = (1 << n) - 1
words = [rng.randrange(1 << n) for _ in range(t)]
best = min(
sum(toggles([(d ^ mask if x else d, x) for d, x in zip(words, xs)]))
for xs in itertools.product((0, 1), repeat=t)
)
assert sum(toggles(encode(words, n))) == best
def expected_toggles(n):
"""E[min(B, n + 1 - B)] with B binomial(n, 1/2), in fractions."""
return sum(Fraction(comb(n, b), 2**n) * min(b, n + 1 - b) for b in range(n + 1))
def test_expected_toggles_on_uniform_data_exhaustive():
# Average over all states and all words equals the binomial formula.
for n in range(1, 7):
tot = Fraction(0)
for bus, inv, d in itertools.product(range(1 << n), (0, 1), range(1 << n)):
tot += toggles(encode([d], n, bus, inv), (bus, inv))[0]
assert tot / (2 * 4**n) == expected_toggles(n)
def test_page_numbers():
assert expected_toggles(8) == Fraction(837, 256)
trace = encode([0b11111110, 0b11110000, 0b00001111, 0b00001110], 8)
assert trace == [(0b00000001, 1), (0b00001111, 1), (0b00001111, 0), (0b00001110, 0)]
assert toggles(trace) == [2, 3, 1, 1]
History
Stan and Burleson (1995) introduced the code for low-power input and output, at a time when off-chip buses took a large share of the power of a chip. They gave the peak bound, the mean for random data and the partitioned form, and they noted that the code also lowers the peak supply current. Later work on bus coding took two directions: codes for the correlated streams of address buses, and codes that count the coupling between on-chip wires.
References
- Mircea R. Stan and Wayne P. Burleson, Bus-invert coding for low-power I/O, IEEE Transactions on VLSI Systems 3(1), 49 to 58, March 1995, the code with one invert line, the decision by the Hamming distance against the bus value, the peak bound, the mean transitions for random data, and the partitioned bus
- Anantha P. Chandrakasan, Samuel Sheng and Robert W. Brodersen, Low-power CMOS digital design, IEEE Journal of Solid-State Circuits 27(4), 473 to 484, April 1992, the switching component of the power as activity times capacitance times the square of the supply times the clock frequency, the cost that the code lowers
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 7HE5BF Edit this page History Report an error Markdown