Mojo function
pack_bits
pack_bits[src_width: Int, //, dtype: DType = ui1 if (src_width == 1) else ui2 if (src_width == 2) else ui4 if (src_width == 4) else uint8 if (src_width == 8) else uint16 if (src_width == 16) else uint32 if (src_width == 32) else uint64 if (src_width == 64) else ui128 if (src_width == 128) else ui256 if (src_width == 256) else invalid, width: Int = 1](val: SIMD[bool, src_width]) -> SIMD[dtype, width]
Packs a SIMD vector of bool
values into an integer.
Examples:
This example packs a vector of 8 bool
values into a single 8-bit integer.
from memory import pack_bits
bits = SIMD[DType.bool, 8](1, 1, 0, 1, 0, 0, 0, 0)
u8 = pack_bits[DType.uint8](bits)
print(bits, u8) # [True, True, False, True, False, False, False, False] 11
from memory import pack_bits
bits = SIMD[DType.bool, 8](1, 1, 0, 1, 0, 0, 0, 0)
u8 = pack_bits[DType.uint8](bits)
print(bits, u8) # [True, True, False, True, False, False, False, False] 11
Constraints:
The logical bitwidth of the bool vector must be the same as the bitwidth of the target type. The target type must be a unsigned type.
Parameters:
- src_width (
Int
): The source width. - dtype (
DType
): The target type. - width (
Int
): The target width.
Args:
- val (
SIMD[bool, src_width]
): The source value.
Returns:
A new integer scalar which has the same bitwidth as the bool vector.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!