Jump to content

Permute instruction

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by BernardoSulzbach (talk | contribs) at 20:36, 9 July 2021 (If we want to point out the issue with the name, we shouldn't say these are combinations. The Wikipedia article itself states that in a combination "the order of selection does not matter (unlike permutations)." The order, however, clearly matters in this case. Furthermore, normal combinations don't allow for repetitions. There is no good simple mathematical term for what you get from these instructions, so I propose just pointing out that they are not permutations.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Permute (and Shuffle) instructions, part of bit manipulation as well as vector processing, copy unaltered contents from a source array to a destination array, where the indices are specified by a second source array. The size (bitwidth) of the source elements is not restricted but remains the same as the destination size.

There exists two important permute variants, known as gather and scatter, respectively. The gather variant is as follows:

for i = 0 to length-1
    dest[i] = src[indices[i]]

where the scatter variant is:

for i = 0 to length-1
    dest[indices[[i]] = src[i]

Note that unlike in memory-based gather-scatter all three of dest, src, and indices are registers (or parts of registers in the case of bit-level permute), not memory locations.

The scatter variant can be seen to "scatter" the source elements across (into) to the destination, where the "gather" variant is gathering data from the indexed source elements.

Given that the indices may be repeated in both variants, the resultant output is not a strict mathematical permutation because duplicates can occur in the output.

A special case of permute is also used in GPU "swizzling" (again, not strictly a permutation) which performs on-the-fly reordering of subvector data so as to align or duplicate elements with the appropriate SIMD lane.

Occurrences of permute instructions

Permute instructions occur in both scalar processors as well as vector processing engines as well as GPUs. In vector instruction sets they are typically named "Register Gather/Scatter" operations such as in RISC-V vectors,[1] and take Vectors as input for both source elements and source array, and output another Vector.

In scalar instruction sets the scalar registers are broken down into smaller sections (unpacked, SIMD style) where the fragments are then used as array sources. The (small, partial) results are then concatenated (packed) back into the scalar register as the result.

Some ISAs, particularly for cryptographic applications, even have bit-level permute operations, such as bdep (bit deposit) in RISC-V bitmanip;[2] in the Power ISA it is known as bpermd and has been included for several decades, and is still in the Power ISA v.3.0 B spec.[3]

Also in some non-vector ISAs, due to there sometimes being insufficient space in the one source input register to specify the permutation source array in full (particularly if the operation involves bit-level permutation), will include partial reordering instructions. Examples include VSHUFF32x4 from AVX-512.

Permute operations in different forms are surprisingly common, occurring in AltiVec, Power ISA, PowerPC G4, AVX-512, SVE2[4] and in Vector processors as well as GPUs. They are sufficiently important that LLVM has included a shufflevector[5] intrinsic and gcc as __builtin_shuffle.[6] GCC's intrinsic matches the functionality of OpenCL's shuffle intrinsics.[7] Note that all of these, mathematically, are combinations despite using the word "permutation" in the respective specifications.

See also

References

  1. ^ [1]
  2. ^ [2]
  3. ^ "Power ISA Version 3.0 B". Power.org. 2017-03-27. Retrieved 2019-08-11.
  4. ^ ARM HPC, SVE2 Extension summary, p32
  5. ^ [3]
  6. ^ [4]
  7. ^ [5]