I'm looking for a fast way to compute a 3D Morton number. This site has a magic-number based trick for doing it for 2D Morton numbers, but it doesn't seem obvious how to extend it to 3D.
So basically I have 3 10-bit numbers that I want to interleave into a single 30 bit number with a minimal number of operations.
The following code finds the Morton number of the three 10 bit input numbers. It uses the idee from your link and performs the bit spreading in the steps 5-5, 3-2-3-2, 2-1-1-1-2-1-1-1, and 1-1-1-1-1-1-1-1-1-1 because 10 is not a power of two.
Above you can see the location of every bit before the first and after every of the four steps.
Good timing, I just did this last month!
The key was to make two functions. One spreads bits out to every-third bit. Then we can combine three of them together (with a shift for the last two) to get the final Morton interleaved value.
This code interleaves starting at the HIGH bits (which is more logical for fixed point values.) If your application is only 10 bits per component, just shift each value left by 22 in order to make it start at the high bits.
Here's a generator I've made in Ruby for producing encoding methods of arbitrary length:
The output is suitably generic and can be adapted as required. Sample output: