How to efficiently de-interleave bits (inverse Morton)

后端 未结 5 1078
情歌与酒
情歌与酒 2020-12-05 03:15

This question: How to de-interleave bits (UnMortonizing?) has a good answer for extracting one of the two halves of a Morton number (just the odd bits), but I need a solutio

5条回答
  •  清歌不尽
    2020-12-05 03:33

    In case someone is using morton codes in 3d, so he needs to read one bit every 3, and 64 bits here is the function I used:

    uint64_t morton3(uint64_t x) {
        x = x & 0x9249249249249249;
        x = (x | (x >> 2))  & 0x30c30c30c30c30c3;
        x = (x | (x >> 4))  & 0xf00f00f00f00f00f;
        x = (x | (x >> 8))  & 0x00ff0000ff0000ff;
        x = (x | (x >> 16)) & 0xffff00000000ffff;
        x = (x | (x >> 32)) & 0x00000000ffffffff;
        return x;
    }
    uint64_t bits; 
    uint64_t x = morton3(bits)
    uint64_t y = morton3(bits>>1)
    uint64_t z = morton3(bits>>2)
    

提交回复
热议问题