Bit Intrinsics

Bit intrinsics are functions that map as closely as possible to behavior and functionality in ISAs without needing to deal with the undefined behavior and non-portability of said architectures. It provides vital functionality that can greatly speed up work on specific kinds of bit operations. The provided intrinsics here are a large subset of the most efficient operations, offered in various flavors for ease-of-use.

β€œLeading” refers to the most significant bit in a given value. This is the β€œleft side” of an integer when writing source code, such that 0b10 has a most significant bit of 1. β€œTrailing” refers to the least significant bit in a given value. This is the β€œleft side” of an integer when writing source code, such that 0b10 has a least significant bit of 0.

ztdc_count_ones(...)

Counts the number of ones in a given unsigned integer.

Parameters
  • ... – [in] The input value.

Returns

An int (or suitably large signed integer type) with the count.

ztdc_count_zeros(...)

Counts the number of zeros in a given unsigned integer.

Parameters
  • ... – [in] The input value.

Returns

An int (or suitably large signed integer type) with the count.

ztdc_count_leading_zeros(...)

Counts the number of leading zeros in a given unsigned integer.

Parameters
  • ... – [in] The input value.

Returns

An int (or suitably large signed integer type) with the count.

ztdc_count_trailing_zeros(...)

Counts the number of trailing zeros in a given unsigned integer.

Parameters
  • ... – [in] The input value.

Returns

An int (or suitably large signed integer type) with the count.

ztdc_count_leading_ones(...)

Counts the number of leading ones in a given unsigned integer.

Parameters
  • ... – [in] The input value.

Returns

An int (or suitably large signed integer type) with the count.

ztdc_count_trailing_ones(...)

Counts the number of trailing ones in a given unsigned integer.

Parameters
  • ... – [in] The input value.

Returns

An int (or suitably large signed integer type) with the count.

ztdc_first_leading_zero(...)

Finds the first trailing zero in a given unsigned integer value.

Parameters
  • ... – [in] The input value.

Returns

If the bit is not found, returns 0. Otherwise, returns an int (or suitably large enough signed integer) indicating the index of the found bit, plus one.

ztdc_first_trailing_zero(...)

Finds the first trailing zero in a given unsigned integer value.

Parameters
  • ... – [in] The input value.

Returns

If the bit is not found, returns 0. Otherwise, returns an int (or suitably large enough signed integer) indicating the index of the found bit, plus one.

ztdc_first_leading_one(...)

Finds the first leading one in a given unsigned integer value.

Parameters
  • ... – [in] The input value.

Returns

If the bit is not found, returns 0. Otherwise, returns an int (or suitably large enough signed integer) indicating the index of the found bit, plus one.

ztdc_first_trailing_one(...)

Finds the first trailing one in a given unsigned integer value.

Parameters
  • ... – [in] The input value.

Returns

If the bit is not found, returns 0. Otherwise, returns an int (or suitably large enough signed integer) indicating the index of the found bit, plus one.

ztdc_rotate_left(_VALUE, ...)

Performs a cyclical shift left.

Remark

If the rotation value is negative, calls ztdc_rotate_right with the negated modulus of the rotation.

Parameters
  • _VALUE – [in] The value to perform the cyclical shift left.

  • ... – [in] The rotation value.

ztdc_rotate_right(_VALUE, ...)

Performs a cyclical shift right.

Remark

If the rotation value is negative, calls ztdc_rotate_right with the negated modulus of the rotation.

Parameters
  • _VALUE – [in] The value to perform the cyclical shift right.

  • ... – [in] The rotation value.

ztdc_has_single_bit(...)

Returns whether or not there is a single bit set in this unsigned integer value (this making it a power of 2).

Parameters
  • ... – [in] The input value.

ztdc_bit_width(...)

Returns the number of bits needed to represent the value exactly.

Parameters
  • ... – [in] The input value.

ztdc_bit_ceil(...)

Returns the value that is the greatest power of 2 that is less than the input value.

Parameters
  • ... – [in] The input value.

Returns

0 when the input value is 0. Otherwise, produces the greatest power of 2 that is less than the input value.

ztdc_bit_floor(...)

Returns the value that is the next power of 2.

Parameters
  • ... – [in] The input value.

Returns

1 when the input value is less than or equal to 1. Otherwise, produces the power of 2 that is higher than the input value.