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): value2^(E-1) * (1 + m/2), so exponent fieldE + 126and mantissa MSBm(mantissa fieldm << 6). - Subnormal-class (
E == 0): value0.5 * m, i.e. bf160x3F00(+0.5) whenm == 1, else0x0000(+0.0). Selected branch-free. - Sign bit (
s) is OR'd into bit 15, so-0(nibble 8) maps to bf160x8000exactly as the table's-0.0entry.
Parameters:
- width (
SIMDLength): SIMD width (lane count) of the nibble vector.
Args:
- nibble (
SIMD[DType.uint16, width]): One E2M1 nibble per lane in the low 4 bits (0..15).
Returns:
SIMD[DType.bfloat16, width]: The decoded values as SIMD[DType.bfloat16, width], bit-identical to
casting E2M1_TO_FLOAT32[nibble] to bfloat16.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!