Release 18.08
[platform/upstream/armnn.git] / src / armnnUtils / VerificationHelpers.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
4 //
5
6 #include "VerificationHelpers.hpp"
7 #include <boost/format.hpp>
8 #include <armnn/Exceptions.hpp>
9
10 using namespace armnn;
11
12 namespace armnnUtils
13 {
14
15 void CheckValidSize(std::initializer_list<size_t> validInputCounts,
16                     size_t actualValue,
17                     const char* validExpr,
18                     const char* actualExpr,
19                     const CheckLocation& location)
20 {
21     bool isValid = std::any_of(validInputCounts.begin(),
22                                validInputCounts.end(),
23                                [&actualValue](size_t x) { return x == actualValue; } );
24     if (!isValid)
25     {
26         throw ParseException(
27             boost::str(
28                 boost::format("%1% = %2% is not valid, not in {%3%}. %4%") %
29                               actualExpr %
30                               actualValue %
31                               validExpr %
32                               location.AsString()));
33     }
34 }
35
36 uint32_t NonNegative(const char* expr,
37                      int32_t value,
38                      const CheckLocation& location)
39 {
40     if (value < 0)
41     {
42         throw ParseException(
43             boost::str(
44                 boost::format("'%1%' must be non-negative, received: %2% at %3%") %
45                               expr %
46                               value %
47                               location.AsString() ));
48     }
49     else
50     {
51         return static_cast<uint32_t>(value);
52     }
53 }
54
55 int32_t VerifyInt32(const char* expr,
56                      int64_t value,
57                      const armnn::CheckLocation& location)
58 {
59     if (value < std::numeric_limits<int>::min()  || value > std::numeric_limits<int>::max())
60     {
61         throw ParseException(
62             boost::str(
63                 boost::format("'%1%' must should fit into a int32 (ArmNN don't support int64), received: %2% at %3%") %
64                               expr %
65                               value %
66                               location.AsString() ));
67     }
68     else
69     {
70         return static_cast<int32_t>(value);
71     }
72 }
73
74 }// armnnUtils