Algorithm Search⌘K

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
Properties
deterministicexactonlineiterative
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 nn-bit bus. Before each transfer the encoder counts how many of the n+1n + 1 lines would toggle if the word went out as it is with INV = 0. If that count hh is more than n/2n/2, it sends the complement of the word with INV = 1, which toggles n+1−hn + 1 - h lines. The receiver complements the bus again when INV = 1. So at most ⌈n/2⌉\lceil n/2 \rceil of the n+1n + 1 lines toggle per transfer, against nn without the code, and for random data the mean also falls, from 44 to 837/256≈3.27837/256 \approx 3.27 toggles for n=8n = 8. 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 nn data lines and one line INV. Its state is a pair (b,x)(b, x) with b∈{0,1}nb \in \{0,1\}^n the values on the data lines and x∈{0,1}x \in \{0, 1\} the value on INV. A stream of words D1,…,DT∈{0,1}nD_1, \ldots, D_T \in \{0,1\}^n must go over the bus, one word per transfer, so that the receiver recovers each DtD_t from the state alone. The cost of a transfer from (b,x)(b, x) to (b′,x′)(b', x') is the number of lines that toggle, the Hamming distance

c((b,x),(b′,x′))=∣b⊕b′∣+(x⊕x′),c\big((b, x), (b', x')\big) = |b \oplus b'| + (x \oplus x'),

where ∣⋅∣|\cdot| is the Hamming weight. Each state that bus-invert coding may send for DtD_t is either (Dt,0)(D_t, 0) or (Dt‾,1)(\overline{D_t}, 1), with D‾\overline{D} 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, P=αCV2fP = \alpha C V^2 f with α\alpha 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 α\alpha is half the number of toggles per cycle. With equal capacitance CC per line, the bus power is 12⋅(mean toggles per transfer)⋅CV2f\frac{1}{2} \cdot (\text{mean toggles per transfer}) \cdot C V^2 f. 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 CC, extracted from the layout or measured for an off-chip bus, turns them into power.

The algorithm

  1. BUS-INVERT-ENCODE(D_1, ..., D_T)
  2. 1(b, x) ← (0, 0), or the state that the bus has now
  3. 2for t ← 1 to T do
  4. 3h ← |b ⊕ D_t| + x ▷ toggles if (D_t, 0) is sent
  5. 4if 2h > n then (b, x) ← (not D_t, 1) ▷ this toggles n + 1 − h lines
  6. 5else (b, x) ← (D_t, 0)
  7. 6send (b, x)
  8. BUS-INVERT-DECODE(b, x)
  9. 7return 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 n+1n + 1 XOR gates and a majority voter over n+1n + 1 bits, and the choice of lines L4 and L5 is nn XOR gates driven by the voter. The decoder is nn XOR gates.

Example

Take n=8n = 8, the start state (00000000,0)(00000000, 0), and the words below.

tt DtD_t hh (line L3) sent bb 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 t=2t = 2 the plain word would toggle 5 data lines and drop INV, 6 toggles, so the encoder sends the complement, which toggles 9−6=39 - 6 = 3. At t=3t = 3 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: 00000001⊕11111111=1111111000000001 \oplus 11111111 = 11111110, and so on.

Correctness

Two facts carry the proofs, which are in the proof file.

Complement. From any state (b,x)(b, x), the two candidates for a word DD toggle h=∣b⊕D∣+xh = |b \oplus D| + x and ∣b⊕D‾∣+(1−x)=(n−∣b⊕D∣)+1−x=n+1−h|b \oplus \overline{D}| + (1 - x) = (n - |b \oplus D|) + 1 - x = n + 1 - h lines. Lines L4 and L5 pick the smaller of the two, so each transfer costs min⁡(h,n+1−h)\min(h, n + 1 - h).

Bound. So each transfer toggles at most ⌊(n+1)/2⌋=⌈n/2⌉\lfloor (n+1)/2 \rfloor = \lceil n/2 \rceil lines. For even nn the bound is n/2n/2, and it is met when h=n/2h = n/2. For odd nn it is (n+1)/2(n+1)/2, and it is met when h=(n+1)/2h = (n+1)/2, where the two candidates tie. INV is counted in both cases. A rule that counts only the data lines, and inverts when ∣b⊕D∣>n/2|b \oplus D| > n/2, can toggle ⌊n/2⌋+1\lfloor n/2 \rfloor + 1 lines, which is more than n/2n/2 for even nn: with INV at 1 and ∣b⊕D∣=n/2|b \oplus D| = n/2 it keeps the plain word, and INV falls too.

Whole sequences. The greedy choice is also optimal over a whole stream, among all 2T2^T choices of the INV bits. The cost of transfer tt depends only on Dt−1⊕DtD_{t-1} \oplus D_t and on xt−1⊕xtx_{t-1} \oplus x_t, 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 n≥1n \ge 1. A bus state is a pair (b,x)(b, x) with b∈{0,1}nb \in \{0,1\}^n and x∈{0,1}x \in \{0,1\}. Write 1\mathbf{1} for the all-ones word, D‾=D⊕1\overline{D} = D \oplus \mathbf{1} for the complement, and ∣v∣|v| for the number of ones of vv. The cost of a transfer from (b,x)(b, x) to (b′,x′)(b', x') is c=∣b⊕b′∣+(x⊕x′)c = |b \oplus b'| + (x \oplus x'), the number of the n+1n + 1 lines that toggle. The candidates for a word DD are (D,0)(D, 0) and (D‾,1)(\overline{D}, 1).

Lemma 1 (decoding). Line L7 returns DD for both candidates of DD.

Proof. For (D,0)(D, 0) it returns D⊕0n=DD \oplus 0^n = D. For (D‾,1)(\overline{D}, 1) it returns D⊕1⊕1=DD \oplus \mathbf{1} \oplus \mathbf{1} = D. □\square

Lemma 2 (complement). From a state (b,x)(b, x), let h=∣b⊕D∣+xh = |b \oplus D| + x. Then (D,0)(D, 0) costs hh and (D‾,1)(\overline{D}, 1) costs n+1−hn + 1 - h.

Proof. The first cost is ∣b⊕D∣+(x⊕0)=h|b \oplus D| + (x \oplus 0) = h. For the second, b⊕D‾=b⊕D‾b \oplus \overline{D} = \overline{b \oplus D} has n−∣b⊕D∣n - |b \oplus D| ones, and x⊕1=1−xx \oplus 1 = 1 - x. The sum is n−∣b⊕D∣+1−x=n+1−hn - |b \oplus D| + 1 - x = n + 1 - h. □\square

Lemma 3 (each transfer is the cheaper candidate). The state sent at line L4 or L5 costs min⁡(h,n+1−h)\min(h, n + 1 - h).

Proof. Line L4 sends (D‾,1)(\overline{D}, 1) when 2h>n2h > n, that is when h>n/2h > n/2. By Lemma 2 it costs n+1−hn + 1 - h. Since hh is an integer, h>n/2h > n/2 gives h≥(n+1)/2h \ge (n+1)/2, so n+1−h≤hn + 1 - h \le h. Line L5 sends (D,0)(D, 0) when h≤n/2h \le n/2, at cost hh, and then h<n+1−hh < n + 1 - h. □\square

Theorem 1 (peak bound). Each transfer toggles at most ⌈n/2⌉\lceil n/2 \rceil of the n+1n + 1 lines, and some transfer from some state reaches this bound.

Proof. By Lemma 3 the cost is min⁡(h,n+1−h)\min(h, n + 1 - h), and the least of two integers that sum to n+1n + 1 is at most ⌊(n+1)/2⌋\lfloor (n+1)/2 \rfloor. For even n=2kn = 2k this is k=n/2k = n/2. For odd n=2k+1n = 2k + 1 it is k+1=(n+1)/2k + 1 = (n+1)/2. In both cases it is ⌈n/2⌉\lceil n/2 \rceil.

The bound is reached. Take x=0x = 0 and a word DD with ∣b⊕D∣=⌈n/2⌉|b \oplus D| = \lceil n/2 \rceil. Then h=⌈n/2⌉h = \lceil n/2 \rceil and n+1−h=⌊n/2⌋+1≥hn + 1 - h = \lfloor n/2 \rfloor + 1 \ge h, so the cost is h=⌈n/2⌉h = \lceil n/2 \rceil. For odd nn the two candidates tie at (n+1)/2(n+1)/2. □\square

Remark (a rule that ignores INV). Suppose that the encoder inverts when ∣b⊕D∣>n/2|b \oplus D| > n/2, without the term xx. Take x=1x = 1 and ∣b⊕D∣=⌊n/2⌋|b \oplus D| = \lfloor n/2 \rfloor. The rule sends (D,0)(D, 0), which costs ⌊n/2⌋+1\lfloor n/2 \rfloor + 1. For even nn this is n/2+1>⌈n/2⌉n/2 + 1 > \lceil n/2 \rceil, so the bound of Theorem 1 fails. For odd nn it is (n+1)/2=⌈n/2⌉(n+1)/2 = \lceil n/2 \rceil, and in fact the two rules then differ only where the candidates tie: with x=1x = 1 they disagree only at ∣b⊕D∣=(n−1)/2|b \oplus D| = (n-1)/2, where both candidates cost (n+1)/2(n+1)/2, and with x=0x = 0 they agree.

Theorem 2 (optimal over whole sequences). Fix a start state (b0,x0)(b_0, x_0) and words D1,…,DTD_1, \ldots, D_T. Among all 2T2^T sequences of choices x1,…,xT∈{0,1}x_1, \ldots, x_T \in \{0, 1\}, with state st=(Dt⊕xt1,xt)s_t = (D_t \oplus x_t \mathbf{1}, x_t), the encoder of lines L1 to L6 gives the least total cost ∑tc(st−1,st)\sum_t c(s_{t-1}, s_t).

Proof. Put D0=b0⊕x01D_0 = b_0 \oplus x_0 \mathbf{1}, so that s0=(D0⊕x01,x0)s_0 = (D_0 \oplus x_0 \mathbf{1}, x_0) has the same form as the other states. Let yt=xt−1⊕xty_t = x_{t-1} \oplus x_t for t=1,…,Tt = 1, \ldots, T. Then

c(st−1,st)=∣Dt−1⊕Dt⊕yt1∣+yt={∣Dt−1⊕Dt∣if yt=0,n+1−∣Dt−1⊕Dt∣if yt=1.c(s_{t-1}, s_t) = |D_{t-1} \oplus D_t \oplus y_t \mathbf{1}| + y_t = \begin{cases} |D_{t-1} \oplus D_t| & \text{if } y_t = 0, \\ n + 1 - |D_{t-1} \oplus D_t| & \text{if } y_t = 1. \end{cases}

So the cost of transfer tt depends only on yty_t. The map from (x1,…,xT)(x_1, \ldots, x_T) to (y1,…,yT)(y_1, \ldots, y_T) is a bijection of {0,1}T\{0,1\}^T, since xt=x0⊕y1⊕⋯⊕ytx_t = x_0 \oplus y_1 \oplus \cdots \oplus y_t. So the least total is ∑tmin⁡(∣Dt−1⊕Dt∣,n+1−∣Dt−1⊕Dt∣)\sum_t \min(|D_{t-1} \oplus D_t|, n + 1 - |D_{t-1} \oplus D_t|), reached by choosing each yty_t alone.

The encoder reaches this sum. At step tt the state is st−1s_{t-1}, and h=∣bt−1⊕Dt∣+xt−1h = |b_{t-1} \oplus D_t| + x_{t-1}, where bt−1=Dt−1⊕xt−11b_{t-1} = D_{t-1} \oplus x_{t-1} \mathbf{1}. The candidate (Dt,0)(D_t, 0) has yt=xt−1y_t = x_{t-1}, and the candidate (Dt‾,1)(\overline{D_t}, 1) has yt=1−xt−1y_t = 1 - x_{t-1}, so the two candidates are the two values of yty_t, with the two costs above. By Lemma 3 the encoder takes the cheaper one, so its cost at step tt is min⁡(∣Dt−1⊕Dt∣,n+1−∣Dt−1⊕Dt∣)\min(|D_{t-1} \oplus D_t|, n + 1 - |D_{t-1} \oplus D_t|) for every tt. □\square

Corollary (mean for random words). If the words are independent and uniform on {0,1}n\{0,1\}^n, then the mean cost of each transfer is E[min⁡(B,n+1−B)]E[\min(B, n + 1 - B)] with BB binomial with parameters nn and 1/21/2.

Proof. Given the state st−1s_{t-1}, the word DtD_t is uniform and independent of it, so ∣Dt−1⊕Dt∣|D_{t-1} \oplus D_t| is binomial with parameters nn and 1/21/2. By the proof of Theorem 2 the cost is min⁡(B,n+1−B)\min(B, n + 1 - B) with this BB. □\square

For n=8n = 8 the sum ∑b=08(8b)2−8min⁡(b,9−b)\sum_{b=0}^{8} \binom{8}{b} 2^{-8} \min(b, 9 - b) is 837/256≈3.27837/256 \approx 3.27. The tests check Lemma 3 and Theorem 1 on every state and word for n≤6n \le 6, Theorem 2 against all 2T2^T choices on random streams, and the corollary by exhaustive averaging for n≤6n \le 6.

Complexity

Each word takes one XOR, one population count and one compare on nn bits, so O(⌈n/w⌉)O(\lceil n/w \rceil) time on a word RAM with ww-bit words and a constant-time population count, and O(T⌈n/w⌉)O(T \lceil n/w \rceil) for the stream. The state is one word of nn 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 O(log⁡n)O(\log n), plus one extra line and 3n+13n + 1 XOR gates on the two sides.

Properties

  • Peak bound, proved. At most ⌈n/2⌉\lceil n/2 \rceil of the n+1n + 1 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 ∣b⊕D∣|b \oplus D| is binomial with parameters nn and 1/21/2 whatever the state is, and the mean cost is E[min⁡(B,n+1−B)]E[\min(B, n + 1 - B)] with B∼Bin(n,1/2)B \sim \mathrm{Bin}(n, 1/2). This gives 3.27 toggles for n=8n = 8 (4 without the code, 18.3% less), 6.83 for n=16n = 16 (8, 14.6% less), and 14.19 for n=32n = 32 (16, 11.3% less). Stan and Burleson report the same trend. The gain falls as nn grows, because the binomial gets narrow around n/2n/2.
  • 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 h>n/2h > n/2, 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 kk groups, each with its own INV line. For random data, a 32-bit bus with 4 groups of 8 has a mean of 4⋅837/256≈13.084 \cdot 837/256 \approx 13.08 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 ⌈n/2⌉\lceil n/2 \rceil 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 nn data lines with n/2n/2 can toggle ⌊n/2⌋+1\lfloor n/2 \rfloor + 1 lines, as shown above. For even nn this breaks the bound ⌈n/2⌉\lceil n/2 \rceil and wastes one toggle. For odd nn the two rules differ only on ties, so the slip stays hidden until the bus width changes.
  • Ties. For odd nn, h=(n+1)/2h = (n+1)/2 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 hh with the wrong half, such as h>n/2+1h > n/2 + 1, 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.

bus_invert_coding.pyI0RawGitHub
# 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

  1. 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
  2. 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.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • at least three sources
  • a scene preset for the example