Use the faster bit interleave for encode_quadkey itself

encode_vertex() computed exactly the same thing as encode_quadkey(),
so there is no reason to have both. Give encode_quadkey() the branch-free
implementation, which also speeds up the default encode_index, and have
the shared node code call it directly. The unit test now compares it
against the old bit-at-a-time loop and checks that decode_quadkey()
reverses it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01P2nBqZisxNQfmEmon3vE9v
This commit is contained in:
Claude
2026-09-23 20:02:49 +00:00
parent e1795ebe32
commit 58ad6e3008
6 changed files with 39 additions and 36 deletions
+13 -29
View File
@@ -156,19 +156,20 @@ void decode_hilbert(unsigned long long index, unsigned *wx, unsigned *wy) {
hilbert_d2xy(1LL << 32, index, wx, wy);
}
// Spread the 32 bits of v out into the even bits of a 64-bit value
static inline unsigned long long spread_bits(unsigned int v) {
unsigned long long x = v;
x = (x | (x << 16)) & 0x0000FFFF0000FFFFULL;
x = (x | (x << 8)) & 0x00FF00FF00FF00FFULL;
x = (x | (x << 4)) & 0x0F0F0F0F0F0F0F0FULL;
x = (x | (x << 2)) & 0x3333333333333333ULL;
x = (x | (x << 1)) & 0x5555555555555555ULL;
return x;
}
// Interleave the bits of wx and wy, with each bit of wx above the same bit of wy
unsigned long long encode_quadkey(unsigned int wx, unsigned int wy) {
unsigned long long out = 0;
int i;
for (i = 0; i < 32; i++) {
unsigned long long v = ((wx >> (32 - (i + 1))) & 1) << 1;
v |= (wy >> (32 - (i + 1))) & 1;
v = v << (64 - 2 * (i + 1));
out |= v;
}
return out;
return (spread_bits(wx) << 1) | spread_bits(wy);
}
static std::atomic<unsigned char> decodex[256];
@@ -217,20 +218,3 @@ void set_projection_or_exit(const char *optarg) {
exit(EXIT_ARGS);
}
}
// Spread the 32 bits of v out into the even bits of a 64-bit value
static inline unsigned long long spread_bits(unsigned int v) {
unsigned long long x = v;
x = (x | (x << 16)) & 0x0000FFFF0000FFFFULL;
x = (x | (x << 8)) & 0x00FF00FF00FF00FFULL;
x = (x | (x << 4)) & 0x0F0F0F0F0F0F0F0FULL;
x = (x | (x << 2)) & 0x3333333333333333ULL;
x = (x | (x << 1)) & 0x5555555555555555ULL;
return x;
}
// The same as encode_quadkey(), but faster, for the vertices of the list of shared nodes,
// so that nodes that are near each other are also near each other in the sorted list.
unsigned long long encode_vertex(unsigned int wx, unsigned int wy) {
return (spread_bits(wx) << 1) | spread_bits(wy);
}