Email or username:

Password:

Forgot your password?
Ken Shirriff

The Intel 386 processor (1985) is a complicated chip. One of its features is a "barrel shifter" (red) that can shift binary values by 0 to 32 bits in one step, much faster than shifting one bit at a time. Let's see how it works. 1/11

14 comments
Ken Shirriff

Shifting is useful in programming, sliding bits to the left or right with the "<<" or ">>" operators. But it takes a lot of hardware to do this efficiently. Older x86 processors could only shift one position at a time, looping to shift multiple positions. The 386 fixed this. 2/11

Ken Shirriff

A barrel shifter uses a matrix of switches to select where each input bit ends up, as this Wikipedia diagram shows. Each diagonal control line selects a particular shift amount. But for a 32-bit processor, the matrix becomes very large. 3/11

Ken Shirriff

The 386 uses a hybrid approach: a smaller matrix shifts, but only by a multiple of 4 bits. A second circuit applies a shift of 0 to 3 bits. In combination, the circuits shift by an arbitrary amount. This approach saves circuitry. 4/11

Ken Shirriff

Here's how the 386's barrel shifter looks on the die. Two values enter at the bottom from the registers and are latched. The shift matrix is in the middle with diagonal control lines selecting the shift. Above, a circuit shifts by 0 to 3 positions, producing the output. 5/11

Ken Shirriff

At each position in the matrix, a transistor is the switch between an input line and an output line. This photo shows eight of the tiny transistors. 6/11

Ken Shirriff

This photo shows part of the shift matrix with the sloping polysilicon control lines (green) and transistors (dark boxes). The diagram shows how three of the control lines are wired. 7/11

Ken Shirriff

This circuit shifts from 0 to 3 bits. It passes the input bit straight through or sends it two positions to the right. Next, it passes that bit straight through or passes it one position to the right. Thus each bit is shifted right 0 to 3 positions. 8/11

Ken Shirriff

At the input to the shifter, latch circuits hold the two input words from the register file. The circuit can also swap the two values. The photos show the circuit on the die. Left shows the metal layers; right shows the underlying silicon with the transistors. 9/11

Ken Shirriff

There are complications, of course. The matrix outputs 37 bits, not 32, for reasons. Supporting bytes and 16-bit shifts needs more circuitry as does rotating through the carry. For more on the 386's barrel shifter, see my blog post: righto.com/2023/12/386-barrel- 10/11

Joshua Barretto

@kenshirriff I love this. Such a simple solution, but somehow still very pleasing.

John Gordon ⚡️

@kenshirriff A barrel shifter was the first thing we had to design in our silicon design class back in the early 1990s.

While doing that, my own home computer was running a very much simpler chip (an early ARM). You can see the barrel shifter and ALU in the ARM1 in the link below. Mine was running an ARM 2 initially (and latterly was upgraded to an ARM 3), but very similar to this design.

hackaday.com/2015/12/21/revers

James Trickle uP

@kenshirriff The first microprocessor I worked with that had a barrel shifter was at my first job at #Tektronix - port the #Magnolia #workstation (#Motorola 68000) V7 #UNIX first to 68010 but a few years later to 68EC040. The '40 had the barrel shifter. Man rotations were zippy. Later for #WindRiver I ported #VxWorks RTOS to #MIPS and #PowerPC chips. PPC bitfield ops were efficiently done with a bunch of rotate with mask instructions - IMO the pinnacle, the kitchen sink of embedded instructions.

Go Up