From a0e5eee397698a14de9208591d7e3abbdbd2fdd3 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Thu, 26 Sep 2024 17:20:20 -0400 Subject: [PATCH 1/3] add SIMD.reversed() Signed-off-by: martinvuyk --- .gitignore | 2 ++ stdlib/src/builtin/simd.mojo | 25 +++++++++++++++++++++++++ stdlib/test/builtin/test_simd.mojo | 16 ++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/.gitignore b/.gitignore index 7beaae8a60..1917d2257b 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,8 @@ venv/ ENV/ env.bak/ venv.bak/ +.magic/ +magic.lock # MacOS .DS_Store diff --git a/stdlib/src/builtin/simd.mojo b/stdlib/src/builtin/simd.mojo index b316e111a0..baeaf64792 100644 --- a/stdlib/src/builtin/simd.mojo +++ b/stdlib/src/builtin/simd.mojo @@ -2741,6 +2741,31 @@ struct SIMD[type: DType, size: Int]( "llvm.vector.splice", Self, has_side_effect=False ](zero_simd, self, Int32(-shift)) + fn reversed(self) -> Self: + """Reverses the SIMD vector by indexes. + + Returns: + The by index reversed vector. + + Examples: + ```mojo + print(SIMD[DType.uint8, 4](1, 2, 3, 4).reversed()) # [4, 3, 2, 1] + ``` + . + """ + fn build_idx() -> StaticIntTuple[size]: + var values = StaticIntTuple[size]() + var idx = 0 + + @parameter + for i in range(size - 1, -1, -1): + values[idx] = i + idx += 1 + return values + + alias idx = build_idx() + return self.shuffle[idx]() + fn _pshuf_or_tbl1( lookup_table: SIMD[DType.uint8, 16], indices: SIMD[DType.uint8, 16] diff --git a/stdlib/test/builtin/test_simd.mojo b/stdlib/test/builtin/test_simd.mojo index ca4d5223fd..67a91d9bf0 100644 --- a/stdlib/test/builtin/test_simd.mojo +++ b/stdlib/test/builtin/test_simd.mojo @@ -1777,6 +1777,21 @@ def test_float_conversion(): assert_almost_equal(float(Float32(34.32)), 34.32) assert_almost_equal(float(UInt64(36)), 36.0) +def test_reversed(): + fn test[D: DType]() raises: + assert_equal(SIMD[D, 4](1, 2, 3, 4).reversed(), SIMD[D, 4](4, 3, 2, 1)) + + test[DType.uint8]() + test[DType.uint16]() + test[DType.uint32]() + test[DType.uint64]() + test[DType.int8]() + test[DType.int16]() + test[DType.int32]() + test[DType.int64]() + test[DType.float16]() + test[DType.float32]() + test[DType.float64]() def main(): test_abs() @@ -1832,4 +1847,5 @@ def main(): test_contains() test_comparison() test_float_conversion() + test_reversed() # TODO: add tests for __and__, __or__, anc comparison operators From 039d4f52e3af63924439e775574856f73118eb5b Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Thu, 26 Sep 2024 17:24:50 -0400 Subject: [PATCH 2/3] mojo format Signed-off-by: martinvuyk --- stdlib/src/builtin/simd.mojo | 7 ++++--- stdlib/test/builtin/test_simd.mojo | 4 +++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/stdlib/src/builtin/simd.mojo b/stdlib/src/builtin/simd.mojo index baeaf64792..d92cdc627b 100644 --- a/stdlib/src/builtin/simd.mojo +++ b/stdlib/src/builtin/simd.mojo @@ -2743,16 +2743,17 @@ struct SIMD[type: DType, size: Int]( fn reversed(self) -> Self: """Reverses the SIMD vector by indexes. - + Returns: The by index reversed vector. - + Examples: ```mojo print(SIMD[DType.uint8, 4](1, 2, 3, 4).reversed()) # [4, 3, 2, 1] ``` . """ + fn build_idx() -> StaticIntTuple[size]: var values = StaticIntTuple[size]() var idx = 0 @@ -2762,7 +2763,7 @@ struct SIMD[type: DType, size: Int]( values[idx] = i idx += 1 return values - + alias idx = build_idx() return self.shuffle[idx]() diff --git a/stdlib/test/builtin/test_simd.mojo b/stdlib/test/builtin/test_simd.mojo index 67a91d9bf0..e1890cb714 100644 --- a/stdlib/test/builtin/test_simd.mojo +++ b/stdlib/test/builtin/test_simd.mojo @@ -1777,10 +1777,11 @@ def test_float_conversion(): assert_almost_equal(float(Float32(34.32)), 34.32) assert_almost_equal(float(UInt64(36)), 36.0) + def test_reversed(): fn test[D: DType]() raises: assert_equal(SIMD[D, 4](1, 2, 3, 4).reversed(), SIMD[D, 4](4, 3, 2, 1)) - + test[DType.uint8]() test[DType.uint16]() test[DType.uint32]() @@ -1793,6 +1794,7 @@ def test_reversed(): test[DType.float32]() test[DType.float64]() + def main(): test_abs() test_add() From 1f4486c68315e3d11b47079afd620da451dbf20d Mon Sep 17 00:00:00 2001 From: martinvuyk <110240700+martinvuyk@users.noreply.github.com> Date: Mon, 30 Sep 2024 11:51:01 -0400 Subject: [PATCH 3/3] Update stdlib/src/builtin/simd.mojo Co-authored-by: Joshua James Venter Signed-off-by: martinvuyk <110240700+martinvuyk@users.noreply.github.com> --- stdlib/src/builtin/simd.mojo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/src/builtin/simd.mojo b/stdlib/src/builtin/simd.mojo index d92cdc627b..04d254fc4e 100644 --- a/stdlib/src/builtin/simd.mojo +++ b/stdlib/src/builtin/simd.mojo @@ -2759,7 +2759,7 @@ struct SIMD[type: DType, size: Int]( var idx = 0 @parameter - for i in range(size - 1, -1, -1): + for i in reversed(range(size)): values[idx] = i idx += 1 return values