Skip to content

Commit

Permalink
Add unit tests for the SIMD __rfloordiv__() method
Browse files Browse the repository at this point in the history
The support for __rfloordiv__() with SIMD types was added in:
f5aee40

This commit adds:
  - unit tests for __rfloordiv__() in SIMD types.
  - the constraint check to the method's implementation.

Signed-off-by: Peyman Barazandeh <peymanb@gmail.com>
  • Loading branch information
peymanbr committed May 7, 2024
1 parent 80f0774 commit 4a432f5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions stdlib/src/builtin/simd.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ struct SIMD[type: DType, size: Int = simdwidthof[type]()](
Returns:
`floor(rhs / self)` value.
"""
constrained[type.is_numeric(), "the type must be numeric"]()
return rhs // self

@always_inline("nodebug")
Expand Down
13 changes: 13 additions & 0 deletions stdlib/test/builtin/test_simd.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ def test_floordiv():
assert_equal(Float32(99) // Float32(-2), -50)


def test_rfloordiv():
alias I = SIMD[DType.int32, 4]
var i = I(2, 4, -2, -4)
assert_equal(i.__rfloordiv__(2), I(1, 0, -1, -1))
assert_equal(i.__rfloordiv__(Int32(2)), I(1, 0, -1, -1))

alias F = SIMD[DType.float32, 4]
var f = F(3, -4, 1, 5)
assert_equal(f.__rfloordiv__(3), F(1, -1, 3, 0))
assert_equal(f.__rfloordiv__(Float32(3)), F(1, -1, 3, 0))


def test_mod():
assert_equal(Int32(99) % Int32(1), 0)
assert_equal(Int32(99) % Int32(3), 0)
Expand Down Expand Up @@ -873,6 +885,7 @@ def main():
test_round()
test_roundeven()
test_floordiv()
test_rfloordiv()
test_mod()
test_rotate()
test_shift()
Expand Down

0 comments on commit 4a432f5

Please sign in to comment.