Search code examples
csimdintrinsicspowerpc

Loading vectors through pointers, casts and dereferences?


The OpenPower Manual | Vector Data Types states to avoid manually loading elements, and to use vector casts:

The preferred way to access vectors at an application-defined address is by using vector pointers and the C/C++ dereference operator *. Similar to other C /C++ data types, the array reference operator [ ] may be used to access vector objects with a vector pointer with the usual definition to access the n-th vector element from a vector pointer. The use of vector built-in functions such as vec_xl and vec_xst is discouraged except for languages where no dereference operators are available.

vector char vca;
vector char vcb;
vector int via;
int a[4];
void *vp;

via = *(vector int *) &a[0];
vca = (vector char) via;
vcb = vca;
vca = *(vector char *)vp;
*(vector char *)&a[0] = vca;

I'm in the early stages of testing the technique, but it appears the code above compiles. Compilers tested are GCC 4.0.1 (Old PowerMac G5) and GCC 4.8.5 (Power 730 Server).

My first question is, does the technique handle unaligned byte arrays properly?

My second question is, does the technique automatically perform big-endian conversions on little-endian systems?

My third question is, should we use the technique in practice? It feels like the technique should yield undefined behavior or punning violations.


Solution

  • My first question is, does the technique handle unaligned byte arrays properly?

    No, it does not handle unaligned arrays properly. Memory addresses and offsets are truncated to effective addresses by masking-off 4 low-order bits.

    My second question is, does the technique automatically perform big-endian conversions on little-endian systems?

    YES.

    My third question is, should we use the technique in practice?

    NO, don't use it.