How to replace manual bit manipulation by bit_permute_step

Viewed 53
1 Answers

Yes, namely:

return bit_permute_step(v, 0x000000ff, 16);

The bits of v indicated by the mask 0x000000ff contain the r component, bit_permute_step will exchange them with the corresponding bits 16 (the distance parameter) places to the left, which corresponds to the b component of v. So bit_permute_step(v, 0x000000ff, 16) will swap the r with the b, and hence turn RGBA into BGRA (and also BGRA into RGBA, because a swap is its own inverse).

This could also be found via the permutation calculator: http://programming.sirrida.de/calcperm.php

Use the indices 16 17 18 19 20 21 22 23 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 24 25 26 27 28 29 30 31 (source indices) and disable bit-group moving.


A C++ implementation (also usable as C code) of bit_permute_step for 32-bit integers could be:

uint32_t bit_permute_step(uint32_t x, uint32_t m, uint32_t shift) {
    uint32_t t;
    t = ((x >> shift) ^ x) & m;
    x = (x ^ t) ^ (t << shift);
    return x;
}
Related