Shifting Vectors

A shift on NEON is very similar to shifts you may have used in scalar ARM code. The shift moves the bits in each element of a vector left or right. Bits that fall of the left or right of each element are discarded; they are not shifted to adjacent elements.

The amount to shift can be specified with a literal encoded in the instruction, or with an additional shift vector. When using a shift vector, the shift applied to each element of the input vector depends on the value of the corresponding element in the shift vector. The elements in the shift vector are treated as signed values, so left, right and zero shifts are possible, on a per-element basis.

A right shift operating on a vector of signed elements, indicated by the type attached to the instruction, will sign extend each element. This is the equivalent of an arithmetic shift you may have used in ARM code. Shifts applied to unsigned vectors do not sign extend.

Shifting and Inserting

NEON also supports shifts with insertion, providing a way to combine bits from two vectors. For example, shift left and insert (VSLI) shifts each element of the source vector left. The new bits inserted at the right of each element are the corresponding bits from the destination vector.

Shifting and Accumulation

Finally, NEON supports shifting the elements of a vector right, and accumulating the results into another vector. This is useful for situations in which interim calculations are made at a high precision, before the result is combined with a lower precision accumulator.

Instruction Modifiers

Each shift instruction can take one or more modifiers. These modifiers do not change the shift operation itself, but the inputs or outputs are adjusted to remove bias or saturate to a range.

There are five shift modifiers:

Rounding, denoted by an R prefix, corrects for the bias caused by truncation when shifting right.

Narrow, denoted by an N suffix, causes the number of bits in each element of the result to be halved. It implies Q (128-bit) source and D (64-bit) destination registers.

Long, denoted by an L suffix, causes the number of bits in each elements of the result to be doubled. It implies D source and Q destination registers.

Saturating, denoted by a Q prefix, sets each result element to the minimum or maximum of the representable range, if the result exceeds that range. The number of bits and sign type of the vector are usedto determine the saturation range.

Unsigned Saturating, denoted by a Q prefix and U suffix, is similar to the saturation modifier, but the result is saturated to an unsigned range when given signed or unsigned inputs.

Some combinations of these modifiers do not describe useful operations, and so the instruction is not provided by NEON. For example, a saturating shift right (which would be called VQSHR) is unnecessary, as right shifting makes results smaller, and so the value cannot exceed the available range.

Table of Shifts Available

All of the shifting instructions provided by NEON are shown in the table below. They are arranged according to the modifiers mentioned earlier. If you are still unsure about what the modifier letters mean, use the table to select the instruction you need.

An Example: Converting Color Depth

Converting between color depths is a frequent operation required in graphics processing. Often, input or output data is in an RGB565 16-bit color format, but working with the data is much easier in RGB888 format. This is particularly true on NEON, as there is no native support for data types like RGB565.

However, NEON can still handle RGB565 data efficiently, and the vector shifts introduced above provide a method to do it.

The effects of each instruction are described in the comments above, but in summary, the operation performed on each channel is:

Remove color data for adjacent channels using shifts to push the bits off either end of the element.

Use a second shift to position the color data in the most-significant bits of each element, and narrow to reduce element size from 16 to eight bits.

Note the use of element sizes in this sequence to address 8 and 16 bit elements, in order to achieve some of the masking operations.

A small problem

You may notice that, if you use the code above to convert to RGB888 format, your whites aren't quite white. This is because, for each channel, the lowest two or three bits are zero, rather than one; a white represented in RGB565 as (0x1F, 0x3F, 0x1F) becomes (0xF8, 0xFC, 0xF8) in RGB888. This can be fixed using shift with insert to place some of the most-significant bits into the lower bits.

From 888 to 565

Now, we can look at the reverse operation, converting RGB888 to RGB565. Here, we ssume that the RGB888 data is in the format produced by the code above; separated cross three registers d0 to d2, with each register containing eight elements of each color. The result will be stored as eight 16-bit RGB565 elements in q2.

Hey, what's the best and/or fastest way to do element shifts/shuffles? So for example having a 4x32bit vector <1,2,3,4> I want to get <2,3,4,1> or <2,3,4,x> with x undefined. I there a way to achieve this with NEON?

You can do this using the VEXT instruction. This extracts a set of contiguous bytes across a pair of registers into a third register. If the input registers are the same, it is effectively a rotate operation. In your example, if the vector is arranged such that <1> is in element 3 and <4> is in element 0 of q0, you can create the new vector <2, 3, 4, 1> using: VEXT q0, q0, q0, #12 // Extract a new Q register at 12 byte offset.

If you need arbitrary element permutation, use the VTBL instruction. This uses a table of values in D registers, the elements of which are indexed by a target D register. For each element in the target, an element in the table is indexed and inserted into the target.

The Arm Neoverse N1 platform is the first compute platform from Arm capable of servicing the wider range of data center workloads with performance levels competitive with the legacy architectures used…