Skip to content

Bitwise elementwise functions #1307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions dpctl/tensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@
asinh,
atan,
atanh,
bitwise_and,
bitwise_invert,
bitwise_left_shift,
bitwise_or,
bitwise_right_shift,
bitwise_xor,
ceil,
conj,
cos,
Expand Down Expand Up @@ -232,6 +238,12 @@
"asinh",
"atan",
"atanh",
"bitwise_and",
"bitwise_invert",
"bitwise_left_shift",
"bitwise_or",
"bitwise_right_shift",
"bitwise_xor",
"ceil",
"conj",
"cos",
Expand Down
190 changes: 184 additions & 6 deletions dpctl/tensor/_elementwise_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,22 +238,200 @@
)

# B03: ===== BITWISE_AND (x1, x2)
# FIXME: implemetn B03
_bitwise_and_docstring_ = """
bitwise_and(x1, x2, out=None, order='K')

Computes the bitwise AND of the underlying binary representation of each
element `x1_i` of the input array `x1` with the respective element `x2_i`
of the input array `x2`.

Args:
x1 (usm_ndarray):
First input array, expected to have integer or boolean data type.
x2 (usm_ndarray):
Second input array, also expected to have integer or boolean data
type.
out ({None, usm_ndarray}, optional):
Output array to populate.
Array have the correct shape and the expected data type.
order ("C","F","A","K", optional):
Memory layout of the newly output array, if parameter `out` is `None`.
Default: "K".
Returns:
usm_narray:
An array containing the element-wise results. The data type
of the returned array is determined by the Type Promotion Rules.
"""

bitwise_and = BinaryElementwiseFunc(
"bitwise_and",
ti._bitwise_and_result_type,
ti._bitwise_and,
_bitwise_and_docstring_,
)

# B04: ===== BITWISE_LEFT_SHIFT (x1, x2)
# FIXME: implement B04
_bitwise_left_shift_docstring_ = """
bitwise_left_shift(x1, x2, out=None, order='K')

Shifts the bits of each element `x1_i` of the input array x1 to the left by
appending `x2_i` (i.e., the respective element in the input array `x2`) zeros to
the right of `x1_i`.

Args:
x1 (usm_ndarray):
First input array, expected to have integer data type.
x2 (usm_ndarray):
Second input array, also expected to have integer data type.
Each element must be greater than or equal to 0.
out ({None, usm_ndarray}, optional):
Output array to populate.
Array have the correct shape and the expected data type.
order ("C","F","A","K", optional):
Memory layout of the newly output array, if parameter `out` is `None`.
Default: "K".
Returns:
usm_narray:
An array containing the element-wise results. The data type
of the returned array is determined by the Type Promotion Rules.
"""

bitwise_left_shift = BinaryElementwiseFunc(
"bitwise_left_shift",
ti._bitwise_left_shift_result_type,
ti._bitwise_left_shift,
_bitwise_left_shift_docstring_,
)


# U08: ===== BITWISE_INVERT (x)
# FIXME: implement U08
_bitwise_invert_docstring = """
bitwise_invert(x, out=None, order='K')

Inverts (flips) each bit for each element `x_i` of the input array `x`.

Args:
x (usm_ndarray):
Input array, expected to have integer or boolean data type.
out ({None, usm_ndarray}, optional):
Output array to populate.
Array have the correct shape and the expected data type.
order ("C","F","A","K", optional):
Memory layout of the newly output array, if parameter `out` is `None`.
Default: "K".
Returns:
usm_narray:
An array containing the element-wise results.
The data type of the returned array is same as the data type of the
input array.
"""

bitwise_invert = UnaryElementwiseFunc(
"bitwise_invert",
ti._bitwise_invert_result_type,
ti._bitwise_invert,
_bitwise_invert_docstring,
)

# B05: ===== BITWISE_OR (x1, x2)
# FIXME: implement B05
_bitwise_or_docstring_ = """
bitwise_or(x1, x2, out=None, order='K')

Computes the bitwise OR of the underlying binary representation of each
element `x1_i` of the input array `x1` with the respective element `x2_i`
of the input array `x2`.

Args:
x1 (usm_ndarray):
First input array, expected to have integer or boolean data type.
x2 (usm_ndarray):
Second input array, also expected to have integer or boolean data
type.
out ({None, usm_ndarray}, optional):
Output array to populate.
Array have the correct shape and the expected data type.
order ("C","F","A","K", optional):
Memory layout of the newly output array, if parameter `out` is `None`.
Default: "K".
Returns:
usm_narray:
An array containing the element-wise results. The data type
of the returned array is determined by the Type Promotion Rules.
"""

bitwise_or = BinaryElementwiseFunc(
"bitwise_or",
ti._bitwise_or_result_type,
ti._bitwise_or,
_bitwise_or_docstring_,
)

# B06: ===== BITWISE_RIGHT_SHIFT (x1, x2)
# FIXME: implement B06
_bitwise_right_shift_docstring_ = """
bitwise_right_shift(x1, x2, out=None, order='K')

Shifts the bits of each element `x1_i` of the input array `x1` to the right
according to the respective element `x2_i` of the input array `x2`.

Args:
x1 (usm_ndarray):
First input array, expected to have integer data type.
x2 (usm_ndarray):
Second input array, also expected to have integer data type.
Each element must be greater than or equal to 0.
out ({None, usm_ndarray}, optional):
Output array to populate.
Array have the correct shape and the expected data type.
order ("C","F","A","K", optional):
Memory layout of the newly output array, if parameter `out` is `None`.
Default: "K".
Returns:
usm_narray:
An array containing the element-wise results. The data type
of the returned array is determined by the Type Promotion Rules.
"""

bitwise_right_shift = BinaryElementwiseFunc(
"bitwise_right_shift",
ti._bitwise_right_shift_result_type,
ti._bitwise_right_shift,
_bitwise_right_shift_docstring_,
)


# B07: ===== BITWISE_XOR (x1, x2)
# FIXME: implement B07
_bitwise_xor_docstring_ = """
bitwise_xor(x1, x2, out=None, order='K')

Computes the bitwise XOR of the underlying binary representation of each
element `x1_i` of the input array `x1` with the respective element `x2_i`
of the input array `x2`.

Args:
x1 (usm_ndarray):
First input array, expected to have integer or boolean data type.
x2 (usm_ndarray):
Second input array, also expected to have integer or boolean data
type.
out ({None, usm_ndarray}, optional):
Output array to populate.
Array have the correct shape and the expected data type.
order ("C","F","A","K", optional):
Memory layout of the newly output array, if parameter `out` is `None`.
Default: "K".
Returns:
usm_narray:
An array containing the element-wise results. The data type
of the returned array is determined by the Type Promotion Rules.
"""

bitwise_xor = BinaryElementwiseFunc(
"bitwise_xor",
ti._bitwise_xor_result_type,
ti._bitwise_xor,
_bitwise_xor_docstring_,
)


# U09: ==== CEIL (x)
_ceil_docstring = """
Expand Down
Loading