|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2023 Google LLC. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * ============================================================================= |
| 16 | + */ |
| 17 | + |
| 18 | +import {ENGINE} from '../engine'; |
| 19 | +import {BitwiseAnd, BitwiseAndInputs} from '../kernel_names'; |
| 20 | +import {Tensor} from '../tensor'; |
| 21 | +import {NamedTensorMap} from '../tensor_types'; |
| 22 | +import {convertToTensor} from '../tensor_util_env'; |
| 23 | +import {Rank} from '../types'; |
| 24 | +import {arraysEqual} from '../util_base'; |
| 25 | + |
| 26 | +import {op} from './operation'; |
| 27 | + |
| 28 | +/** |
| 29 | + * Bitwise `AND` operation for input tensors. |
| 30 | + * |
| 31 | + * Given two input tensors, returns a new tensor |
| 32 | + * with the `AND` calculated values. |
| 33 | + * |
| 34 | + * The method supports int32 values |
| 35 | + * |
| 36 | + * |
| 37 | + * ```js |
| 38 | + * const x = tf.tensor1d([0, 5, 3, 14], 'int32'); |
| 39 | + * const y = tf.tensor1d([5, 0, 7, 11], 'int32'); |
| 40 | + * tf.bitwiseAnd(x, y).print(); |
| 41 | + * ``` |
| 42 | + * |
| 43 | + * @param x The input tensor to be calculated. |
| 44 | + * @param y The input tensor to be calculated. |
| 45 | + * |
| 46 | + * @doc {heading: 'Operations', subheading: 'Logical'} |
| 47 | + */ |
| 48 | +function bitwiseAnd_<R extends Rank>(x: Tensor, y: Tensor): Tensor<R> { |
| 49 | + const $x = convertToTensor(x, 'x', 'bitwiseAnd'); |
| 50 | + const $y = convertToTensor(y, 'y', 'bitwiseAnd'); |
| 51 | + |
| 52 | + if (!arraysEqual($x.shape, $y.shape)) { |
| 53 | + throw new Error(`BitwiseAnd: Tensors must have the same shape. x: ${ |
| 54 | + $x.shape}, y: ${$y.shape}`); |
| 55 | + } |
| 56 | + if ($x.dtype !== 'int32' || $y.dtype !== 'int32') { |
| 57 | + throw new Error( |
| 58 | + `BitwiseAnd: Only supports 'int32' values in tensor, found type of x: ${ |
| 59 | + $x.dtype} and type of y: ${$y.dtype}`); |
| 60 | + } |
| 61 | + |
| 62 | + const inputs: BitwiseAndInputs = {a: $x, b: $y}; |
| 63 | + return ENGINE.runKernel(BitwiseAnd, inputs as unknown as NamedTensorMap); |
| 64 | +} |
| 65 | +export const bitwiseAnd = /* @__PURE__ */ op({bitwiseAnd_}); |
0 commit comments