I'm building a simulation of the Ising Model in OpenCL which means that my data consists of a bunch of states which can either be up/1 or down/-1.
To save memory bandwidth 8 of these states are encoded into a single byte (up=1, down=0). Now in one of the calculations I need an integer vector with values corresponding to the original states, i.e. 1 or -1.
Example:
Input byte (uchar in OpenCL): 01010011
Convert to: (int8)(-1,1,-1,1,-1,-1,1,1);
I do have a working solution for that problem, but I'm wondering if there is a quicker, more efficient way:
uchar c = spins[id];
int8 spin;
spin.s0 = (c >> 0) & 1;
spin.s1 = (c >> 1) & 1;
spin.s2 = (c >> 2) & 1;
spin.s3 = (c >> 3) & 1;
spin.s4 = (c >> 4) & 1;
spin.s5 = (c >> 5) & 1;
spin.s6 = (c >> 6) & 1;
spin.s7 = (c >> 7) & 1;
spin = spin * 2 - 1;
EDIT:
Does not seem to be faster in my situation, but it's more concise at least:
__constant uchar8 bits = (uchar8)(0,1,2,3,4,5,6,7);
uchar c = spins[id];
int8 spin = convert_int8((uchar8)(c) >> bits & 1) * 2 - 1;