blob: df1d7a543a42b0f701fa5f5bc976a4024fb587a3 [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>
SiCong Li3e363692017-07-04 15:02:10 +010039#include <random>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040#include <sstream>
41#include <string>
42#include <type_traits>
SiCong Li3e363692017-07-04 15:02:10 +010043#include <vector>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044
45namespace arm_compute
46{
47namespace test
48{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049/** Round floating-point value with half value rounding to positive infinity.
50 *
51 * @param[in] value floating-point value to be rounded.
52 *
53 * @return Floating-point value of rounded @p value.
54 */
55template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
56inline T round_half_up(T value)
57{
58 return std::floor(value + 0.5f);
59}
60
61/** Round floating-point value with half value rounding to nearest even.
62 *
63 * @param[in] value floating-point value to be rounded.
64 * @param[in] epsilon precision.
65 *
66 * @return Floating-point value of rounded @p value.
67 */
68template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
69inline T round_half_even(T value, T epsilon = std::numeric_limits<T>::epsilon())
70{
71 T positive_value = std::abs(value);
72 T ipart = 0;
73 std::modf(positive_value, &ipart);
74 // If 'value' is exactly halfway between two integers
75 if(std::abs(positive_value - (ipart + 0.5f)) < epsilon)
76 {
77 // If 'ipart' is even then return 'ipart'
78 if(std::fmod(ipart, 2.f) < epsilon)
79 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010080 return support::cpp11::copysign(ipart, value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010081 }
82 // Else return the nearest even integer
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010083 return support::cpp11::copysign(std::ceil(ipart + 0.5f), value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084 }
85 // Otherwise use the usual round to closest
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010086 return support::cpp11::copysign(support::cpp11::round(positive_value), value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087}
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088
89namespace traits
90{
91// *INDENT-OFF*
92// clang-format off
93template <typename T> struct promote { };
94template <> struct promote<uint8_t> { using type = uint16_t; };
95template <> struct promote<int8_t> { using type = int16_t; };
96template <> struct promote<uint16_t> { using type = uint32_t; };
97template <> struct promote<int16_t> { using type = int32_t; };
98template <> struct promote<uint32_t> { using type = uint64_t; };
99template <> struct promote<int32_t> { using type = int64_t; };
100template <> struct promote<float> { using type = float; };
Georgios Pinitas583137c2017-08-31 18:12:42 +0100101template <> struct promote<half> { using type = half; };
Pablo Tello383deec2017-06-23 10:40:05 +0100102
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103
104template <typename T>
105using promote_t = typename promote<T>::type;
106
107template <typename T>
108using make_signed_conditional_t = typename std::conditional<std::is_integral<T>::value, std::make_signed<T>, std::common_type<T>>::type;
John Richardson3c5f9492017-10-04 15:27:37 +0100109
110template <typename T>
111using make_unsigned_conditional_t = typename std::conditional<std::is_integral<T>::value, std::make_unsigned<T>, std::common_type<T>>::type;
112
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113// 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;
Moritz Pflanzera1848362017-08-25 12:30:03 +0100200 anchor.set_num_dimensions(shape.num_dimensions());
201
SiCong Libacaf9a2017-06-19 13:41:45 +0100202 if(border_undefined)
203 {
204 ARM_COMPUTE_ERROR_ON(shape.num_dimensions() < 2);
Moritz Pflanzera1848362017-08-25 12:30:03 +0100205
SiCong Libacaf9a2017-06-19 13:41:45 +0100206 anchor.set(0, border_size.left);
207 anchor.set(1, border_size.top);
Moritz Pflanzera1848362017-08-25 12:30:03 +0100208
209 const int valid_shape_x = std::max(0, static_cast<int>(shape.x()) - static_cast<int>(border_size.left) - static_cast<int>(border_size.right));
210 const int valid_shape_y = std::max(0, static_cast<int>(shape.y()) - static_cast<int>(border_size.top) - static_cast<int>(border_size.bottom));
211
212 shape.set(0, valid_shape_x);
213 shape.set(1, valid_shape_y);
SiCong Libacaf9a2017-06-19 13:41:45 +0100214 }
Moritz Pflanzera1848362017-08-25 12:30:03 +0100215
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100216 return ValidRegion(std::move(anchor), std::move(shape));
217}
218
Gian Marco37908d92017-11-07 14:38:22 +0000219/** Create a valid region for Gaussian Pyramid Half based on tensor shape and valid region at level "i - 1" and border mode
220 *
221 * @note The border size is 2 in case of Gaussian Pyramid Half
222 *
223 * @param[in] shape Shape used at level "i - 1" of Gaussian Pyramid Half
224 * @param[in] valid_region Valid region used at level "i - 1" of Gaussian Pyramid Half
225 * @param[in] border_undefined (Optional) Boolean indicating if the border mode is undefined.
226 *
227 * return The valid region for the level "i" of Gaussian Pyramid Half
228 */
229inline ValidRegion shape_to_valid_region_gaussian_pyramid_half(TensorShape shape, ValidRegion valid_region, bool border_undefined = false)
230{
231 constexpr int border_size = 2;
232 Coordinates anchor;
233 anchor.set_num_dimensions(shape.num_dimensions());
234
235 // Compute tensor shape for level "i" of Gaussian Pyramid Half
236 // dst_width = (src_width + 1) * 0.5f
237 // dst_height = (src_height + 1) * 0.5f
238 TensorShape dst_shape = shape;
239 dst_shape.set(0, (shape[0] + 1) * 0.5f);
240 dst_shape.set(1, (shape[1] + 1) * 0.5f);
241
242 if(border_undefined)
243 {
244 ARM_COMPUTE_ERROR_ON(shape.num_dimensions() < 2);
245
246 // Compute the left and top invalid borders
247 float invalid_border_left = static_cast<float>(valid_region.anchor.x() + border_size) / 2.0f;
248 float invalid_border_top = static_cast<float>(valid_region.anchor.y() + border_size) / 2.0f;
249
250 // For the new anchor point we can have 2 cases:
251 // 1) If the width/height of the tensor shape is odd, we have to take the ceil value of (valid_region.anchor.x() + border_size) / 2.0f or (valid_region.anchor.y() + border_size / 2.0f
252 // 2) If the width/height of the tensor shape is even, we have to take the floor value of (valid_region.anchor.x() + border_size) / 2.0f or (valid_region.anchor.y() + border_size) / 2.0f
253 // In this manner we should be able to propagate correctly the valid region along all levels of the pyramid
254 invalid_border_left = (shape[0] % 2) ? std::ceil(invalid_border_left) : std::floor(invalid_border_left);
255 invalid_border_top = (shape[1] % 2) ? std::ceil(invalid_border_top) : std::floor(invalid_border_top);
256
257 // Set the anchor point
258 anchor.set(0, static_cast<int>(invalid_border_left));
259 anchor.set(1, static_cast<int>(invalid_border_top));
260
261 // Compute shape
262 // Calculate the right and bottom invalid borders at the previous level of the pyramid
263 const float prev_invalid_border_right = static_cast<float>(shape[0] - (valid_region.anchor.x() + valid_region.shape[0]));
264 const float prev_invalid_border_bottom = static_cast<float>(shape[1] - (valid_region.anchor.y() + valid_region.shape[1]));
265
266 // Calculate the right and bottom invalid borders at the current level of the pyramid
267 const float invalid_border_right = std::ceil((prev_invalid_border_right + static_cast<float>(border_size)) / 2.0f);
268 const float invalid_border_bottom = std::ceil((prev_invalid_border_bottom + static_cast<float>(border_size)) / 2.0f);
269
270 const int valid_shape_x = std::max(0, static_cast<int>(dst_shape.x()) - static_cast<int>(invalid_border_left) - static_cast<int>(invalid_border_right));
271 const int valid_shape_y = std::max(0, static_cast<int>(dst_shape.y()) - static_cast<int>(invalid_border_top) - static_cast<int>(invalid_border_bottom));
272
273 dst_shape.set(0, valid_shape_x);
274 dst_shape.set(1, valid_shape_y);
275 }
276
277 return ValidRegion(std::move(anchor), std::move(dst_shape));
278}
279
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100280/** Write the value after casting the pointer according to @p data_type.
281 *
282 * @warning The type of the value must match the specified data type.
283 *
284 * @param[out] ptr Pointer to memory where the @p value will be written.
285 * @param[in] value Value that will be written.
286 * @param[in] data_type Data type that will be written.
287 */
288template <typename T>
289void store_value_with_data_type(void *ptr, T value, DataType data_type)
290{
291 switch(data_type)
292 {
293 case DataType::U8:
Chunosovd621bca2017-11-03 17:33:15 +0700294 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100295 *reinterpret_cast<uint8_t *>(ptr) = value;
296 break;
297 case DataType::S8:
298 case DataType::QS8:
299 *reinterpret_cast<int8_t *>(ptr) = value;
300 break;
301 case DataType::U16:
302 *reinterpret_cast<uint16_t *>(ptr) = value;
303 break;
304 case DataType::S16:
Michalis Spyrou0a8334c2017-06-14 18:00:05 +0100305 case DataType::QS16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100306 *reinterpret_cast<int16_t *>(ptr) = value;
307 break;
308 case DataType::U32:
309 *reinterpret_cast<uint32_t *>(ptr) = value;
310 break;
311 case DataType::S32:
312 *reinterpret_cast<int32_t *>(ptr) = value;
313 break;
314 case DataType::U64:
315 *reinterpret_cast<uint64_t *>(ptr) = value;
316 break;
317 case DataType::S64:
318 *reinterpret_cast<int64_t *>(ptr) = value;
319 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100320 case DataType::F16:
Georgios Pinitas583137c2017-08-31 18:12:42 +0100321 *reinterpret_cast<half *>(ptr) = value;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100322 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100323 case DataType::F32:
324 *reinterpret_cast<float *>(ptr) = value;
325 break;
326 case DataType::F64:
327 *reinterpret_cast<double *>(ptr) = value;
328 break;
329 case DataType::SIZET:
330 *reinterpret_cast<size_t *>(ptr) = value;
331 break;
332 default:
333 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
334 }
335}
336
337/** Saturate a value of type T against the numeric limits of type U.
338 *
339 * @param[in] val Value to be saturated.
340 *
341 * @return saturated value.
342 */
343template <typename U, typename T>
344T saturate_cast(T val)
345{
346 if(val > static_cast<T>(std::numeric_limits<U>::max()))
347 {
348 val = static_cast<T>(std::numeric_limits<U>::max());
349 }
350 if(val < static_cast<T>(std::numeric_limits<U>::lowest()))
351 {
352 val = static_cast<T>(std::numeric_limits<U>::lowest());
353 }
354 return val;
355}
356
357/** Find the signed promoted common type.
358 */
359template <typename... T>
360struct common_promoted_signed_type
361{
362 using common_type = typename std::common_type<T...>::type;
363 using promoted_type = traits::promote_t<common_type>;
364 using intermediate_type = typename traits::make_signed_conditional_t<promoted_type>::type;
365};
366
John Richardson3c5f9492017-10-04 15:27:37 +0100367/** Find the unsigned promoted common type.
368 */
369template <typename... T>
370struct common_promoted_unsigned_type
371{
372 using common_type = typename std::common_type<T...>::type;
373 using promoted_type = traits::promote_t<common_type>;
374 using intermediate_type = typename traits::make_unsigned_conditional_t<promoted_type>::type;
375};
376
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100377/** Convert a linear index into n-dimensional coordinates.
378 *
379 * @param[in] shape Shape of the n-dimensional tensor.
380 * @param[in] index Linear index specifying the i-th element.
381 *
382 * @return n-dimensional coordinates.
383 */
384inline Coordinates index2coord(const TensorShape &shape, int index)
385{
386 int num_elements = shape.total_size();
387
388 ARM_COMPUTE_ERROR_ON_MSG(index < 0 || index >= num_elements, "Index has to be in [0, num_elements]");
389 ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create coordinate from empty shape");
390
391 Coordinates coord{ 0 };
392
393 for(int d = shape.num_dimensions() - 1; d >= 0; --d)
394 {
395 num_elements /= shape[d];
396 coord.set(d, index / num_elements);
397 index %= num_elements;
398 }
399
400 return coord;
401}
402
403/** Linearise the given coordinate.
404 *
405 * Transforms the given coordinate into a linear offset in terms of
406 * elements.
407 *
408 * @param[in] shape Shape of the n-dimensional tensor.
409 * @param[in] coord The to be converted coordinate.
410 *
411 * @return Linear offset to the element.
412 */
413inline int coord2index(const TensorShape &shape, const Coordinates &coord)
414{
415 ARM_COMPUTE_ERROR_ON_MSG(shape.total_size() == 0, "Cannot get index from empty shape");
416 ARM_COMPUTE_ERROR_ON_MSG(coord.num_dimensions() == 0, "Cannot get index of empty coordinate");
417
418 int index = 0;
419 int dim_size = 1;
420
421 for(unsigned int i = 0; i < coord.num_dimensions(); ++i)
422 {
423 index += coord[i] * dim_size;
424 dim_size *= shape[i];
425 }
426
427 return index;
428}
429
430/** Check if a coordinate is within a valid region */
Moritz Pflanzera1848362017-08-25 12:30:03 +0100431inline bool is_in_valid_region(const ValidRegion &valid_region, Coordinates coord)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100432{
Moritz Pflanzer219c6912017-09-23 19:22:51 +0100433 for(size_t d = 0; d < Coordinates::num_max_dimensions; ++d)
Moritz Pflanzera1848362017-08-25 12:30:03 +0100434 {
435 if(coord[d] < valid_region.start(d) || coord[d] >= valid_region.end(d))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100436 {
437 return false;
438 }
439 }
Moritz Pflanzera1848362017-08-25 12:30:03 +0100440
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100441 return true;
442}
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100443
444/** Create and initialize a tensor of the given type.
445 *
446 * @param[in] shape Tensor shape.
447 * @param[in] data_type Data type.
448 * @param[in] num_channels (Optional) Number of channels.
449 * @param[in] fixed_point_position (Optional) Number of fractional bits.
Chunosovd621bca2017-11-03 17:33:15 +0700450 * @param[in] quantization_info (Optional) Quantization info for asymmetric quantized types.
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100451 *
452 * @return Initialized tensor of given type.
453 */
454template <typename T>
Chunosovd621bca2017-11-03 17:33:15 +0700455inline T create_tensor(const TensorShape &shape, DataType data_type, int num_channels = 1,
456 int fixed_point_position = 0, QuantizationInfo quantization_info = QuantizationInfo())
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100457{
Chunosovd621bca2017-11-03 17:33:15 +0700458 T tensor;
459 TensorInfo info(shape, num_channels, data_type, fixed_point_position);
460 info.set_quantization_info(quantization_info);
461 tensor.allocator()->init(info);
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100462
463 return tensor;
464}
SiCong Li3e363692017-07-04 15:02:10 +0100465
466/** Create a vector of random ROIs.
467 *
468 * @param[in] shape The shape of the input tensor.
469 * @param[in] pool_info The ROI pooling information.
470 * @param[in] num_rois The number of ROIs to be created.
471 * @param[in] seed The random seed to be used.
472 *
473 * @return A vector that contains the requested number of random ROIs
474 */
475inline std::vector<ROI> generate_random_rois(const TensorShape &shape, const ROIPoolingLayerInfo &pool_info, unsigned int num_rois, std::random_device::result_type seed)
476{
477 ARM_COMPUTE_ERROR_ON((pool_info.pooled_width() < 4) || (pool_info.pooled_height() < 4));
478
479 std::vector<ROI> rois;
480 std::mt19937 gen(seed);
481 const int pool_width = pool_info.pooled_width();
482 const int pool_height = pool_info.pooled_height();
483 const float roi_scale = pool_info.spatial_scale();
484
485 // Calculate distribution bounds
486 const auto scaled_width = static_cast<int>((shape.x() / roi_scale) / pool_width);
487 const auto scaled_height = static_cast<int>((shape.y() / roi_scale) / pool_height);
488 const auto min_width = static_cast<int>(pool_width / roi_scale);
489 const auto min_height = static_cast<int>(pool_height / roi_scale);
490
491 // Create distributions
492 std::uniform_int_distribution<int> dist_batch(0, shape[3] - 1);
493 std::uniform_int_distribution<int> dist_x(0, scaled_width);
494 std::uniform_int_distribution<int> dist_y(0, scaled_height);
495 std::uniform_int_distribution<int> dist_w(min_width, std::max(min_width, (pool_width - 2) * scaled_width));
496 std::uniform_int_distribution<int> dist_h(min_height, std::max(min_height, (pool_height - 2) * scaled_height));
497
498 for(unsigned int r = 0; r < num_rois; ++r)
499 {
500 ROI roi;
501 roi.batch_idx = dist_batch(gen);
502 roi.rect.x = dist_x(gen);
503 roi.rect.y = dist_y(gen);
504 roi.rect.width = dist_w(gen);
505 roi.rect.height = dist_h(gen);
506 rois.push_back(roi);
507 }
508
509 return rois;
510}
511
512template <typename T, typename ArrayAccessor_T>
513inline void fill_array(ArrayAccessor_T &&array, const std::vector<T> &v)
514{
515 array.resize(v.size());
516 std::memcpy(array.buffer(), v.data(), v.size() * sizeof(T));
517}
SiCong Li86b53332017-08-23 11:02:43 +0100518
519/** Obtain numpy type string from DataType.
520 *
521 * @param[in] data_type Data type.
522 *
523 * @return numpy type string.
524 */
525inline std::string get_typestring(DataType data_type)
526{
527 // Check endianness
528 const unsigned int i = 1;
529 const char *c = reinterpret_cast<const char *>(&i);
530 std::string endianness;
531 if(*c == 1)
532 {
533 endianness = std::string("<");
534 }
535 else
536 {
537 endianness = std::string(">");
538 }
539 const std::string no_endianness("|");
540
541 switch(data_type)
542 {
543 case DataType::U8:
544 return no_endianness + "u" + support::cpp11::to_string(sizeof(uint8_t));
545 case DataType::S8:
546 return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t));
547 case DataType::U16:
548 return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t));
549 case DataType::S16:
550 return endianness + "i" + support::cpp11::to_string(sizeof(int16_t));
551 case DataType::U32:
552 return endianness + "u" + support::cpp11::to_string(sizeof(uint32_t));
553 case DataType::S32:
554 return endianness + "i" + support::cpp11::to_string(sizeof(int32_t));
555 case DataType::U64:
556 return endianness + "u" + support::cpp11::to_string(sizeof(uint64_t));
557 case DataType::S64:
558 return endianness + "i" + support::cpp11::to_string(sizeof(int64_t));
559 case DataType::F32:
560 return endianness + "f" + support::cpp11::to_string(sizeof(float));
561 case DataType::F64:
562 return endianness + "f" + support::cpp11::to_string(sizeof(double));
563 case DataType::SIZET:
564 return endianness + "u" + support::cpp11::to_string(sizeof(size_t));
565 default:
566 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
567 }
568}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100569} // namespace test
570} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100571#endif /* __ARM_COMPUTE_TEST_UTILS_H__ */