Code patternsAdvanced: wrapping cells of a known widthShift, round and clamp

Shift, round and clamp

Divides a signed number by a power of two, rounds it, and keeps the result between −128 and 127.

Watch it run

The form “Divide by 2ᵏ, round, clamp to −128…127” works on 12 cells; the named ones are “X” and “result”. Press Play, drag the step slider forwards and back, or change shift k and X with the sliders:

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

Dividing by 2ᵏ means: throw away the k lowest bits. The bit peel reads the bits one by one, so it can simply not write the low ones.

Rounding is free: add half of 2ᵏ before the division. Negative numbers are free too: add a fixed offset first so that everything is positive, and take it off the result.

The limits are checked without comparing. A second copy of the number has every peeled bit taken off again. If that copy is not zero at the end, the number was outside the range, and only then are the high bits looked at to see on which side.

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 playground11,212 characters

What it needs

Common mistakes
  • Letting the sum wrap before it is clamped. A clamp cannot repair a number that has already gone round.
  • Using this for a divisor that is not a power of two. For that the project multiplies by a reciprocal from a table first (see table lookup as a decision tree).

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
Divide by 2ᵏ, round, clamp to −128…127tens of thousands and more75 to 100 inside the range, about 160 when the result is clamped
The naive way: divide by countinggrows with X × 2ᵏabout 11 per unit of X
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
Divide by 2ᵏ, round, clamp to −128…127shift k = 7; X = 11,8683211,212more than 30,000,00094
Divide by 2ᵏ, round, clamp to −128…127shift k = 7; X = −3,0003211,212more than 30,000,00076
Divide by 2ᵏ, round, clamp to −128…127shift k = 7; X = 99,9993211,212more than 30,000,000156
Divide by 2ᵏ, round, clamp to −128…127shift k = 4; X = 1,0003211,691more than 30,000,00075
The naive way: divide by countingshift k = 7; X = 11,8683217213,316,525131,538
The naive way: divide by countingshift k = 7; X = 30032172424,0534,017
The naive way: divide by countingshift k = 4; X = 1,00032113163,23411,155

Against the naive way

The same division by counting: take 2ᵏ units off, add 1 to the result, again and again. That baseline does not even clamp, and it only takes numbers that are not negative.

The numbers, row by row
this pattern: shift k = 4; X = 1,000
75the naive way: shift k = 4; X = 1,000
11,155

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

ValuesOptimised stepsPlain stepsCharacters
patternnaivepatternnaivepatternnaive
shift k = 7; X = 11,86894131,538more than 30,000,00013,316,52511,212172
shift k = 7; X = 300794,017more than 30,000,000424,05311,212172
shift k = 4; X = 1,0007511,155more than 30,000,000163,23411,691113

Measured in the project on the sums of a real layer (64 inputs, shift 10, 32-bit cells): about 100 optimised steps per result with the peel, against 1.2 to 1.5 million by counting. measured in the project

Other forms

to measure against The naive way: divide by counting

Rounds, but does not clamp, and X must not be negative.

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

The form “The naive way: divide by counting” works on 6 cells; the named ones are “X”, “result” and “counter”. Press Play, drag the step slider forwards and back, or change shift k and X 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.