Marat Dukhan | 80fc932 | 2019-09-29 21:06:36 -0700 | [diff] [blame] | 1 | // 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 Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 5 | |
| 6 | #include <assert.h> |
| 7 | |
| 8 | #include <emmintrin.h> |
| 9 | |
| 10 | #include <xnnpack/argmaxpool.h> |
| 11 | |
| 12 | |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 13 | void xnn_f32_argmaxpool_ukernel_4x__sse2_c4( |
| 14 | size_t output_pixels, |
| 15 | size_t pooling_elements, |
| 16 | size_t channels, |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 17 | const float** input, |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 18 | size_t input_offset, |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 19 | float* output, |
| 20 | uint32_t* index, |
| 21 | size_t input_increment, |
| 22 | size_t output_increment, |
Marat Dukhan | f196d01 | 2020-04-15 11:50:03 -0700 | [diff] [blame] | 23 | const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 24 | { |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 25 | assert(output_pixels != 0); |
| 26 | assert(pooling_elements != 0); |
| 27 | assert(pooling_elements <= 4); |
| 28 | assert(channels != 0); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 29 | |
| 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 Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 33 | const float* i0 = input[0]; |
| 34 | const float* i1 = input[1]; |
| 35 | const float* i2 = input[2]; |
| 36 | const float* i3 = input[3]; |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 37 | 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 Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 42 | i1 = i0; |
| 43 | } |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 44 | if (pooling_elements <= 2) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 45 | i2 = i0; |
| 46 | } |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 47 | if (pooling_elements != 4) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 48 | i3 = i0; |
| 49 | } |
| 50 | |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 51 | size_t c = channels; |
| 52 | for (; c >= 4; c -= 4) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 53 | 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 Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 79 | _mm_storeu_ps(output, vout); |
| 80 | output += 4; |
| 81 | _mm_storeu_si128((__m128i*) index, vidx); |
| 82 | index += 4; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 83 | } |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 84 | if (c != 0) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 85 | 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 Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 107 | if (c & 2) { |
| 108 | _mm_store_sd((double*) output, _mm_castps_pd(vout)); |
| 109 | _mm_storel_epi64((__m128i*) index, vidx); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 110 | vout = _mm_movehl_ps(vout, vout); |
| 111 | vidx = _mm_unpackhi_epi64(vidx, vidx); |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 112 | output += 2; |
| 113 | index += 2; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 114 | } |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 115 | if (c & 1) { |
| 116 | _mm_store_ss(output, vout); |
| 117 | *index = (uint32_t) _mm_cvtsi128_si32(vidx); |
| 118 | output += 1; |
| 119 | index += 1; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | input = (const float**) ((uintptr_t) input + input_increment); |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 123 | output = (float*) ((uintptr_t) output + output_increment); |
| 124 | } while (--output_pixels != 0); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 125 | } |