Mojo struct
FastDiv
@register_passable(trivial)
struct FastDiv[dtype: DType]
Implements fast division for a given type.
This struct provides optimized division by a constant divisor, replacing the division operation with a series of shifts and multiplications. This approach significantly improves performance, especially in scenarios where division is a frequent operation.
Implemented traits
AnyType
,
ExplicitlyCopyable
,
ImplicitlyCopyable
,
Movable
,
Stringable
,
UnknownDestructibility
,
Writable
Aliases
__copyinit__is_trivial
alias __copyinit__is_trivial = True
__del__is_trivial
alias __del__is_trivial = True
__moveinit__is_trivial
alias __moveinit__is_trivial = True
uint_type
alias uint_type = _uint_type_of_width[bit_width_of[dtype]()]()
Methods
__init__
__init__(divisor: Int = 1) -> Self
Initializes FastDiv with the divisor.
Constraints:
ConstraintError: If the bitwidth of the type is > 32.
Args:
- divisor (
Int
): The divisor to use for fast division. Defaults to 1.
__rtruediv__
__rtruediv__(self, other: Scalar[_uint_type_of_width[bit_width_of[dtype]()]()]) -> Scalar[_uint_type_of_width[bit_width_of[dtype]()]()]
Divides the other scalar by the divisor (true division).
Uses the fast division algorithm, with optimized path for power-of-2 divisors.
Args:
- other (
Scalar
): The dividend.
Returns:
Scalar
: The result of the division.
__rmod__
__rmod__(self, other: Scalar[_uint_type_of_width[bit_width_of[dtype]()]()]) -> Scalar[_uint_type_of_width[bit_width_of[dtype]()]()]
Computes the remainder of division.
Args:
- other (
Scalar
): The dividend.
Returns:
Scalar
: The remainder.
__rdiv__
__rdiv__(self, other: Scalar[_uint_type_of_width[bit_width_of[dtype]()]()]) -> Scalar[_uint_type_of_width[bit_width_of[dtype]()]()]
Divides the other scalar by the divisor.
Args:
- other (
Scalar
): The dividend.
Returns:
Scalar
: The result of the division.
__divmod__
__divmod__(self, other: Scalar[_uint_type_of_width[bit_width_of[dtype]()]()]) -> Tuple[Scalar[_uint_type_of_width[bit_width_of[dtype]()]()], Scalar[_uint_type_of_width[bit_width_of[dtype]()]()]]
Computes both quotient and remainder.
Args:
- other (
Scalar
): The dividend.
Returns:
Tuple
: A tuple containing the quotient and remainder.
write_to
write_to[W: Writer](self, mut writer: W)
Writes the FastDiv parameters to a writer.
Args:
- writer (
W
): The writer to which the parameters are written.
__str__
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!