Elffuss BraidHub

kikocis/ncnn public

master · 32.0 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#include "binaryop_x86.h"
16
17#if __SSE2__
18#include <emmintrin.h>
19#include "sse_mathfun.h"
20#if __AVX__
21#include <immintrin.h>
22#include "avx_mathfun.h"
23#if __AVX512F__
24#include "avx512_mathfun.h"
25#endif // __AVX512F__
26#endif // __AVX__
27#endif // __SSE2__
28
29namespace ncnn {
30
31BinaryOp_x86::BinaryOp_x86()
32{
33#if __SSE2__
34 support_packing = true;
35#endif // __SSE2__
36}
37
38template<typename Op>
39static void binary_op_vector_no_broadcast(const float* ptr, const float* ptr1, float* outptr, int size)
40{
41 const Op op;
42
43 int i = 0;
44#if __SSE2__
45#if __AVX__
46#if __AVX512F__
47 for (; i + 15 < size; i += 16)
48 {
49 __m512 _p = _mm512_loadu_ps(ptr);
50 __m512 _b = _mm512_loadu_ps(ptr1);
51 __m512 _outp = op.func_pack16(_p, _b);
52 _mm512_storeu_ps(outptr, _outp);
53 ptr += 16;
54 ptr1 += 16;
55 outptr += 16;
56 }
57#endif // __AVX512F__
58 for (; i + 7 < size; i += 8)
59 {
60 __m256 _p = _mm256_loadu_ps(ptr);
61 __m256 _b = _mm256_loadu_ps(ptr1);
62 __m256 _outp = op.func_pack8(_p, _b);
63 _mm256_storeu_ps(outptr, _outp);
64 ptr += 8;
65 ptr1 += 8;
66 outptr += 8;
67 }
68#endif // __AVX__
69 for (; i + 3 < size; i += 4)
70 {
71 __m128 _p = _mm_loadu_ps(ptr);
72 __m128 _b = _mm_loadu_ps(ptr1);
73 __m128 _outp = op.func_pack4(_p, _b);
74 _mm_storeu_ps(outptr, _outp);
75 ptr += 4;
76 ptr1 += 4;
77 outptr += 4;
78 }
79#endif // __SSE2__
80 for (; i < size; i++)
81 {
82 *outptr = op.func(*ptr, *ptr1);
83 ptr += 1;
84 ptr1 += 1;
85 outptr += 1;
86 }
87}
88
89template<typename Op>
90static void binary_op_vector_broadcast_b(const float* ptr, const float* ptr1, float* outptr, int size, int elempack)
91{
92 const Op op;
93
94 const float b = *ptr1;
95
96 int i = 0;
97#if __SSE2__
98 __m128 _b_128 = (elempack == 4) ? _mm_loadu_ps(ptr1) : _mm_set1_ps(b);
99#if __AVX__
100 __m256 _b_256 = (elempack == 8) ? _mm256_loadu_ps(ptr1) : _mm256_insertf128_ps(_mm256_castps128_ps256(_b_128), _b_128, 1);
101#if __AVX512F__
102 __m512 _b_512 = (elempack == 16) ? _mm512_loadu_ps(ptr1) : _mm512_insertf32x8(_mm512_castps256_ps512(_b_256), _b_256, 1);
103 for (; i + 15 < size; i += 16)
104 {
105 __m512 _p = _mm512_loadu_ps(ptr);
106 __m512 _outp = op.func_pack16(_p, _b_512);
107 _mm512_storeu_ps(outptr, _outp);
108 ptr += 16;
109 outptr += 16;
110 }
111#endif // __AVX512F__
112 for (; i + 7 < size; i += 8)
113 {
114 __m256 _p = _mm256_loadu_ps(ptr);
115 __m256 _outp = op.func_pack8(_p, _b_256);
116 _mm256_storeu_ps(outptr, _outp);
117 ptr += 8;
118 outptr += 8;
119 }
120#endif // __AVX__
121 for (; i + 3 < size; i += 4)
122 {
123 __m128 _p = _mm_loadu_ps(ptr);
124 __m128 _outp = op.func_pack4(_p, _b_128);
125 _mm_storeu_ps(outptr, _outp);
126 ptr += 4;
127 outptr += 4;
128 }
129#endif // __SSE2__
130 for (; i < size; i++)
131 {
132 *outptr = op.func(*ptr, b);
133 ptr += 1;
134 outptr += 1;
135 }
136}
137
138template<typename Op>
139static void binary_op_vector_broadcast_a(const float* ptr, const float* ptr1, float* outptr, int size, int elempack)
140{
141 const Op op;
142
143 const float a = *ptr;
144
145 int i = 0;
146#if __SSE2__
147 __m128 _a_128 = (elempack == 4) ? _mm_loadu_ps(ptr) : _mm_set1_ps(a);
148#if __AVX__
149 __m256 _a_256 = (elempack == 8) ? _mm256_loadu_ps(ptr) : _mm256_insertf128_ps(_mm256_castps128_ps256(_a_128), _a_128, 1);
150#if __AVX512F__
151 __m512 _a_512 = (elempack == 16) ? _mm512_loadu_ps(ptr) : _mm512_insertf32x8(_mm512_castps256_ps512(_a_256), _a_256, 1);
152 for (; i + 15 < size; i += 16)
153 {
154 __m512 _b = _mm512_loadu_ps(ptr1);
155 __m512 _outp = op.func_pack16(_a_512, _b);
156 _mm512_storeu_ps(outptr, _outp);
157 ptr1 += 16;
158 outptr += 16;
159 }
160#endif // __AVX512F__
161 for (; i + 7 < size; i += 8)
162 {
163 __m256 _b = _mm256_loadu_ps(ptr1);
164 __m256 _outp = op.func_pack8(_a_256, _b);
165 _mm256_storeu_ps(outptr, _outp);
166 ptr1 += 8;
167 outptr += 8;
168 }
169#endif // __AVX__
170 for (; i + 3 < size; i += 4)
171 {
172 __m128 _b = _mm_loadu_ps(ptr1);
173 __m128 _outp = op.func_pack4(_a_128, _b);
174 _mm_storeu_ps(outptr, _outp);
175 ptr1 += 4;
176 outptr += 4;
177 }
178#endif // __SSE2__
179 for (; i < size; i++)
180 {
181 *outptr = op.func(a, *ptr1);
182 ptr1 += 1;
183 outptr += 1;
184 }
185}
186
187template<typename Op>
188static void binary_op_vector_broadcast_pb(const float* ptr, const float* ptr1, float* outptr, int w, int elempack)
189{
190 const Op op;
191
192#if __SSE2__
193#if __AVX__
194#if __AVX512F__
195 if (elempack == 16)
196 {
197 int i = 0;
198 for (; i < w; i++)
199 {
200 __m512 _p = _mm512_loadu_ps(ptr);
201 __m512 _b = _mm512_set1_ps(*ptr1);
202 __m512 _outp = op.func_pack16(_p, _b);
203 _mm512_storeu_ps(outptr, _outp);
204 ptr += 16;
205 ptr1 += 1;
206 outptr += 16;
207 }
208 }
209#endif // __AVX512F__
210 if (elempack == 8)
211 {
212 int i = 0;
213#if __AVX512F__
214 for (; i + 1 < w; i += 2)
215 {
216 __m512 _p = _mm512_loadu_ps(ptr);
217 __m256 _b0 = _mm256_set1_ps(ptr1[0]);
218 __m256 _b1 = _mm256_set1_ps(ptr1[1]);
219 __m512 _b = _mm512_insertf32x8(_mm512_castps256_ps512(_b0), _b1, 1);
220 __m512 _outp = op.func_pack16(_p, _b);
221 _mm512_storeu_ps(outptr, _outp);
222 ptr += 16;
223 ptr1 += 2;
224 outptr += 16;
225 }
226#endif // __AVX512F__
227 for (; i < w; i++)
228 {
229 __m256 _p = _mm256_loadu_ps(ptr);
230 __m256 _b = _mm256_set1_ps(*ptr1);
231 __m256 _outp = op.func_pack8(_p, _b);
232 _mm256_storeu_ps(outptr, _outp);
233 ptr += 8;
234 ptr1 += 1;
235 outptr += 8;
236 }
237 }
238#endif // __AVX__
239 if (elempack == 4)
240 {
241 int i = 0;
242#if __AVX__
243#if __AVX512F__
244 for (; i + 3 < w; i += 4)
245 {
246 __m512 _p = _mm512_loadu_ps(ptr);
247 __m128 _b0 = _mm_set1_ps(ptr1[0]);
248 __m128 _b1 = _mm_set1_ps(ptr1[1]);
249 __m128 _b2 = _mm_set1_ps(ptr1[2]);
250 __m128 _b3 = _mm_set1_ps(ptr1[3]);
251 __m256 _b01 = _mm256_insertf128_ps(_mm256_castps128_ps256(_b0), _b1, 1);
252 __m256 _b23 = _mm256_insertf128_ps(_mm256_castps128_ps256(_b2), _b3, 1);
253 __m512 _b = _mm512_insertf32x8(_mm512_castps256_ps512(_b01), _b23, 1);
254 __m512 _outp = op.func_pack16(_p, _b);
255 _mm512_storeu_ps(outptr, _outp);
256 ptr += 16;
257 ptr1 += 4;
258 outptr += 16;
259 }
260#endif // __AVX512F__
261 for (; i + 1 < w; i += 2)
262 {
263 __m256 _p = _mm256_loadu_ps(ptr);
264 __m128 _b0 = _mm_set1_ps(ptr1[0]);
265 __m128 _b1 = _mm_set1_ps(ptr1[1]);
266 __m256 _b = _mm256_insertf128_ps(_mm256_castps128_ps256(_b0), _b1, 1);
267 __m256 _outp = op.func_pack8(_p, _b);
268 _mm256_storeu_ps(outptr, _outp);
269 ptr += 8;
270 ptr1 += 2;
271 outptr += 8;
272 }
273#endif // __AVX__
274 for (; i < w; i++)
275 {
276 __m128 _p = _mm_loadu_ps(ptr);
277 __m128 _b = _mm_set1_ps(*ptr1);
278 __m128 _outp = op.func_pack4(_p, _b);
279 _mm_storeu_ps(outptr, _outp);
280 ptr += 4;
281 ptr1 += 1;
282 outptr += 4;
283 }
284 }
285#endif // __SSE2__
286}
287
288template<typename Op>
289static void binary_op_vector_broadcast_pb_b(const float* ptr, const float* ptr1, float* outptr, int w, int elempack)
290{
291 const Op op;
292
293 const int size = w * elempack;
294
295 int i = 0;
296#if __SSE2__
297#if __AVX__
298#if __AVX512F__
299 __m512 _b_avx512 = _mm512_set1_ps(*ptr1);
300 for (; i + 15 < size; i += 16)
301 {
302 __m512 _p = _mm512_loadu_ps(ptr);
303 __m512 _outp = op.func_pack16(_p, _b_avx512);
304 _mm512_storeu_ps(outptr, _outp);
305 ptr += 16;
306 outptr += 16;
307 }
308#endif // __AVX512F__
309 __m256 _b_avx = _mm256_set1_ps(*ptr1);
310 for (; i + 7 < size; i += 8)
311 {
312 __m256 _p = _mm256_loadu_ps(ptr);
313 __m256 _outp = op.func_pack8(_p, _b_avx);
314 _mm256_storeu_ps(outptr, _outp);
315 ptr += 8;
316 outptr += 8;
317 }
318#endif // __AVX__
319 __m128 _b = _mm_set1_ps(*ptr1);
320 for (; i + 3 < size; i += 4)
321 {
322 __m128 _p = _mm_loadu_ps(ptr);
323 __m128 _outp = op.func_pack4(_p, _b);
324 _mm_storeu_ps(outptr, _outp);
325 ptr += 4;
326 outptr += 4;
327 }
328#endif // __SSE2__
329}
330
331template<typename Op>
332static void binary_op_vector_broadcast_pb_a(const float* ptr, const float* ptr1, float* outptr, int w, int elempack)
333{
334 const Op op;
335
336#if __SSE2__
337#if __AVX__
338#if __AVX512F__
339 if (elempack == 16)
340 {
341 int i = 0;
342 __m512 _p = _mm512_loadu_ps(ptr);
343 for (; i < w; i++)
344 {
345 __m512 _b = _mm512_set1_ps(*ptr1);
346 __m512 _outp = op.func_pack16(_p, _b);
347 _mm512_storeu_ps(outptr, _outp);
348 ptr1 += 1;
349 outptr += 16;
350 }
351 }
352#endif // __AVX512F__
353 if (elempack == 8)
354 {
355 int i = 0;
356 __m256 _p = _mm256_loadu_ps(ptr);
357#if __AVX512F__
358 __m512 _p_512 = _mm512_insertf32x8(_mm512_castps256_ps512(_p), _p, 1);
359 for (; i + 1 < w; i += 2)
360 {
361 __m256 _b0 = _mm256_set1_ps(ptr1[0]);
362 __m256 _b1 = _mm256_set1_ps(ptr1[1]);
363 __m512 _b = _mm512_insertf32x8(_mm512_castps256_ps512(_b0), _b1, 1);
364 __m512 _outp = op.func_pack16(_p_512, _b);
365 _mm512_storeu_ps(outptr, _outp);
366 ptr1 += 2;
367 outptr += 16;
368 }
369#endif // __AVX512F__
370 for (; i < w; i++)
371 {
372 __m256 _b = _mm256_set1_ps(*ptr1);
373 __m256 _outp = op.func_pack8(_p, _b);
374 _mm256_storeu_ps(outptr, _outp);
375 ptr1 += 1;
376 outptr += 8;
377 }
378 }
379#endif // __AVX__
380 if (elempack == 4)
381 {
382 int i = 0;
383 __m128 _p = _mm_loadu_ps(ptr);
384#if __AVX__
385 __m256 _p_256 = _mm256_insertf128_ps(_mm256_castps128_ps256(_p), _p, 1);
386#if __AVX512F__
387 __m512 _p_512 = _mm512_insertf32x8(_mm512_castps256_ps512(_p_256), _p_256, 1);
388 for (; i + 3 < w; i += 4)
389 {
390 __m128 _b0 = _mm_set1_ps(ptr1[0]);
391 __m128 _b1 = _mm_set1_ps(ptr1[1]);
392 __m128 _b2 = _mm_set1_ps(ptr1[2]);
393 __m128 _b3 = _mm_set1_ps(ptr1[3]);
394 __m256 _b01 = _mm256_insertf128_ps(_mm256_castps128_ps256(_b0), _b1, 1);
395 __m256 _b23 = _mm256_insertf128_ps(_mm256_castps128_ps256(_b2), _b3, 1);
396 __m512 _b = _mm512_insertf32x8(_mm512_castps256_ps512(_b01), _b23, 1);
397 __m512 _outp = op.func_pack16(_p_512, _b);
398 _mm512_storeu_ps(outptr, _outp);
399 ptr1 += 4;
400 outptr += 16;
401 }
402#endif // __AVX512F__
403 for (; i + 1 < w; i += 2)
404 {
405 __m128 _b0 = _mm_set1_ps(ptr1[0]);
406 __m128 _b1 = _mm_set1_ps(ptr1[1]);
407 __m256 _b = _mm256_insertf128_ps(_mm256_castps128_ps256(_b0), _b1, 1);
408 __m256 _outp = op.func_pack8(_p_256, _b);
409 _mm256_storeu_ps(outptr, _outp);
410 ptr1 += 2;
411 outptr += 8;
412 }
413#endif // __AVX__
414 for (; i < w; i++)
415 {
416 __m128 _b = _mm_set1_ps(*ptr1);
417 __m128 _outp = op.func_pack4(_p, _b);
418 _mm_storeu_ps(outptr, _outp);
419 ptr1 += 1;
420 outptr += 4;
421 }
422 }
423#endif // __SSE2__
424}
425
426template<typename Op>
427static void binary_op_vector(const float* ptr, const float* ptr1, float* outptr, int aw, int bw, int ap, int bp)
428{
429 const int w = std::max(aw, bw);
430 const int elempack = std::max(ap, bp);
431 const int size = w * elempack;
432
433 if (ap == bp)
434 {
435 if (aw == bw)
436 {
437 // no broadcast
438 return binary_op_vector_no_broadcast<Op>(ptr, ptr1, outptr, size);
439 }
440
441 if (bw == 1)
442 {
443 // broadcast single b
444 return binary_op_vector_broadcast_b<Op>(ptr, ptr1, outptr, size, elempack);
445 }
446
447 if (aw == 1)
448 {
449 // broadcast single a
450 return binary_op_vector_broadcast_a<Op>(ptr, ptr1, outptr, size, elempack);
451 }
452 }
453
454 if (bp == 1)
455 {
456 if (aw == bw)
457 {
458 // broadcast pack1 b
459 return binary_op_vector_broadcast_pb<Op>(ptr, ptr1, outptr, w, elempack);
460 }
461
462 if (bw == 1)
463 {
464 // broadcast pack1 single b
465 return binary_op_vector_broadcast_pb_b<Op>(ptr, ptr1, outptr, w, elempack);
466 }
467
468 if (aw == 1)
469 {
470 // broadcast single a and pack1 b
471 return binary_op_vector_broadcast_pb_a<Op>(ptr, ptr1, outptr, w, elempack);
472 }
473 }
474
475 // shall never reach here
476}
477
478namespace BinaryOp_x86_functor {
479
480struct binary_op_add
481{
482 NCNN_FORCEINLINE float func(const float& x, const float& y) const
483 {
484 return x + y;
485 }
486#if __SSE2__
487 NCNN_FORCEINLINE __m128 func_pack4(const __m128& x, const __m128& y) const
488 {
489 return _mm_add_ps(x, y);
490 }
491#if __AVX__
492 NCNN_FORCEINLINE __m256 func_pack8(const __m256& x, const __m256& y) const
493 {
494 return _mm256_add_ps(x, y);
495 }
496#if __AVX512F__
497 NCNN_FORCEINLINE __m512 func_pack16(const __m512& x, const __m512& y) const
498 {
499 return _mm512_add_ps(x, y);
500 }
501#endif // __AVX512F__
502#endif // __AVX__
503#endif // __SSE2__
504};
505
506struct binary_op_sub
507{
508 NCNN_FORCEINLINE float func(const float& x, const float& y) const
509 {
510 return x - y;
511 }
512#if __SSE2__
513 NCNN_FORCEINLINE __m128 func_pack4(const __m128& x, const __m128& y) const
514 {
515 return _mm_sub_ps(x, y);
516 }
517#if __AVX__
518 NCNN_FORCEINLINE __m256 func_pack8(const __m256& x, const __m256& y) const
519 {
520 return _mm256_sub_ps(x, y);
521 }
522#if __AVX512F__
523 NCNN_FORCEINLINE __m512 func_pack16(const __m512& x, const __m512& y) const
524 {
525 return _mm512_sub_ps(x, y);
526 }
527#endif // __AVX512F__
528#endif // __AVX__
529#endif // __SSE2__
530};
531
532struct binary_op_mul
533{
534 NCNN_FORCEINLINE float func(const float& x, const float& y) const
535 {
536 return x * y;
537 }
538#if __SSE2__
539 NCNN_FORCEINLINE __m128 func_pack4(const __m128& x, const __m128& y) const
540 {
541 return _mm_mul_ps(x, y);
542 }
543#if __AVX__
544 NCNN_FORCEINLINE __m256 func_pack8(const __m256& x, const __m256& y) const
545 {
546 return _mm256_mul_ps(x, y);
547 }
548#if __AVX512F__
549 NCNN_FORCEINLINE __m512 func_pack16(const __m512& x, const __m512& y) const
550 {
551 return _mm512_mul_ps(x, y);
552 }
553#endif // __AVX512F__
554#endif // __AVX__
555#endif // __SSE2__
556};
557
558struct binary_op_div
559{
560 NCNN_FORCEINLINE float func(const float& x, const float& y) const
561 {
562 return x / y;
563 }
564#if __SSE2__
565 NCNN_FORCEINLINE __m128 func_pack4(const __m128& x, const __m128& y) const
566 {
567 return _mm_div_ps(x, y);
568 }
569#if __AVX__
570 NCNN_FORCEINLINE __m256 func_pack8(const __m256& x, const __m256& y) const
571 {
572 return _mm256_div_ps(x, y);
573 }
574#if __AVX512F__
575 NCNN_FORCEINLINE __m512 func_pack16(const __m512& x, const __m512& y) const
576 {
577 return _mm512_div_ps(x, y);
578 }
579#endif // __AVX512F__
580#endif // __AVX__
581#endif // __SSE2__
582};
583
584struct binary_op_max
585{
586 NCNN_FORCEINLINE float func(const float& x, const float& y) const
587 {
588 return std::max(x, y);
589 }
590#if __SSE2__
591 NCNN_FORCEINLINE __m128 func_pack4(const __m128& x, const __m128& y) const
592 {
593 return _mm_max_ps(x, y);
594 }
595#if __AVX__
596 NCNN_FORCEINLINE __m256 func_pack8(const __m256& x, const __m256& y) const
597 {
598 return _mm256_max_ps(x, y);
599 }
600#if __AVX512F__
601 NCNN_FORCEINLINE __m512 func_pack16(const __m512& x, const __m512& y) const
602 {
603 return _mm512_max_ps(x, y);
604 }
605#endif // __AVX512F__
606#endif // __AVX__
607#endif // __SSE2__
608};
609
610struct binary_op_min
611{
612 NCNN_FORCEINLINE float func(const float& x, const float& y) const
613 {
614 return std::min(x, y);
615 }
616#if __SSE2__
617 NCNN_FORCEINLINE __m128 func_pack4(const __m128& x, const __m128& y) const
618 {
619 return _mm_min_ps(x, y);
620 }
621#if __AVX__
622 NCNN_FORCEINLINE __m256 func_pack8(const __m256& x, const __m256& y) const
623 {
624 return _mm256_min_ps(x, y);
625 }
626#if __AVX512F__
627 NCNN_FORCEINLINE __m512 func_pack16(const __m512& x, const __m512& y) const
628 {
629 return _mm512_min_ps(x, y);
630 }
631#endif // __AVX512F__
632#endif // __AVX__
633#endif // __SSE2__
634};
635
636struct binary_op_pow
637{
638 NCNN_FORCEINLINE float func(const float& x, const float& y) const
639 {
640 return (float)powf(x, y);
641 }
642#if __SSE2__
643 NCNN_FORCEINLINE __m128 func_pack4(const __m128& x, const __m128& y) const
644 {
645 return pow_ps(x, y);
646 }
647#if __AVX__
648 NCNN_FORCEINLINE __m256 func_pack8(const __m256& x, const __m256& y) const
649 {
650 return pow256_ps(x, y);
651 }
652#if __AVX512F__
653 NCNN_FORCEINLINE __m512 func_pack16(const __m512& x, const __m512& y) const
654 {
655 return pow512_ps(x, y);
656 }
657#endif // __AVX512F__
658#endif // __AVX__
659#endif // __SSE2__
660};
661
662struct binary_op_rsub
663{
664 NCNN_FORCEINLINE float func(const float& x, const float& y) const
665 {
666 return y - x;
667 }
668#if __SSE2__
669 NCNN_FORCEINLINE __m128 func_pack4(const __m128& x, const __m128& y) const
670 {
671 return _mm_sub_ps(y, x);
672 }
673#if __AVX__
674 NCNN_FORCEINLINE __m256 func_pack8(const __m256& x, const __m256& y) const
675 {
676 return _mm256_sub_ps(y, x);
677 }
678#if __AVX512F__
679 NCNN_FORCEINLINE __m512 func_pack16(const __m512& x, const __m512& y) const
680 {
681 return _mm512_sub_ps(y, x);
682 }
683#endif // __AVX512F__
684#endif // __AVX__
685#endif // __SSE2__
686};
687
688struct binary_op_rdiv
689{
690 NCNN_FORCEINLINE float func(const float& x, const float& y) const
691 {
692 return y / x;
693 }
694#if __SSE2__
695 NCNN_FORCEINLINE __m128 func_pack4(const __m128& x, const __m128& y) const
696 {
697 return _mm_div_ps(y, x);
698 }
699#if __AVX__
700 NCNN_FORCEINLINE __m256 func_pack8(const __m256& x, const __m256& y) const
701 {
702 return _mm256_div_ps(y, x);
703 }
704#if __AVX512F__
705 NCNN_FORCEINLINE __m512 func_pack16(const __m512& x, const __m512& y) const
706 {
707 return _mm512_div_ps(y, x);
708 }
709#endif // __AVX512F__
710#endif // __AVX__
711#endif // __SSE2__
712};
713
714struct binary_op_rpow
715{
716 NCNN_FORCEINLINE float func(const float& x, const float& y) const
717 {
718 return (float)powf(y, x);
719 }
720#if __SSE2__
721 NCNN_FORCEINLINE __m128 func_pack4(const __m128& x, const __m128& y) const
722 {
723 return pow_ps(y, x);
724 }
725#if __AVX__
726 NCNN_FORCEINLINE __m256 func_pack8(const __m256& x, const __m256& y) const
727 {
728 return pow256_ps(y, x);
729 }
730#if __AVX512F__
731 NCNN_FORCEINLINE __m512 func_pack16(const __m512& x, const __m512& y) const
732 {
733 return pow512_ps(y, x);
734 }
735#endif // __AVX512F__
736#endif // __AVX__
737#endif // __SSE2__
738};
739
740struct binary_op_atan2
741{
742 NCNN_FORCEINLINE float func(const float& x, const float& y) const
743 {
744 return (float)atan2f(x, y);
745 }
746#if __SSE2__
747 NCNN_FORCEINLINE __m128 func_pack4(const __m128& x, const __m128& y) const
748 {
749 return atan2_ps(x, y);
750 }
751#if __AVX__
752 NCNN_FORCEINLINE __m256 func_pack8(const __m256& x, const __m256& y) const
753 {
754 return atan2256_ps(x, y);
755 }
756#if __AVX512F__
757 NCNN_FORCEINLINE __m512 func_pack16(const __m512& x, const __m512& y) const
758 {
759 return atan2512_ps(x, y);
760 }
761#endif // __AVX512F__
762#endif // __AVX__
763#endif // __SSE2__
764};
765
766struct binary_op_ratan2
767{
768 NCNN_FORCEINLINE float func(const float& x, const float& y) const
769 {
770 return (float)atan2f(y, x);
771 }
772#if __SSE2__
773 NCNN_FORCEINLINE __m128 func_pack4(const __m128& x, const __m128& y) const
774 {
775 return atan2_ps(y, x);
776 }
777#if __AVX__
778 NCNN_FORCEINLINE __m256 func_pack8(const __m256& x, const __m256& y) const
779 {
780 return atan2256_ps(y, x);
781 }
782#if __AVX512F__
783 NCNN_FORCEINLINE __m512 func_pack16(const __m512& x, const __m512& y) const
784 {
785 return atan2512_ps(y, x);
786 }
787#endif // __AVX512F__
788#endif // __AVX__
789#endif // __SSE2__
790};
791
792} // namespace BinaryOp_x86_functor
793
794static void binary_op_vector(const float* ptr, const float* ptr1, float* outptr, int aw, int bw, int ap, int bp, int op_type)
795{
796 using namespace BinaryOp_x86_functor;
797
798 if (op_type == BinaryOp::Operation_ADD) return binary_op_vector<binary_op_add>(ptr, ptr1, outptr, aw, bw, ap, bp);
799 if (op_type == BinaryOp::Operation_SUB) return binary_op_vector<binary_op_sub>(ptr, ptr1, outptr, aw, bw, ap, bp);
800 if (op_type == BinaryOp::Operation_MUL) return binary_op_vector<binary_op_mul>(ptr, ptr1, outptr, aw, bw, ap, bp);
801 if (op_type == BinaryOp::Operation_DIV) return binary_op_vector<binary_op_div>(ptr, ptr1, outptr, aw, bw, ap, bp);
802 if (op_type == BinaryOp::Operation_MAX) return binary_op_vector<binary_op_max>(ptr, ptr1, outptr, aw, bw, ap, bp);
803 if (op_type == BinaryOp::Operation_MIN) return binary_op_vector<binary_op_min>(ptr, ptr1, outptr, aw, bw, ap, bp);
804 if (op_type == BinaryOp::Operation_POW) return binary_op_vector<binary_op_pow>(ptr, ptr1, outptr, aw, bw, ap, bp);
805 if (op_type == BinaryOp::Operation_RSUB) return binary_op_vector<binary_op_rsub>(ptr, ptr1, outptr, aw, bw, ap, bp);
806 if (op_type == BinaryOp::Operation_RDIV) return binary_op_vector<binary_op_rdiv>(ptr, ptr1, outptr, aw, bw, ap, bp);
807 if (op_type == BinaryOp::Operation_RPOW) return binary_op_vector<binary_op_rpow>(ptr, ptr1, outptr, aw, bw, ap, bp);
808 if (op_type == BinaryOp::Operation_ATAN2) return binary_op_vector<binary_op_atan2>(ptr, ptr1, outptr, aw, bw, ap, bp);
809 if (op_type == BinaryOp::Operation_RATAN2) return binary_op_vector<binary_op_ratan2>(ptr, ptr1, outptr, aw, bw, ap, bp);
810
811 // should never reach here
812}
813
814static void binary_op_scalar(const Mat& a, float b, Mat& c, int op_type, const Option& opt)
815{
816 const int channels = a.c;
817 const int size = a.w * a.h * a.d * a.elempack;
818
819 #pragma omp parallel for num_threads(opt.num_threads)
820 for (int q = 0; q < channels; q++)
821 {
822 const float* ptr = a.channel(q);
823 float* outptr = c.channel(q);
824
825 binary_op_vector(ptr, &b, outptr, size, 1, 1, 1, op_type);
826 }
827}
828
829static void binary_op_no_broadcast(const Mat& a, const Mat& b, Mat& c, int op_type, const Option& opt)
830{
831 const int channels = a.c;
832 const int size = a.w * a.h * a.d * a.elempack;
833
834 #pragma omp parallel for num_threads(opt.num_threads)
835 for (int q = 0; q < channels; q++)
836 {
837 const float* ptr = a.channel(q);
838 const float* ptr1 = b.channel(q);
839 float* outptr = c.channel(q);
840
841 binary_op_vector(ptr, ptr1, outptr, size, size, 1, 1, op_type);
842 }
843}
844
845static void binary_op_broadcast(const Mat& a, const Mat& b, Mat& c, int op_type, const Option& opt)
846{
847 if (b.w * b.h * b.d * b.c * b.elempack == 1)
848 {
849 return binary_op_scalar(a, b[0], c, op_type, opt);
850 }
851
852 if (a.dims == b.dims && a.w == b.w && a.h == b.h && a.d == b.d && a.c == b.c && a.elempack == b.elempack)
853 {
854 return binary_op_no_broadcast(a, b, c, op_type, opt);
855 }
856
857 const int dims = c.dims;
858
859 if (dims == 2)
860 {
861 const int h = c.h;
862
863 #pragma omp parallel for num_threads(opt.num_threads)
864 for (int y = 0; y < h; y++)
865 {
866 const int y0 = std::min(y, a.h - 1);
867 const int y1 = std::min(y, b.h - 1);
868
869 const float* ptr = a.row(y0);
870 const float* ptr1 = b.row(y1);
871 float* outptr = c.row(y);
872
873 binary_op_vector(ptr, ptr1, outptr, a.w, b.w, a.elempack, b.elempack, op_type);
874 }
875 }
876
877 if (dims == 3 || dims == 4)
878 {
879 const int channels = c.c;
880
881 #pragma omp parallel for num_threads(opt.num_threads)
882 for (int q = 0; q < channels; q++)
883 {
884 const int q0 = std::min(q, a.c - 1);
885 const int q1 = std::min(q, b.c - 1);
886
887 if (b.d * b.h * b.w == 1)
888 {
889 const float* ptr = a.channel(q0);
890 const float* ptr1 = b.channel(q1);
891 float* outptr = c.channel(q);
892
893 binary_op_vector(ptr, ptr1, outptr, a.w * a.h * a.d, 1, a.elempack, b.elempack, op_type);
894 continue;
895 }
896
897 if (b.h * b.w == 1)
898 {
899 for (int z = 0; z < c.d; z++)
900 {
901 const int z0 = std::min(z, a.d - 1);
902 const int z1 = std::min(z, b.d - 1);
903
904 const float* ptr = a.channel(q0).depth(z0);
905 const float* ptr1 = b.channel(q1).depth(z1);
906 float* outptr = c.channel(q).depth(z);
907
908 binary_op_vector(ptr, ptr1, outptr, a.w * a.h, 1, a.elempack, b.elempack, op_type);
909 }
910 continue;
911 }
912
913 for (int z = 0; z < c.d; z++)
914 {
915 const int z0 = std::min(z, a.d - 1);
916 const int z1 = std::min(z, b.d - 1);
917
918 for (int y = 0; y < c.h; y++)
919 {
920 const int y0 = std::min(y, a.h - 1);
921 const int y1 = std::min(y, b.h - 1);
922
923 const float* ptr = a.channel(q0).depth(z0).row(y0);
924 const float* ptr1 = b.channel(q1).depth(z1).row(y1);
925 float* outptr = c.channel(q).depth(z).row(y);
926
927 binary_op_vector(ptr, ptr1, outptr, a.w, b.w, a.elempack, b.elempack, op_type);
928 }
929 }
930 }
931 }
932}
933
934static void binary_op_scalar_inplace(Mat& a, float b, int op_type, const Option& opt)
935{
936 const int channels = a.c;
937 const int size = a.w * a.h * a.d * a.elempack;
938
939 #pragma omp parallel for num_threads(opt.num_threads)
940 for (int q = 0; q < channels; q++)
941 {
942 float* ptr = a.channel(q);
943
944 binary_op_vector(ptr, &b, ptr, size, 1, 1, 1, op_type);
945 }
946}
947
948static int get_reverse_op_type(int op_type)
949{
950 if (op_type == BinaryOp::Operation_SUB) return BinaryOp::Operation_RSUB;
951 if (op_type == BinaryOp::Operation_DIV) return BinaryOp::Operation_RDIV;
952 if (op_type == BinaryOp::Operation_POW) return BinaryOp::Operation_RPOW;
953 if (op_type == BinaryOp::Operation_ATAN2) return BinaryOp::Operation_RATAN2;
954 if (op_type == BinaryOp::Operation_RSUB) return BinaryOp::Operation_SUB;
955 if (op_type == BinaryOp::Operation_RDIV) return BinaryOp::Operation_DIV;
956 if (op_type == BinaryOp::Operation_RPOW) return BinaryOp::Operation_POW;
957 if (op_type == BinaryOp::Operation_RATAN2) return BinaryOp::Operation_ATAN2;
958 return op_type;
959}
960
961int BinaryOp_x86::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
962{
963 const Mat& A = bottom_blobs[0];
964 const Mat& B = bottom_blobs[1];
965 const int outdims = std::max(A.dims, B.dims);
966
967 Mat A2 = A;
968 Mat B2 = B;
969 if (A.dims < outdims)
970 {
971 // expand inner axes
972 if (outdims == 2)
973 {
974 if (A.w * A.elempack == B.h * B.elempack)
975 A2 = A.reshape(1, A.w, opt.workspace_allocator);
976 else // if (A.w == B.w)
977 {
978 A2.dims = 2;
979 A2.w = A.w * A.elempack;
980 A2.elempack = 1;
981 A2.elemsize = A.elemsize / A.elempack;
982 A2.cstep = A2.w;
983 }
984 }
985 if (outdims == 3 && A.dims == 1)
986 {
987 if (A.w * A.elempack == B.c * B.elempack)
988 A2 = A.reshape(1, 1, A.w, opt.workspace_allocator);
989 else // if (A.w == B.w)
990 {
991 A2.dims = 3;
992 A2.w = A.w * A.elempack;
993 A2.elempack = 1;
994 A2.elemsize = A.elemsize / A.elempack;
995 A2.cstep = A2.w;
996 }
997 }
998 if (outdims == 3 && A.dims == 2)
999 A2 = A.reshape(1, A.w, A.h, opt.workspace_allocator);
1000 if (outdims == 4 && A.dims == 1)
1001 {
1002 if (A.w * A.elempack == B.c * B.elempack)
1003 A2 = A.reshape(1, 1, 1, A.w, opt.workspace_allocator);
1004 else // if (A.w == B.w)
1005 {
1006 A2.dims = 4;
1007 A2.w = A.w * A.elempack;
1008 A2.elempack = 1;
1009 A2.elemsize = A.elemsize / A.elempack;
1010 A2.cstep = A2.w;
1011 }
1012 }
1013 if (outdims == 4 && A.dims == 2)
1014 A2 = A.reshape(1, 1, A.w, A.h, opt.workspace_allocator);
1015 if (outdims == 4 && A.dims == 3)
1016 A2 = A.reshape(1, A.w, A.h, A.c, opt.workspace_allocator);
1017 }
1018 if (B.dims < outdims)
1019 {
1020 // expand inner axes
1021 if (outdims == 2)
1022 {
1023 if (B.w * B.elempack == A.h * A.elempack)
1024 B2 = B.reshape(1, B.w, opt.workspace_allocator);
1025 else // if (B.w == A.w)
1026 {
1027 B2.dims = 2;
1028 B2.w = B.w * B.elempack;
1029 B2.elempack = 1;
1030 B2.elemsize = B.elemsize / B.elempack;
1031 B2.cstep = B2.w;
1032 }
1033 }
1034 if (outdims == 3 && B.dims == 1)
1035 {
1036 if (B.w * B.elempack == A.c * A.elempack)
1037 B2 = B.reshape(1, 1, B.w, opt.workspace_allocator);
1038 else // if (B.w == A.w)
1039 {
1040 B2.dims = 3;
1041 B2.w = B.w * B.elempack;
1042 B2.elempack = 1;
1043 B2.elemsize = B.elemsize / B.elempack;
1044 B2.cstep = B2.w;
1045 }
1046 }
1047 if (outdims == 3 && B.dims == 2)
1048 B2 = B.reshape(1, B.w, B.h, opt.workspace_allocator);
1049 if (outdims == 4 && B.dims == 1)
1050 {
1051 if (B.w * B.elempack == A.c * A.elempack)
1052 B2 = B.reshape(1, 1, 1, B.w, opt.workspace_allocator);
1053 else // if (B.w == A.w)
1054 {
1055 B2.dims = 4;
1056 B2.w = B.w * B.elempack;
1057 B2.elempack = 1;
1058 B2.elemsize = B.elemsize / B.elempack;
1059 B2.cstep = B2.w;
1060 }
1061 }
1062 if (outdims == 4 && B.dims == 2)
1063 B2 = B.reshape(1, 1, B.w, B.h, opt.workspace_allocator);
1064 if (outdims == 4 && B.dims == 3)
1065 B2 = B.reshape(1, B.w, B.h, B.c, opt.workspace_allocator);
1066 }
1067
1068 const int outw = std::max(A2.w, B2.w);
1069 const int outh = std::max(A2.h, B2.h);
1070 const int outd = std::max(A2.d, B2.d);
1071 const int outc = std::max(A2.c, B2.c);
1072 const size_t out_elemsize = std::max(A2.elemsize, B2.elemsize);
1073 const int out_elempack = std::max(A2.elempack, B2.elempack);
1074
1075 Mat& top_blob = top_blobs[0];
1076 if (outdims == 1)
1077 {
1078 top_blob.create(outw, out_elemsize, out_elempack, opt.blob_allocator);
1079 }
1080 if (outdims == 2)
1081 {
1082 top_blob.create(outw, outh, out_elemsize, out_elempack, opt.blob_allocator);
1083 }
1084 if (outdims == 3)
1085 {
1086 top_blob.create(outw, outh, outc, out_elemsize, out_elempack, opt.blob_allocator);
1087 }
1088 if (outdims == 4)
1089 {
1090 top_blob.create(outw, outh, outd, outc, out_elemsize, out_elempack, opt.blob_allocator);
1091 }
1092 if (top_blob.empty())
1093 return -100;
1094
1095 const bool a_pack_is_lower = A2.elempack < B2.elempack;
1096 const bool a_pack_is_equal = A2.elempack == B2.elempack;
1097 const bool a_size_is_lower = A2.w * A2.h * A2.d * A2.c * A2.elempack < B2.w * B2.h * B2.d * B2.c * B2.elempack;
1098 if (a_pack_is_lower || (a_pack_is_equal && a_size_is_lower))
1099 {
1100 binary_op_broadcast(B2, A2, top_blob, get_reverse_op_type(op_type), opt);
1101 }
1102 else
1103 {
1104 binary_op_broadcast(A2, B2, top_blob, op_type, opt);
1105 }
1106
1107 return 0;
1108}
1109
1110int BinaryOp_x86::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
1111{
1112 binary_op_scalar_inplace(bottom_top_blob, b, op_type, opt);
1113
1114 return 0;
1115}
1116
1117} // namespace ncnn