Code patternsAdvanced: wrapping cells of a known widthSigned multiplication by shift-and-add

Signed multiplication by shift-and-add

Multiplies two numbers from the tape, negative ones too, at a cost that does not depend on the numbers.

Watch it run

The form “acc gets a × b added” works on 11 cells; the named ones are “a”, “b” and “acc”. Press Play, drag the step slider forwards and back, or change a and b with the sliders:

The figure needs JavaScript. The program below is the result for the example values.

This is long multiplication in base 2. Read a bit by bit with the bit peel. For every set bit j, add b × 2ʲ to the answer.

b × 2ʲ is the pattern multiply by a constant, a flat loop: one step, and b is kept with a second step.

The top bit of a signed number counts negative, so for that one bit the same loop subtracts. Nothing else is needed for negative numbers.

So the cost is about 25 steps for the eight tests and 5 more for every set bit of a: 28 to 60 in all, whether b is 7 or 7 million.

The code

This is the program the figure ran, written by the generators for these values and this cell size. It is brainfuck as it stands: copy it, or open it in the playground, where you can change it:

[->>>>>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>>>>+<<<<<<<<<<]>>>>>>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[-<<<<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>>>>]>>>[-<<<<<<<<<<+>>>>>>>>>>]<<<<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]<<<<[->+>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<<]>>[[-]>>>[-<<<<->>>>>+<]>[-<+>]<<<<<<<<[->+>>>>>>>>+<<<<<<<<<]>>>>>>>>>[-<<<<<<<<<+>>>>>>>>>]<<<<<]<[-<+>>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<]>[[-]>>>[-<<<<<-->>>>>>+<]>[-<+>]<<<<<<<<[->++>>>>>>>>+<<<<<<<<<]>>>>>>>>>[-<<<<<<<<<+>>>>>>>>>]<<<<<]<<[->+>++++++++++++++++++++++++++++++++<<]>>[[-]>>>[-<<<<---->>>>>+<]>[-<+>]<<<<<<<<[->++++>>>>>>>>+<<<<<<<<<]>>>>>>>>>[-<<<<<<<<<+>>>>>>>>>]<<<<<]<[-<+>>++++++++++++++++<]>[[-]>>>[-<<<<<-------->>>>>>+<]>[-<+>]<<<<<<<<[->++++++++>>>>>>>>+<<<<<<<<<]>>>>>>>>>[-<<<<<<<<<+>>>>>>>>>]<<<<<]<<[->+>++++++++<<]>>[[-]>>>[-<<<<---------------->>>>>+<]>[-<+>]<<<<<<<<[->++++++++++++++++>>>>>>>>+<<<<<<<<<]>>>>>>>>>[-<<<<<<<<<+>>>>>>>>>]<<<<<]<[-<+>>++++<]>[[-]>>>[-<<<<<-------------------------------->>>>>>+<]>[-<+>]<<<<<<<<[->++++++++++++++++++++++++++++++++>>>>>>>>+<<<<<<<<<]>>>>>>>>>[-<<<<<<<<<+>>>>>>>>>]<<<<<]<<[->+>++<<]>>[[-]>>>[-<<<<---------------------------------------------------------------->>>>>+<]>[-<+>]<<<<<<<<[->++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>>>>>>>>+<<<<<<<<<]>>>>>>>>>[-<<<<<<<<<+>>>>>>>>>]<<<<<]<[->+<]>[[-]<<<<[->-------------------------------------------------------------------------------------------------------------------------------->>>>>>>>+<<<<<<<<<]>>>>>>>>>[-<<<<<<<<<+>>>>>>>>>]<<<<<]>>>[-]
Open in the playground3,093 characters

What it needs

Common mistakes
  • Peeling the wider of the two numbers. The cost follows the bits of a: peel the narrow one.
  • An a outside −128…127: only its low 8 bits are used.

What it costs

A plain step is one command. An optimised step is one step of an interpreter that folds runs of the same command and flat loops. Choose the values and watch the two bars; What costs what explains how cost is counted:

The figure needs JavaScript. The table below has the measured steps.

Cost as a formula, and measured
Table 1. The cost as a function of the values.
FormPlain stepsOptimised steps
acc gets a × b addedtens of thousands and moreabout 25 + 5 per set bit of a
The naive way: repeated additiongrows with a × b, each read as a count2 + 5 × a, with a read as a count
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
acc gets a × b addeda = 6; b = 7323,093more than 30,000,00036
acc gets a × b addeda = 100; b = −60323,093more than 30,000,00042
acc gets a × b addeda = −25; b = −60323,093more than 30,000,00058
acc gets a × b addeda = −128; b = −128323,093more than 30,000,00028
The naive way: repeated additiona = 6; b = 785095932
The naive way: repeated additiona = 100; b = −60850375,005502
The naive way: repeated additiona = −25; b = −60850866,2551,157

Against the naive way

The same product by repeated addition, the loop of multiply two cells. A negative a is a huge count there: −3 on 16-bit cells is 65,533 rounds.

The numbers, row by row
this pattern: a = −25; b = −60
54the naive way: a = −25; b = −60
327,557

This pattern against the naive way, on 16-bit cells, measured when this page was written. The bars show the last row: optimised steps.

ValuesOptimised stepsPlain stepsCharacters
patternnaivepatternnaivepatternnaive
a = 6; b = 73232502,0239592,04150
a = 100; b = −603850222,688,925more than 30,000,0002,04150
a = −25; b = −6054327,557more than 30,000,000more than 30,000,0002,04150

Measured in the project on 16-bit cells with both numbers between −127 and 127: about 43 optimised steps per product, against about 195,000 by repeated addition. measured in the project

Other forms

to measure against The naive way: repeated addition

The loop of multiply two cells. Correct for negative numbers on wrapping cells, and very slow for them.

[->>>+<<[->+>>+<<<]>>>[-<<<+>>>]<<<<]>>>[-<<<+>>>]
Open in the playground50 characters
Run this form step by step

The form “The naive way: repeated addition” works on 5 cells; the named ones are “a”, “b”, “acc” and “scratch”. Press Play, drag the step slider forwards and back, or change a and b with the sliders:

The figure needs JavaScript. The program below is the result for the example values.

Where it is used

Sources

Every program on this page was run on many inputs, in plain and in optimised mode, and compared with JavaScript. The explanations are written for this page.