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

cast_fp_to_fp4e2m1

def cast_fp_to_fp4e2m1[dtype: DType, width: SIMDLength, //](x: SIMD[dtype, width]) -> SIMD[dtype, width]

Rounds each floating-point element to the nearest FP4 E2M1 representable value.

Implements nearest-even rounding into the 16 representable FP4 E2M1 values: {±0, ±0.5, ±1, ±1.5, ±2, ±3, ±4, ±6}. Values outside [−6, 6] saturate to ±6. The result is returned in the original dtype, not packed as nibbles.

Parameters:

  • dtype (DType): Floating-point element type (float32, bfloat16, or float16).
  • width (SIMDLength): SIMD lane count.

Args:

Returns:

SIMD[dtype, width]: Vector of the same dtype with each element rounded to the nearest FP4 E2M1 value.

Was this page helpful?