to_bytes

Mojo function 🡭

to_bytes

fn def to_bytes[dtype: DType, //, output_size: Int, input_size: Int, endian: Endian = Endian.native()](input: SIMD[dtype, input_size]) -> Array[UInt8, output_size]

Reinterpret a SIMD vector as its output_size bytes.

SIMD.as_bytes is typed Array[UInt8, size_of[Self]()], a size expression that is not folded at parse time, so the result has to be rebound to the caller’s output_size. The size equality is checked here rather than in a where clause: a where clause must be proven where the call is written, and size_of cannot be evaluated while the lane count is still symbolic — a generic caller such as _reverse[SIZE] would be rejected even though every concrete instantiation is sound.

Parameters:

  • dtype (DType): The lane type of the input vector.
  • output_size (Int): The length of the returned byte array.
  • input_size (Int): The number of lanes in the input vector.
  • endian (Endian): The byte order to emit each lane in; defaults to the target’s own, matching a raw memory reinterpretation.

Args:

  • input (SIMD[dtype, input_size]): The vector to reinterpret.

Returns:

Array[UInt8, output_size]: The output_size bytes of input.