• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MINDSPORE_CORE_UTILS_CONVERT_UTILS_BASE_H_
18 #define MINDSPORE_CORE_UTILS_CONVERT_UTILS_BASE_H_
19 
20 #include <limits>
21 #include <memory>
22 
23 #include "utils/log_adapter.h"
24 
25 namespace mindspore {
SizeToInt(size_t u)26 inline int SizeToInt(size_t u) {
27   if (u > static_cast<size_t>((std::numeric_limits<int>::max)())) {
28     MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of int.";
29   }
30   return static_cast<int>(u);
31 }
32 
SizeToUint(size_t u)33 inline uint32_t SizeToUint(size_t u) {
34   if (u > static_cast<size_t>((std::numeric_limits<uint32_t>::max)())) {
35     MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of uint32_t.";
36   }
37   return static_cast<uint32_t>(u);
38 }
39 
SizeToLong(size_t u)40 inline int64_t SizeToLong(size_t u) {
41   if (u > static_cast<size_t>((std::numeric_limits<int64_t>::max)())) {
42     MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of int64_t.";
43   }
44   return static_cast<int64_t>(u);
45 }
46 
SizeToUlong(size_t u)47 inline uint64_t SizeToUlong(size_t u) { return static_cast<uint64_t>(u); }
48 
IntToSize(int u)49 inline size_t IntToSize(int u) {
50   if (u < 0) {
51     MS_LOG(WARNING) << "The int value(" << u << ") is less than 0.";
52     return SIZE_MAX;
53   }
54   return static_cast<size_t>(u);
55 }
56 
LongToSize(int64_t u)57 inline size_t LongToSize(int64_t u) {
58   if (u < 0) {
59     MS_LOG(EXCEPTION) << "The int64_t value(" << u << ") is less than 0.";
60   }
61   return static_cast<size_t>(u);
62 }
63 
FloatToSize(float u)64 inline size_t FloatToSize(float u) {
65   if (u < 0) {
66     MS_LOG(EXCEPTION) << "The float value(" << u << ") is less than 0.";
67   }
68 
69   if (u > static_cast<float>((std::numeric_limits<size_t>::max)())) {
70     MS_LOG(EXCEPTION) << "The float value(" << u << ") exceeds the maximum value of size_t.";
71   }
72   return static_cast<size_t>(u);
73 }
IntToFloat(int32_t v)74 inline float IntToFloat(int32_t v) { return static_cast<float>(v); }
75 
FloatToInt(float u)76 inline int FloatToInt(float u) {
77   if (u > static_cast<float>((std::numeric_limits<int>::max)())) {
78     MS_LOG(EXCEPTION) << "The float value(" << u << ") exceeds the maximum value of int.";
79   }
80   return static_cast<int>(u);
81 }
82 
FloatToLong(float u)83 inline int FloatToLong(float u) {
84   if (u > static_cast<float>((std::numeric_limits<int64_t>::max)())) {
85     MS_LOG(EXCEPTION) << "The float value(" << u << ") exceeds the maximum value of int64_t.";
86   }
87   return static_cast<int64_t>(u);
88 }
89 
DoubleToLong(double u)90 inline int64_t DoubleToLong(double u) {
91   if (u > static_cast<double>((std::numeric_limits<int64_t>::max)())) {
92     MS_LOG(EXCEPTION) << "The double value(" << u << ") exceeds the maximum value of int64_t.";
93   }
94   return static_cast<int64_t>(u);
95 }
96 
SizeToFloat(size_t v)97 inline float SizeToFloat(size_t v) { return static_cast<float>(v); }
98 
LongToDouble(int64_t v)99 inline double LongToDouble(int64_t v) { return static_cast<double>(v); }
100 
FloatToDouble(float v)101 inline double FloatToDouble(float v) { return static_cast<double>(v); }
102 
IntToUint(int32_t u)103 inline uint32_t IntToUint(int32_t u) {
104   if (u < 0) {
105     MS_LOG(EXCEPTION) << "The int32_t value(" << u << ") is less than 0.";
106   }
107   return static_cast<uint32_t>(u);
108 }
109 
UintToInt(uint32_t u)110 inline int32_t UintToInt(uint32_t u) {
111   if (u > static_cast<uint32_t>((std::numeric_limits<int32_t>::max)())) {
112     MS_LOG(EXCEPTION) << "The uint32_t value(" << u << ") exceeds the maximum value of int32_t.";
113   }
114   return static_cast<int32_t>(u);
115 }
116 
LongToUlong(int64_t u)117 inline uint64_t LongToUlong(int64_t u) {
118   if (u < 0) {
119     MS_LOG(EXCEPTION) << "The int64_t value(" << u << ") is less than 0.";
120   }
121   return static_cast<uint64_t>(u);
122 }
123 
LongToInt(int64_t u)124 inline int32_t LongToInt(int64_t u) {
125   if (u > static_cast<int64_t>((std::numeric_limits<int32_t>::max)())) {
126     MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of int.";
127   }
128   return static_cast<int32_t>(u);
129 }
130 
IntToLong(int32_t v)131 inline int64_t IntToLong(int32_t v) { return static_cast<int64_t>(v); }
132 
UlongToLong(uint64_t u)133 inline int64_t UlongToLong(uint64_t u) {
134   if (u > static_cast<uint64_t>((std::numeric_limits<int64_t>::max)())) {
135     MS_LOG(EXCEPTION) << "The uint64_t value(" << u << ") exceeds the maximum value of int64_t.";
136   }
137   return static_cast<int64_t>(u);
138 }
139 
UlongToUint(uint64_t u)140 inline unsigned int UlongToUint(uint64_t u) {
141   if (u > static_cast<uint64_t>((std::numeric_limits<unsigned int>::max)())) {
142     MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of unsigned int.";
143   }
144   return static_cast<unsigned int>(u);
145 }
146 
IntMulWithOverflowCheck(int a,int b)147 inline int IntMulWithOverflowCheck(int a, int b) {
148   int out = a * b;
149   if (a != 0) {
150     bool overflow = ((out / a) != b);
151     if (overflow) {
152       MS_LOG(EXCEPTION) << "Mul: a(" << a << ") * b(" << b << ") result is overflow";
153     }
154   }
155   return out;
156 }
157 
LongMulWithOverflowCheck(int64_t a,int64_t b)158 inline int64_t LongMulWithOverflowCheck(int64_t a, int64_t b) {
159   int64_t out = a * b;
160   if (a != 0) {
161     bool overflow = ((out / a) != b);
162     if (overflow) {
163       MS_LOG(EXCEPTION) << "Mul: a(" << a << ") * b(" << b << ") result is overflow";
164     }
165   }
166   return out;
167 }
168 
SizetMulWithOverflowCheck(size_t a,size_t b)169 inline size_t SizetMulWithOverflowCheck(size_t a, size_t b) {
170   size_t out = a * b;
171   if (a != 0) {
172     if ((out / a) != b) {
173       MS_LOG(EXCEPTION) << "Mul: a(" << a << ") * b(" << b << ") result is overflow";
174     }
175   }
176   return out;
177 }
178 
Uint32tMulWithOverflowCheck(uint32_t a,uint32_t b)179 inline uint32_t Uint32tMulWithOverflowCheck(uint32_t a, uint32_t b) {
180   uint32_t out = a * b;
181   if (a != 0) {
182     if ((out / a) != b) {
183       MS_LOG(EXCEPTION) << "Mul: a(" << a << ") * b(" << b << ") result is overflow";
184     }
185   }
186   return out;
187 }
188 
SizetAddWithOverflowCheck(size_t x,size_t y)189 inline size_t SizetAddWithOverflowCheck(size_t x, size_t y) {
190   size_t sum = x + y;
191   if (sum < x || sum < y) {
192     MS_LOG(EXCEPTION) << "Add: a(" << x << ") + b(" << y << ") result is overflow";
193   }
194   return sum;
195 }
196 
Uint32tAddWithOverflowCheck(uint32_t x,uint32_t y)197 inline uint32_t Uint32tAddWithOverflowCheck(uint32_t x, uint32_t y) {
198   uint32_t sum = x + y;
199   if (sum < x || sum < y) {
200     MS_LOG(EXCEPTION) << "Add: a(" << x << ") + b(" << y << ") result is overflow";
201   }
202   return sum;
203 }
204 
AddressOffset(void * address,size_t offset)205 inline uint8_t *AddressOffset(void *address, size_t offset) {
206   MS_EXCEPTION_IF_NULL(address);
207   return static_cast<uint8_t *>(address) + offset;
208 }
209 }  // namespace mindspore
210 
211 #endif  // MINDSPORE_CORE_UTILS_CONVERT_UTILS_BASE_H_
212