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/sub_fp32.h"
17 #include "nnacl/sub_fp32_simd.h"
18 #include "nnacl/errorcode.h"
19
ElementOptSub(const float * in0,const float * in1,float * out,int size,bool first_scalar)20 int ElementOptSub(const float *in0, const float *in1, float *out, int size, bool first_scalar) {
21 int index = 0;
22 if (first_scalar) {
23 SIMD_RUN_NO_SCALAR(ElementOptSubNum0, index, in0, in1, out, size);
24 for (; index < size; index++) {
25 out[index] = in0[0] - in1[index];
26 }
27 } else {
28 SIMD_RUN_NO_SCALAR(ElementOptSubNum1, index, in0, in1, out, size);
29 for (; index < size; index++) {
30 out[index] = in0[index] - in1[0];
31 }
32 }
33 return NNACL_OK;
34 }
35
ElementOptSubExt(const float * in0,const float * in1,const float alpha,float * out,int size,bool first_scalar)36 int ElementOptSubExt(const float *in0, const float *in1, const float alpha, float *out, int size, bool first_scalar) {
37 int index = 0;
38 if (first_scalar) {
39 SIMD_RUN_NO_SCALAR(ElementOptSubExtNum0, index, in0, in1, alpha, out, size);
40 for (; index < size; index++) {
41 out[index] = in0[0] - in1[index] * alpha;
42 }
43 } else {
44 SIMD_RUN_NO_SCALAR(ElementOptSubExtNum1, index, in0, in1, alpha, out, size);
45 for (; index < size; index++) {
46 out[index] = in0[index] - in1[0] * alpha;
47 }
48 }
49 return NNACL_OK;
50 }
51
ElementSubExt(const float * in0,const float * in1,const float alpha,float * out,int size)52 int ElementSubExt(const float *in0, const float *in1, const float alpha, float *out, int size) {
53 int index = 0;
54
55 SIMD_RUN_NO_SCALAR(ElementSubExt, index, in0, in1, alpha, out, size);
56 for (; index < size; index++) {
57 out[index] = in0[index] - in1[index] * alpha;
58 }
59 return NNACL_OK;
60 }
61
ElementOptSubInt(const int32_t * in0,const int32_t * in1,int32_t * out,int size,bool first_scalar)62 int ElementOptSubInt(const int32_t *in0, const int32_t *in1, int32_t *out, int size, bool first_scalar) {
63 int index = 0;
64 if (first_scalar) {
65 SIMD_RUN_NO_SCALAR(ElementOptSubIntNum0, index, in0, in1, out, size);
66 for (; index < size; index++) {
67 out[index] = in0[0] - in1[index];
68 }
69 } else {
70 SIMD_RUN_NO_SCALAR(ElementOptSubIntNum1, index, in0, in1, out, size);
71 for (; index < size; index++) {
72 out[index] = in0[index] - in1[0];
73 }
74 }
75 return NNACL_OK;
76 }
77
ElementOptSubRelu(const float * in0,const float * in1,float * out,int size,bool first_scalar)78 int ElementOptSubRelu(const float *in0, const float *in1, float *out, int size, bool first_scalar) {
79 int index = 0;
80 if (first_scalar) {
81 SIMD_RUN_NO_SCALAR(ElementOptSubReluNum0, index, in0, in1, out, size);
82 for (; index < size; index++) {
83 out[index] = MSMAX(in0[0] - in1[index], 0);
84 }
85 } else {
86 SIMD_RUN_NO_SCALAR(ElementOptSubReluNum1, index, in0, in1, out, size);
87 for (; index < size; index++) {
88 out[index] = MSMAX(in0[index] - in1[0], 0);
89 }
90 }
91 return NNACL_OK;
92 }
93
ElementOptSubRelu6(const float * in0,const float * in1,float * out,int size,bool first_scalar)94 int ElementOptSubRelu6(const float *in0, const float *in1, float *out, int size, bool first_scalar) {
95 int index = 0;
96 if (first_scalar) {
97 SIMD_RUN_NO_SCALAR(ElementOptSubRelu6Num0, index, in0, in1, out, size);
98 for (; index < size; index++) {
99 out[index] = MSMIN(MSMAX(in0[0] - in1[index], 0), 6);
100 }
101 } else {
102 SIMD_RUN_NO_SCALAR(ElementOptSubRelu6Num1, index, in0, in1, out, size);
103 for (; index < size; index++) {
104 out[index] = MSMIN(MSMAX(in0[index] - in1[0], 0), 6);
105 }
106 }
107 return NNACL_OK;
108 }
109
ElementSub(const float * in0,const float * in1,float * out,int size)110 int ElementSub(const float *in0, const float *in1, float *out, int size) {
111 int index = 0;
112
113 SIMD_RUN_NO_SCALAR(ElementSub, index, in0, in1, out, size);
114 for (; index < size; index++) {
115 out[index] = in0[index] - in1[index];
116 }
117 return NNACL_OK;
118 }
119
ElementSubInt(const int32_t * in0,const int32_t * in1,int32_t * out,int size)120 int ElementSubInt(const int32_t *in0, const int32_t *in1, int32_t *out, int size) {
121 int index = 0;
122
123 SIMD_RUN_NO_SCALAR(ElementSubInt, index, in0, in1, out, size);
124 for (; index < size; index++) {
125 out[index] = in0[index] - in1[index];
126 }
127 return NNACL_OK;
128 }
129
ElementSubRelu(const float * in0,const float * in1,float * out,int size)130 int ElementSubRelu(const float *in0, const float *in1, float *out, int size) {
131 int index = 0;
132
133 SIMD_RUN_NO_SCALAR(ElementSubRelu, index, in0, in1, out, size);
134 for (; index < size; index++) {
135 float res = in0[index] - in1[index];
136 out[index] = res > 0 ? res : 0;
137 }
138 return NNACL_OK;
139 }
140
ElementSubRelu6(const float * in0,const float * in1,float * out,int size)141 int ElementSubRelu6(const float *in0, const float *in1, float *out, int size) {
142 int index = 0;
143
144 SIMD_RUN_NO_SCALAR(ElementSubRelu6, index, in0, in1, out, size);
145 for (; index < size; index++) {
146 out[index] = MSMIN(MSMAX(in0[index] - in1[index], 0), 6);
147 }
148
149 return NNACL_OK;
150 }
151