arm_compute v17.12
[platform/upstream/armcl.git] / tests / validation / reference / Utils.h
1 /*
2  * Copyright (c) 2017 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 #ifndef __ARM_COMPUTE_TEST_VALIDATION_UTILS_H__
25 #define __ARM_COMPUTE_TEST_VALIDATION_UTILS_H__
26
27 #include "arm_compute/core/Types.h"
28 #include "tests/Globals.h"
29 #include "tests/ILutAccessor.h"
30 #include "tests/Types.h"
31
32 #include <array>
33 #include <random>
34 #include <type_traits>
35 #include <utility>
36 #include <vector>
37
38 namespace arm_compute
39 {
40 namespace test
41 {
42 namespace validation
43 {
44 /** Checks if a pixel has valid coordinates
45  *
46  * @param x            X coordinate
47  * @param y            Y coordinate
48  * @param width        Width of the image
49  * @param height       Height of the image
50  * @param border_size  Border size
51  *
52  * @return True if pixel is valid else false
53  */
54 inline bool is_valid_pixel_index(int x, int y, int width, int height, int border_size)
55 {
56     return ((x >= -border_size) && (y >= -border_size) && (x < (width + border_size)) && (y < height + border_size));
57 }
58
59 // Return a tensor element at a specified coordinate with different border modes
60 template <typename T>
61 T tensor_elem_at(const SimpleTensor<T> &src, Coordinates coord, BorderMode border_mode, T constant_border_value)
62 {
63     const int x      = coord.x();
64     const int y      = coord.y();
65     const int width  = src.shape().x();
66     const int height = src.shape().y();
67
68     // If coordinates beyond range of tensor's width or height
69     if(x < 0 || y < 0 || x >= width || y >= height)
70     {
71         if(border_mode == BorderMode::REPLICATE)
72         {
73             coord.set(0, std::max(0, std::min(x, width - 1)));
74             coord.set(1, std::max(0, std::min(y, height - 1)));
75         }
76         else
77         {
78             return constant_border_value;
79         }
80     }
81
82     return src[coord2index(src.shape(), coord)];
83 }
84
85 template <typename T>
86 T bilinear_policy(const SimpleTensor<T> &in, Coordinates id, float xn, float yn, BorderMode border_mode, T constant_border_value);
87
88 /* Apply 2D spatial filter on a single element of @p in at coordinates @p coord
89  *
90  * - filter sizes have to be odd number
91  * - Row major order of filter assumed
92  * - TO_ZERO rounding policy assumed
93  * - SATURATE convert policy assumed
94  */
95 template <typename T, typename U, typename V>
96 void apply_2d_spatial_filter(Coordinates coord, const SimpleTensor<T> &src, SimpleTensor<U> &dst, const TensorShape &filter_shape, const V *filter_itr, double scale, BorderMode border_mode,
97                              T constant_border_value = T(0))
98 {
99     double    val = 0.;
100     const int x   = coord.x();
101     const int y   = coord.y();
102     for(int j = y - static_cast<int>(filter_shape[1] / 2); j <= y + static_cast<int>(filter_shape[1] / 2); ++j)
103     {
104         for(int i = x - static_cast<int>(filter_shape[0] / 2); i <= x + static_cast<int>(filter_shape[0] / 2); ++i)
105         {
106             coord.set(0, i);
107             coord.set(1, j);
108             val += static_cast<double>(*filter_itr) * tensor_elem_at(src, coord, border_mode, constant_border_value);
109             ++filter_itr;
110         }
111     }
112     coord.set(0, x);
113     coord.set(1, y);
114     dst[coord2index(src.shape(), coord)] = saturate_cast<U>(support::cpp11::trunc(val * scale));
115 }
116
117 RawTensor transpose(const RawTensor &src, int chunk_width = 1);
118
119 /** Fill matrix random.
120  *
121  * @param[in,out] matrix Matrix
122  */
123 template <std::size_t SIZE>
124 inline void fill_warp_matrix(std::array<float, SIZE> &matrix)
125 {
126     std::mt19937                          gen(library.get()->seed());
127     std::uniform_real_distribution<float> dist(-1, 1);
128     for(auto &x : matrix)
129     {
130         x = dist(gen);
131     }
132     if(SIZE == 9)
133     {
134         // This is only used in Warp Perspective, we set M[3][3] = 1 so that Z0 is not 0 and we avoid division by 0.
135         matrix[8] = 1.f;
136     }
137 }
138
139 bool valid_bilinear_policy(float xn, float yn, int width, int height, BorderMode border_mode);
140 } // namespace validation
141 } // namespace test
142 } // namespace arm_compute
143 #endif /* __ARM_COMPUTE_TEST_VALIDATION_UTILS_H__ */