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