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

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.
@dcvz

dcvz commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator

Sorry for the long silence here. We looked at this and hit a snag: MLX itself doesn't validate shift counts, so negative or >= bit-width counts are C++ UB and the results are inconsistent across dtypes (int32 wraps the count, uint8 truncates). We don't want to freeze that into the API, so we're holding shift wrappers until upstream defines the behavior — leaving this open to track it.

@dcvz

dcvz commented Oct 31, 2026

Copy link
Copy Markdown
Collaborator

Quick update: upstream fixed this in ml-explore/mlx#4694 (merged 10-29). Out-of-range counts now give 0, or -1 for a negative value shifted right, like numpy and jax. It isn't in an MLX release yet, so we'll pick this up once our pinned MLX includes it. Main has moved a fair bit since April (new ops don't get generate_macro or _device variants anymore), so I'm happy to rework it on top of your commit and add tests for the out-of-range cases when we get there, unless you'd rather do it yourself.

This branch has not been deployed

No deployments
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

2 participants