arm_compute v17.09
[platform/upstream/armcl.git] / utils / TypePrinter.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_TYPE_PRINTER_H__
25 #define __ARM_COMPUTE_TEST_TYPE_PRINTER_H__
26
27 #include "arm_compute/core/Dimensions.h"
28 #include "arm_compute/core/Error.h"
29 #include "arm_compute/core/Types.h"
30
31 #include <ostream>
32 #include <sstream>
33 #include <string>
34
35 namespace arm_compute
36 {
37 /** Formatted output of the Dimensions type. */
38 template <typename T>
39 inline ::std::ostream &operator<<(::std::ostream &os, const Dimensions<T> &dimensions)
40 {
41     if(dimensions.num_dimensions() > 0)
42     {
43         os << dimensions[0];
44
45         for(unsigned int d = 1; d < dimensions.num_dimensions(); ++d)
46         {
47             os << "x" << dimensions[d];
48         }
49     }
50
51     return os;
52 }
53
54 /** Formatted output of the NonLinearFilterFunction type. */
55 inline ::std::ostream &operator<<(::std::ostream &os, const NonLinearFilterFunction &function)
56 {
57     switch(function)
58     {
59         case NonLinearFilterFunction::MAX:
60             os << "MAX";
61             break;
62         case NonLinearFilterFunction::MEDIAN:
63             os << "MEDIAN";
64             break;
65         case NonLinearFilterFunction::MIN:
66             os << "MIN";
67             break;
68         default:
69             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
70     }
71
72     return os;
73 }
74
75 inline std::string to_string(const NonLinearFilterFunction &function)
76 {
77     std::stringstream str;
78     str << function;
79     return str.str();
80 }
81
82 /** Formatted output of the MatrixPattern type. */
83 inline ::std::ostream &operator<<(::std::ostream &os, const MatrixPattern &pattern)
84 {
85     switch(pattern)
86     {
87         case MatrixPattern::BOX:
88             os << "BOX";
89             break;
90         case MatrixPattern::CROSS:
91             os << "CROSS";
92             break;
93         case MatrixPattern::DISK:
94             os << "DISK";
95             break;
96         case MatrixPattern::OTHER:
97             os << "OTHER";
98             break;
99         default:
100             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
101     }
102
103     return os;
104 }
105
106 inline std::string to_string(const MatrixPattern &pattern)
107 {
108     std::stringstream str;
109     str << pattern;
110     return str.str();
111 }
112
113 /** Formatted output of the RoundingPolicy type. */
114 inline ::std::ostream &operator<<(::std::ostream &os, const RoundingPolicy &rounding_policy)
115 {
116     switch(rounding_policy)
117     {
118         case RoundingPolicy::TO_ZERO:
119             os << "TO_ZERO";
120             break;
121         case RoundingPolicy::TO_NEAREST_UP:
122             os << "TO_NEAREST_UP";
123             break;
124         case RoundingPolicy::TO_NEAREST_EVEN:
125             os << "TO_NEAREST_EVEN";
126             break;
127         default:
128             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
129     }
130
131     return os;
132 }
133
134 /** Formatted output of the WeightsInfo type. */
135 inline ::std::ostream &operator<<(::std::ostream &os, const WeightsInfo &weights_info)
136 {
137     os << weights_info.are_reshaped() << ";";
138     os << weights_info.num_kernels() << ";" << weights_info.kernel_size().first << "," << weights_info.kernel_size().second;
139
140     return os;
141 }
142
143 /** Formatted output of the ROIPoolingInfo type. */
144 inline ::std::ostream &operator<<(::std::ostream &os, const ROIPoolingLayerInfo &pool_info)
145 {
146     os << pool_info.pooled_width() << "x" << pool_info.pooled_height() << "~" << pool_info.spatial_scale();
147     return os;
148 }
149
150 /** Formatted output of the activation function type. */
151 inline ::std::ostream &operator<<(::std::ostream &os, const ActivationLayerInfo::ActivationFunction &act_function)
152 {
153     switch(act_function)
154     {
155         case ActivationLayerInfo::ActivationFunction::ABS:
156             os << "ABS";
157             break;
158         case ActivationLayerInfo::ActivationFunction::LINEAR:
159             os << "LINEAR";
160             break;
161         case ActivationLayerInfo::ActivationFunction::LOGISTIC:
162             os << "LOGISTIC";
163             break;
164         case ActivationLayerInfo::ActivationFunction::RELU:
165             os << "RELU";
166             break;
167         case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU:
168             os << "BOUNDED_RELU";
169             break;
170         case ActivationLayerInfo::ActivationFunction::LEAKY_RELU:
171             os << "LEAKY_RELU";
172             break;
173         case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
174             os << "SOFT_RELU";
175             break;
176         case ActivationLayerInfo::ActivationFunction::SQRT:
177             os << "SQRT";
178             break;
179         case ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU:
180             os << "LU_BOUNDED_RELU";
181         case ActivationLayerInfo::ActivationFunction::SQUARE:
182             os << "SQUARE";
183             break;
184         case ActivationLayerInfo::ActivationFunction::TANH:
185             os << "TANH";
186             break;
187         default:
188             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
189     }
190
191     return os;
192 }
193
194 inline std::string to_string(const arm_compute::ActivationLayerInfo &info)
195 {
196     std::stringstream str;
197     str << info.activation();
198     return str.str();
199 }
200
201 inline std::string to_string(const arm_compute::ActivationLayerInfo::ActivationFunction &function)
202 {
203     std::stringstream str;
204     str << function;
205     return str.str();
206 }
207
208 /** Formatted output of the NormType type. */
209 inline ::std::ostream &operator<<(::std::ostream &os, const NormType &norm_type)
210 {
211     switch(norm_type)
212     {
213         case NormType::CROSS_MAP:
214             os << "CROSS_MAP";
215             break;
216         case NormType::IN_MAP_1D:
217             os << "IN_MAP_1D";
218             break;
219         case NormType::IN_MAP_2D:
220             os << "IN_MAP_2D";
221             break;
222         default:
223             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
224     }
225
226     return os;
227 }
228
229 inline std::string to_string(const arm_compute::NormalizationLayerInfo &info)
230 {
231     std::stringstream str;
232     str << info.type();
233     return str.str();
234 }
235
236 /** Formatted output of the PoolingType type. */
237 inline ::std::ostream &operator<<(::std::ostream &os, const PoolingType &pool_type)
238 {
239     switch(pool_type)
240     {
241         case PoolingType::AVG:
242             os << "AVG";
243             break;
244         case PoolingType::MAX:
245             os << "MAX";
246             break;
247         case PoolingType::L2:
248             os << "L2";
249             break;
250         default:
251             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
252     }
253
254     return os;
255 }
256
257 /** Formatted output of @ref PoolingLayerInfo. */
258 inline ::std::ostream &operator<<(::std::ostream &os, const PoolingLayerInfo &info)
259 {
260     os << info.pool_type();
261
262     return os;
263 }
264
265 /** Formatted output of the DataType type. */
266 inline ::std::ostream &operator<<(::std::ostream &os, const DataType &data_type)
267 {
268     switch(data_type)
269     {
270         case DataType::UNKNOWN:
271             os << "UNKNOWN";
272             break;
273         case DataType::U8:
274             os << "U8";
275             break;
276         case DataType::QS8:
277             os << "QS8";
278             break;
279         case DataType::S8:
280             os << "S8";
281             break;
282         case DataType::U16:
283             os << "U16";
284             break;
285         case DataType::S16:
286             os << "S16";
287             break;
288         case DataType::QS16:
289             os << "QS16";
290             break;
291         case DataType::U32:
292             os << "U32";
293             break;
294         case DataType::S32:
295             os << "S32";
296             break;
297         case DataType::U64:
298             os << "U64";
299             break;
300         case DataType::S64:
301             os << "S64";
302             break;
303         case DataType::F16:
304             os << "F16";
305             break;
306         case DataType::F32:
307             os << "F32";
308             break;
309         case DataType::F64:
310             os << "F64";
311             break;
312         case DataType::SIZET:
313             os << "SIZET";
314             break;
315         default:
316             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
317     }
318
319     return os;
320 }
321
322 inline std::string to_string(const arm_compute::DataType &data_type)
323 {
324     std::stringstream str;
325     str << data_type;
326     return str.str();
327 }
328
329 /** Formatted output of the Format type. */
330 inline ::std::ostream &operator<<(::std::ostream &os, const Format &format)
331 {
332     switch(format)
333     {
334         case Format::UNKNOWN:
335             os << "UNKNOWN";
336             break;
337         case Format::U8:
338             os << "U8";
339             break;
340         case Format::S16:
341             os << "S16";
342             break;
343         case Format::U16:
344             os << "U16";
345             break;
346         case Format::S32:
347             os << "S32";
348             break;
349         case Format::U32:
350             os << "U32";
351             break;
352         case Format::F16:
353             os << "F16";
354             break;
355         case Format::F32:
356             os << "F32";
357             break;
358         case Format::UV88:
359             os << "UV88";
360             break;
361         case Format::RGB888:
362             os << "RGB888";
363             break;
364         case Format::RGBA8888:
365             os << "RGBA8888";
366             break;
367         case Format::YUV444:
368             os << "YUV444";
369             break;
370         case Format::YUYV422:
371             os << "YUYV422";
372             break;
373         case Format::NV12:
374             os << "NV12";
375             break;
376         case Format::NV21:
377             os << "NV21";
378             break;
379         case Format::IYUV:
380             os << "IYUV";
381             break;
382         case Format::UYVY422:
383             os << "UYVY422";
384             break;
385         default:
386             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
387     }
388
389     return os;
390 }
391
392 inline std::string to_string(const Format &format)
393 {
394     std::stringstream str;
395     str << format;
396     return str.str();
397 }
398
399 /** Formatted output of the Channel type. */
400 inline ::std::ostream &operator<<(::std::ostream &os, const Channel &channel)
401 {
402     switch(channel)
403     {
404         case Channel::UNKNOWN:
405             os << "UNKNOWN";
406             break;
407         case Channel::C0:
408             os << "C0";
409             break;
410         case Channel::C1:
411             os << "C1";
412             break;
413         case Channel::C2:
414             os << "C2";
415             break;
416         case Channel::C3:
417             os << "C3";
418             break;
419         case Channel::R:
420             os << "R";
421             break;
422         case Channel::G:
423             os << "G";
424             break;
425         case Channel::B:
426             os << "B";
427             break;
428         case Channel::A:
429             os << "A";
430             break;
431         case Channel::Y:
432             os << "Y";
433             break;
434         case Channel::U:
435             os << "U";
436             break;
437         case Channel::V:
438             os << "V";
439             break;
440         default:
441             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
442     }
443
444     return os;
445 }
446
447 /** Formatted output of the BorderMode type. */
448 inline ::std::ostream &operator<<(::std::ostream &os, const BorderMode &mode)
449 {
450     switch(mode)
451     {
452         case BorderMode::UNDEFINED:
453             os << "UNDEFINED";
454             break;
455         case BorderMode::CONSTANT:
456             os << "CONSTANT";
457             break;
458         case BorderMode::REPLICATE:
459             os << "REPLICATE";
460             break;
461         default:
462             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
463     }
464
465     return os;
466 }
467
468 /** Formatted output of the BorderSize type. */
469 inline ::std::ostream &operator<<(::std::ostream &os, const BorderSize &border)
470 {
471     os << border.top << ","
472        << border.right << ","
473        << border.bottom << ","
474        << border.left;
475
476     return os;
477 }
478
479 /** Formatted output of the InterpolationPolicy type. */
480 inline ::std::ostream &operator<<(::std::ostream &os, const InterpolationPolicy &policy)
481 {
482     switch(policy)
483     {
484         case InterpolationPolicy::NEAREST_NEIGHBOR:
485             os << "NEAREST_NEIGHBOR";
486             break;
487         case InterpolationPolicy::BILINEAR:
488             os << "BILINEAR";
489             break;
490         case InterpolationPolicy::AREA:
491             os << "AREA";
492             break;
493         default:
494             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
495     }
496
497     return os;
498 }
499
500 template <typename T>
501 inline std::string to_string(const Dimensions<T> &dimensions)
502 {
503     std::stringstream str;
504     str << dimensions;
505     return str.str();
506 }
507
508 /** Formatted output of the TensorShape type. */
509 inline std::string to_string(const TensorShape &shape)
510 {
511     std::stringstream str;
512     str << shape;
513     return str.str();
514 }
515
516 /** Formatted output of the Rectangle type. */
517 inline ::std::ostream &operator<<(::std::ostream &os, const Rectangle &rect)
518 {
519     os << rect.width << "x" << rect.height;
520     os << "+" << rect.x << "+" << rect.y;
521
522     return os;
523 }
524
525 /** Formatted output of the PadStridInfo type. */
526 inline ::std::ostream &operator<<(::std::ostream &os, const PadStrideInfo &pad_stride_info)
527 {
528     os << pad_stride_info.stride().first << "," << pad_stride_info.stride().second;
529     os << ";";
530     os << pad_stride_info.pad().first << "," << pad_stride_info.pad().second;
531
532     return os;
533 }
534
535 inline std::string to_string(const PadStrideInfo &pad_stride_info)
536 {
537     std::stringstream str;
538     str << pad_stride_info;
539     return str.str();
540 }
541
542 inline std::string to_string(const BorderMode &mode)
543 {
544     std::stringstream str;
545     str << mode;
546     return str.str();
547 }
548
549 inline std::string to_string(const InterpolationPolicy &policy)
550 {
551     std::stringstream str;
552     str << policy;
553     return str.str();
554 }
555
556 /** Formatted output of the ConversionPolicy type. */
557 inline ::std::ostream &operator<<(::std::ostream &os, const ConvertPolicy &policy)
558 {
559     switch(policy)
560     {
561         case ConvertPolicy::WRAP:
562             os << "WRAP";
563             break;
564         case ConvertPolicy::SATURATE:
565             os << "SATURATE";
566             break;
567         default:
568             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
569     }
570
571     return os;
572 }
573
574 inline std::string to_string(const ConvertPolicy &policy)
575 {
576     std::stringstream str;
577     str << policy;
578     return str.str();
579 }
580
581 /** Formatted output of the Reduction Operations. */
582 inline ::std::ostream &operator<<(::std::ostream &os, const ReductionOperation &op)
583 {
584     switch(op)
585     {
586         case ReductionOperation::SUM_SQUARE:
587             os << "SUM_SQUARE";
588             break;
589         default:
590             ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
591     }
592
593     return os;
594 }
595
596 inline std::string to_string(const ReductionOperation &op)
597 {
598     std::stringstream str;
599     str << op;
600     return str.str();
601 }
602
603 inline std::string to_string(const NormType &type)
604 {
605     std::stringstream str;
606     str << type;
607     return str.str();
608 }
609
610 inline std::string to_string(const PoolingType &type)
611 {
612     std::stringstream str;
613     str << type;
614     return str.str();
615 }
616
617 inline std::string to_string(const PoolingLayerInfo &info)
618 {
619     std::stringstream str;
620     str << info.pool_type();
621     return str.str();
622 }
623
624 /** Formatted output of the KeyPoint type. */
625 inline ::std::ostream &operator<<(::std::ostream &os, const KeyPoint &point)
626 {
627     os << "{x=" << point.x << ","
628        << "y=" << point.y << ","
629        << "strength=" << point.strength << ","
630        << "scale=" << point.scale << ","
631        << "orientation=" << point.orientation << ","
632        << "tracking_status=" << point.tracking_status << ","
633        << "error=" << point.error << "}";
634
635     return os;
636 }
637 } // namespace arm_compute
638 #endif /* __ARM_COMPUTE_TEST_TYPE_PRINTER_H__ */