Skip to content

Commit ccb4c67

Browse files
Move all binary bitwise functions and reuse them
1 parent a51d34f commit ccb4c67

File tree

17 files changed

+3110
-18
lines changed

17 files changed

+3110
-18
lines changed

dpctl_ext/tensor/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ set(_elementwise_sources
8484
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/elementwise_functions/atanh.cpp
8585
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/elementwise_functions/bitwise_and.cpp
8686
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/elementwise_functions/bitwise_invert.cpp
87-
#${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/elementwise_functions/bitwise_left_shift.cpp
88-
#${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/elementwise_functions/bitwise_or.cpp
89-
#${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/elementwise_functions/bitwise_right_shift.cpp
90-
#${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/elementwise_functions/bitwise_xor.cpp
87+
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/elementwise_functions/bitwise_left_shift.cpp
88+
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/elementwise_functions/bitwise_or.cpp
89+
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/elementwise_functions/bitwise_right_shift.cpp
90+
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/elementwise_functions/bitwise_xor.cpp
9191
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/elementwise_functions/cbrt.cpp
9292
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/elementwise_functions/ceil.cpp
9393
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/elementwise_functions/conj.cpp

dpctl_ext/tensor/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@
102102
atanh,
103103
bitwise_and,
104104
bitwise_invert,
105+
bitwise_left_shift,
106+
bitwise_or,
107+
bitwise_right_shift,
108+
bitwise_xor,
105109
cbrt,
106110
ceil,
107111
conj,
@@ -181,6 +185,10 @@
181185
"atan2",
182186
"bitwise_and",
183187
"bitwise_invert",
188+
"bitwise_left_shift",
189+
"bitwise_or",
190+
"bitwise_right_shift",
191+
"bitwise_xor",
184192
"broadcast_arrays",
185193
"broadcast_to",
186194
"can_cast",

dpctl_ext/tensor/_elementwise_funcs.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,43 @@
347347
)
348348
del _bitwise_and_docstring_
349349

350+
# B04: ===== BITWISE_LEFT_SHIFT (x1, x2)
351+
_bitwise_left_shift_docstring_ = r"""
352+
bitwise_left_shift(x1, x2, /, \*, out=None, order='K')
353+
354+
Shifts the bits of each element `x1_i` of the input array x1 to the left by
355+
appending `x2_i` (i.e., the respective element in the input array `x2`) zeros to
356+
the right of `x1_i`.
357+
358+
Args:
359+
x1 (usm_ndarray):
360+
First input array, expected to have integer data type.
361+
x2 (usm_ndarray):
362+
Second input array, also expected to have integer data type.
363+
Each element must be greater than or equal to 0.
364+
out (Union[usm_ndarray, None], optional):
365+
Output array to populate.
366+
Array must have the correct shape and the expected data type.
367+
order ("C","F","A","K", optional):
368+
Memory layout of the new output array, if parameter
369+
`out` is ``None``.
370+
Default: "K".
371+
372+
Returns:
373+
usm_ndarray:
374+
An array containing the element-wise results. The data type
375+
of the returned array is determined by the Type Promotion Rules.
376+
"""
377+
378+
bitwise_left_shift = BinaryElementwiseFunc(
379+
"bitwise_left_shift",
380+
ti._bitwise_left_shift_result_type,
381+
ti._bitwise_left_shift,
382+
_bitwise_left_shift_docstring_,
383+
binary_inplace_fn=ti._bitwise_left_shift_inplace,
384+
)
385+
del _bitwise_left_shift_docstring_
386+
350387
# U08: ===== BITWISE_INVERT (x)
351388
_bitwise_invert_docstring = r"""
352389
bitwise_invert(x, /, \*, out=None, order='K')
@@ -379,6 +416,117 @@
379416
)
380417
del _bitwise_invert_docstring
381418

419+
# B05: ===== BITWISE_OR (x1, x2)
420+
_bitwise_or_docstring_ = r"""
421+
bitwise_or(x1, x2, /, \*, out=None, order='K')
422+
423+
Computes the bitwise OR of the underlying binary representation of each
424+
element `x1_i` of the input array `x1` with the respective element `x2_i`
425+
of the input array `x2`.
426+
427+
Args:
428+
x1 (usm_ndarray):
429+
First input array, expected to have integer or boolean data type.
430+
x2 (usm_ndarray):
431+
Second input array, also expected to have integer or boolean data
432+
type.
433+
out (Union[usm_ndarray, None], optional):
434+
Output array to populate.
435+
Array must have the correct shape and the expected data type.
436+
order ("C","F","A","K", optional):
437+
Memory layout of the new output array, if parameter
438+
`out` is ``None``.
439+
Default: "K".
440+
441+
Returns:
442+
usm_ndarray:
443+
An array containing the element-wise results. The data type
444+
of the returned array is determined by the Type Promotion Rules.
445+
"""
446+
447+
bitwise_or = BinaryElementwiseFunc(
448+
"bitwise_or",
449+
ti._bitwise_or_result_type,
450+
ti._bitwise_or,
451+
_bitwise_or_docstring_,
452+
binary_inplace_fn=ti._bitwise_or_inplace,
453+
)
454+
del _bitwise_or_docstring_
455+
456+
# B06: ===== BITWISE_RIGHT_SHIFT (x1, x2)
457+
_bitwise_right_shift_docstring_ = r"""
458+
bitwise_right_shift(x1, x2, /, \*, out=None, order='K')
459+
460+
Shifts the bits of each element `x1_i` of the input array `x1` to the right
461+
according to the respective element `x2_i` of the input array `x2`.
462+
463+
Args:
464+
x1 (usm_ndarray):
465+
First input array, expected to have integer data type.
466+
x2 (usm_ndarray):
467+
Second input array, also expected to have integer data type.
468+
Each element must be greater than or equal to 0.
469+
out (Union[usm_ndarray, None], optional):
470+
Output array to populate.
471+
Array must have the correct shape and the expected data type.
472+
order ("C","F","A","K", optional):
473+
Memory layout of the new output array, if parameter
474+
`out` is ``None``.
475+
Default: "K".
476+
477+
Returns:
478+
usm_ndarray:
479+
An array containing the element-wise results. The data type
480+
of the returned array is determined by the Type Promotion Rules.
481+
"""
482+
483+
bitwise_right_shift = BinaryElementwiseFunc(
484+
"bitwise_right_shift",
485+
ti._bitwise_right_shift_result_type,
486+
ti._bitwise_right_shift,
487+
_bitwise_right_shift_docstring_,
488+
binary_inplace_fn=ti._bitwise_right_shift_inplace,
489+
)
490+
del _bitwise_right_shift_docstring_
491+
492+
493+
# B07: ===== BITWISE_XOR (x1, x2)
494+
_bitwise_xor_docstring_ = r"""
495+
bitwise_xor(x1, x2, /, \*, out=None, order='K')
496+
497+
Computes the bitwise XOR of the underlying binary representation of each
498+
element `x1_i` of the input array `x1` with the respective element `x2_i`
499+
of the input array `x2`.
500+
501+
Args:
502+
x1 (usm_ndarray):
503+
First input array, expected to have integer or boolean data type.
504+
x2 (usm_ndarray):
505+
Second input array, also expected to have integer or boolean data
506+
type.
507+
out (Union[usm_ndarray, None], optional):
508+
Output array to populate.
509+
Array must have the correct shape and the expected data type.
510+
order ("C","F","A","K", optional):
511+
Memory layout of the new output array, if parameter
512+
`out` is ``None``.
513+
Default: "K".
514+
515+
Returns:
516+
usm_ndarray:
517+
An array containing the element-wise results. The data type
518+
of the returned array is determined by the Type Promotion Rules.
519+
"""
520+
521+
bitwise_xor = BinaryElementwiseFunc(
522+
"bitwise_xor",
523+
ti._bitwise_xor_result_type,
524+
ti._bitwise_xor,
525+
_bitwise_xor_docstring_,
526+
binary_inplace_fn=ti._bitwise_xor_inplace,
527+
)
528+
del _bitwise_xor_docstring_
529+
382530
# U09: ==== CEIL (x)
383531
_ceil_docstring = r"""
384532
ceil(x, /, \*, out=None, order='K')

0 commit comments

Comments
 (0)