Accessing to bytes in array as to short and int values?

Hi GPGPU hackers!

In my kernel i try to access to __global unsigned char * array as to short and int values and keep having
weird results with it, for example


__global unsigned short *l2NumPtr = buffer+tolookPos;
unsigned short l2Num = *l2NumPtr;

Here i can have l2Num value constructed from tolookPos and tolookPos-1 bit
(also from position-1 and bits swapped).
And with:


__global unsigned short *l2NumPtr = buffer+tolookPos-1

i get the SAME VALUE.
so in order to acces to any short value correctly, i must have this:

__attribute__((always_inline))
unsigned short getUShort(__global unsigned char * array, unsigned int position){
	
	return ((unsigned short)array[position]) << BYTE_SHIFT | array[position+1] ;
}


Here, i have 2 operations “wasted” and with Int value, it is already 6 operations.

Is there any option to have the right access by pointer?

You should really read the specification, what you’re doing has ‘undefined’ behaviour (i.e. is not valid code).

See these sections of the specification:

6.1.5 Alignment of Types
6.2.5 Pointer Casting