Global Pointer - Bitwise operation


#1

Hi There,

Is it possible to perform a bitwise xor, and, or on a __global uint * pointer in OpenCL? I’m trying to do this but compiler throws me an error.
The goal is to perform something like:
__global uint *p = an address on global memory
then
*p = some value
*(p ^ 1) = other value

In C we can manipulate pointers freely - can it be done on OpenCL?

Thanks in advance.


#2

Why not share the error the compiler threw? It would be helpful. Was it that “1” isn’t the same type as “p”? If so, perhaps *(p ^ (__global uint *)1) would work. If not, try casting the pointer to an unsigned integer of the same width, then doing your bitops there, and then casting back.


#3

Hi There,

I have tried what you proposed - something like:
__global uint *p1 = p2 ^ (__global uint *)1 (where p2 is a valid __global uint *)
however compiler throws: "invalid operands to binary expression (’__global uint *’ and ‘__global uint *’)


#4

Seems like you’ll need to use the cast to int, do xor, cast to ptr route.


#5

OK - casting to ulong - then XORing - then cast to __global uint * - worked like a charm. Thanks.

__global uint *p1 = (__global uint *)((ulong)p2 ^ 1);