1 /*
2 * Copyright (c) 2021 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 <array>
19 #include <cassert>
20 #include <cmath>
21 #include <iomanip>
22 #include <string_view>
23
24 namespace panda::helpers {
25
ValueUnit(uint64_t value,std::string_view literal)26 ValueUnit::ValueUnit(uint64_t value, std::string_view literal) : value_(value), literal_(literal) {}
27
ValueUnit(double value,std::string_view literal)28 ValueUnit::ValueUnit(double value, std::string_view literal) : value_(value), literal_(literal) {}
29
SetPrecision(size_t precision)30 void ValueUnit::SetPrecision(size_t precision)
31 {
32 precision_ = precision;
33 }
34
GetValue() const35 std::variant<double, uint64_t> ValueUnit::GetValue() const
36 {
37 return value_;
38 }
39
GetDoubleValue() const40 double ValueUnit::GetDoubleValue() const
41 {
42 return std::get<double>(value_);
43 }
44
GetUint64Value() const45 uint64_t ValueUnit::GetUint64Value() const
46 {
47 return std::get<uint64_t>(value_);
48 }
49
GetLiteral() const50 std::string_view ValueUnit::GetLiteral() const
51 {
52 return literal_;
53 }
54
GetPrecision() const55 size_t ValueUnit::GetPrecision() const
56 {
57 return precision_;
58 }
59
operator ==(const ValueUnit & lhs,const ValueUnit & rhs)60 bool operator==(const ValueUnit &lhs, const ValueUnit &rhs)
61 {
62 constexpr size_t NUMERAL_SYSTEM = 10;
63 if (lhs.GetValue().index() != rhs.GetValue().index()) {
64 return false;
65 }
66 if (lhs.GetLiteral() != rhs.GetLiteral()) {
67 return false;
68 }
69 if (lhs.GetValue().index() == 0U) {
70 return std::fabs(lhs.GetDoubleValue() - rhs.GetDoubleValue()) <
71 std::pow(NUMERAL_SYSTEM, -std::max(lhs.GetPrecision(), rhs.GetPrecision()));
72 }
73 if (lhs.GetValue().index() == 1U) {
74 return lhs.GetUint64Value() == rhs.GetUint64Value();
75 }
76 return false;
77 }
78
operator !=(const ValueUnit & lhs,const ValueUnit & rhs)79 bool operator!=(const ValueUnit &lhs, const ValueUnit &rhs)
80 {
81 return !(lhs == rhs);
82 }
83
operator <<(std::ostream & os,const ValueUnit & element)84 std::ostream &operator<<(std::ostream &os, const ValueUnit &element)
85 {
86 if (element.GetValue().index() == 0U) {
87 os << std::fixed << std::setprecision(static_cast<int>(element.GetPrecision())) << element.GetDoubleValue()
88 << std::setprecision(-1);
89 } else {
90 os << element.GetUint64Value();
91 }
92 return os << element.GetLiteral();
93 }
94
95 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)96 ValueUnit TypeConverter(const std::array<double, SIZE> &coeffs, const std::array<std::string_view, SIZE + 1> &literals,
97 uint64_t value_base_dimension)
98 {
99 constexpr double LIMIT = 1.0;
100 double division_ratio = 1;
101 for (size_t index_coeff = 0; index_coeff < SIZE; ++index_coeff) {
102 if (value_base_dimension / (division_ratio * coeffs[index_coeff]) < LIMIT) {
103 return ValueUnit(value_base_dimension / division_ratio, literals[index_coeff]);
104 }
105 division_ratio *= coeffs[index_coeff];
106 }
107
108 ASSERT(division_ratio != 0);
109 return ValueUnit(value_base_dimension / division_ratio, literals[SIZE]);
110 }
111
TimeConverter(uint64_t times_in_nanos)112 ValueUnit TimeConverter(uint64_t times_in_nanos)
113 {
114 return TypeConverter(COEFFS_TIME, LITERALS_TIME, times_in_nanos);
115 }
116
MemoryConverter(uint64_t bytes)117 ValueUnit MemoryConverter(uint64_t bytes)
118 {
119 ValueUnit bytes_format = TypeConverter(COEFFS_MEMORY, LITERALS_MEMORY, bytes);
120 bytes_format.SetPrecision(0);
121 return bytes_format;
122 }
123
ValueConverter(uint64_t value,ValueType type)124 ValueUnit ValueConverter(uint64_t value, ValueType type)
125 {
126 switch (type) {
127 case ValueType::VALUE_TYPE_TIME:
128 return TimeConverter(value);
129 case ValueType::VALUE_TYPE_MEMORY:
130 return MemoryConverter(value);
131 default:
132 return ValueUnit(value, "");
133 }
134 }
135
136 } // namespace panda::helpers
137