numerics

Module

Defines utilities to work with numeric types.

You can import these APIs from the math package. For example:

from math.numeric import FPUtils

FPUtils

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).

Aliases:

  • integral_type = _integral_type_of[$builtin::$dtype::DType][_142x16_type](): The equivalent integer type of the float type.

Functions:

mantissa_width

mantissa_width() -> Int

Returns the mantissa width of a floating point type.

Returns:

The mantissa width.

max_exponent

max_exponent() -> Int

Returns the max exponent of a floating point type.

Returns:

The max exponent.

exponent_width

exponent_width() -> Int

Returns the exponent width of a floating point type.

Returns:

The exponent width.

mantissa_mask

mantissa_mask() -> Int

Returns the mantissa mask of a floating point type.

Returns:

The mantissa mask.

exponent_bias

exponent_bias() -> Int

Returns the exponent bias of a floating point type.

Returns:

The exponent bias.

sign_mask

sign_mask() -> Int

Returns the sign mask of a floating point type. It is computed by 1 << (exponent_width + mantissa_mask).

Returns:

The sign mask.

exponent_mask

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

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

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)

Returns:

The quiet NaN mask.

bitcast_to_integer

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_from_integer

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

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

Returns the sign of the floating point value. True if the sign is set and False otherwise.

Args:

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

Returns:

Returns True if the sign is set and False otherwise.

set_sign

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

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_without_bias

get_exponent_without_bias(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.

set_exponent

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

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.

set_mantissa

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

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.

FlushDenormals

Flushes and denormals are set to zero within the context and the state is restored to the prior value on exit.

Fields:

  • state (SIMD[si32, 1]): The current state.

Functions:

__init__

__init__(inout self: Self)

Initializes the FlushDenormals.

__enter__

__enter__(self: Self)

Enters the context. This will set denormals to zero.

__exit__

__exit__(self: Self)

Exits the context. This will restore the prior FPState.