Skip to content

Commit

Permalink
feat: Add __ilshift__ numpy frontend (ivy-llc#24959)
Browse files Browse the repository at this point in the history
Co-authored-by: ZoeCD <[email protected]>
  • Loading branch information
PushpamKJha and ZoeCD authored Sep 29, 2023
1 parent 31e93c1 commit 7959aa7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ivy/functional/frontends/numpy/ndarray/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,3 +723,6 @@ def _unsigned_int_bytes_repr(item_val, /, *, dtype=None):
return b"".join(bytes_reprs)
else:
raise ValueError("Unsupported data type for the array.")

def __ilshift__(self, value, /):
return ivy.bitwise_left_shift(self.ivy_array, value, out=self)
Original file line number Diff line number Diff line change
Expand Up @@ -3681,3 +3681,54 @@ def test_numpy_ndarray_view(
frontend_method_data=frontend_method_data,
on_device=on_device,
)


#__ilshift__
@handle_frontend_method(
class_tree=CLASS_TREE,
init_tree="numpy.array",
method_name="__ilshift__",
dtype_and_x=helpers.dtype_and_values(
available_dtypes=helpers.get_dtypes("integer"),
num_arrays=2,
max_dim_size=1,
max_value=2**31 - 1,
),
)
def test_numpy_instance_ilshift__(
dtype_and_x,
frontend_method_data,
init_flags,
method_flags,
frontend,
backend_fw,
on_device,
):
input_dtypes, x = dtype_and_x
max_bits = np.iinfo(input_dtypes[0]).bits
max_shift = max_bits - 1
x[1] = np.asarray(np.clip(x[1], 0, max_shift), dtype=input_dtypes[1])
max_value_before_shift = 2 ** (max_bits - x[1]) - 1
overflow_threshold = 2 ** (max_bits - 1)
x[0] = np.asarray(
np.clip(x[0], None, max_value_before_shift), dtype=input_dtypes[0]
)
if np.any(x[0] > overflow_threshold):
x[0] = np.clip(x[0], None, overflow_threshold)
if np.any(x[0] < 0):
x[0] = np.abs(x[0])
helpers.test_frontend_method(
init_input_dtypes=input_dtypes,
init_all_as_kwargs_np={
"object": x[0],
},
backend_to_test=backend_fw,
method_all_as_kwargs_np={
"value": x[1],
},
frontend=frontend,
frontend_method_data=frontend_method_data,
init_flags=init_flags,
method_flags=method_flags,
on_device=on_device,
)

0 comments on commit 7959aa7

Please sign in to comment.