t = copy(q); // temporary copy of q register
for( X = 0; X < N; ++X) // for each element of the state array
{
a = X >> m; // top m bits
b = X & mask; // bottom m bits, mask = 2m-1 = 0111..1 (m 1's)
c = (a + b) % M; // classical addition
Y = (a << m) | c; // concatenate the bits
q[Y] = t[X]; // perform the permutation
}