@@ -1467,141 +1467,6 @@ def decode_morton(z: int, chunk_shape: tuple[int, ...]) -> tuple[int, ...]:
14671467 return tuple (out )
14681468
14691469
1470- # Magic numbers for 2D Morton decode: extract every 2nd bit and compact them.
1471- # Bits for dimension d are at positions d, d+2, d+4, ...
1472- _MORTON_2D_MASKS : tuple [int , ...] = (
1473- 0x5555555555555555 , # Extract mask: bits 0, 2, 4, ...
1474- 0x3333333333333333 , # Compact stage 1
1475- 0x0F0F0F0F0F0F0F0F , # Compact stage 2
1476- 0x00FF00FF00FF00FF , # Compact stage 3
1477- 0x0000FFFF0000FFFF , # Compact stage 4
1478- )
1479-
1480- # Magic numbers for 3D Morton decode: extract every 3rd bit and compact them.
1481- # Bits for dimension d are at positions d, d+3, d+6, d+9, ...
1482- # Uses uint64 to handle large mask values correctly with numpy.
1483- _MORTON_3D_MASKS : tuple [np .uint64 , ...] = (
1484- np .uint64 (0x1249249249249249 ), # Extract mask: bits 0, 3, 6, 9, ...
1485- np .uint64 (0x30C30C30C30C30C3 ), # Compact stage 1
1486- np .uint64 (0xF00F00F00F00F00F ), # Compact stage 2
1487- np .uint64 (0x00FF0000FF0000FF ), # Compact stage 3
1488- np .uint64 (0x00FF00000000FFFF ), # Compact stage 4
1489- np .uint64 (0x00000000001FFFFF ), # Compact stage 5
1490- )
1491-
1492- # Magic numbers for 4D Morton decode: extract every 4th bit and compact them.
1493- # Bits for dimension d are at positions d, d+4, d+8, d+12, ...
1494- _MORTON_4D_MASKS : tuple [np .uint64 , ...] = (
1495- np .uint64 (0x1111111111111111 ), # Extract mask: bits 0, 4, 8, 12, ...
1496- np .uint64 (0x0303030303030303 ), # Compact stage 1
1497- np .uint64 (0x000F000F000F000F ), # Compact stage 2
1498- np .uint64 (0x000000FF000000FF ), # Compact stage 3
1499- np .uint64 (0x000000000000FFFF ), # Compact stage 4
1500- )
1501-
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-
1513-
1514- def _decode_morton_2d (z : npt .NDArray [np .intp ]) -> npt .NDArray [np .intp ]:
1515- """Decode 2D Morton codes using magic number bit manipulation.
1516-
1517- This extracts interleaved x,y coordinates from Morton codes using
1518- parallel bit operations instead of bit-by-bit loops.
1519- """
1520- out = np .zeros ((len (z ), 2 ), dtype = np .intp )
1521-
1522- # Extract x (bits 0, 2, 4, ...) and compact to (bits 0, 1, 2, ...)
1523- x = z & _MORTON_2D_MASKS [0 ]
1524- x = (x | (x >> 1 )) & _MORTON_2D_MASKS [1 ]
1525- x = (x | (x >> 2 )) & _MORTON_2D_MASKS [2 ]
1526- x = (x | (x >> 4 )) & _MORTON_2D_MASKS [3 ]
1527- x = (x | (x >> 8 )) & _MORTON_2D_MASKS [4 ]
1528- out [:, 0 ] = x
1529-
1530- # Extract y (bits 1, 3, 5, ...) and compact
1531- y = (z >> 1 ) & _MORTON_2D_MASKS [0 ]
1532- y = (y | (y >> 1 )) & _MORTON_2D_MASKS [1 ]
1533- y = (y | (y >> 2 )) & _MORTON_2D_MASKS [2 ]
1534- y = (y | (y >> 4 )) & _MORTON_2D_MASKS [3 ]
1535- y = (y | (y >> 8 )) & _MORTON_2D_MASKS [4 ]
1536- out [:, 1 ] = y
1537-
1538- return out
1539-
1540-
1541- def _decode_morton_3d (z : npt .NDArray [np .intp ]) -> npt .NDArray [np .intp ]:
1542- """Decode 3D Morton codes using magic number bit manipulation.
1543-
1544- This extracts interleaved x,y,z coordinates from Morton codes using
1545- parallel bit operations instead of bit-by-bit loops.
1546- """
1547- # Convert to uint64 for bitwise operations with large masks
1548- z_u64 = z .astype (np .uint64 )
1549- out = np .zeros ((len (z ), 3 ), dtype = np .intp )
1550-
1551- for dim in range (3 ):
1552- x = (z_u64 >> dim ) & _MORTON_3D_MASKS [0 ]
1553- x = (x ^ (x >> 2 )) & _MORTON_3D_MASKS [1 ]
1554- x = (x ^ (x >> 4 )) & _MORTON_3D_MASKS [2 ]
1555- x = (x ^ (x >> 8 )) & _MORTON_3D_MASKS [3 ]
1556- x = (x ^ (x >> 16 )) & _MORTON_3D_MASKS [4 ]
1557- x = (x ^ (x >> 32 )) & _MORTON_3D_MASKS [5 ]
1558- out [:, dim ] = x
1559-
1560- return out
1561-
1562-
1563- def _decode_morton_4d (z : npt .NDArray [np .intp ]) -> npt .NDArray [np .intp ]:
1564- """Decode 4D Morton codes using magic number bit manipulation.
1565-
1566- This extracts interleaved x,y,z,w coordinates from Morton codes using
1567- parallel bit operations instead of bit-by-bit loops.
1568- """
1569- # Convert to uint64 for bitwise operations with large masks
1570- z_u64 = z .astype (np .uint64 )
1571- out = np .zeros ((len (z ), 4 ), dtype = np .intp )
1572-
1573- for dim in range (4 ):
1574- x = (z_u64 >> dim ) & _MORTON_4D_MASKS [0 ]
1575- x = (x ^ (x >> 3 )) & _MORTON_4D_MASKS [1 ]
1576- x = (x ^ (x >> 6 )) & _MORTON_4D_MASKS [2 ]
1577- x = (x ^ (x >> 12 )) & _MORTON_4D_MASKS [3 ]
1578- x = (x ^ (x >> 24 )) & _MORTON_4D_MASKS [4 ]
1579- out [:, dim ] = x
1580-
1581- return out
1582-
1583-
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-
16051470def decode_morton_vectorized (
16061471 z : npt .NDArray [np .intp ], chunk_shape : tuple [int , ...]
16071472) -> npt .NDArray [np .intp ]:
@@ -1622,20 +1487,6 @@ def decode_morton_vectorized(
16221487 n_dims = len (chunk_shape )
16231488 bits = tuple ((c - 1 ).bit_length () for c in chunk_shape )
16241489
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.
1627- if len (set (bits )) == 1 : # All dimensions have same bit width
1628- max_bits = bits [0 ] if bits else 0
1629- if n_dims == 2 and max_bits <= 32 :
1630- return _decode_morton_2d (z )
1631- if n_dims == 3 and max_bits <= 21 :
1632- return _decode_morton_3d (z )
1633- if n_dims == 4 and max_bits <= 16 :
1634- return _decode_morton_4d (z )
1635- if n_dims == 5 and max_bits <= 12 :
1636- return _decode_morton_5d (z )
1637-
1638- # Fall back to generic bit-by-bit decoding
16391490 max_coords_bits = max (bits ) if bits else 0
16401491 out = np .zeros ((len (z ), n_dims ), dtype = np .intp )
16411492
0 commit comments