t27.aiРусский

What arithmetic costs

You will learn

Why a multiplier is expensive and an adder is cheap, measured on a real chip.

In logic, every operation has a price paid in chip resources. An adder is a carry chain, cheap and fast. A multiplier built from LUTs is large; the chip has DSP blocks made for multiplying, and a synthesizer uses them when it may. These curves are real yosys counts from 4 to 64 bits. Switch DSPs off and on, and see why hardware people care so much about the width of every number.

Try it

Pick Multiply, switch DSP48E1 to -nodsp, and compare the LUTs at 32 bits with and without DSPs.

Open the interactive lesson →

Cost curves: adders, multipliers and MACs on a Xilinx 7-series
Cost curves: adders, multipliers and MACs on a Xilinx 7-series ↗

Real yosys synth_xilinx counts for add, multiply, MAC and compare from 4 to 64 bits, with and without DSPs, against a whole XC7A200T, and t27 ternary specs beside them.

specs/tutorial/05_widths_and_casts.t27

// SPDX-License-Identifier: Apache-2.0
; 05 — Widths and casts, and the one that catches everybody
; Lesson 5. Converting between widths, and the single sharpest edge in the
; language today: an integer literal types as i32, so arithmetic on a narrower
; mutable local fails the type check unless you say what you mean.
; .
; Everything below is measured against the compiler in this repository, not
; guessed. The failing forms are written out as comments rather than as code,
; because a spec that does not compile teaches nothing.
; phi^2 + 1/phi^2 = 3 | TRINITY

module tutorial-05-widths;

// ============================================================================
// Casting
//
//   @as(TYPE, value)          assert a type
//   @intCast(value)           change integer width
//
// They are usually written together: @as names the destination, @intCast does
// the narrowing.
// ============================================================================

pub fn narrow(wide: u16) u8 {
    return @as(u8, @intCast(wide));
}

pub fn widen(small: u8) u16 {
    return @as(u16, small);
}

// ============================================================================
// The rule
//
// An integer literal is typed i32. So `x + 1` where x is an i8 is an i32
// expression, and assigning it back to an i8 is a type error.
//
// This does NOT compile -- do not copy it:
//
//     var x : i8 = a;
//     x = x + 1;              // type mismatch: cannot assign I32 to I8
//
// Nor does the compound form:
//
//     var x : i8 = a;
//     x += 1;                 // same mismatch
//
// It affects every width narrower than i32, signed or unsigned. At i32 and
// wider there is nothing to convert and the plain form is fine -- which is why
// lesson 04's loops all count in i32.
// ============================================================================

// ── Fix 1: say what you mean with a cast ────────────────────────────────────
pub fn increment_narrow(a: i8) i8 {
    var x : i8 = a;
    x = @as(i8, @intCast(x + 1));
    return x;
}

// ── Fix 2: don't mutate; bind a new value ───────────────────────────────────
// Usually the better answer. An immutable local sidesteps the assignment
// entirely, and the arithmetic still happens at i32 where it is safe.
pub fn increment_by_binding(a: i8) i8 {
    const stepped = a + 1;
    return @as(i8, @intCast(stepped));
}

// ── Fix 3: do the arithmetic wide, narrow once at the end ───────────────────
// The habit to build for anything with more than one step: stay at i32 while
// computing, convert once when you are done.
pub fn average_of_three(a: i8, b: i8, c: i8) i8 {
    const total : i32 = a + b + c;
    return @as(i8, @intCast(total / 3));
}

// ============================================================================
// The loop exception
//
// A `while` continue-expression accepts `i += 1` on a narrow counter, where
// the same statement in the body would not:
//
//     while (i < limit) : (i += 1) { ... }     // u8 counter: fine
//
// Worth knowing, but not worth relying on: the continue-expression is stored
// as text rather than as a node in the tree, so the type checker never sees
// it. It is not that this form is checked and passes -- it is that it is not
// checked at all. Prefer an i32 counter when the width is yours to choose.
// ============================================================================

pub fn sum_narrow_counter(limit: u8) u8 {
    var total : u8 = 0;
    var i : u8 = 0;
    while (i < limit) : (i += 1) {
        total = total + i;
    }
    return total;
}

// ============================================================================
// Tests
// ============================================================================

test "the three fixes agree with each other" {
    try std.testing.expectEqual(@as(i8, 4), increment_narrow(3));
    try std.testing.expectEqual(@as(i8, 4), increment_by_binding(3));
}

test "wide arithmetic then one narrowing" {
    try std.testing.expectEqual(@as(i8, 2), average_of_three(1, 2, 3));
}

test "narrowing keeps the low bits" {
    try std.testing.expectEqual(@as(u8, 0x34), narrow(0x1234));
    try std.testing.expectEqual(@as(u16, 7), widen(7));
}

Open the lesson's spec in the player ↗

All lessons