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
17 #include "nnacl/fp32/add_fp32.h"
18 #include "nnacl/fp32/arithmetic_fp32.h"
19 #include "nnacl/add_fp32_simd.h"
20
ElementOptAdd(const float * in0,const float * in1,float * out,int size,bool first_scalar)21 int ElementOptAdd(const float *in0, const float *in1, float *out, int size, bool first_scalar) {
22 int index = 0;
23 if (first_scalar) {
24 SIMD_RUN_NO_SCALAR(ElementOptAdd, index, in0, in1, out, size);
25 for (; index < size; index++) {
26 out[index] = in0[0] + in1[index];
27 }
28 } else {
29 SIMD_RUN_NO_SCALAR(ElementOptAdd, index, in1, in0, out, size);
30 for (; index < size; index++) {
31 out[index] = in0[index] + in1[0];
32 }
33 }
34 return NNACL_OK;
35 }
36
ElementOptAddExt(const float * in0,const float * in1,const float alpha,float * out,int size,bool first_scalar)37 int ElementOptAddExt(const float *in0, const float *in1, const float alpha, float *out, int size, bool first_scalar) {
38 int index = 0;
39 if (first_scalar) {
40 SIMD_RUN_NO_SCALAR(ElementOptAddExtNum0, index, in0, in1, alpha, out, size);
41 for (; index < size; index++) {
42 out[index] = in0[0] + in1[index] * alpha;
43 }
44 } else {
45 SIMD_RUN_NO_SCALAR(ElementOptAddExtNum1, index, in0, in1, alpha, out, size);
46 for (; index < size; index++) {
47 out[index] = in0[index] + in1[0] * alpha;
48 }
49 }
50 return NNACL_OK;
51 }
52
ElementOptAddInt(const int32_t * in0,const int32_t * in1,int32_t * out,int size,bool first_scalar)53 int ElementOptAddInt(const int32_t *in0, const int32_t *in1, int32_t *out, int size, bool first_scalar) {
54 int index = 0;
55 if (first_scalar) {
56 SIMD_RUN_NO_SCALAR(ElementOptAddInt, index, in0, in1, out, size);
57 for (; index < size; index++) {
58 out[index] = in0[0] + in1[index];
59 }
60 } else {
61 SIMD_RUN_NO_SCALAR(ElementOptAddInt, index, in1, in0, out, size);
62 for (; index < size; index++) {
63 out[index] = in0[index] + in1[0];
64 }
65 }
66 return NNACL_OK;
67 }
68
ElementOptAddRelu(const float * in0,const float * in1,float * out,int size,bool first_scalar)69 int ElementOptAddRelu(const float *in0, const float *in1, float *out, int size, bool first_scalar) {
70 int index = 0;
71 if (first_scalar) {
72 SIMD_RUN_NO_SCALAR(ElementOptAddRelu, index, in0, in1, out, size);
73 for (; index < size; index++) {
74 out[index] = MSMAX(in0[0] + in1[index], 0);
75 }
76 } else {
77 SIMD_RUN_NO_SCALAR(ElementOptAddRelu, index, in1, in0, out, size);
78 for (; index < size; index++) {
79 out[index] = MSMAX(in0[index] + in1[0], 0);
80 }
81 }
82 return NNACL_OK;
83 }
84
ElementOptAddRelu6(const float * in0,const float * in1,float * out,int size,bool first_scalar)85 int ElementOptAddRelu6(const float *in0, const float *in1, float *out, int size, bool first_scalar) {
86 int index = 0;
87 if (first_scalar) {
88 SIMD_RUN_NO_SCALAR(ElementOptAddRelu6, index, in0, in1, out, size);
89 for (; index < size; index++) {
90 out[index] = MSMIN(MSMAX(in0[0] + in1[index], 0), 6);
91 }
92 } else {
93 SIMD_RUN_NO_SCALAR(ElementOptAddRelu6, index, in1, in0, out, size);
94 for (; index < size; index++) {
95 out[index] = MSMIN(MSMAX(in0[index] + in1[0], 0), 6);
96 }
97 }
98 return NNACL_OK;
99 }
100
BroadcastAdd(const float * in0,const float * in1,float * tile_in0,float * tile_in1,float * out,int size,ArithmeticParameter * param)101 int BroadcastAdd(const float *in0, const float *in1, float *tile_in0, float *tile_in1, float *out, int size,
102 ArithmeticParameter *param) {
103 TileDimensionsFp32(in0, in1, tile_in0, tile_in1, param);
104 return ElementAdd(tile_in0, tile_in1, out, size);
105 }
106
ElementAdd(const float * in0,const float * in1,float * out,int size)107 int ElementAdd(const float *in0, const float *in1, float *out, int size) {
108 int index = 0;
109
110 SIMD_RUN_NO_SCALAR(ElementAdd, index, in0, in1, out, size);
111 for (; index < size; index++) {
112 out[index] = in0[index] + in1[index];
113 }
114 return NNACL_OK;
115 }
116
ElementAddExt(const float * in0,const float * in1,const float alpha,float * out,int size)117 int ElementAddExt(const float *in0, const float *in1, const float alpha, float *out, int size) {
118 int index = 0;
119
120 SIMD_RUN_NO_SCALAR(ElementAddExt, index, in0, in1, alpha, out, size);
121 for (; index < size; index++) {
122 out[index] = in0[index] + in1[index] * alpha;
123 }
124 return NNACL_OK;
125 }
126
ElementAddRelu(const float * in0,const float * in1,float * out,int size)127 int ElementAddRelu(const float *in0, const float *in1, float *out, int size) {
128 int index = 0;
129
130 SIMD_RUN_NO_SCALAR(ElementAddRelu, index, in0, in1, out, size);
131 for (; index < size; index++) {
132 float res = in0[index] + in1[index];
133 out[index] = res > 0 ? res : 0;
134 }
135 return NNACL_OK;
136 }
137
ElementAddRelu6(const float * in0,const float * in1,float * out,int size)138 int ElementAddRelu6(const float *in0, const float *in1, float *out, int size) {
139 int index = 0;
140
141 SIMD_RUN_NO_SCALAR(ElementAddRelu6, index, in0, in1, out, size);
142 for (; index < size; index++) {
143 out[index] = MSMIN(MSMAX(in0[index] + in1[index], 0), 6);
144 }
145 return NNACL_OK;
146 }
147
ElementAddInt(const int32_t * in0,const int32_t * in1,int32_t * out,int size)148 int ElementAddInt(const int32_t *in0, const int32_t *in1, int32_t *out, int size) {
149 int index = 0;
150
151 SIMD_RUN_NO_SCALAR(ElementAddInt, index, in0, in1, out, size);
152 for (; index < size; index++) {
153 out[index] = in0[index] + in1[index];
154 }
155 return NNACL_OK;
156 }
157