Skip to content

Comment on The most useful GCC options and extensionsparent

Comments

Can you give an example where such shuffle instructions are useful? I'm genuinely curious.

The shuffle instructions are commonly used when encoding or decoding integer serialization formats or processing integer arrays. It can also be a fast intermediate operation for some bit-twiddling algorithms, which would require multiple sequential operations to achieve an equivalent distribution of bits for processing.

If you are not working on high-performance bit-twiddling algorithms then you will probably have limited use for shuffle intrinsics. For those applications, it can save a few clock cycles for each call relative to more naive methods.

Shuffles are what makes SIMD go. It's trivial to make simple repeated math ops done in parallel instead of 4 (SIMD vector width) times sequentially, but shuffling vectors cleverly is where you can get big performance wins.

Basic example, doing addition of 4 values in 2 ALU operations:

  vec4 sum(vec4 v) // return v.x+v.y+v.z+v.w repeated 4 times
  {
    vec4 temp = v + v.yxzw;
    return temp + temp.zwxy; // did I get this right?
  }
Practical examples: https://github.com/rikusalminen/threedee-simd (work in progress) Requires this: http://gruntthepeon.free.fr/ssemath/

Cross product is a good example: (v1.yzx * v2.zxy) - (v1.zxy * v2.yzx)

If you think of processors as plumbing, shuffle operations are like pipes: they put the data in the right place for other operations. General shuffle operations let you do things like convert RGB data to RGBA data without trouble. Shuffling is a more flexible version of the more common pack, unpack, and bytewise shift operations.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.