IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /max/get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /max/get-started.md).

Mojo function

decode_e2m1_to_bf16

def decode_e2m1_to_bf16[width: SIMDLength, //](nibble: SIMD[DType.uint16, width]) -> SIMD[DType.bfloat16, width]

Decodes E2M1 nibbles to bfloat16 with branch-free bit arithmetic.

Maps each 4-bit E2M1 value (s e1 e0 m0: bit3 sign, bits2:1 exponent, bit0 mantissa) directly to a bfloat16 bit pattern using shifts / masks / select, with no data-dependent table gather. This is the vectorizable equivalent of indexing E2M1_TO_FLOAT32 and is bit-identical to it: all 16 E2M1 values ({+-0, +-0.5, +-1, +-1.5, +-2, +-3, +-4, +-6}) are exactly representable in bfloat16, so the constructed bit pattern equals the table entry exactly.

Construction (bf16 layout s | 8-bit exp (bias 127) | 7-bit mantissa): let E = bits2:1 and m = bit0.

  • Normal (E >= 1): value 2^(E-1) * (1 + m/2), so exponent field E + 126 and mantissa MSB m (mantissa field m << 6).
  • Subnormal-class (E == 0): value 0.5 * m, i.e. bf16 0x3F00 (+0.5) when m == 1, else 0x0000 (+0.0). Selected branch-free.
  • Sign bit (s) is OR'd into bit 15, so -0 (nibble 8) maps to bf16 0x8000 exactly as the table's -0.0 entry.

Parameters:

  • width (SIMDLength): SIMD width (lane count) of the nibble vector.

Args:

Returns:

SIMD[DType.bfloat16, width]: The decoded values as SIMD[DType.bfloat16, width], bit-identical to casting E2M1_TO_FLOAT32[nibble] to bfloat16.