1

I have 16 indexes going from 0 to 255 (8 bits each) stored in a big uint4 and another into a ulong[2].

How can I convert them so I can have access to each of their individual 8-bit (uchar) values?

right now I'm doing convertion like this for uint4:

index1 =  myUint4Val.s0       & 0xff;  
index2 = (myUint4Val.s0 >>8)  & 0xff;
...
index16 =(myUint4Val.s3 >>24) & 0xff;

then I can use them like:

value = dataAt[index1]; ....

I would prefer not using those >>, & 0xff, since these are extra operations I wish to avoid.

instead, accessing them as uchar8.s0 .. uchar8.s7, seems to be okay,

but I'm stucked in converting types to the one I want...

Jean F.
  • 127
  • 8

2 Answers2

1

In OpenCL there are reinterpret functions as_*()

For example,

uint16 a = as_uint16(myUint4Val); // now you can use a.s0, a.s01, a.s2 etc
uint16 b = as_uint16(myUint8Val);
kanna
  • 1,412
  • 1
  • 15
  • 33
0

You could take the address of your myUint4Val, cast the resulting pointer to uchar* and treat it as an array of uchar variables:

uchar* index_array = (uchar*)&myUint4Val;

value = dataAt[index_array[0]];
value = dataAt[index_array[1]];

Alternatively you can do a C-Style reinterpret-cast construct, that tells the compiler to treat the memory of your myUint4Val as a some other type (be careful with type sizes here). To get the first 8 indices into a uchar8, you could try the following, which creates a copy of the right type. It basically takes the address via &, changes to pointer type to the desired uchar8* pointer type and dereferences the pointer to an uchar8 value type.

uchar8 index = *((uchar8*)&myUint4Val); // takes first 8 byte
value = dataAt[index.s0];

Alternatively, you could create just a correctly typed pointer:

uchar8* index = (uchar8*)&myUint4Val;
value = dataAt[(*index).s0];

Note: I didn't have the chance to test this, but I hope the general idea helps.

noma
  • 1,171
  • 6
  • 15
  • I didn't thought we could make things like what C does, because when I saw codes in openCL ,I've never seen such a simpler way to convert data types. thanks for the info – Jean F. Nov 03 '17 at 18:33