/** * Copyright 2020 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef MINDSPORE_CCSRC_UTILS_CONVERT_UTILS_BASE_H_ #define MINDSPORE_CCSRC_UTILS_CONVERT_UTILS_BASE_H_ #include #include #include "utils/log_adapter.h" namespace mindspore { inline int SizeToInt(size_t u) { if (u > static_cast((std::numeric_limits::max)())) { MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of int."; } return static_cast(u); } inline uint32_t SizeToUint(size_t u) { if (u > static_cast((std::numeric_limits::max)())) { MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of uint32_t."; } return static_cast(u); } inline int64_t SizeToLong(size_t u) { if (u > static_cast((std::numeric_limits::max)())) { MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of int64_t."; } return static_cast(u); } inline size_t IntToSize(int u) { if (u < 0) { MS_LOG(EXCEPTION) << "The int value(" << u << ") is less than 0."; } return static_cast(u); } inline size_t LongToSize(int64_t u) { if (u < 0) { MS_LOG(EXCEPTION) << "The int64_t value(" << u << ") is less than 0."; } return static_cast(u); } inline size_t FloatToSize(float u) { if (u < 0) { MS_LOG(EXCEPTION) << "The float value(" << u << ") is less than 0."; } if (u > static_cast((std::numeric_limits::max)())) { MS_LOG(EXCEPTION) << "The float value(" << u << ") exceeds the maximum value of size_t."; } return static_cast(u); } inline float IntToFloat(int32_t v) { return static_cast(v); } inline uint32_t IntToUint(int32_t u) { if (u < 0) { MS_LOG(EXCEPTION) << "The int32_t value(" << u << ") is less than 0."; } return static_cast(u); } inline int32_t UintToInt(uint32_t u) { if (u > static_cast((std::numeric_limits::max)())) { MS_LOG(EXCEPTION) << "The uint32_t value(" << u << ") exceeds the maximum value of int32_t."; } return static_cast(u); } inline unsigned int UlongToUint(size_t u) { if (u > static_cast((std::numeric_limits::max)())) { MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of unsigned int."; } return static_cast(u); } inline void IntMulWithOverflowCheck(int a, int b, int *c) { int out = a * b; if (a != 0) { bool ok = ((out / a) != b); if (ok) { MS_LOG(EXCEPTION) << "Mul: a(" << a << ") * b(" << b << ") result is overflow"; } } *c = out; } inline size_t SizetMulWithOverflowCheck(size_t a, size_t b) { size_t out = a * b; if (a != 0) { if ((out / a) != b) { MS_LOG(EXCEPTION) << "Mul: a(" << a << ") * b(" << b << ") result is overflow"; } } return out; } inline uint8_t *AddressOffset(void *address, size_t offset) { MS_EXCEPTION_IF_NULL(address); return static_cast(address) + offset; } } // namespace mindspore #endif // MINDSPORE_CCSRC_UTILS_CONVERT_UTILS_BASE_H_