gpt4 book ai didi

c - 二维莫顿解码功能 64bits

转载 作者:太空宇宙 更新时间:2023-11-04 04:36:01 25 4
gpt4 key购买 nike

第一个函数将 [x, y] 编码为 64 位宽的 Morton 代码,其中 x 和 y 是 32 位宽的整数,使用二进制魔数(Magic Number)的交错位。

反向函数是什么?

void xy2d_morton_64bits(uint64_t x, uint64_t y, uint64_t *d)
{
x = (x | (x << 16)) & 0x0000FFFF0000FFFF;
x = (x | (x << 8)) & 0x00FF00FF00FF00FF;
x = (x | (x << 4)) & 0x0F0F0F0F0F0F0F0F;
x = (x | (x << 2)) & 0x3333333333333333;
x = (x | (x << 1)) & 0x5555555555555555;

y = (y | (y << 16)) & 0x0000FFFF0000FFFF;
y = (y | (y << 8)) & 0x00FF00FF00FF00FF;
y = (y | (y << 4)) & 0x0F0F0F0F0F0F0F0F;
y = (y | (y << 2)) & 0x3333333333333333;
y = (y | (y << 1)) & 0x5555555555555555;

*d = x | (y << 1);
}

void d2xy_morton_64bits(uint64_t d, uint64_t *x, uint64_t *y)
{
// ????
}

最佳答案

void xy2d_morton(uint64_t x, uint64_t y, uint64_t *d)
{
x = (x | (x << 16)) & 0x0000FFFF0000FFFF;
x = (x | (x << 8)) & 0x00FF00FF00FF00FF;
x = (x | (x << 4)) & 0x0F0F0F0F0F0F0F0F;
x = (x | (x << 2)) & 0x3333333333333333;
x = (x | (x << 1)) & 0x5555555555555555;

y = (y | (y << 16)) & 0x0000FFFF0000FFFF;
y = (y | (y << 8)) & 0x00FF00FF00FF00FF;
y = (y | (y << 4)) & 0x0F0F0F0F0F0F0F0F;
y = (y | (y << 2)) & 0x3333333333333333;
y = (y | (y << 1)) & 0x5555555555555555;

*d = x | (y << 1);
}

// morton_1 - extract even bits

uint64_t morton_1(uint64_t x)
{
x = x & 0x5555555555555555;
x = (x | (x >> 1)) & 0x3333333333333333;
x = (x | (x >> 2)) & 0x0F0F0F0F0F0F0F0F;
x = (x | (x >> 4)) & 0x00FF00FF00FF00FF;
x = (x | (x >> 8)) & 0x0000FFFF0000FFFF;
x = (x | (x >> 16)) & 0xFFFFFFFFFFFFFFFF;
return x;
}

void d2xy_morton(uint64_t d, uint64_t *x, uint64_t *y)
{
*x = morton_1(d);
*y = morton_1(d >> 1);
}

关于c - 二维莫顿解码功能 64bits,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30540457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com