Skip to content

Commit

Permalink
[stdlib] Add __rfloordiv__ to SIMD
Browse files Browse the repository at this point in the history
The __rfloordiv__() method is already supported by Int and Object.
The built-in SIMD includes support for reverse division operators,
specifically __rtruediv__() and __rmod__(). Furthermore, the SIMD
library has been extended to also support __rfloordiv__().

Fixes: #2415

Signed-off-by: Peyman Barazandeh <peymanb@gmail.com>
  • Loading branch information
Peyman Barazandeh authored and peymanbr committed May 3, 2024
1 parent a936693 commit c867d15
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
16 changes: 16 additions & 0 deletions stdlib/src/builtin/simd.mojo
Expand Up @@ -1232,6 +1232,22 @@ struct SIMD[type: DType, size: Int = simdwidthof[type]()](
constrained[type.is_numeric(), "the SIMD type must be numeric"]()
return value / self

@always_inline("nodebug")
fn __rfloordiv__(self, value: Self) -> Self:
"""Returns value / self rounded down to the nearest integer.
# Constraints:
The element type of the SIMD vector must be numeric.
Args:
value: The value that is diveded to self.
Returns:
`floor(value // self)`.
"""
constrained[type.is_numeric(), "the type must be numeric"]()
return value // self

# TODO: Move to global function.
@always_inline("nodebug")
fn fma(self, multiplier: Self, accumulator: Self) -> Self:
Expand Down
18 changes: 18 additions & 0 deletions stdlib/test/builtin/test_simd.mojo
Expand Up @@ -222,6 +222,24 @@ def test_floordiv():
assert_equal(Float32(2) // Float32(-2), -1)
assert_equal(Float32(99) // Float32(-2), -50)

assert_equal(
Int32(2) // SIMD[DType.int32, 4](2, 4, -2, -4),
SIMD[DType.int32, 4](1, 0, -1, -1),
)
assert_equal(
2 // SIMD[DType.int32, 4](2, 4, -2, -4),
SIMD[DType.int32, 4](1, 0, -1, -1),
)

assert_equal(
Float32(3) // SIMD[DType.float32, 4](3, -4, 1, 5),
SIMD[DType.float32, 4](1, -1, 3, 0),
)
assert_equal(
3 // SIMD[DType.float32, 4](3, -4, 1, 5),
SIMD[DType.float32, 4](1, -1, 3, 0),
)


def test_mod():
assert_equal(Int32(99) % Int32(1), 0)
Expand Down

0 comments on commit c867d15

Please sign in to comment.