Skip to main content
Log in

Mojo struct

FastDiv

@register_passable(trivial) struct FastDiv[type: 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.

Aliases

  • uint_type = _uint_type_of_width[::Int]():

Implemented traits

AnyType, UnknownDestructibility

Methods

__init__

@implicit __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: SIMD[_uint_type_of_width[::Int](), 1]) -> SIMD[_uint_type_of_width[::Int](), 1]

Divides the other scalar by the divisor (true division).

Uses the fast division algorithm.

Args:

  • other (SIMD[_uint_type_of_width[::Int](), 1]): The dividend.

Returns:

The result of the division.

__rmod__

__rmod__(self, other: SIMD[_uint_type_of_width[::Int](), 1]) -> SIMD[_uint_type_of_width[::Int](), 1]

Computes the remainder of division.

Args:

  • other (SIMD[_uint_type_of_width[::Int](), 1]): The dividend.

Returns:

The remainder.

__rdiv__

__rdiv__(self, other: SIMD[_uint_type_of_width[::Int](), 1]) -> SIMD[_uint_type_of_width[::Int](), 1]

Divides the other scalar by the divisor.

Args:

  • other (SIMD[_uint_type_of_width[::Int](), 1]): The dividend.

Returns:

The result of the division.

__divmod__

__divmod__(self, other: SIMD[_uint_type_of_width[::Int](), 1]) -> Tuple[SIMD[_uint_type_of_width[::Int](), 1], SIMD[_uint_type_of_width[::Int](), 1]]

Computes both quotient and remainder.

Args:

  • other (SIMD[_uint_type_of_width[::Int](), 1]): The dividend.

Returns:

A tuple containing the quotient and remainder.

Was this page helpful?