Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 1 | // Auto-generated file. Do not edit! |
Marat Dukhan | 66ccf64 | 2020-09-28 16:23:42 -0700 | [diff] [blame] | 2 | // Template: src/qs8-gemm/MRx4c2-sse.c.in |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 3 | // Generator: tools/xngen |
| 4 | // |
| 5 | // Copyright 2020 Google LLC |
| 6 | // |
| 7 | // This source code is licensed under the BSD-style license found in the |
| 8 | // LICENSE file in the root directory of this source tree. |
| 9 | |
| 10 | #include <assert.h> |
| 11 | |
| 12 | #include <tmmintrin.h> |
| 13 | |
| 14 | #include <xnnpack/gemm.h> |
Frank Barchard | 6d8ca7d | 2021-03-01 11:05:08 -0800 | [diff] [blame] | 15 | #include <xnnpack/math.h> |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 16 | |
| 17 | |
Marat Dukhan | fc188ed | 2021-06-03 12:21:22 -0700 | [diff] [blame] | 18 | |
Marat Dukhan | b07c26a | 2021-05-24 19:44:51 -0700 | [diff] [blame] | 19 | void xnn_qs8_gemm_minmax_gemmlowp_ukernel_4x4c2__ssse3_ld128( |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 20 | size_t mr, |
| 21 | size_t nc, |
| 22 | size_t kc, |
| 23 | const int8_t* restrict a, |
| 24 | size_t a_stride, |
| 25 | const void* restrict w, |
| 26 | int8_t* restrict c, |
| 27 | size_t cm_stride, |
| 28 | size_t cn_stride, |
Marat Dukhan | e3d17bf | 2021-05-24 22:22:43 -0700 | [diff] [blame] | 29 | const union xnn_qs8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN XNN_DISABLE_MSAN |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 30 | { |
| 31 | assert(mr != 0); |
| 32 | assert(mr <= 4); |
| 33 | assert(nc != 0); |
| 34 | assert(kc != 0); |
| 35 | assert(kc % sizeof(int8_t) == 0); |
| 36 | assert(a != NULL); |
| 37 | assert(w != NULL); |
| 38 | assert(c != NULL); |
| 39 | |
Frank Barchard | 6d8ca7d | 2021-03-01 11:05:08 -0800 | [diff] [blame] | 40 | kc = round_up_po2(kc, 2); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 41 | const int8_t* a0 = a; |
| 42 | int8_t* c0 = c; |
| 43 | const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride); |
| 44 | int8_t* c1 = (int8_t*) ((uintptr_t) c0 + cm_stride); |
| 45 | if XNN_UNPREDICTABLE(mr < 2) { |
| 46 | a1 = a0; |
| 47 | c1 = c0; |
| 48 | } |
| 49 | const int8_t* a2 = (const int8_t*) ((uintptr_t) a1 + a_stride); |
| 50 | int8_t* c2 = (int8_t*) ((uintptr_t) c1 + cm_stride); |
| 51 | if XNN_UNPREDICTABLE(mr <= 2) { |
| 52 | a2 = a1; |
| 53 | c2 = c1; |
| 54 | } |
| 55 | const int8_t* a3 = (const int8_t*) ((uintptr_t) a2 + a_stride); |
| 56 | int8_t* c3 = (int8_t*) ((uintptr_t) c2 + cm_stride); |
| 57 | if XNN_UNPREDICTABLE(mr != 4) { |
| 58 | a3 = a2; |
| 59 | c3 = c2; |
| 60 | } |
| 61 | |
| 62 | do { |
| 63 | __m128i vacc0x0123 = _mm_loadu_si128((const __m128i*) w); |
| 64 | __m128i vacc1x0123 = vacc0x0123; |
| 65 | __m128i vacc2x0123 = vacc0x0123; |
| 66 | __m128i vacc3x0123 = vacc0x0123; |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 67 | w = (const void*) ((const int32_t*) w + 4); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 68 | |
| 69 | size_t k = kc; |
| 70 | while (k >= 8 * sizeof(int8_t)) { |
| 71 | const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0); |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 72 | const __m128i vxa0 = _mm_srai_epi16(_mm_unpacklo_epi8(va0, va0), 8); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 73 | a0 += 8; |
| 74 | const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1); |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 75 | const __m128i vxa1 = _mm_srai_epi16(_mm_unpacklo_epi8(va1, va1), 8); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 76 | a1 += 8; |
| 77 | const __m128i va2 = _mm_loadl_epi64((const __m128i*) a2); |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 78 | const __m128i vxa2 = _mm_srai_epi16(_mm_unpacklo_epi8(va2, va2), 8); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 79 | a2 += 8; |
| 80 | const __m128i va3 = _mm_loadl_epi64((const __m128i*) a3); |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 81 | const __m128i vxa3 = _mm_srai_epi16(_mm_unpacklo_epi8(va3, va3), 8); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 82 | a3 += 8; |
| 83 | |
| 84 | const __m128i vb01 = _mm_loadu_si128((const __m128i*) w); |
| 85 | const __m128i vsb01 = _mm_cmpgt_epi8(_mm_setzero_si128(), vb01); |
| 86 | const __m128i vxb0 = _mm_unpacklo_epi8(vb01, vsb01); |
| 87 | const __m128i vxb1 = _mm_unpackhi_epi8(vb01, vsb01); |
| 88 | |
| 89 | vacc0x0123 = _mm_add_epi32(vacc0x0123, |
| 90 | _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(0, 0, 0, 0)), vxb0)); |
| 91 | vacc1x0123 = _mm_add_epi32(vacc1x0123, |
| 92 | _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(0, 0, 0, 0)), vxb0)); |
| 93 | vacc2x0123 = _mm_add_epi32(vacc2x0123, |
| 94 | _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(0, 0, 0, 0)), vxb0)); |
| 95 | vacc3x0123 = _mm_add_epi32(vacc3x0123, |
| 96 | _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(0, 0, 0, 0)), vxb0)); |
| 97 | |
| 98 | vacc0x0123 = _mm_add_epi32(vacc0x0123, |
| 99 | _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1)); |
| 100 | vacc1x0123 = _mm_add_epi32(vacc1x0123, |
| 101 | _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(1, 1, 1, 1)), vxb1)); |
| 102 | vacc2x0123 = _mm_add_epi32(vacc2x0123, |
| 103 | _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(1, 1, 1, 1)), vxb1)); |
| 104 | vacc3x0123 = _mm_add_epi32(vacc3x0123, |
| 105 | _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(1, 1, 1, 1)), vxb1)); |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 106 | const __m128i vb23 = _mm_loadu_si128((const __m128i*) ((const int8_t*) w + 16)); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 107 | const __m128i vsb23 = _mm_cmpgt_epi8(_mm_setzero_si128(), vb23); |
| 108 | const __m128i vxb2 = _mm_unpacklo_epi8(vb23, vsb23); |
| 109 | const __m128i vxb3 = _mm_unpackhi_epi8(vb23, vsb23); |
| 110 | |
| 111 | vacc0x0123 = _mm_add_epi32(vacc0x0123, |
| 112 | _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2)); |
| 113 | vacc1x0123 = _mm_add_epi32(vacc1x0123, |
| 114 | _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(2, 2, 2, 2)), vxb2)); |
| 115 | vacc2x0123 = _mm_add_epi32(vacc2x0123, |
| 116 | _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(2, 2, 2, 2)), vxb2)); |
| 117 | vacc3x0123 = _mm_add_epi32(vacc3x0123, |
| 118 | _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(2, 2, 2, 2)), vxb2)); |
| 119 | |
| 120 | vacc0x0123 = _mm_add_epi32(vacc0x0123, |
| 121 | _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(3, 3, 3, 3)), vxb3)); |
| 122 | vacc1x0123 = _mm_add_epi32(vacc1x0123, |
| 123 | _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(3, 3, 3, 3)), vxb3)); |
| 124 | vacc2x0123 = _mm_add_epi32(vacc2x0123, |
| 125 | _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(3, 3, 3, 3)), vxb3)); |
| 126 | vacc3x0123 = _mm_add_epi32(vacc3x0123, |
| 127 | _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(3, 3, 3, 3)), vxb3)); |
| 128 | |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 129 | w = (const void*) ((const int8_t*) w + 32); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 130 | k -= 8 * sizeof(int8_t); |
| 131 | } |
| 132 | if (k != 0) { |
| 133 | const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0); |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 134 | const __m128i vxa0 = _mm_srai_epi16(_mm_unpacklo_epi8(va0, va0), 8); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 135 | a0 = (const int8_t*) ((uintptr_t) a0 + k); |
| 136 | const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1); |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 137 | const __m128i vxa1 = _mm_srai_epi16(_mm_unpacklo_epi8(va1, va1), 8); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 138 | a1 = (const int8_t*) ((uintptr_t) a1 + k); |
| 139 | const __m128i va2 = _mm_loadl_epi64((const __m128i*) a2); |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 140 | const __m128i vxa2 = _mm_srai_epi16(_mm_unpacklo_epi8(va2, va2), 8); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 141 | a2 = (const int8_t*) ((uintptr_t) a2 + k); |
| 142 | const __m128i va3 = _mm_loadl_epi64((const __m128i*) a3); |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 143 | const __m128i vxa3 = _mm_srai_epi16(_mm_unpacklo_epi8(va3, va3), 8); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 144 | a3 = (const int8_t*) ((uintptr_t) a3 + k); |
| 145 | |
| 146 | const __m128i vb0 = _mm_loadl_epi64((const __m128i*) w); |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 147 | const __m128i vxb0 = _mm_srai_epi16(_mm_unpacklo_epi8(vb0, vb0), 8); |
| 148 | w = (const void*) ((const int8_t*) w + 8); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 149 | |
| 150 | vacc0x0123 = _mm_add_epi32(vacc0x0123, |
| 151 | _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(0, 0, 0, 0)), vxb0)); |
| 152 | vacc1x0123 = _mm_add_epi32(vacc1x0123, |
| 153 | _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(0, 0, 0, 0)), vxb0)); |
| 154 | vacc2x0123 = _mm_add_epi32(vacc2x0123, |
| 155 | _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(0, 0, 0, 0)), vxb0)); |
| 156 | vacc3x0123 = _mm_add_epi32(vacc3x0123, |
| 157 | _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(0, 0, 0, 0)), vxb0)); |
| 158 | |
| 159 | if (k > 2 * sizeof(int8_t)) { |
| 160 | const __m128i vb1 = _mm_loadl_epi64((const __m128i*) w); |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 161 | const __m128i vxb1 = _mm_srai_epi16(_mm_unpacklo_epi8(vb1, vb1), 8); |
| 162 | w = (const void*) ((const int8_t*) w + 8); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 163 | |
| 164 | vacc0x0123 = _mm_add_epi32(vacc0x0123, |
| 165 | _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1)); |
| 166 | vacc1x0123 = _mm_add_epi32(vacc1x0123, |
| 167 | _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(1, 1, 1, 1)), vxb1)); |
| 168 | vacc2x0123 = _mm_add_epi32(vacc2x0123, |
| 169 | _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(1, 1, 1, 1)), vxb1)); |
| 170 | vacc3x0123 = _mm_add_epi32(vacc3x0123, |
| 171 | _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(1, 1, 1, 1)), vxb1)); |
| 172 | |
| 173 | if (k > 4 * sizeof(int8_t)) { |
| 174 | const __m128i vb2 = _mm_loadl_epi64((const __m128i*) w); |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 175 | const __m128i vxb2 = _mm_srai_epi16(_mm_unpacklo_epi8(vb2, vb2), 8); |
| 176 | w = (const void*) ((const int8_t*) w + 8); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 177 | |
| 178 | vacc0x0123 = _mm_add_epi32(vacc0x0123, |
| 179 | _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2)); |
| 180 | vacc1x0123 = _mm_add_epi32(vacc1x0123, |
| 181 | _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(2, 2, 2, 2)), vxb2)); |
| 182 | vacc2x0123 = _mm_add_epi32(vacc2x0123, |
| 183 | _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(2, 2, 2, 2)), vxb2)); |
| 184 | vacc3x0123 = _mm_add_epi32(vacc3x0123, |
| 185 | _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(2, 2, 2, 2)), vxb2)); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
Marat Dukhan | 9b474cf | 2021-05-25 16:37:48 -0700 | [diff] [blame] | 190 | const __m128i vmultiplier = _mm_load_si128((const __m128i*) params->gemmlowp_sse2.multiplier); |
| 191 | const __m128i vrounding = _mm_load_si128((const __m128i*) params->gemmlowp_sse2.rounding); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 192 | |
| 193 | const __m128i vnmask0x0123 = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc0x0123); |
| 194 | const __m128i vnmask1x0123 = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc1x0123); |
| 195 | const __m128i vnmask2x0123 = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc2x0123); |
| 196 | const __m128i vnmask3x0123 = _mm_cmpgt_epi32(_mm_setzero_si128(), vacc3x0123); |
| 197 | |
| 198 | const __m128i vabsacc0x0123 = _mm_abs_epi32(vacc0x0123); |
| 199 | const __m128i vabsacc1x0123 = _mm_abs_epi32(vacc1x0123); |
| 200 | const __m128i vabsacc2x0123 = _mm_abs_epi32(vacc2x0123); |
| 201 | const __m128i vabsacc3x0123 = _mm_abs_epi32(vacc3x0123); |
| 202 | |
Marat Dukhan | 23848db | 2020-08-05 09:10:16 -0700 | [diff] [blame] | 203 | const __m128i vabsacc0x1133 = _mm_shuffle_epi32(vabsacc0x0123, _MM_SHUFFLE(3, 3, 1, 1)); |
| 204 | const __m128i vabsacc1x1133 = _mm_shuffle_epi32(vabsacc1x0123, _MM_SHUFFLE(3, 3, 1, 1)); |
| 205 | const __m128i vabsacc2x1133 = _mm_shuffle_epi32(vabsacc2x0123, _MM_SHUFFLE(3, 3, 1, 1)); |
| 206 | const __m128i vabsacc3x1133 = _mm_shuffle_epi32(vabsacc3x0123, _MM_SHUFFLE(3, 3, 1, 1)); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 207 | |
| 208 | const __m128i vabsprod0x02 = _mm_mul_epu32(vabsacc0x0123, vmultiplier); |
| 209 | const __m128i vabsprod1x02 = _mm_mul_epu32(vabsacc1x0123, vmultiplier); |
| 210 | const __m128i vabsprod2x02 = _mm_mul_epu32(vabsacc2x0123, vmultiplier); |
| 211 | const __m128i vabsprod3x02 = _mm_mul_epu32(vabsacc3x0123, vmultiplier); |
| 212 | |
| 213 | const __m128i vnmask0x02 = _mm_shuffle_epi32(vnmask0x0123, _MM_SHUFFLE(2, 2, 0, 0)); |
| 214 | const __m128i vnmask1x02 = _mm_shuffle_epi32(vnmask1x0123, _MM_SHUFFLE(2, 2, 0, 0)); |
| 215 | const __m128i vnmask2x02 = _mm_shuffle_epi32(vnmask2x0123, _MM_SHUFFLE(2, 2, 0, 0)); |
| 216 | const __m128i vnmask3x02 = _mm_shuffle_epi32(vnmask3x0123, _MM_SHUFFLE(2, 2, 0, 0)); |
| 217 | |
| 218 | const __m128i vprod0x02 = _mm_sub_epi64(_mm_xor_si128(vabsprod0x02, vnmask0x02), vnmask0x02); |
| 219 | const __m128i vprod1x02 = _mm_sub_epi64(_mm_xor_si128(vabsprod1x02, vnmask1x02), vnmask1x02); |
| 220 | const __m128i vprod2x02 = _mm_sub_epi64(_mm_xor_si128(vabsprod2x02, vnmask2x02), vnmask2x02); |
| 221 | const __m128i vprod3x02 = _mm_sub_epi64(_mm_xor_si128(vabsprod3x02, vnmask3x02), vnmask3x02); |
| 222 | |
| 223 | const __m128i vq31prod0x02 = _mm_srli_epi64(_mm_add_epi64(vprod0x02, vrounding), 31); |
| 224 | const __m128i vq31prod1x02 = _mm_srli_epi64(_mm_add_epi64(vprod1x02, vrounding), 31); |
| 225 | const __m128i vq31prod2x02 = _mm_srli_epi64(_mm_add_epi64(vprod2x02, vrounding), 31); |
| 226 | const __m128i vq31prod3x02 = _mm_srli_epi64(_mm_add_epi64(vprod3x02, vrounding), 31); |
| 227 | |
Marat Dukhan | 23848db | 2020-08-05 09:10:16 -0700 | [diff] [blame] | 228 | const __m128i vabsprod0x13 = _mm_mul_epu32(vabsacc0x1133, vmultiplier); |
| 229 | const __m128i vabsprod1x13 = _mm_mul_epu32(vabsacc1x1133, vmultiplier); |
| 230 | const __m128i vabsprod2x13 = _mm_mul_epu32(vabsacc2x1133, vmultiplier); |
| 231 | const __m128i vabsprod3x13 = _mm_mul_epu32(vabsacc3x1133, vmultiplier); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 232 | |
| 233 | const __m128i vnmask0x13 = _mm_shuffle_epi32(vnmask0x0123, _MM_SHUFFLE(3, 3, 1, 1)); |
| 234 | const __m128i vnmask1x13 = _mm_shuffle_epi32(vnmask1x0123, _MM_SHUFFLE(3, 3, 1, 1)); |
| 235 | const __m128i vnmask2x13 = _mm_shuffle_epi32(vnmask2x0123, _MM_SHUFFLE(3, 3, 1, 1)); |
| 236 | const __m128i vnmask3x13 = _mm_shuffle_epi32(vnmask3x0123, _MM_SHUFFLE(3, 3, 1, 1)); |
| 237 | |
| 238 | const __m128i vprod0x13 = _mm_sub_epi64(_mm_xor_si128(vabsprod0x13, vnmask0x13), vnmask0x13); |
| 239 | const __m128i vprod1x13 = _mm_sub_epi64(_mm_xor_si128(vabsprod1x13, vnmask1x13), vnmask1x13); |
| 240 | const __m128i vprod2x13 = _mm_sub_epi64(_mm_xor_si128(vabsprod2x13, vnmask2x13), vnmask2x13); |
| 241 | const __m128i vprod3x13 = _mm_sub_epi64(_mm_xor_si128(vabsprod3x13, vnmask3x13), vnmask3x13); |
| 242 | |
| 243 | const __m128i vq31prod0x13 = _mm_srli_epi64(_mm_add_epi64(vprod0x13, vrounding), 31); |
| 244 | const __m128i vq31prod1x13 = _mm_srli_epi64(_mm_add_epi64(vprod1x13, vrounding), 31); |
| 245 | const __m128i vq31prod2x13 = _mm_srli_epi64(_mm_add_epi64(vprod2x13, vrounding), 31); |
| 246 | const __m128i vq31prod3x13 = _mm_srli_epi64(_mm_add_epi64(vprod3x13, vrounding), 31); |
| 247 | |
| 248 | const __m128i vq31prod0x0213 = _mm_castps_si128(_mm_shuffle_ps( |
| 249 | _mm_castsi128_ps(vq31prod0x02), _mm_castsi128_ps(vq31prod0x13), _MM_SHUFFLE(2, 0, 2, 0))); |
| 250 | const __m128i vq31prod1x0213 = _mm_castps_si128(_mm_shuffle_ps( |
| 251 | _mm_castsi128_ps(vq31prod1x02), _mm_castsi128_ps(vq31prod1x13), _MM_SHUFFLE(2, 0, 2, 0))); |
| 252 | const __m128i vq31prod2x0213 = _mm_castps_si128(_mm_shuffle_ps( |
| 253 | _mm_castsi128_ps(vq31prod2x02), _mm_castsi128_ps(vq31prod2x13), _MM_SHUFFLE(2, 0, 2, 0))); |
| 254 | const __m128i vq31prod3x0213 = _mm_castps_si128(_mm_shuffle_ps( |
| 255 | _mm_castsi128_ps(vq31prod3x02), _mm_castsi128_ps(vq31prod3x13), _MM_SHUFFLE(2, 0, 2, 0))); |
| 256 | |
| 257 | const __m128i vq31prod0x0123 = _mm_shuffle_epi32(vq31prod0x0213, _MM_SHUFFLE(3, 1, 2, 0)); |
| 258 | const __m128i vq31prod1x0123 = _mm_shuffle_epi32(vq31prod1x0213, _MM_SHUFFLE(3, 1, 2, 0)); |
| 259 | const __m128i vq31prod2x0123 = _mm_shuffle_epi32(vq31prod2x0213, _MM_SHUFFLE(3, 1, 2, 0)); |
| 260 | const __m128i vq31prod3x0123 = _mm_shuffle_epi32(vq31prod3x0213, _MM_SHUFFLE(3, 1, 2, 0)); |
| 261 | |
Marat Dukhan | 9b474cf | 2021-05-25 16:37:48 -0700 | [diff] [blame] | 262 | const __m128i vremainder_mask = _mm_load_si128((const __m128i*) params->gemmlowp_sse2.remainder_mask); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 263 | const __m128i vrem0x0123 = |
| 264 | _mm_add_epi32(_mm_and_si128(vq31prod0x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod0x0123)); |
| 265 | const __m128i vrem1x0123 = |
| 266 | _mm_add_epi32(_mm_and_si128(vq31prod1x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod1x0123)); |
| 267 | const __m128i vrem2x0123 = |
| 268 | _mm_add_epi32(_mm_and_si128(vq31prod2x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod2x0123)); |
| 269 | const __m128i vrem3x0123 = |
| 270 | _mm_add_epi32(_mm_and_si128(vq31prod3x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod3x0123)); |
| 271 | |
Marat Dukhan | 9b474cf | 2021-05-25 16:37:48 -0700 | [diff] [blame] | 272 | const __m128i vremainder_threshold = _mm_load_si128((const __m128i*) params->gemmlowp_sse2.remainder_threshold); |
| 273 | const __m128i vshift = _mm_loadl_epi64((const __m128i*) params->gemmlowp_sse2.shift); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 274 | vacc0x0123 = |
| 275 | _mm_sub_epi32(_mm_sra_epi32(vq31prod0x0123, vshift), _mm_cmpgt_epi32(vrem0x0123, vremainder_threshold)); |
| 276 | vacc1x0123 = |
| 277 | _mm_sub_epi32(_mm_sra_epi32(vq31prod1x0123, vshift), _mm_cmpgt_epi32(vrem1x0123, vremainder_threshold)); |
| 278 | vacc2x0123 = |
| 279 | _mm_sub_epi32(_mm_sra_epi32(vq31prod2x0123, vshift), _mm_cmpgt_epi32(vrem2x0123, vremainder_threshold)); |
| 280 | vacc3x0123 = |
| 281 | _mm_sub_epi32(_mm_sra_epi32(vq31prod3x0123, vshift), _mm_cmpgt_epi32(vrem3x0123, vremainder_threshold)); |
| 282 | |
Marat Dukhan | 9b474cf | 2021-05-25 16:37:48 -0700 | [diff] [blame] | 283 | const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->gemmlowp_sse2.output_zero_point); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 284 | __m128i vacc01x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc0x0123, vacc1x0123), voutput_zero_point); |
| 285 | __m128i vacc23x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc2x0123, vacc3x0123), voutput_zero_point); |
| 286 | |
Marat Dukhan | 9b474cf | 2021-05-25 16:37:48 -0700 | [diff] [blame] | 287 | const __m128i voutput_min = _mm_load_si128((const __m128i*) params->gemmlowp_sse2.output_min); |
| 288 | const __m128i voutput_max = _mm_load_si128((const __m128i*) params->gemmlowp_sse2.output_max); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 289 | vacc01x0123 = _mm_min_epi16(_mm_max_epi16(vacc01x0123, voutput_min), voutput_max); |
| 290 | vacc23x0123 = _mm_min_epi16(_mm_max_epi16(vacc23x0123, voutput_min), voutput_max); |
| 291 | |
| 292 | __m128i vout = _mm_packs_epi16(vacc01x0123, vacc23x0123); |
| 293 | |
Marat Dukhan | 748fd12 | 2021-05-22 20:46:56 -0700 | [diff] [blame] | 294 | |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 295 | if (nc >= 4) { |
| 296 | *((uint32_t*) c0) = (uint32_t) _mm_cvtsi128_si32(vout); |
| 297 | vout = _mm_srli_si128(vout, 4); |
| 298 | *((uint32_t*) c1) = (uint32_t) _mm_cvtsi128_si32(vout); |
| 299 | vout = _mm_srli_si128(vout, 4); |
| 300 | *((uint32_t*) c2) = (uint32_t) _mm_cvtsi128_si32(vout); |
| 301 | vout = _mm_srli_si128(vout, 4); |
| 302 | *((uint32_t*) c3) = (uint32_t) _mm_cvtsi128_si32(vout); |
| 303 | |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 304 | c0 = (int8_t*) ((uintptr_t) c0 + cn_stride); |
| 305 | c1 = (int8_t*) ((uintptr_t) c1 + cn_stride); |
| 306 | c2 = (int8_t*) ((uintptr_t) c2 + cn_stride); |
| 307 | c3 = (int8_t*) ((uintptr_t) c3 + cn_stride); |
| 308 | |
Frank Barchard | 6d8ca7d | 2021-03-01 11:05:08 -0800 | [diff] [blame] | 309 | a0 = (const int8_t*) ((uintptr_t) a0 - kc); |
| 310 | a1 = (const int8_t*) ((uintptr_t) a1 - kc); |
| 311 | a2 = (const int8_t*) ((uintptr_t) a2 - kc); |
| 312 | a3 = (const int8_t*) ((uintptr_t) a3 - kc); |
| 313 | |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 314 | nc -= 4; |
| 315 | } else { |
| 316 | if (nc & 2) { |
| 317 | *((uint16_t*) c0) = (uint16_t) _mm_extract_epi16(vout, 0); |
| 318 | c0 += 2; |
| 319 | *((uint16_t*) c1) = (uint16_t) _mm_extract_epi16(vout, 2); |
| 320 | c1 += 2; |
| 321 | *((uint16_t*) c2) = (uint16_t) _mm_extract_epi16(vout, 4); |
| 322 | c2 += 2; |
| 323 | *((uint16_t*) c3) = (uint16_t) _mm_extract_epi16(vout, 6); |
| 324 | c3 += 2; |
| 325 | vout = _mm_srli_epi32(vout, 16); |
| 326 | } |
| 327 | if (nc & 1) { |
Marat Dukhan | cdbe9a3 | 2021-07-01 23:52:04 -0700 | [diff] [blame^] | 328 | *c0 = (int8_t) _mm_cvtsi128_si32(vout); |
| 329 | *c1 = (int8_t) _mm_extract_epi16(vout, 2); |
| 330 | *c2 = (int8_t) _mm_extract_epi16(vout, 4); |
| 331 | *c3 = (int8_t) _mm_extract_epi16(vout, 6); |
Marat Dukhan | dee732b | 2020-07-31 17:32:04 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | nc = 0; |
| 335 | } |
| 336 | } while (nc != 0); |
| 337 | } |