• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "type_converter.h"
17 
18 #include <algorithm>
19 #include <array>
20 #include <cmath>
21 #include <iomanip>
22 #include <ios>
23 #include <ostream>
24 #include <string_view>
25 #include <variant>
26 
27 namespace panda::helpers {
28 
ValueUnit(uint64_t value,std::string_view literal)29 ValueUnit::ValueUnit(uint64_t value, std::string_view literal) : value_(value), literal_(literal) {}
30 
ValueUnit(double value,std::string_view literal)31 ValueUnit::ValueUnit(double value, std::string_view literal) : value_(value), literal_(literal) {}
32 
SetPrecision(size_t precision)33 void ValueUnit::SetPrecision(size_t precision)
34 {
35     precision_ = precision;
36 }
37 
GetValue() const38 std::variant<double, uint64_t> ValueUnit::GetValue() const
39 {
40     return value_;
41 }
42 
GetDoubleValue() const43 double ValueUnit::GetDoubleValue() const
44 {
45     return std::get<double>(value_);
46 }
47 
GetUint64Value() const48 uint64_t ValueUnit::GetUint64Value() const
49 {
50     return std::get<uint64_t>(value_);
51 }
52 
GetLiteral() const53 std::string_view ValueUnit::GetLiteral() const
54 {
55     return literal_;
56 }
57 
GetPrecision() const58 size_t ValueUnit::GetPrecision() const
59 {
60     return precision_;
61 }
62 
operator ==(const ValueUnit & lhs,const ValueUnit & rhs)63 bool operator==(const ValueUnit &lhs, const ValueUnit &rhs)
64 {
65     constexpr size_t NUMERAL_SYSTEM = 10;
66     if (lhs.GetLiteral() != rhs.GetLiteral()) {
67         return false;
68     }
69     if (lhs.GetValue().index() != rhs.GetValue().index()) {
70         return false;
71     }
72     if (lhs.GetValue().index() == 0U) {
73         return std::fabs(lhs.GetDoubleValue() - rhs.GetDoubleValue()) <
74                std::pow(NUMERAL_SYSTEM, -std::max(lhs.GetPrecision(), rhs.GetPrecision()));
75     }
76     if (lhs.GetValue().index() == 1U) {
77         return lhs.GetUint64Value() == rhs.GetUint64Value();
78     }
79     return false;
80 }
81 
operator !=(const ValueUnit & lhs,const ValueUnit & rhs)82 bool operator!=(const ValueUnit &lhs, const ValueUnit &rhs)
83 {
84     return !(lhs == rhs);
85 }
86 
operator <<(std::ostream & os,const ValueUnit & element)87 std::ostream &operator<<(std::ostream &os, const ValueUnit &element)
88 {
89     if (element.GetValue().index() == 0U) {
90         os << std::fixed << std::setprecision(static_cast<int>(element.GetPrecision())) << element.GetDoubleValue()
91            << std::setprecision(-1);
92     } else {
93         os << element.GetUint64Value();
94     }
95     return os << element.GetLiteral();
96 }
97 
98 template <size_t SIZE>
TypeConverter(const std::array<double,SIZE> & coeffs,const std::array<std::string_view,SIZE+1> & literals,uint64_t value_base_dimension)99 ValueUnit TypeConverter(const std::array<double, SIZE> &coeffs, const std::array<std::string_view, SIZE + 1> &literals,
100                         uint64_t value_base_dimension)
101 {
102     constexpr double LIMIT = 1.0;
103     double division_ratio = 1;
104     for (size_t index_coeff = 0; index_coeff < SIZE; ++index_coeff) {
105         if (value_base_dimension / (division_ratio * coeffs[index_coeff]) < LIMIT) {
106             return ValueUnit(value_base_dimension / division_ratio, literals[index_coeff]);
107         }
108         division_ratio *= coeffs[index_coeff];
109     }
110 
111     return ValueUnit(value_base_dimension / division_ratio, literals[SIZE]);
112 }
113 
TimeConverter(uint64_t times_in_nanos)114 ValueUnit TimeConverter(uint64_t times_in_nanos)
115 {
116     return TypeConverter(COEFFS_TIME, LITERALS_TIME, times_in_nanos);
117 }
118 
MemoryConverter(uint64_t bytes)119 ValueUnit MemoryConverter(uint64_t bytes)
120 {
121     ValueUnit bytes_format = TypeConverter(COEFFS_MEMORY, LITERALS_MEMORY, bytes);
122     bytes_format.SetPrecision(0);
123     return bytes_format;
124 }
125 
ValueConverter(uint64_t value,ValueType type)126 ValueUnit ValueConverter(uint64_t value, ValueType type)
127 {
128     switch (type) {
129         case ValueType::VALUE_TYPE_TIME:
130             return TimeConverter(value);
131         case ValueType::VALUE_TYPE_MEMORY:
132             return MemoryConverter(value);
133         default:
134             return ValueUnit(value, "");
135     }
136 }
137 
138 }  // namespace panda::helpers
139