Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef HCCLV2_RANGE_UTIL_H
12 : #define HCCLV2_RANGE_UTIL_H
13 : #include <limits>
14 :
15 : namespace Hccl {
16 : /**
17 : * 判断以begin1为起点,长度为len1的范围是否包含以begin2为起点,长度为len2的范围
18 : * @tparam T1
19 : * @tparam T2
20 : * @param begin1 范围1的起点
21 : * @param len1 范围1的长度
22 : * @param begin2 范围2的起点
23 : * @param len2 范围2的长度
24 : * @return 范围1包含范围2则返回true;否则返回false
25 : */
26 : template <typename T1, typename T2>
27 17 : inline bool IsRangeInclude(T1 begin1, T2 len1, T1 begin2, T2 len2)
28 : {
29 : // 检查begin1 + len1是否会溢出
30 17 : if (len1 > static_cast<T2>(std::numeric_limits<T1>::max() - begin1)) {
31 0 : return false;
32 : }
33 : // 检查begin2 + len2是否会溢出
34 17 : if (len2 > static_cast<T2>(std::numeric_limits<T1>::max() - begin2)) {
35 0 : return false;
36 : }
37 17 : return (begin1 <= begin2 && begin1 + len1 >= begin2 + len2);
38 : }
39 : } // namespace Hccl
40 :
41 : #endif // HCCLV2_RANGE_UTIL_H
|