VECTOR UNSIGNED SHORT
Mike Acton
April 26, 2006
April 26, 2006
Format
Eight 16 bit unsigned integer values packed into a single 128 bit vector stored in big-endian format.
Elements are refered to by index from low-address to high-address.
| 0x00 | 0x02 | 0x04 | 0x06 | 0x08 | 0x0a | 0x0c | 0x0e |
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
Initialization (PPU and SPU)
The vector can be initialized in method similar to initializing an array.
vector unsigned short a = { 0x1122, 0x2233, 0x4455, 0x6677, 0x8899, 0xaabb, 0xccdd, 0xeeff };
The vector can be also be intialized using a vector cast.
vector unsigned short a = (vector unsigned short)( 0x1122, 0x2233, 0x4455, 0x6677, 0x8899, 0xaabb, 0xccdd, 0xeeff );
Extracting a scalar component (PPU)
On the PPU, there are two valid methods for moving the elements of the vector into a scalar register (in C99).
- Cast through a (char*):
unsigned short vec_ushort_extract( vector unsigned short v, int index ) { return ((unsigned short*)(char*)&v)[ index ]; }Note that a direct cast from (vector unsigned short*) to (unsigned short*) is not permitted under the C strict-aliasing rules. Casts to and from (char*) are excepted.
- Cast through a union. This method is explicitly permitted under the C strict-aliasing rules.
#include <altivec.h> #include <stdint.h> #include <stdio.h> typedef union VEC_USHORT VEC_USHORT; union VEC_USHORT { vector unsigned short v; unsigned short e[8]; }; int main( void ) { vector unsigned short a = { 0x1122, 0x2233, 0x4455, 0x6677, 0x8899, 0xaabb, 0xccdd, 0xeeff }; VEC_USHORT A = { .v = a }; printf("a = 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x, 0x%04x\n" ,A.e[0], A.e[1], A.e[2], A.e[3], A.e[4], A.e[5], A.e[6], A.e[7] ); return (0); }
Extracting a scalar component (SPU)
On the SPU, the elements of the vector can be moved into a scalar variable by using the spu_extract() function. Note that there are no scalar registers on the SPU. This process effectively allows the compiler to move the selected vector component to a component of another vector register of its own choosing.
#include <spu_intrinsics.h>
#include <stdint.h>
#include <stdio.h>
int
main( void )
{
vector unsigned short a = (vector unsigned short)( 0x1122, 0x2233, 0x4455, 0x6677, 0x8899, 0xaabb, 0xccdd, 0xeeff );
unsigned short e0 = spu_extract( a, 0 );
unsigned short e1 = spu_extract( a, 1 );
unsigned short e2 = spu_extract( a, 2 );
unsigned short e3 = spu_extract( a, 3 );
unsigned short e4 = spu_extract( a, 4 );
unsigned short e5 = spu_extract( a, 5 );
unsigned short e6 = spu_extract( a, 6 );
unsigned short e7 = spu_extract( a, 7 );
printf("a = 0x%04x, 0x%04x, 0x%04x, 0x%04x\n", e0, e1, e2, e3 );
printf(" = 0x%04x, 0x%04x, 0x%04x, 0x%04x\n", e4, e5, e6, e7 );
return (0);
}