Modifica

Condividi tramite


BitHelper.HasLookupFlag Method

Definition

Overloads

HasLookupFlag(UInt32, Int32, Int32)

Checks whether or not a given bit is set in a given bitwise lookup table. This method provides a branchless, register-based (with no memory accesses) way to check whether a given value is valid, according to a precomputed lookup table. It is similar in behavior to HasFlag(UInt32, Int32), with the main difference being that this method will also validate the input x parameter, and will always return false if it falls outside of the expected interval. Additionally, this method accepts a min parameter, which is used to decrement the input parameter x to ensure that the range of accepted values fits within the available 32 bits of the lookup table in use. For more info on this optimization technique, see https://egorbo.com/llvm-range-checks.html. Here is how the code from the link above would be implemented using this method:

bool IsReservedCharacter(char c)
{
    return BitHelper.HasLookupFlag(314575237u, c, 36);
}

The resulted assembly is virtually identical, with the added optimization that the one produced by HasLookupFlag(UInt32, Int32, Int32) has no conditional branches at all.

HasLookupFlag(UInt64, Int32, Int32)

Checks whether or not a given bit is set in a given bitwise lookup table. For more info, check the XML docs of the HasLookupFlag(UInt32, Int32, Int32) overload.

HasLookupFlag(UInt32, Int32, Int32)

Checks whether or not a given bit is set in a given bitwise lookup table. This method provides a branchless, register-based (with no memory accesses) way to check whether a given value is valid, according to a precomputed lookup table. It is similar in behavior to HasFlag(UInt32, Int32), with the main difference being that this method will also validate the input x parameter, and will always return false if it falls outside of the expected interval. Additionally, this method accepts a min parameter, which is used to decrement the input parameter x to ensure that the range of accepted values fits within the available 32 bits of the lookup table in use. For more info on this optimization technique, see https://egorbo.com/llvm-range-checks.html. Here is how the code from the link above would be implemented using this method:

bool IsReservedCharacter(char c)
{
    return BitHelper.HasLookupFlag(314575237u, c, 36);
}

The resulted assembly is virtually identical, with the added optimization that the one produced by HasLookupFlag(UInt32, Int32, Int32) has no conditional branches at all.

public static bool HasLookupFlag (uint table, int x, int min = 0);
static member HasLookupFlag : uint32 * int * int -> bool
Public Shared Function HasLookupFlag (table As UInteger, x As Integer, Optional min As Integer = 0) As Boolean

Parameters

table
UInt32

The input lookup table to use.

x
Int32

The input value to check.

min
Int32

The minimum accepted value for x (defaults to 0).

Returns

Whether or not the corresponding flag for x is set in table.

Remarks

For best results, as shown in the sample code, both table and min should be compile-time constants, so that the JIT compiler will be able to produce more efficient code.

Applies to

HasLookupFlag(UInt64, Int32, Int32)

Checks whether or not a given bit is set in a given bitwise lookup table. For more info, check the XML docs of the HasLookupFlag(UInt32, Int32, Int32) overload.

public static bool HasLookupFlag (ulong table, int x, int min = 0);
static member HasLookupFlag : uint64 * int * int -> bool
Public Shared Function HasLookupFlag (table As ULong, x As Integer, Optional min As Integer = 0) As Boolean

Parameters

table
UInt64

The input lookup table to use.

x
Int32

The input value to check.

min
Int32

The minimum accepted value for x (defaults to 0).

Returns

Whether or not the corresponding flag for x is set in table.

Remarks

For best results, as shown in the sample code, both table and min should be compile-time constants, so that the JIT compiler will be able to produce more efficient code.

Applies to