blob: 91b6b66c423c455cf9d13138fd6a02281cd00738 [file] [log] [blame]
Georgios Pinitasaaa27182018-11-21 16:32:15 +00001/*
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"
27#include "tests/validation/Helpers.h"
28
29namespace arm_compute
30{
31namespace test
32{
33namespace validation
34{
35namespace reference
36{
37template <typename T>
38SimpleTensor<T> select(const SimpleTensor<uint8_t> &c, const SimpleTensor<T> &x, const SimpleTensor<T> &y)
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
78template SimpleTensor<uint8_t> select(const SimpleTensor<uint8_t> &c, const SimpleTensor<uint8_t> &x, const SimpleTensor<uint8_t> &y);
79template SimpleTensor<half> select(const SimpleTensor<uint8_t> &c, const SimpleTensor<half> &x, const SimpleTensor<half> &y);
80template SimpleTensor<float> select(const SimpleTensor<uint8_t> &c, const SimpleTensor<float> &x, const SimpleTensor<float> &y);
81} // namespace reference
82} // namespace validation
83} // namespace test
84} // namespace arm_compute