1 /** 2 * Copyright 2021 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 #ifndef AICPU_OPS_AICPU_COMMON_KERNEL_UTIL_H_ 17 #define AICPU_OPS_AICPU_COMMON_KERNEL_UTIL_H_ 18 #include <climits> 19 #include <limits> 20 #include "common/kernel_log.h" 21 #ifndef AICPU_VISIBILITY_API 22 #define AICPU_VISIBILITY_API __attribute__((visibility("default"))) IntToSize(int u)23inline size_t IntToSize(int u) { 24 if (u < 0) { 25 AICPU_LOGE("The int value [%d] is less than 0.", u); 26 return SIZE_MAX; 27 } 28 return static_cast<size_t>(u); 29 } 30 SizeToInt(size_t u)31inline int SizeToInt(size_t u) { 32 if (u > static_cast<size_t>((std::numeric_limits<int>::max)())) { 33 AICPU_LOGE("The size_t value [%lu] exceeds the maximum value of int.", u); 34 return INT_MAX; 35 } 36 return static_cast<int>(u); 37 } 38 LongToSize(int64_t u)39inline size_t LongToSize(int64_t u) { 40 if (u < 0) { 41 AICPU_LOGE("The int64_t value [%ld] is less than 0.", u); 42 return SIZE_MAX; 43 } 44 return static_cast<size_t>(u); 45 } 46 LongToInt(int64_t u)47inline int32_t LongToInt(int64_t u) { 48 if (u > static_cast<int64_t>((std::numeric_limits<int32_t>::max)())) { 49 AICPU_LOGE("The size_t value [%ld] exceeds the maximum value of int.", u); 50 return INT_MAX; 51 } 52 return static_cast<int32_t>(u); 53 } 54 #endif 55 #endif // AICPU_OPS_AICPU_COMMON_KERNEL_UTIL_H_ 56