1 /**
2 * Copyright 2021-2022 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 #include "nnacl/fp32/mul_fp32.h"
17 #include "nnacl/fp32/arithmetic_fp32.h"
18 #include "nnacl/mul_fp32_simd.h"
19 #include "nnacl/errorcode.h"
20
BroadcastMul(const float * in0,const float * in1,float * tile_in0,float * tile_in1,float * out,int size,ArithmeticParameter * param)21 int BroadcastMul(const float *in0, const float *in1, float *tile_in0, float *tile_in1, float *out, int size,
22 ArithmeticParameter *param) {
23 TileDimensionsFp32(in0, in1, tile_in0, tile_in1, param);
24 return ElementMul(tile_in0, tile_in1, out, size);
25 }
26
ElementMul(const float * in0,const float * in1,float * out,int size)27 int ElementMul(const float *in0, const float *in1, float *out, int size) {
28 int index = 0;
29
30 SIMD_RUN_NO_SCALAR(ElementMul, index, in0, in1, out, size);
31 for (; index < size; index++) {
32 out[index] = in0[index] * in1[index];
33 }
34 return NNACL_OK;
35 }
36
ElementMulRelu(const float * in0,const float * in1,float * out,int size)37 int ElementMulRelu(const float *in0, const float *in1, float *out, int size) {
38 int index = 0;
39
40 SIMD_RUN_NO_SCALAR(ElementMulRelu, index, in0, in1, out, size);
41 for (; index < size; index++) {
42 float res = in0[index] * in1[index];
43 out[index] = res > 0 ? res : 0;
44 }
45 return NNACL_OK;
46 }
47
ElementMulRelu6(const float * in0,const float * in1,float * out,int size)48 int ElementMulRelu6(const float *in0, const float *in1, float *out, int size) {
49 int index = 0;
50
51 SIMD_RUN_NO_SCALAR(ElementMulRelu6, index, in0, in1, out, size);
52 for (; index < size; index++) {
53 out[index] = MSMIN(MSMAX(in0[index] * in1[index], 0), 6);
54 }
55 return NNACL_OK;
56 }
57
ElementMulInt(const int32_t * in0,const int32_t * in1,int32_t * out,int size)58 int ElementMulInt(const int32_t *in0, const int32_t *in1, int32_t *out, int size) {
59 int index = 0;
60
61 SIMD_RUN_NO_SCALAR(ElementMulInt, index, in0, in1, out, size);
62 for (; index < size; index++) {
63 out[index] = in0[index] * in1[index];
64 }
65 return NNACL_OK;
66 }
67
ElementMulReluInt(const int32_t * in0,const int32_t * in1,int32_t * out,int size)68 int ElementMulReluInt(const int32_t *in0, const int32_t *in1, int32_t *out, int size) {
69 int index = 0;
70
71 SIMD_RUN_NO_SCALAR(ElementMulReluInt, index, in0, in1, out, size);
72 for (; index < size; index++) {
73 int res = in0[index] * in1[index];
74 out[index] = res > 0 ? res : 0;
75 }
76 return NNACL_OK;
77 }
78
ElementMulRelu6Int(const int32_t * in0,const int32_t * in1,int32_t * out,int size)79 int ElementMulRelu6Int(const int32_t *in0, const int32_t *in1, int32_t *out, int size) {
80 int index = 0;
81
82 SIMD_RUN_NO_SCALAR(ElementMulRelu6Int, index, in0, in1, out, size);
83 for (; index < size; index++) {
84 out[index] = MSMIN(MSMAX(in0[index] * in1[index], 0), 6);
85 }
86 return NNACL_OK;
87 }
88
ElementOptMul(const float * in0,const float * in1,float * out,int size,bool first_scalar)89 int ElementOptMul(const float *in0, const float *in1, float *out, int size, bool first_scalar) {
90 int index = 0;
91 if (first_scalar) {
92 SIMD_RUN_NO_SCALAR(ElementOptMulNum0, index, in0, in1, out, size);
93
94 for (; index < size; index++) {
95 out[index] = in0[0] * in1[index];
96 }
97 } else {
98 SIMD_RUN_NO_SCALAR(ElementOptMulNum1, index, in0, in1, out, size);
99
100 for (; index < size; index++) {
101 out[index] = in0[index] * in1[0];
102 }
103 }
104 return NNACL_OK;
105 }
106
ElementOptMulRelu(const float * in0,const float * in1,float * out,int size,bool first_scalar)107 int ElementOptMulRelu(const float *in0, const float *in1, float *out, int size, bool first_scalar) {
108 int index = 0;
109 if (first_scalar) {
110 SIMD_RUN_NO_SCALAR(ElementOptMulReluNum0, index, in0, in1, out, size);
111 for (; index < size; index++) {
112 out[index] = MSMAX(in0[0] * in1[index], 0);
113 }
114 } else {
115 SIMD_RUN_NO_SCALAR(ElementOptMulReluNum1, index, in0, in1, out, size);
116 for (; index < size; index++) {
117 out[index] = MSMAX(in0[index] * in1[0], 0);
118 }
119 }
120 return NNACL_OK;
121 }
122
ElementOptMulRelu6(const float * in0,const float * in1,float * out,int size,bool first_scalar)123 int ElementOptMulRelu6(const float *in0, const float *in1, float *out, int size, bool first_scalar) {
124 int index = 0;
125 if (first_scalar) {
126 SIMD_RUN_NO_SCALAR(ElementOptMulRelu6Num0, index, in0, in1, out, size);
127
128 for (; index < size; index++) {
129 out[index] = MSMIN(MSMAX(in0[0] * in1[index], 0), 6);
130 }
131 } else {
132 SIMD_RUN_NO_SCALAR(ElementOptMulRelu6Num1, index, in0, in1, out, size);
133
134 for (; index < size; index++) {
135 out[index] = MSMIN(MSMAX(in0[index] * in1[0], 0), 6);
136 }
137 }
138 return NNACL_OK;
139 }
140
ElementOptMulInt(const int32_t * in0,const int32_t * in1,int32_t * out,int size,bool first_scalar)141 int ElementOptMulInt(const int32_t *in0, const int32_t *in1, int32_t *out, int size, bool first_scalar) {
142 int index = 0;
143 if (first_scalar) {
144 SIMD_RUN_NO_SCALAR(ElementOptMulIntNum0, index, in0, in1, out, size);
145 for (; index < size; index++) {
146 out[index] = in0[0] * in1[index];
147 }
148 } else {
149 SIMD_RUN_NO_SCALAR(ElementOptMulIntNum1, index, in0, in1, out, size);
150 for (; index < size; index++) {
151 out[index] = in0[index] * in1[0];
152 }
153 }
154 return NNACL_OK;
155 }
156
ElementOptMulReluInt(const int32_t * in0,const int32_t * in1,int32_t * out,int size,bool first_scalar)157 int ElementOptMulReluInt(const int32_t *in0, const int32_t *in1, int32_t *out, int size, bool first_scalar) {
158 int index = 0;
159 if (first_scalar) {
160 SIMD_RUN_NO_SCALAR(ElementOptMulReluIntNum0, index, in0, in1, out, size);
161 for (; index < size; index++) {
162 out[index] = MSMAX(in0[0] * in1[index], 0);
163 }
164 } else {
165 SIMD_RUN_NO_SCALAR(ElementOptMulReluIntNum1, index, in0, in1, out, size);
166 for (; index < size; index++) {
167 out[index] = MSMAX(in0[index] * in1[0], 0);
168 }
169 }
170 return NNACL_OK;
171 }
172
ElementOptMulRelu6Int(const int32_t * in0,const int32_t * in1,int32_t * out,int size,bool first_scalar)173 int ElementOptMulRelu6Int(const int32_t *in0, const int32_t *in1, int32_t *out, int size, bool first_scalar) {
174 int index = 0;
175 if (first_scalar) {
176 SIMD_RUN_NO_SCALAR(ElementOptMulRelu6IntNum0, index, in0, in1, out, size);
177 for (; index < size; index++) {
178 out[index] = MSMIN(MSMAX(in0[0] * in1[index], 0), 6);
179 }
180 } else {
181 SIMD_RUN_NO_SCALAR(ElementOptMulRelu6IntNum1, index, in0, in1, out, size);
182 for (; index < size; index++) {
183 out[index] = MSMIN(MSMAX(in0[index] * in1[0], 0), 6);
184 }
185 }
186 return NNACL_OK;
187 }
188