blob: 3be046182a01e645834f003125cf60f28c4b1a2c [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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_UTILS_H__
25#define __ARM_COMPUTE_TEST_UTILS_H__
26
27#include "arm_compute/core/Coordinates.h"
28#include "arm_compute/core/Error.h"
29#include "arm_compute/core/FixedPoint.h"
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010030#include "arm_compute/core/TensorInfo.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/core/TensorShape.h"
32#include "arm_compute/core/Types.h"
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010033#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
35#include <cmath>
36#include <cstddef>
37#include <limits>
38#include <memory>
39#include <sstream>
40#include <string>
41#include <type_traits>
42
Pablo Tello383deec2017-06-23 10:40:05 +010043#if ARM_COMPUTE_ENABLE_FP16
44#include <arm_fp16.h> // needed for float16_t
45#endif
46
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047namespace arm_compute
48{
49namespace test
50{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051/** Round floating-point value with half value rounding to positive infinity.
52 *
53 * @param[in] value floating-point value to be rounded.
54 *
55 * @return Floating-point value of rounded @p value.
56 */
57template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
58inline T round_half_up(T value)
59{
60 return std::floor(value + 0.5f);
61}
62
63/** Round floating-point value with half value rounding to nearest even.
64 *
65 * @param[in] value floating-point value to be rounded.
66 * @param[in] epsilon precision.
67 *
68 * @return Floating-point value of rounded @p value.
69 */
70template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
71inline T round_half_even(T value, T epsilon = std::numeric_limits<T>::epsilon())
72{
73 T positive_value = std::abs(value);
74 T ipart = 0;
75 std::modf(positive_value, &ipart);
76 // If 'value' is exactly halfway between two integers
77 if(std::abs(positive_value - (ipart + 0.5f)) < epsilon)
78 {
79 // If 'ipart' is even then return 'ipart'
80 if(std::fmod(ipart, 2.f) < epsilon)
81 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010082 return support::cpp11::copysign(ipart, value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083 }
84 // Else return the nearest even integer
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010085 return support::cpp11::copysign(std::ceil(ipart + 0.5f), value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086 }
87 // Otherwise use the usual round to closest
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010088 return support::cpp11::copysign(support::cpp11::round(positive_value), value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089}
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090
91namespace traits
92{
93// *INDENT-OFF*
94// clang-format off
95template <typename T> struct promote { };
96template <> struct promote<uint8_t> { using type = uint16_t; };
97template <> struct promote<int8_t> { using type = int16_t; };
98template <> struct promote<uint16_t> { using type = uint32_t; };
99template <> struct promote<int16_t> { using type = int32_t; };
100template <> struct promote<uint32_t> { using type = uint64_t; };
101template <> struct promote<int32_t> { using type = int64_t; };
102template <> struct promote<float> { using type = float; };
Pablo Tello383deec2017-06-23 10:40:05 +0100103#ifdef ARM_COMPUTE_ENABLE_FP16
104template <> struct promote<float16_t> { using type = float16_t; };
105#endif
106
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107
108template <typename T>
109using promote_t = typename promote<T>::type;
110
111template <typename T>
112using make_signed_conditional_t = typename std::conditional<std::is_integral<T>::value, std::make_signed<T>, std::common_type<T>>::type;
113// clang-format on
114// *INDENT-ON*
115}
116
117/** Look up the format corresponding to a channel.
118 *
119 * @param[in] channel Channel type.
120 *
121 * @return Format that contains the given channel.
122 */
123inline Format get_format_for_channel(Channel channel)
124{
125 switch(channel)
126 {
127 case Channel::R:
128 case Channel::G:
129 case Channel::B:
130 return Format::RGB888;
131 default:
132 throw std::runtime_error("Unsupported channel");
133 }
134}
135
136/** Return the format of a channel.
137 *
138 * @param[in] channel Channel type.
139 *
140 * @return Format of the given channel.
141 */
142inline Format get_channel_format(Channel channel)
143{
144 switch(channel)
145 {
146 case Channel::R:
147 case Channel::G:
148 case Channel::B:
149 return Format::U8;
150 default:
151 throw std::runtime_error("Unsupported channel");
152 }
153}
154
155/** Base case of foldl.
156 *
157 * @return value.
158 */
159template <typename F, typename T>
160inline T foldl(F &&, const T &value)
161{
162 return value;
163}
164
165/** Base case of foldl.
166 *
167 * @return func(value1, value2).
168 */
169template <typename F, typename T, typename U>
170inline auto foldl(F &&func, T &&value1, U &&value2) -> decltype(func(value1, value2))
171{
172 return func(value1, value2);
173}
174
175/** Fold left.
176 *
177 * @param[in] func Binary function to be called.
178 * @param[in] initial Initial value.
179 * @param[in] value Argument passed to the function.
180 * @param[in] values Remaining arguments.
181 */
182template <typename F, typename I, typename T, typename... Vs>
183inline I foldl(F &&func, I &&initial, T &&value, Vs &&... values)
184{
185 return foldl(std::forward<F>(func), func(std::forward<I>(initial), std::forward<T>(value)), std::forward<Vs>(values)...);
186}
187
SiCong Libacaf9a2017-06-19 13:41:45 +0100188/** Create a valid region based on tensor shape, border mode and border size
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100189 *
SiCong Libacaf9a2017-06-19 13:41:45 +0100190 * @param[in] shape Shape used as size of the valid region.
191 * @param[in] border_undefined (Optional) Boolean indicating if the border mode is undefined.
192 * @param[in] border_size (Optional) Border size used to specify the region to exclude.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100193 *
SiCong Libacaf9a2017-06-19 13:41:45 +0100194 * @return A valid region starting at (0, 0, ...) with size of @p shape if @p border_undefined is false; otherwise
195 * return A valid region starting at (@p border_size.left, @p border_size.top, ...) with reduced size of @p shape.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196 */
SiCong Libacaf9a2017-06-19 13:41:45 +0100197inline ValidRegion shape_to_valid_region(TensorShape shape, bool border_undefined = false, BorderSize border_size = BorderSize(0))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198{
199 Coordinates anchor;
SiCong Libacaf9a2017-06-19 13:41:45 +0100200 anchor.set(std::max(0, static_cast<int>(shape.num_dimensions()) - 1), 0);
201 if(border_undefined)
202 {
203 ARM_COMPUTE_ERROR_ON(shape.num_dimensions() < 2);
204 anchor.set(0, border_size.left);
205 anchor.set(1, border_size.top);
206 shape.set(0, shape.x() - border_size.left - border_size.right);
207 shape.set(1, shape.y() - border_size.top - border_size.bottom);
208 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209 return ValidRegion(std::move(anchor), std::move(shape));
210}
211
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100212/** Write the value after casting the pointer according to @p data_type.
213 *
214 * @warning The type of the value must match the specified data type.
215 *
216 * @param[out] ptr Pointer to memory where the @p value will be written.
217 * @param[in] value Value that will be written.
218 * @param[in] data_type Data type that will be written.
219 */
220template <typename T>
221void store_value_with_data_type(void *ptr, T value, DataType data_type)
222{
223 switch(data_type)
224 {
225 case DataType::U8:
226 *reinterpret_cast<uint8_t *>(ptr) = value;
227 break;
228 case DataType::S8:
229 case DataType::QS8:
230 *reinterpret_cast<int8_t *>(ptr) = value;
231 break;
232 case DataType::U16:
233 *reinterpret_cast<uint16_t *>(ptr) = value;
234 break;
235 case DataType::S16:
Michalis Spyrou0a8334c2017-06-14 18:00:05 +0100236 case DataType::QS16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100237 *reinterpret_cast<int16_t *>(ptr) = value;
238 break;
239 case DataType::U32:
240 *reinterpret_cast<uint32_t *>(ptr) = value;
241 break;
242 case DataType::S32:
243 *reinterpret_cast<int32_t *>(ptr) = value;
244 break;
245 case DataType::U64:
246 *reinterpret_cast<uint64_t *>(ptr) = value;
247 break;
248 case DataType::S64:
249 *reinterpret_cast<int64_t *>(ptr) = value;
250 break;
Pablo Tello383deec2017-06-23 10:40:05 +0100251#if ARM_COMPUTE_ENABLE_FP16
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100252 case DataType::F16:
253 *reinterpret_cast<float16_t *>(ptr) = value;
254 break;
Pablo Tello383deec2017-06-23 10:40:05 +0100255#endif /* ARM_COMPUTE_ENABLE_FP16 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100256 case DataType::F32:
257 *reinterpret_cast<float *>(ptr) = value;
258 break;
259 case DataType::F64:
260 *reinterpret_cast<double *>(ptr) = value;
261 break;
262 case DataType::SIZET:
263 *reinterpret_cast<size_t *>(ptr) = value;
264 break;
265 default:
266 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
267 }
268}
269
270/** Saturate a value of type T against the numeric limits of type U.
271 *
272 * @param[in] val Value to be saturated.
273 *
274 * @return saturated value.
275 */
276template <typename U, typename T>
277T saturate_cast(T val)
278{
279 if(val > static_cast<T>(std::numeric_limits<U>::max()))
280 {
281 val = static_cast<T>(std::numeric_limits<U>::max());
282 }
283 if(val < static_cast<T>(std::numeric_limits<U>::lowest()))
284 {
285 val = static_cast<T>(std::numeric_limits<U>::lowest());
286 }
287 return val;
288}
289
290/** Find the signed promoted common type.
291 */
292template <typename... T>
293struct common_promoted_signed_type
294{
295 using common_type = typename std::common_type<T...>::type;
296 using promoted_type = traits::promote_t<common_type>;
297 using intermediate_type = typename traits::make_signed_conditional_t<promoted_type>::type;
298};
299
300/** Convert a linear index into n-dimensional coordinates.
301 *
302 * @param[in] shape Shape of the n-dimensional tensor.
303 * @param[in] index Linear index specifying the i-th element.
304 *
305 * @return n-dimensional coordinates.
306 */
307inline Coordinates index2coord(const TensorShape &shape, int index)
308{
309 int num_elements = shape.total_size();
310
311 ARM_COMPUTE_ERROR_ON_MSG(index < 0 || index >= num_elements, "Index has to be in [0, num_elements]");
312 ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create coordinate from empty shape");
313
314 Coordinates coord{ 0 };
315
316 for(int d = shape.num_dimensions() - 1; d >= 0; --d)
317 {
318 num_elements /= shape[d];
319 coord.set(d, index / num_elements);
320 index %= num_elements;
321 }
322
323 return coord;
324}
325
326/** Linearise the given coordinate.
327 *
328 * Transforms the given coordinate into a linear offset in terms of
329 * elements.
330 *
331 * @param[in] shape Shape of the n-dimensional tensor.
332 * @param[in] coord The to be converted coordinate.
333 *
334 * @return Linear offset to the element.
335 */
336inline int coord2index(const TensorShape &shape, const Coordinates &coord)
337{
338 ARM_COMPUTE_ERROR_ON_MSG(shape.total_size() == 0, "Cannot get index from empty shape");
339 ARM_COMPUTE_ERROR_ON_MSG(coord.num_dimensions() == 0, "Cannot get index of empty coordinate");
340
341 int index = 0;
342 int dim_size = 1;
343
344 for(unsigned int i = 0; i < coord.num_dimensions(); ++i)
345 {
346 index += coord[i] * dim_size;
347 dim_size *= shape[i];
348 }
349
350 return index;
351}
352
Georgios Pinitasce093142017-06-19 16:11:53 +0100353/** Check if Coordinates dimensionality can match the respective shape one.
354 *
355 * @param coords Coordinates
356 * @param shape Shape to match dimensionality
357 *
358 * @return True if Coordinates can match the dimensionality of the shape else false.
359 */
360inline bool match_shape(Coordinates &coords, const TensorShape &shape)
361{
362 auto check_nz = [](unsigned int i)
363 {
364 return i != 0;
365 };
366
367 unsigned int coords_dims = coords.num_dimensions();
368 unsigned int shape_dims = shape.num_dimensions();
369
370 // Increase coordinates scenario
371 if(coords_dims < shape_dims)
372 {
373 coords.set_num_dimensions(shape_dims);
374 return true;
375 }
376 // Decrease coordinates scenario
377 if(coords_dims > shape_dims && !std::any_of(coords.begin() + shape_dims, coords.end(), check_nz))
378 {
379 coords.set_num_dimensions(shape_dims);
380 return true;
381 }
382
383 return (coords_dims == shape_dims);
384}
385
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100386/** Check if a coordinate is within a valid region */
387inline bool is_in_valid_region(const ValidRegion &valid_region, const Coordinates &coord)
388{
Georgios Pinitasce093142017-06-19 16:11:53 +0100389 Coordinates coords(coord);
390 ARM_COMPUTE_ERROR_ON_MSG(!match_shape(coords, valid_region.shape), "Shapes of valid region and coordinates do not agree");
391 for(int d = 0; static_cast<size_t>(d) < coords.num_dimensions(); ++d)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100392 {
Georgios Pinitasce093142017-06-19 16:11:53 +0100393 if(coords[d] < valid_region.start(d) || coords[d] >= valid_region.end(d))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100394 {
395 return false;
396 }
397 }
398 return true;
399}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100400} // namespace test
401} // namespace arm_compute
402#endif