Store, modify and retrieve strings with GCC Vector Extensions?

The GCC Vector Extensions provide an abstraction of SIMD instructions.

I am wondering how to use them for string processing, e.g. to mask each byte of a buffer:

typedef uint8_t v32ui __attribute__ ((vector_size(32)));

void f(const uint8_t *begin, const uint8_t *end, uint8_t *o)
{
    for (; begin < end; begin += 32, o+=32)
      *(v32ui*) o = (*(v32ui*) begin) & 0x0fu;
}

Assuming that the input and output buffers are properly aligned (at 32 byte), is such casting supported and well defined with the GCC verctor extensions?

And is this the most efficient way to use the vector extensions on strings?

Or do I have to explicitly store/retrieve parts of the string into the vectors?

For example like this:

void f(const uint8_t *begin, const uint8_t *end, uint8_t *o)
{
    for (; begin < end; begin += 32, o+=32) {
      v32ui t;
      memcpy(&t, begin, 32);
      t &= 0f0u;
      memcpy(o, &t, 32);
    }
}

Or are there better/more efficient ways than to memcpy?

And when assuming that the input or output buffer (or both) are unaligned, how then can be used the vector extensions safely/efficiently for string processing?


Source: gcc

Leave a Reply