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_NNACL_COMMON_FUNC_H_
18 #define MINDSPORE_NNACL_COMMON_FUNC_H_
19
20 #include <string.h>
21 #include "nnacl/op_base.h"
22 #include "nnacl/conv_parameter.h"
23 #include "nnacl/nnacl_common.h"
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 int8_t MinInt8(int8_t a, int8_t b);
30 int8_t MaxInt8(int8_t a, int8_t b);
31 int Offset(const int *shape, const int dim0, const int dim1, const int dim2, const int dim3);
32 int OffsetComm(const int *shape, const int dim0, const int dim1, const int dim2);
33 int Offset4d(const int *shape, const int *dims);
34 int Offset6d(const int *shape, const int *dims);
35
isAddOverflow(int32_t x,int32_t y)36 static inline bool isAddOverflow(int32_t x, int32_t y) {
37 int32_t sum = x + y;
38 return (x > 0 && y > 0 && sum < 0) || (x < 0 && y < 0 && sum > 0);
39 }
40
isMulOverflow(int32_t x,int32_t y)41 static inline bool isMulOverflow(int32_t x, int32_t y) {
42 int32_t p = x * y;
43 return (x != 0) && (p / x != y);
44 }
45
GetStride(int * strides,const int * shape,int length)46 static inline int GetStride(int *strides, const int *shape, int length) {
47 if (length <= 0) {
48 return 1;
49 }
50 int stride = 1;
51 for (int i = length - 1; i >= 0; --i) {
52 strides[i] = stride;
53 stride *= shape[i];
54 }
55 return stride;
56 }
57 #ifdef __cplusplus
58 }
59 #endif
60
61 #endif /* MINDSPORE_NNACL_COMMON_FUNC_H_ */
62