The magic-mask stages spread and compact the bits of a coordinate full
This proof shows: correctness.
All words are 64-bit unsigned. For a word v we write vp for bit p of v. Let 0≤x<232 with bits x0,…,x31. Number the stages of SPREAD by j=4,3,2,1,0, so that the stage j computes v←(v∣(v≪2j))&Mj, where Mj is the 64-bit mask whose bit p is 1 exactly when pmod2j+1<2j. The masks of lines L1 to L5 are M4,M3,M2,M1,M0: for example M4=0x0000FFFF0000FFFF and M0=0x5555555555555555.
Position map. For 0≤j≤5 and 0≤i<32 let
pj(i)=i+2j⌊i/2j⌋. Write i=q⋅2j+r with 0≤r<2j; then pj(i)=q⋅2j+1+r. So pj(i)mod2j+1=r<2j, and pj is injective, since q and r can be read back from pj(i). Also p5(i)=i for i<32, and p0(i)=2i.
Invariant. Let v(j) be the word after the stage j, and let v(5)=x be the word before line L1. Then for each j from 5 down to 0:
v(j)=i=0∑31xi2pj(i). In words, bit i of x is at position pj(i), and every other bit of the word is 0.
Base. For j=5, p5(i)=i, so the claim is v(5)=x.
Step. Assume the claim for j+1, with 0≤j≤4. Take a bit i and write i=q⋅2j+1+h⋅2j+r with h∈{0,1} and 0≤r<2j. Then ⌊i/2j+1⌋=q, so by the claim for j+1 the bit xi is at position P=i+2j+1q=q⋅2j+2+h⋅2j+r of v(j+1). All positions pj(i) and pj+1(i) are at most p0(31)=62, so the kept copies below are inside the word; a copy that the shift moves past bit 63 is one that the mask would clear. The word u=v(j+1)∣(v(j+1)≪2j) has a copy of xi at P and at P+2j, and the AND with Mj keeps a position p only when pmod2j+1<2j.
- If h=0, then Pmod2j+1=r<2j, so the copy at P is kept, and (P+2j)mod2j+1=r+2j≥2j, so the copy at P+2j is cleared. The kept position is q⋅2j+2+r.
- If h=1, then Pmod2j+1=2j+r≥2j, so the copy at P is cleared, and P+2j=q⋅2j+2+2j+1+r has remainder r<2j modulo 2j+1, so it is kept.
In both cases the kept position is q⋅2j+2+h⋅2j+1+r=i+2j(2q+h)=i+2j⌊i/2j⌋=pj(i).
It remains to show that the OR does not merge two different bits, and that no other one-bits survive. Every one-bit of u is a copy of some xi, at P or at P+2j. Suppose that a position p kept by the mask holds a copy of xi and a copy of xi′. By the case analysis, the kept copy of each bit is at pj(i) and at pj(i′) respectively, and the cleared copies are not kept; so pj(i)=p=pj(i′), and i=i′ because pj is injective. Hence position pj(i) of v(j)=u&Mj holds exactly xi, and every position that is not of the form pj(i) is 0. This proves the claim for j.
SPREAD. For j=0 the invariant gives SPREAD(x)=∑ixi22i. Line L7 then returns ∑ixi22i+∑iyi22i+1, since the two terms have their ones at even and at odd positions and so the OR is a sum. This is Z(x,y), and it is below 264.
COMPACT. Number the stages of COMPACT by j=0,1,2,3,4, with shift 2j and the mask Mj+1, where M5=0x00000000FFFFFFFF is the mask of the positions with pmod64<32. Let z be a word whose odd bits are 0 after line D1, so z=∑ixi2p0(i) for the bits xi=z2i. We claim that after the stage j the word is ∑ixi2pj+1(i). Assume that before the stage the word is v=∑ixi2pj(i). Write i=q⋅2j+1+h⋅2j+r as before; then pj(i)=q⋅2j+2+h⋅2j+1+r and pj+1(i)=q⋅2j+2+h⋅2j+r. The word v⊕(v≫2j) has, at each position p, the XOR of vp and vp+2j. Take a position p that Mj+1 keeps, so p=Q⋅2j+2+s with 0≤s<2j+1. Write s=H⋅2j+R with H∈{0,1} and 0≤R<2j. Every one-bit of v is at a position whose remainder modulo 2j+1 is below 2j.
- If H=0, then p+2j has remainder 2j+R≥2j modulo 2j+1, so vp+2j=0, and the result at p is vp, which is xi for the i with q=Q, h=0, r=R, if that i is below 32, and 0 otherwise.
- If H=1, then p has remainder 2j+R≥2j modulo 2j+1, so vp=0, and p+2j=Q⋅2j+2+2j+1+R, so the result is xi for q=Q, h=1, r=R, or 0 if that i is not below 32.
In both cases position p of the result holds xi for the unique i with pj+1(i)=p, and the positions with no such i hold 0. The positions that Mj+1 clears hold 0 by definition. This proves the claim. After the stage j=4 the word is ∑ixi2p5(i)=∑ixi2i, so COMPACT returns the even bits of z, packed. Line D8 applies it to z and to z≫1, which gives the x bits and the y bits of the code. Hence MORTON-DECODE inverts MORTON-ENCODE, and since both maps are between sets of 264 elements, encoding is a bijection. ■