feat: add element-wise left_shift and right_shift ops - #345
Open
dschulmeist wants to merge 1 commit into
Open
Conversation
Adds Array methods, free functions, and macros for element-wise left_shift and right_shift, mirroring the pattern of the existing arithmetic ops. Both inputs must be of integer dtype; right shift on signed integers is arithmetic (sign-preserving), matching the upstream MLX behaviour. Includes tests for broadcast, signed shift, and incompatible-shape error paths.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #129.
Changes
Adds element-wise
left_shiftandright_shifttomlx-rs/src/ops/arithmetic.rs:Array::left_shift/Array::left_shift_deviceand the matching free functions inops::, plus theleft_shift!macro generated by#[generate_macro].right_shift.Both operands must be of integer dtype. Right shift on signed integers is arithmetic (sign-preserving), matching upstream MLX, mlx-swift, and numpy semantics.
Naming
The issue title is
shift_left/shift_right, but I went withleft_shift/right_shift:mlx_left_shift/mlx_right_shift.mx.left_shift/mx.right_shift.leftShift/rightShift.Happy to rename if the maintainers prefer the issue's wording.
Testing
Six new unit tests cover the public surface (
cargo test -p mlx-rs --lib left_shift right_shift):test_left_shift—[1,2,4,8] << [1,1,2,3] == [2,4,16,64]and inputs unchanged.test_left_shift_broadcast— scalar broadcast (free function form).test_left_shift_invalid_broadcast— shape mismatch returnsErr.test_right_shift—[16,32,64,128] >> [1,2,3,4] == [8,8,8,8].test_right_shift_arithmetic_for_signed—[-8,-16,-32] >> 1 == [-4,-8,-16].test_right_shift_invalid_broadcast.Doc examples for both methods compile and run via
cargo test -p mlx-rs --doc.