Skip to content

feat: add element-wise left_shift and right_shift ops - #345

Open
dschulmeist wants to merge 1 commit into
oxiglade:mainfrom
dschulmeist:feat/shift-left-right
Open

feat: add element-wise left_shift and right_shift ops#345
dschulmeist wants to merge 1 commit into
oxiglade:mainfrom
dschulmeist:feat/shift-left-right

Conversation

@dschulmeist

Copy link
Copy Markdown

Closes #129.

Changes

Adds element-wise left_shift and right_shift to mlx-rs/src/ops/arithmetic.rs:

  • Array::left_shift / Array::left_shift_device and the matching free functions in ops::, plus the left_shift! macro generated by #[generate_macro].
  • Same set for 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 with left_shift / right_shift:

  • mlx-c exports mlx_left_shift / mlx_right_shift.
  • mlx-python uses mx.left_shift / mx.right_shift.
  • mlx-swift uses 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 returns Err.
  • 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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add shift_left and shift_right methods

1 participant