Marat Dukhan | fc188ed | 2021-06-03 12:21:22 -0700 | [diff] [blame^] | 1 | // Auto-generated file. Do not edit! |
| 2 | // Template: src/qs8-igemm/MRx4c2-sse.c.in |
| 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 <smmintrin.h> |
| 13 | |
| 14 | #include <xnnpack/igemm.h> |
| 15 | #include <xnnpack/math.h> |
| 16 | |
| 17 | |
| 18 | void xnn_qc8_igemm_minmax_fp32_ukernel_4x4c2__sse41_ld64( |
| 19 | size_t mr, |
| 20 | size_t nc, |
| 21 | size_t kc, |
| 22 | size_t ks, |
| 23 | const int8_t** restrict a, |
| 24 | const void* restrict w, |
| 25 | int8_t* restrict c, |
| 26 | size_t cm_stride, |
| 27 | size_t cn_stride, |
| 28 | size_t a_offset, |
| 29 | const int8_t* zero, |
| 30 | const union xnn_qs8_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN XNN_DISABLE_MSAN |
| 31 | { |
| 32 | assert(mr != 0); |
| 33 | assert(mr <= 4); |
| 34 | assert(nc != 0); |
| 35 | assert(kc != 0); |
| 36 | assert(ks != 0); |
| 37 | assert(ks % (4 * sizeof(void*)) == 0); |
| 38 | assert(a_offset % sizeof(int8_t) == 0); |
| 39 | assert(a != NULL); |
| 40 | assert(w != NULL); |
| 41 | assert(c != NULL); |
| 42 | |
| 43 | kc = round_up_po2(kc, 2); |
| 44 | int8_t* c0 = c; |
| 45 | int8_t* c1 = (int8_t*) ((uintptr_t) c0 + cm_stride); |
| 46 | if XNN_UNPREDICTABLE(mr < 2) { |
| 47 | c1 = c0; |
| 48 | } |
| 49 | int8_t* c2 = (int8_t*) ((uintptr_t) c1 + cm_stride); |
| 50 | if XNN_UNPREDICTABLE(mr <= 2) { |
| 51 | c2 = c1; |
| 52 | } |
| 53 | int8_t* c3 = (int8_t*) ((uintptr_t) c2 + cm_stride); |
| 54 | if XNN_UNPREDICTABLE(mr != 4) { |
| 55 | c3 = c2; |
| 56 | } |
| 57 | |
| 58 | do { |
| 59 | __m128i vacc0x0123 = _mm_loadu_si128((const __m128i*) w); |
| 60 | __m128i vacc1x0123 = vacc0x0123; |
| 61 | __m128i vacc2x0123 = vacc0x0123; |
| 62 | __m128i vacc3x0123 = vacc0x0123; |
| 63 | w = (const void*) ((uintptr_t) w + 4 * sizeof(int32_t)); |
| 64 | |
| 65 | size_t p = ks; |
| 66 | do { |
| 67 | const int8_t* restrict a0 = a[0]; |
| 68 | if XNN_UNPREDICTABLE(a0 != zero) { |
| 69 | a0 = (const int8_t*) ((uintptr_t) a0 + a_offset); |
| 70 | } |
| 71 | const int8_t* restrict a1 = a[1]; |
| 72 | if XNN_UNPREDICTABLE(a1 != zero) { |
| 73 | a1 = (const int8_t*) ((uintptr_t) a1 + a_offset); |
| 74 | } |
| 75 | const int8_t* restrict a2 = a[2]; |
| 76 | if XNN_UNPREDICTABLE(a2 != zero) { |
| 77 | a2 = (const int8_t*) ((uintptr_t) a2 + a_offset); |
| 78 | } |
| 79 | const int8_t* restrict a3 = a[3]; |
| 80 | if XNN_UNPREDICTABLE(a3 != zero) { |
| 81 | a3 = (const int8_t*) ((uintptr_t) a3 + a_offset); |
| 82 | } |
| 83 | a += 4; |
| 84 | |
| 85 | size_t k = kc; |
| 86 | while (k >= 8 * sizeof(int8_t)) { |
| 87 | const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0); |
| 88 | const __m128i vxa0 = _mm_cvtepi8_epi16(va0); |
| 89 | a0 += 8; |
| 90 | const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1); |
| 91 | const __m128i vxa1 = _mm_cvtepi8_epi16(va1); |
| 92 | a1 += 8; |
| 93 | const __m128i va2 = _mm_loadl_epi64((const __m128i*) a2); |
| 94 | const __m128i vxa2 = _mm_cvtepi8_epi16(va2); |
| 95 | a2 += 8; |
| 96 | const __m128i va3 = _mm_loadl_epi64((const __m128i*) a3); |
| 97 | const __m128i vxa3 = _mm_cvtepi8_epi16(va3); |
| 98 | a3 += 8; |
| 99 | |
| 100 | const __m128i vb0 = _mm_loadl_epi64((const __m128i*) w); |
| 101 | const __m128i vxb0 = _mm_cvtepi8_epi16(vb0); |
| 102 | |
| 103 | vacc0x0123 = _mm_add_epi32(vacc0x0123, |
| 104 | _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(0, 0, 0, 0)), vxb0)); |
| 105 | vacc1x0123 = _mm_add_epi32(vacc1x0123, |
| 106 | _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(0, 0, 0, 0)), vxb0)); |
| 107 | vacc2x0123 = _mm_add_epi32(vacc2x0123, |
| 108 | _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(0, 0, 0, 0)), vxb0)); |
| 109 | vacc3x0123 = _mm_add_epi32(vacc3x0123, |
| 110 | _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(0, 0, 0, 0)), vxb0)); |
| 111 | const __m128i vb1 = _mm_loadl_epi64((const __m128i*) ((uintptr_t) w + 8)); |
| 112 | const __m128i vxb1 = _mm_cvtepi8_epi16(vb1); |
| 113 | |
| 114 | vacc0x0123 = _mm_add_epi32(vacc0x0123, |
| 115 | _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1)); |
| 116 | vacc1x0123 = _mm_add_epi32(vacc1x0123, |
| 117 | _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(1, 1, 1, 1)), vxb1)); |
| 118 | vacc2x0123 = _mm_add_epi32(vacc2x0123, |
| 119 | _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(1, 1, 1, 1)), vxb1)); |
| 120 | vacc3x0123 = _mm_add_epi32(vacc3x0123, |
| 121 | _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(1, 1, 1, 1)), vxb1)); |
| 122 | const __m128i vb2 = _mm_loadl_epi64((const __m128i*) ((uintptr_t) w + 16)); |
| 123 | const __m128i vxb2 = _mm_cvtepi8_epi16(vb2); |
| 124 | |
| 125 | vacc0x0123 = _mm_add_epi32(vacc0x0123, |
| 126 | _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2)); |
| 127 | vacc1x0123 = _mm_add_epi32(vacc1x0123, |
| 128 | _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(2, 2, 2, 2)), vxb2)); |
| 129 | vacc2x0123 = _mm_add_epi32(vacc2x0123, |
| 130 | _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(2, 2, 2, 2)), vxb2)); |
| 131 | vacc3x0123 = _mm_add_epi32(vacc3x0123, |
| 132 | _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(2, 2, 2, 2)), vxb2)); |
| 133 | const __m128i vb3 = _mm_loadl_epi64((const __m128i*) ((uintptr_t) w + 24)); |
| 134 | const __m128i vxb3 = _mm_cvtepi8_epi16(vb3); |
| 135 | |
| 136 | vacc0x0123 = _mm_add_epi32(vacc0x0123, |
| 137 | _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(3, 3, 3, 3)), vxb3)); |
| 138 | vacc1x0123 = _mm_add_epi32(vacc1x0123, |
| 139 | _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(3, 3, 3, 3)), vxb3)); |
| 140 | vacc2x0123 = _mm_add_epi32(vacc2x0123, |
| 141 | _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(3, 3, 3, 3)), vxb3)); |
| 142 | vacc3x0123 = _mm_add_epi32(vacc3x0123, |
| 143 | _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(3, 3, 3, 3)), vxb3)); |
| 144 | |
| 145 | w = (const void*) ((uintptr_t) w + 32); |
| 146 | k -= 8 * sizeof(int8_t); |
| 147 | } |
| 148 | if (k != 0) { |
| 149 | const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0); |
| 150 | const __m128i vxa0 = _mm_cvtepi8_epi16(va0); |
| 151 | a0 = (const int8_t*) ((uintptr_t) a0 + k); |
| 152 | const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1); |
| 153 | const __m128i vxa1 = _mm_cvtepi8_epi16(va1); |
| 154 | a1 = (const int8_t*) ((uintptr_t) a1 + k); |
| 155 | const __m128i va2 = _mm_loadl_epi64((const __m128i*) a2); |
| 156 | const __m128i vxa2 = _mm_cvtepi8_epi16(va2); |
| 157 | a2 = (const int8_t*) ((uintptr_t) a2 + k); |
| 158 | const __m128i va3 = _mm_loadl_epi64((const __m128i*) a3); |
| 159 | const __m128i vxa3 = _mm_cvtepi8_epi16(va3); |
| 160 | a3 = (const int8_t*) ((uintptr_t) a3 + k); |
| 161 | |
| 162 | const __m128i vb0 = _mm_loadl_epi64((const __m128i*) w); |
| 163 | w = (const void*) ((uintptr_t) w + 8); |
| 164 | const __m128i vxb0 = _mm_unpacklo_epi8(vb0, _mm_cmpgt_epi8(_mm_setzero_si128(), vb0)); |
| 165 | |
| 166 | vacc0x0123 = _mm_add_epi32(vacc0x0123, |
| 167 | _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(0, 0, 0, 0)), vxb0)); |
| 168 | vacc1x0123 = _mm_add_epi32(vacc1x0123, |
| 169 | _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(0, 0, 0, 0)), vxb0)); |
| 170 | vacc2x0123 = _mm_add_epi32(vacc2x0123, |
| 171 | _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(0, 0, 0, 0)), vxb0)); |
| 172 | vacc3x0123 = _mm_add_epi32(vacc3x0123, |
| 173 | _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(0, 0, 0, 0)), vxb0)); |
| 174 | |
| 175 | if (k > 2 * sizeof(int8_t)) { |
| 176 | const __m128i vb1 = _mm_loadl_epi64((const __m128i*) w); |
| 177 | w = (const void*) ((uintptr_t) w + 8); |
| 178 | const __m128i vxb1 = _mm_unpacklo_epi8(vb1, _mm_cmpgt_epi8(_mm_setzero_si128(), vb1)); |
| 179 | |
| 180 | vacc0x0123 = _mm_add_epi32(vacc0x0123, |
| 181 | _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1)); |
| 182 | vacc1x0123 = _mm_add_epi32(vacc1x0123, |
| 183 | _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(1, 1, 1, 1)), vxb1)); |
| 184 | vacc2x0123 = _mm_add_epi32(vacc2x0123, |
| 185 | _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(1, 1, 1, 1)), vxb1)); |
| 186 | vacc3x0123 = _mm_add_epi32(vacc3x0123, |
| 187 | _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(1, 1, 1, 1)), vxb1)); |
| 188 | |
| 189 | if (k > 4 * sizeof(int8_t)) { |
| 190 | const __m128i vb2 = _mm_loadl_epi64((const __m128i*) w); |
| 191 | w = (const void*) ((uintptr_t) w + 8); |
| 192 | const __m128i vxb2 = _mm_unpacklo_epi8(vb2, _mm_cmpgt_epi8(_mm_setzero_si128(), vb2)); |
| 193 | |
| 194 | vacc0x0123 = _mm_add_epi32(vacc0x0123, |
| 195 | _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2)); |
| 196 | vacc1x0123 = _mm_add_epi32(vacc1x0123, |
| 197 | _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(2, 2, 2, 2)), vxb2)); |
| 198 | vacc2x0123 = _mm_add_epi32(vacc2x0123, |
| 199 | _mm_madd_epi16(_mm_shuffle_epi32(vxa2, _MM_SHUFFLE(2, 2, 2, 2)), vxb2)); |
| 200 | vacc3x0123 = _mm_add_epi32(vacc3x0123, |
| 201 | _mm_madd_epi16(_mm_shuffle_epi32(vxa3, _MM_SHUFFLE(2, 2, 2, 2)), vxb2)); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | p -= 4 * sizeof(void*); |
| 206 | } while (p != 0); |
| 207 | |
| 208 | __m128 vscaled0x0123 = _mm_cvtepi32_ps(vacc0x0123); |
| 209 | __m128 vscaled1x0123 = _mm_cvtepi32_ps(vacc1x0123); |
| 210 | __m128 vscaled2x0123 = _mm_cvtepi32_ps(vacc2x0123); |
| 211 | __m128 vscaled3x0123 = _mm_cvtepi32_ps(vacc3x0123); |
| 212 | |
| 213 | const __m128 vscale0123 = _mm_loadu_ps((const float*) w); |
| 214 | w = (const void*) ((uintptr_t) w + 4 * sizeof(float)); |
| 215 | vscaled0x0123 = _mm_mul_ps(vscaled0x0123, vscale0123); |
| 216 | vscaled1x0123 = _mm_mul_ps(vscaled1x0123, vscale0123); |
| 217 | vscaled2x0123 = _mm_mul_ps(vscaled2x0123, vscale0123); |
| 218 | vscaled3x0123 = _mm_mul_ps(vscaled3x0123, vscale0123); |
| 219 | |
| 220 | vacc0x0123 = _mm_cvtps_epi32(vscaled0x0123); |
| 221 | vacc1x0123 = _mm_cvtps_epi32(vscaled1x0123); |
| 222 | vacc2x0123 = _mm_cvtps_epi32(vscaled2x0123); |
| 223 | vacc3x0123 = _mm_cvtps_epi32(vscaled3x0123); |
| 224 | |
| 225 | const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->sse4.output_zero_point); |
| 226 | __m128i vacc01x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc0x0123, vacc1x0123), voutput_zero_point); |
| 227 | __m128i vacc23x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc2x0123, vacc3x0123), voutput_zero_point); |
| 228 | |
| 229 | |
| 230 | __m128i vout = _mm_packs_epi16(vacc01x0123, vacc23x0123); |
| 231 | |
| 232 | vout = _mm_max_epi8(vout, _mm_load_si128((const __m128i*) params->sse4.output_min)); |
| 233 | vout = _mm_min_epi8(vout, _mm_load_si128((const __m128i*) params->sse4.output_max)); |
| 234 | |
| 235 | if (nc >= 4) { |
| 236 | *((uint32_t*) c3) = (uint32_t) _mm_extract_epi32(vout, 3); |
| 237 | c3 = (int8_t*) ((uintptr_t) c3 + cn_stride); |
| 238 | *((uint32_t*) c2) = (uint32_t) _mm_extract_epi32(vout, 2); |
| 239 | c2 = (int8_t*) ((uintptr_t) c2 + cn_stride); |
| 240 | *((uint32_t*) c1) = (uint32_t) _mm_extract_epi32(vout, 1); |
| 241 | c1 = (int8_t*) ((uintptr_t) c1 + cn_stride); |
| 242 | *((uint32_t*) c0) = (uint32_t) _mm_cvtsi128_si32(vout); |
| 243 | c0 = (int8_t*) ((uintptr_t) c0 + cn_stride); |
| 244 | |
| 245 | a = (const int8_t**restrict) ((uintptr_t) a - ks); |
| 246 | |
| 247 | nc -= 4; |
| 248 | } else { |
| 249 | if (nc & 2) { |
| 250 | *((uint16_t*) c3) = (uint16_t) _mm_extract_epi16(vout, 6); |
| 251 | c3 += 2; |
| 252 | *((uint16_t*) c2) = (uint16_t) _mm_extract_epi16(vout, 4); |
| 253 | c2 += 2; |
| 254 | *((uint16_t*) c1) = (uint16_t) _mm_extract_epi16(vout, 2); |
| 255 | c1 += 2; |
| 256 | *((uint16_t*) c0) = (uint16_t) _mm_extract_epi16(vout, 0); |
| 257 | c0 += 2; |
| 258 | vout = _mm_srli_epi32(vout, 16); |
| 259 | } |
| 260 | if (nc & 1) { |
| 261 | *((int8_t*) c3) = (int8_t) _mm_extract_epi8(vout, 12); |
| 262 | *((int8_t*) c2) = (int8_t) _mm_extract_epi8(vout, 8); |
| 263 | *((int8_t*) c1) = (int8_t) _mm_extract_epi8(vout, 4); |
| 264 | *((int8_t*) c0) = (int8_t) _mm_extract_epi8(vout, 0); |
| 265 | } |
| 266 | |
| 267 | nc = 0; |
| 268 | } |
| 269 | } while (nc != 0); |
| 270 | } |