blob: 721b21cf794bc78b7ce70159cc1ce2d3de90279e [file] [log] [blame]
Marat Dukhan80fc9322019-09-29 21:06:36 -07001// Copyright 2019 Google LLC
2//
3// This source code is licensed under the BSD-style license found in the
4// LICENSE file in the root directory of this source tree.
XNNPACK Teamb455b122019-09-27 18:10:33 -07005
6#include <assert.h>
7
8#include <emmintrin.h>
9
10#include <xnnpack/argmaxpool.h>
11
12
Marat Dukhan329da642019-11-19 21:44:39 -080013void xnn_f32_argmaxpool_ukernel_4x__sse2_c4(
14 size_t output_pixels,
15 size_t pooling_elements,
16 size_t channels,
XNNPACK Teamb455b122019-09-27 18:10:33 -070017 const float** input,
Marat Dukhan329da642019-11-19 21:44:39 -080018 size_t input_offset,
XNNPACK Teamb455b122019-09-27 18:10:33 -070019 float* output,
20 uint32_t* index,
21 size_t input_increment,
22 size_t output_increment,
Marat Dukhanf196d012020-04-15 11:50:03 -070023 const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
XNNPACK Teamb455b122019-09-27 18:10:33 -070024{
Marat Dukhan329da642019-11-19 21:44:39 -080025 assert(output_pixels != 0);
26 assert(pooling_elements != 0);
27 assert(pooling_elements <= 4);
28 assert(channels != 0);
XNNPACK Teamb455b122019-09-27 18:10:33 -070029
30 const __m128 voutput_max = _mm_load_ps(params->sse.max);
31 const __m128 voutput_min = _mm_load_ps(params->sse.min);
32 do {
XNNPACK Teamb455b122019-09-27 18:10:33 -070033 const float* i0 = input[0];
34 const float* i1 = input[1];
35 const float* i2 = input[2];
36 const float* i3 = input[3];
Marat Dukhan329da642019-11-19 21:44:39 -080037 i0 = (const float*) ((uintptr_t) i0 + input_offset);
38 i1 = (const float*) ((uintptr_t) i1 + input_offset);
39 i2 = (const float*) ((uintptr_t) i2 + input_offset);
40 i3 = (const float*) ((uintptr_t) i3 + input_offset);
41 if (pooling_elements < 2) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070042 i1 = i0;
43 }
Marat Dukhan329da642019-11-19 21:44:39 -080044 if (pooling_elements <= 2) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070045 i2 = i0;
46 }
Marat Dukhan329da642019-11-19 21:44:39 -080047 if (pooling_elements != 4) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070048 i3 = i0;
49 }
50
Marat Dukhan329da642019-11-19 21:44:39 -080051 size_t c = channels;
52 for (; c >= 4; c -= 4) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070053 const __m128 vi0 = _mm_loadu_ps(i0);
54 i0 += 4;
55 const __m128 vi1 = _mm_loadu_ps(i1);
56 i1 += 4;
57 const __m128 vi2 = _mm_loadu_ps(i2);
58 i2 += 4;
59 const __m128 vi3 = _mm_loadu_ps(i3);
60 i3 += 4;
61
62 __m128 vmax = vi0;
63 __m128i vidx = _mm_setzero_si128();
64
65 const __m128i vm1 = _mm_castps_si128(_mm_cmpgt_ps(vi1, vmax));
66 vmax = _mm_max_ps(vi1, vmax);
67 vidx = _mm_or_si128(_mm_andnot_si128(vm1, vidx), _mm_and_si128(vm1, _mm_set1_epi32(1)));
68
69 const __m128i vm2 = _mm_castps_si128(_mm_cmpgt_ps(vi2, vmax));
70 vmax = _mm_max_ps(vi2, vmax);
71 vidx = _mm_or_si128(_mm_andnot_si128(vm2, vidx), _mm_and_si128(vm2, _mm_set1_epi32(2)));
72
73 const __m128i vm3 = _mm_castps_si128(_mm_cmpgt_ps(vi3, vmax));
74 vmax = _mm_max_ps(vi3, vmax);
75 vidx = _mm_or_si128(_mm_andnot_si128(vm3, vidx), _mm_and_si128(vm3, _mm_set1_epi32(3)));
76
77 const __m128 vout = _mm_max_ps(_mm_min_ps(vmax, voutput_max), voutput_min);
78
Marat Dukhan329da642019-11-19 21:44:39 -080079 _mm_storeu_ps(output, vout);
80 output += 4;
81 _mm_storeu_si128((__m128i*) index, vidx);
82 index += 4;
XNNPACK Teamb455b122019-09-27 18:10:33 -070083 }
Marat Dukhan329da642019-11-19 21:44:39 -080084 if (c != 0) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070085 const __m128 vi0 = _mm_loadu_ps(i0);
86 const __m128 vi1 = _mm_loadu_ps(i1);
87 const __m128 vi2 = _mm_loadu_ps(i2);
88 const __m128 vi3 = _mm_loadu_ps(i3);
89
90 __m128 vmax = vi0;
91 __m128i vidx = _mm_setzero_si128();
92
93 const __m128i vm1 = _mm_castps_si128(_mm_cmpgt_ps(vi1, vmax));
94 vmax = _mm_max_ps(vi1, vmax);
95 vidx = _mm_or_si128(_mm_andnot_si128(vm1, vidx), _mm_and_si128(vm1, _mm_set1_epi32(1)));
96
97 const __m128i vm2 = _mm_castps_si128(_mm_cmpgt_ps(vi2, vmax));
98 vmax = _mm_max_ps(vi2, vmax);
99 vidx = _mm_or_si128(_mm_andnot_si128(vm2, vidx), _mm_and_si128(vm2, _mm_set1_epi32(2)));
100
101 const __m128i vm3 = _mm_castps_si128(_mm_cmpgt_ps(vi3, vmax));
102 vmax = _mm_max_ps(vi3, vmax);
103 vidx = _mm_or_si128(_mm_andnot_si128(vm3, vidx), _mm_and_si128(vm3, _mm_set1_epi32(3)));
104
105 __m128 vout = _mm_max_ps(_mm_min_ps(vmax, voutput_max), voutput_min);
106
Marat Dukhan329da642019-11-19 21:44:39 -0800107 if (c & 2) {
108 _mm_store_sd((double*) output, _mm_castps_pd(vout));
109 _mm_storel_epi64((__m128i*) index, vidx);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700110 vout = _mm_movehl_ps(vout, vout);
111 vidx = _mm_unpackhi_epi64(vidx, vidx);
Marat Dukhan329da642019-11-19 21:44:39 -0800112 output += 2;
113 index += 2;
XNNPACK Teamb455b122019-09-27 18:10:33 -0700114 }
Marat Dukhan329da642019-11-19 21:44:39 -0800115 if (c & 1) {
116 _mm_store_ss(output, vout);
117 *index = (uint32_t) _mm_cvtsi128_si32(vidx);
118 output += 1;
119 index += 1;
XNNPACK Teamb455b122019-09-27 18:10:33 -0700120 }
121 }
122 input = (const float**) ((uintptr_t) input + input_increment);
Marat Dukhan329da642019-11-19 21:44:39 -0800123 output = (float*) ((uintptr_t) output + output_increment);
124 } while (--output_pixels != 0);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700125}