@@ -1499,6 +1499,17 @@ def decode_morton(z: int, chunk_shape: tuple[int, ...]) -> tuple[int, ...]:
14991499 np .uint64 (0x000000000000FFFF ), # Compact stage 4
15001500)
15011501
1502+ # Magic numbers for 5D Morton decode: extract every 5th bit and compact them.
1503+ # Bits for dimension d are at positions d, d+5, d+10, d+15, ...
1504+ # Max 12 bits per dimension (64 // 5 = 12), supporting values up to 4095.
1505+ _MORTON_5D_MASKS : tuple [np .uint64 , ...] = (
1506+ np .uint64 (0x1084210842108421 ), # Extract mask: bits 0, 5, 10, 15, ...
1507+ np .uint64 (0x0318C6318C6318C63 ), # Compact stage 1
1508+ np .uint64 (0xF03C0F03C0F03C0F ), # Compact stage 2
1509+ np .uint64 (0x0000FF000FF000FF ), # Compact stage 3
1510+ np .uint64 (0x0000000000000FFF ), # Compact stage 4 (12 bits max)
1511+ )
1512+
15021513
15031514def _decode_morton_2d (z : npt .NDArray [np .intp ]) -> npt .NDArray [np .intp ]:
15041515 """Decode 2D Morton codes using magic number bit manipulation.
@@ -1570,6 +1581,27 @@ def _decode_morton_4d(z: npt.NDArray[np.intp]) -> npt.NDArray[np.intp]:
15701581 return out
15711582
15721583
1584+ def _decode_morton_5d (z : npt .NDArray [np .intp ]) -> npt .NDArray [np .intp ]:
1585+ """Decode 5D Morton codes using magic number bit manipulation.
1586+
1587+ This extracts interleaved coordinates from Morton codes using
1588+ parallel bit operations instead of bit-by-bit loops.
1589+ """
1590+ # Convert to uint64 for bitwise operations with large masks
1591+ z_u64 = z .astype (np .uint64 )
1592+ out = np .zeros ((len (z ), 5 ), dtype = np .intp )
1593+
1594+ for dim in range (5 ):
1595+ x = (z_u64 >> dim ) & _MORTON_5D_MASKS [0 ]
1596+ x = (x ^ (x >> 4 )) & _MORTON_5D_MASKS [1 ]
1597+ x = (x ^ (x >> 8 )) & _MORTON_5D_MASKS [2 ]
1598+ x = (x ^ (x >> 16 )) & _MORTON_5D_MASKS [3 ]
1599+ x = (x ^ (x >> 32 )) & _MORTON_5D_MASKS [4 ]
1600+ out [:, dim ] = x
1601+
1602+ return out
1603+
1604+
15731605def decode_morton_vectorized (
15741606 z : npt .NDArray [np .intp ], chunk_shape : tuple [int , ...]
15751607) -> npt .NDArray [np .intp ]:
@@ -1590,8 +1622,8 @@ def decode_morton_vectorized(
15901622 n_dims = len (chunk_shape )
15911623 bits = tuple ((c - 1 ).bit_length () for c in chunk_shape )
15921624
1593- # Use magic number optimization for 2D/3D/4D with uniform bit widths.
1594- # Magic numbers have bit limits : 2D up to 32, 3D up to 21, 4D up to 16 bits per dimension.
1625+ # Use magic number optimization for 2D/3D/4D/5D with uniform bit widths.
1626+ # Bit limits are floor(64 / n_dims) : 2D= 32, 3D= 21, 4D=16, 5D=12 bits per dimension.
15951627 if len (set (bits )) == 1 : # All dimensions have same bit width
15961628 max_bits = bits [0 ] if bits else 0
15971629 if n_dims == 2 and max_bits <= 32 :
@@ -1600,6 +1632,8 @@ def decode_morton_vectorized(
16001632 return _decode_morton_3d (z )
16011633 if n_dims == 4 and max_bits <= 16 :
16021634 return _decode_morton_4d (z )
1635+ if n_dims == 5 and max_bits <= 12 :
1636+ return _decode_morton_5d (z )
16031637
16041638 # Fall back to generic bit-by-bit decoding
16051639 max_coords_bits = max (bits ) if bits else 0
0 commit comments