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/bytes/BytePredicates.lvt

namespace Eccelerators.BitTwiddling.Bytes

/**
 * Provides parallel predicates and counts over the four bytes of a 32-bit word.
 *
 * Each helper treats value as four independent unsigned bytes without relying
 * on pointer aliasing or host byte order.
 */
class BytePredicates
{
    /**
     * Returns true when at least one of value's four bytes equals zero.
     */
    public static fn HasZeroByte[](value: uint) bool
    {
        return ((value & (0xFF as uint)) == (0 as uint)) ||
            (((value >> 8) & (0xFF as uint)) == (0 as uint)) ||
            (((value >> 16) & (0xFF as uint)) == (0 as uint)) ||
            (((value >> 24) & (0xFF as uint)) == (0 as uint))
    }

    /**
     * Returns true when at least one byte in value equals expected.
     */
    public static fn HasByteEqual[](value: uint, expected: byte) bool
    {
        var expectedValue: uint = expected as uint
        return ((value & (0xFF as uint)) == expectedValue) ||
            (((value >> 8) & (0xFF as uint)) == expectedValue) ||
            (((value >> 16) & (0xFF as uint)) == expectedValue) ||
            (((value >> 24) & (0xFF as uint)) == expectedValue)
    }

    /**
     * Counts zero-valued bytes in value and returns a result from 0 through 4.
     */
    public static fn CountZeroBytes[](value: uint) int
    {
        var count: int = 0
        if ((value & (0xFF as uint)) == (0 as uint))
        {
            count = count + 1
        }
        if (((value >> 8) & (0xFF as uint)) == (0 as uint))
        {
            count = count + 1
        }
        if (((value >> 16) & (0xFF as uint)) == (0 as uint))
        {
            count = count + 1
        }
        if (((value >> 24) & (0xFF as uint)) == (0 as uint))
        {
            count = count + 1
        }
        return count
    }
}