Livt packages

Eccelerators.BitTwiddling

Version 0.1.1

Readable 32-bit bit manipulation, counting, scanning, permutation, and arithmetic utilities for Livt.

Published files (26)

src/permutation/MortonCodes.lvt

namespace Eccelerators.BitTwiddling.Permutation

/**
 * Creates two-dimensional Morton, or Z-order, codes from integer coordinates.
 *
 * Morton codes preserve spatial locality by interleaving coordinate bits into
 * one word. This class implements the common 16-bit-coordinate to 32-bit-code
 * form.
 */
class MortonCodes
{
    /**
     * Interleaves the low 16 bits of xValue and yValue into a 32-bit Morton code.
     *
     * x bit n is written to result bit 2*n. y bit n is written to result bit
     * 2*n + 1. Input bits above bit 15 are ignored.
     */
    public static fn Interleave16[](xValue: uint, yValue: uint) uint
    {
        var x: uint = xValue & (0x0000FFFF as uint)
        var y: uint = yValue & (0x0000FFFF as uint)

        x = (x | (x << 8)) & (0x00FF00FF as uint)
        x = (x | (x << 4)) & (0x0F0F0F0F as uint)
        x = (x | (x << 2)) & (0x33333333 as uint)
        x = (x | (x << 1)) & (0x55555555 as uint)

        y = (y | (y << 8)) & (0x00FF00FF as uint)
        y = (y | (y << 4)) & (0x0F0F0F0F as uint)
        y = (y | (y << 2)) & (0x33333333 as uint)
        y = (y | (y << 1)) & (0x55555555 as uint)

        return x | (y << 1)
    }
}