ビットを効率的にデインターリーブする方法 (逆モートン)
-
29-10-2019 - |
質問
この質問: ビットをデインターリーブする方法 (UnMortonizing?) には、モートン数の半分の一方(奇数ビットのみ)を抽出するための良い答えがありますが、できるだけ少ない操作で両方の部分(奇数ビットと偶数ビット)を抽出する解決策が必要です。
私の用途では、32 ビット整数を取得し、2 つの 16 ビット整数を抽出する必要があります。1 つは偶数ビットで、もう 1 つは 1 ビット右にシフトされた奇数ビットです。
input, z: 11101101 01010111 11011011 01101110
output, x: 11100001 10110111 // odd bits shifted right by 1
y: 10111111 11011010 // even bits
モートン数を生成するためのマジックナンバーを使用したシフトとマスクを使用する解決策はたくさんあるようです(つまり、インターリーブビット)、例: バイナリマジックナンバーによるビットのインターリーブ, 、しかし、その逆を行うためのものはまだ見つかりません(つまり、デインターリーブ)。
アップデート
Hacker's Delight の完璧なシャッフル/アンシャッフルに関するセクションを再読したところ、いくつかの有用な例を見つけたので、以下のようにアレンジしました。
// morton1 - extract even bits
uint32_t morton1(uint32_t x)
{
x = x & 0x55555555;
x = (x | (x >> 1)) & 0x33333333;
x = (x | (x >> 2)) & 0x0F0F0F0F;
x = (x | (x >> 4)) & 0x00FF00FF;
x = (x | (x >> 8)) & 0x0000FFFF;
return x;
}
// morton2 - extract odd and even bits
void morton2(uint32_t *x, uint32_t *y, uint32_t z)
{
*x = morton1(z);
*y = morton1(z >> 1);
}
これは、現在のスカラー形式と SIMD の利用の両方でまだ改善できると思うので、より良いソリューション (スカラーまたは SIMD のいずれか) に今でも興味があります。
解決
あなたのプロセッサが64ビットintsを効率的に処理する場合は、操作を組み合わせることができます...
int64 w = (z &0xAAAAAAAA)<<31 | (z &0x55555555 )
w = (w | (w >> 1)) & 0x3333333333333333;
w = (w | (w >> 2)) & 0x0F0F0F0F0F0F0F0F;
...
. 他のヒント
Intel Haswell以降のCPUのコード。PEXTおよびPDEPの命令を含むBMI2命令セットを使用できます。これらは(他の素晴らしいものの中で)機能を作るために使用されます。
#include <immintrin.h>
#include <stdint.h>
// on GCC, compile with option -mbmi2, requires Haswell or better.
uint64_t xy_to_morton (uint32_t x, uint32_t y)
{
return _pdep_u32(x, 0x55555555) | _pdep_u32(y,0xaaaaaaaa);
}
uint64_t morton_to_xy (uint64_t m, uint32_t *x, uint32_t *y)
{
*x = _pext_u64(m, 0x5555555555555555);
*y = _pext_u64(m, 0xaaaaaaaaaaaaaaaa);
}
. 誰かが3Dでモートンコードを使用している場合、彼は3毎に1ビットを読む必要があり、ここで64ビットが使用されている関数です:
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)
. スピードが必要な場合は、一度に1バイト変換のためにテーブルルックアップを使用できる場合(2バイトのテーブルは大きくなります)。手順はDelphi IDEの下で行われますが、アセンブラ/アルゴリズムは同じです。
const
MortonTableLookup : array[byte] of byte = ($00, $01, $10, $11, $12, ... ;
procedure DeinterleaveBits(Input: cardinal);
//In: eax
//Out: dx = EvenBits; ax = OddBits;
asm
movzx ecx, al //Use 0th byte
mov dl, byte ptr[MortonTableLookup + ecx]
//
shr eax, 8
movzx ecx, ah //Use 2th byte
mov dh, byte ptr[MortonTableLookup + ecx]
//
shl edx, 16
movzx ecx, al //Use 1th byte
mov dl, byte ptr[MortonTableLookup + ecx]
//
shr eax, 8
movzx ecx, ah //Use 3th byte
mov dh, byte ptr[MortonTableLookup + ecx]
//
mov ecx, edx
and ecx, $F0F0F0F0
mov eax, ecx
rol eax, 12
or eax, ecx
rol edx, 4
and edx, $F0F0F0F0
mov ecx, edx
rol ecx, 12
or edx, ecx
end;
. 固定サイズの整数に限定されたり、ハードコーディングされた定数を使用して同様のコマンドのリストを作成したりしたくなかったので、テンプレート メタプログラミングを利用して関数と定数を生成する C++11 ソリューションを開発しました。で生成されたアセンブリコード -O3
BMIを使用せずに限界までタイトに見える:
andl $0x55555555, %eax
movl %eax, %ecx
shrl %ecx
orl %eax, %ecx
andl $0x33333333, %ecx
movl %ecx, %eax
shrl $2, %eax
orl %ecx, %eax
andl $0xF0F0F0F, %eax
movl %eax, %ecx
shrl $4, %ecx
orl %eax, %ecx
movzbl %cl, %esi
shrl $8, %ecx
andl $0xFF00, %ecx
orl %ecx, %esi
実装
基本的にすべてのステップで、 morton1
関数は、次のような一連の定数をシフトして追加することで機能します。
0b0101010101010101
(1 と 0 を交互に表示)0b0011001100110011
(1 と 0 を交互に 2 つ)0b0000111100001111
(1 と 0 を交互に 4 つ)0b0000000011111111
(8x 1 と 0 を交互)
もし使うとしたら D
寸法としては、次のようなパターンが考えられます。 D-1
ゼロと 1
1つ。したがって、これらを生成するには、連続したものを生成し、ビットごとに適用するか、次のようにするだけで十分です。
/// @brief Generates 0b1...1 with @tparam n ones
template <class T, unsigned n>
using n_ones = std::integral_constant<T, (~static_cast<T>(0) >> (sizeof(T) * 8 - n))>;
/// @brief Performs `@tparam input | (@tparam input << @tparam width` @tparam repeat times.
template <class T, T input, unsigned width, unsigned repeat>
struct lshift_add :
public lshift_add<T, lshift_add<T, input, width, 1>::value, width, repeat - 1> {
};
/// @brief Specialization for 1 repetition, just does the shift-and-add operation.
template <class T, T input, unsigned width>
struct lshift_add<T, input, width, 1> : public std::integral_constant<T,
(input & n_ones<T, width>::value) | (input << (width < sizeof(T) * 8 ? width : 0))> {
};
これで、以下を使用してコンパイル時に任意の次元の定数を生成できるようになりました。
template <class T, unsigned step, unsigned dimensions = 2u>
using mask = lshift_add<T, n_ones<T, 1 << step>::value, dimensions * (1 << step), sizeof(T) * 8 / (2 << step)>;
同じタイプの再帰を使用して、アルゴリズムの各ステップの関数を生成できます。 x = (x | (x >> K)) & M
:
template <class T, unsigned step, unsigned dimensions>
struct deinterleave {
static T work(T input) {
input = deinterleave<T, step - 1, dimensions>::work(input);
return (input | (input >> ((dimensions - 1) * (1 << (step - 1))))) & mask<T, step, dimensions>::value;
}
};
// Omitted specialization for step 0, where there is just a bitwise and
「何ステップ必要か?」という質問に答える必要があります。これは次元数にも依存します。一般的に、 k
ステップを計算する 2^k - 1
出力ビット。各次元の意味のあるビットの最大数は次の式で与えられます。 z = sizeof(T) * 8 / dimensions
, 、したがって、それを取るのに十分です 1 + log_2 z
ステップ。問題は、これが次のように必要になることです。 constexpr
テンプレートパラメータとして使用するためです。これを回避するために私が見つけた最良の方法は、次のように定義することです log2
メタプログラミング経由:
template <unsigned arg>
struct log2 : public std::integral_constant<unsigned, log2<(arg >> 1)>::value + 1> {};
template <>
struct log2<1u> : public std::integral_constant<unsigned, 0u> {};
/// @brief Helper constexpr which returns the number of steps needed to fully interleave a type @tparam T.
template <class T, unsigned dimensions>
using num_steps = std::integral_constant<unsigned, log2<sizeof(T) * 8 / dimensions>::value + 1>;
そして最後に、単一の呼び出しを実行できます。
/// @brief Helper function which combines @see deinterleave and @see num_steps into a single call.
template <class T, unsigned dimensions>
T deinterleave_first(T n) {
return deinterleave<T, num_steps<T, dimensions>::value - 1, dimensions>::work(n);
}