Last active
August 29, 2015 14:19
-
-
Save aavenel/a5349516aa33b7499eef to your computer and use it in GitHub Desktop.
Addition with Morton interleaved coordinates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const uint64_t x3_mask = 0x9249249249249249; // 0b...01001001 | |
const uint64_t y3_mask = 0x2492492492492492; // 0b...10010010 | |
const uint64_t z3_mask = 0x4924924924924924; // 0b...00100100 | |
const uint64_t xy3_mask = x3_mask | y3_mask; | |
const uint64_t xz3_mask = x3_mask | z3_mask; | |
const uint64_t yz3_mask = y3_mask | z3_mask; | |
const uint64_t x2_mask = 0x55555555; //0b...01010101 | |
const uint64_t y2_mask = 0xAAAAAAAA; //0b...10101010 | |
/* Add two morton keys (xy interleaving) */ | |
uint64_t Morton2Add(uint64_t m1, uint64_t m2) | |
{ | |
uint64_t x_sum = ( m1 | y2_mask) + (m2 & x2_mask); | |
uint64_t y_sum = ( m1 | x2_mask) + (m2 & y2_mask); | |
return (x_sum & x2_mask) | (y_sum & y2_mask); | |
} | |
/* Add two morton keys (xyz interleaving) */ | |
uint64_t Morton3Add(uint64_t m1, uint64_t m2) | |
{ | |
uint64_t x_sum = ( m1 | yz3_mask) + (m2 & x3_mask); | |
uint64_t y_sum = ( m1 | xz3_mask) + (m2 & y3_mask); | |
uint64_t z_sum = ( m1 | xy3_mask) + (m2 & z3_mask); | |
return (x_sum & x3_mask ) | (y_sum & y3_mask ) | (z_sum & z3_mask); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment