Skip to main content
Log in

Mojo struct

FPUtils

struct FPUtils[type: DType, *, _constraint: NoneType = NoneType(_constrain_fp_type[::DType]())]

Collection of utility functions for working with FP values.

Constraints:

The type is floating point.

Parameters

  • type (DType): The concrete FP dtype (FP32/FP64/etc).
  • _constraint (NoneType): Implements the constraint. Do not pass explicitly.

Aliases

  • integral_type = _integral_type_of[::DType](): The equivalent integer type of the float type.
  • uint_type = _uint_type_of[::DType](): The equivalent uint type of the float type.

Implemented traits

AnyType, UnknownDestructibility

Methods

mantissa_width

static mantissa_width() -> IntLiteral

Returns the mantissa width of a floating point type.

Returns:

The mantissa width.

max_exponent

static max_exponent() -> IntLiteral

Returns the max exponent of a floating point type without accounting for inf representations. This is not the maximum representable exponent, which is generally equal to the exponent_bias.

Returns:

The max exponent.

exponent_width

static exponent_width() -> IntLiteral

Returns the exponent width of a floating point type.

Returns:

The exponent width.

mantissa_mask

static mantissa_mask() -> Int

Returns the mantissa mask of a floating point type.

Returns:

The mantissa mask.

exponent_bias

static exponent_bias() -> IntLiteral

Returns the exponent bias of a floating point type.

Returns:

The exponent bias.

sign_mask

static sign_mask() -> Int

Returns the sign mask of a floating point type.

It is computed by 1 << (exponent_width + mantissa_width).

Returns:

The sign mask.

exponent_mask

static exponent_mask() -> Int

Returns the exponent mask of a floating point type.

It is computed by ~(sign_mask | mantissa_mask).

Returns:

The exponent mask.

exponent_mantissa_mask

static exponent_mantissa_mask() -> Int

Returns the exponent and mantissa mask of a floating point type.

It is computed by exponent_mask | mantissa_mask.

Returns:

The exponent and mantissa mask.

quiet_nan_mask

static quiet_nan_mask() -> Int

Returns the quiet NaN mask for a floating point type.

The mask is defined by evaluating:

(1<<exponent_width-1)<<mantissa_width + 1<<(mantissa_width-1)
(1<<exponent_width-1)<<mantissa_width + 1<<(mantissa_width-1)

Returns:

The quiet NaN mask.

bitcast_to_integer

static bitcast_to_integer(value: SIMD[type, 1]) -> Int

Bitcasts the floating-point value to an integer.

Args:

  • value (SIMD[type, 1]): The floating-point type.

Returns:

An integer representation of the floating-point value.

bitcast_to_uint

static bitcast_to_uint(value: SIMD[type, 1]) -> SIMD[_uint_type_of[::DType](), 1]

Bitcasts the floating-point value to an integer.

Args:

  • value (SIMD[type, 1]): The floating-point type.

Returns:

An integer representation of the floating-point value.

bitcast_from_integer

static bitcast_from_integer(value: Int) -> SIMD[type, 1]

Bitcasts the floating-point value from an integer.

Args:

  • value (Int): The int value.

Returns:

An floating-point representation of the Int.

get_sign

static get_sign(value: SIMD[type, 1]) -> Bool

Returns the sign of the floating point value.

Args:

  • value (SIMD[type, 1]): The floating-point type.

Returns:

Returns True if the sign is set and False otherwise.

set_sign

static set_sign(value: SIMD[type, 1], sign: Bool) -> SIMD[type, 1]

Sets the sign of the floating point value.

Args:

  • value (SIMD[type, 1]): The floating-point value.
  • sign (Bool): True to set the sign and false otherwise.

Returns:

Returns the floating point value with the sign set.

get_exponent

static get_exponent(value: SIMD[type, 1]) -> Int

Returns the exponent bits of the floating-point value.

Args:

  • value (SIMD[type, 1]): The floating-point value.

Returns:

Returns the exponent bits.

get_exponent_biased

static get_exponent_biased(value: SIMD[type, 1]) -> Int

Returns the biased exponent of the floating-point value as an Int, this is how the value is stored before subtracting the exponent bias.

Args:

  • value (SIMD[type, 1]): The floating-point value.

Returns:

The biased exponent as an Int.

set_exponent

static set_exponent(value: SIMD[type, 1], exponent: Int) -> SIMD[type, 1]

Sets the exponent bits of the floating-point value.

Args:

  • value (SIMD[type, 1]): The floating-point value.
  • exponent (Int): The exponent bits.

Returns:

Returns the floating-point value with the exponent bits set.

get_mantissa

static get_mantissa(value: SIMD[type, 1]) -> Int

Gets the mantissa bits of the floating-point value.

Args:

  • value (SIMD[type, 1]): The floating-point value.

Returns:

The mantissa bits.

get_mantissa_uint

static get_mantissa_uint(value: SIMD[type, 1]) -> SIMD[_uint_type_of[::DType](), 1]

Gets the mantissa bits of the floating-point value.

Args:

  • value (SIMD[type, 1]): The floating-point value.

Returns:

The mantissa bits.

set_mantissa

static set_mantissa(value: SIMD[type, 1], mantissa: Int) -> SIMD[type, 1]

Sets the mantissa bits of the floating-point value.

Args:

  • value (SIMD[type, 1]): The floating-point value.
  • mantissa (Int): The mantissa bits.

Returns:

Returns the floating-point value with the mantissa bits set.

pack

static pack(sign: Bool, exponent: Int, mantissa: Int) -> SIMD[type, 1]

Construct a floating-point value from its constituent sign, exponent, and mantissa.

Args:

  • sign (Bool): The sign of the floating-point value.
  • exponent (Int): The exponent of the floating-point value.
  • mantissa (Int): The mantissa of the floating-point value.

Returns:

Returns the floating-point value.