Numerics
Module
Defines utilities to work with numeric types.
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[type]()
The equivalent integer type of the float type.
Functions:
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.
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.
exponent_bias
exponent_bias() -> Int
Returns the exponent bias of a floating point type.
Returns:
The exponent bias.
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.
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_width
exponent_width() -> Int
Returns the exponent width of a floating point type.
Returns:
The exponent width.
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.
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.
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.
mantissa_mask
mantissa_mask() -> Int
Returns the mantissa mask of a floating point type.
Returns:
The mantissa mask.
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.
pack
pack(sign: Bool, exponent: Int, mantissa: Int) -> SIMD[type, 1]
Construct a floating-point value from its constituent sign, exponent, and mantisssa.
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.
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.
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.
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.
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.
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.
FlushDenormals
Flushes and denormals are set to zero within the context and the state is restored to the prior value on exit.
Fields:
state
The current state.
Functions:
__init__
__init__(self: Self&)
Initialize the FlushDenormals.
__enter__
__enter__(self: Self)
Enter the context. This will set denormals to zero.
__exit__
__exit__(self: Self)
Exit the context. This will restore the prior FPState.
inf
inf[type: DType]() -> SIMD[type, 1]
Get a +inf value for the given dtype.
Constraints:
Can only be used for FP dtypes.
Parameters:
- type (
DType
): The value dtype.
Returns:
The +inf value of the given dtype.
isfinite
isfinite[type: DType](val: scalar<#lit.struct.extract<:_"$DType"::_DType type, "value">>) -> Bool
Check if the value is finite.
This is always True for non-FP data types.
Parameters:
- type (
DType
): The value dtype.
Args:
- val (
scalar<#lit.struct.extract<:_"$DType"::_DType type, "value">>
): The value to check.
Returns:
True if val is finite and False otherwise.
isinf
isinf[type: DType](val: scalar<#lit.struct.extract<:_"$DType"::_DType type, "value">>) -> Bool
Check if the value is inf.
This is always False for non-FP data types.
Parameters:
- type (
DType
): The value dtype.
Args:
- val (
scalar<#lit.struct.extract<:_"$DType"::_DType type, "value">>
): The value to check.
Returns:
True if val is inf and False otherwise.
isnan
isnan[type: DType](val: scalar<#lit.struct.extract<:_"$DType"::_DType type, "value">>) -> Bool
Check if the value is NaN.
IEEE 754 says that only NaNs satisfy f != f.
Parameters:
- type (
DType
): The value dtype.
Args:
- val (
scalar<#lit.struct.extract<:_"$DType"::_DType type, "value">>
): The value to check.
Returns:
True if val is NaN and False otherwise.
nan
nan[type: DType]() -> SIMD[type, 1]
Get a NaN value for the given dtype.
Constraints:
Can only be used for FP dtypes.
Parameters:
- type (
DType
): The value dtype.
Returns:
The NaN value of the given dtype.
neginf
neginf[type: DType]() -> SIMD[type, 1]
Get a -inf value for the given dtype.
Constraints:
Can only be used for FP dtypes.
Parameters:
- type (
DType
): The value dtype.
Returns:
The -inf value of the given dtype.