Compute Library
 21.08
Select.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "Select.h"
25 
26 #include "arm_compute/core/Types.h"
28 
29 namespace arm_compute
30 {
31 namespace test
32 {
33 namespace validation
34 {
35 namespace reference
36 {
37 template <typename T>
39 {
40  // Check if condition has the same rank as c
41  const bool is_same_rank = (c.shape().num_dimensions() == x.shape().num_dimensions());
42 
43  // Check shapes
44  ARM_COMPUTE_ERROR_ON(x.shape() != y.shape());
45  ARM_COMPUTE_ERROR_ON(is_same_rank && (x.shape() != c.shape()));
46  ARM_COMPUTE_ERROR_ON(!is_same_rank && (c.shape().num_dimensions() > 1) && (c.shape().x() != x.shape()[x.shape().num_dimensions() - 1]));
47 
48  // Create reference
49  SimpleTensor<T> dst{ x.shape(), x.data_type(), 1 };
50 
51  // Run select core
52  if(is_same_rank)
53  {
54  for(int i = 0; i < x.num_elements(); ++i)
55  {
56  dst[i] = c[i] > 0 ? x[i] : y[i];
57  }
58  }
59  else
60  {
61  T *output_ptr = dst.data();
62 
63  const int outer_size = c.num_elements();
64  const int inner_size = x.num_elements() / outer_size;
65  size_t offset = 0;
66 
67  for(int i = 0; i < outer_size; ++i)
68  {
69  const T *input_ptr = c[i] > 0 ? x.data() : y.data();
70  memcpy(output_ptr + offset, input_ptr + offset, inner_size * sizeof(T));
71  offset += inner_size;
72  }
73  }
74 
75  return dst;
76 }
77 
81 } // namespace reference
82 } // namespace validation
83 } // namespace test
84 } // namespace arm_compute
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:861
DataType data_type() const override
Data type of the tensor.
Definition: SimpleTensor.h:357
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:320
Copyright (c) 2017-2021 Arm Limited.
T x() const
Alias to access the size of the first dimension.
Definition: Dimensions.h:87
SimpleTensor< T > select(const SimpleTensor< uint8_t > &c, const SimpleTensor< T > &x, const SimpleTensor< T > &y)
Definition: Select.cpp:38
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:143
int num_elements() const override
Number of elements of the tensor.
Definition: SimpleTensor.h:406
const T * data() const
Constant pointer to the underlying buffer.
Definition: SimpleTensor.h:418