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_2x4c2__avx_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 <= 2); |
| 34 | assert(nc != 0); |
| 35 | assert(kc != 0); |
| 36 | assert(ks != 0); |
| 37 | assert(ks % (2 * 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 | |
| 50 | do { |
| 51 | __m128i vacc0x0123 = _mm_loadu_si128((const __m128i*) w); |
| 52 | __m128i vacc1x0123 = vacc0x0123; |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 53 | w = (const void*) ((const int32_t*) w + 4); |
Marat Dukhan | fc188ed | 2021-06-03 12:21:22 -0700 | [diff] [blame] | 54 | |
| 55 | size_t p = ks; |
| 56 | do { |
| 57 | const int8_t* restrict a0 = a[0]; |
| 58 | if XNN_UNPREDICTABLE(a0 != zero) { |
| 59 | a0 = (const int8_t*) ((uintptr_t) a0 + a_offset); |
| 60 | } |
| 61 | const int8_t* restrict a1 = a[1]; |
| 62 | if XNN_UNPREDICTABLE(a1 != zero) { |
| 63 | a1 = (const int8_t*) ((uintptr_t) a1 + a_offset); |
| 64 | } |
| 65 | a += 2; |
| 66 | |
| 67 | size_t k = kc; |
| 68 | while (k >= 8 * sizeof(int8_t)) { |
| 69 | const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0); |
| 70 | const __m128i vxa0 = _mm_cvtepi8_epi16(va0); |
| 71 | a0 += 8; |
| 72 | const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1); |
| 73 | const __m128i vxa1 = _mm_cvtepi8_epi16(va1); |
| 74 | a1 += 8; |
| 75 | |
| 76 | const __m128i vb0 = _mm_loadl_epi64((const __m128i*) w); |
| 77 | const __m128i vxb0 = _mm_cvtepi8_epi16(vb0); |
| 78 | |
| 79 | vacc0x0123 = _mm_add_epi32(vacc0x0123, |
| 80 | _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(0, 0, 0, 0)), vxb0)); |
| 81 | vacc1x0123 = _mm_add_epi32(vacc1x0123, |
| 82 | _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(0, 0, 0, 0)), vxb0)); |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 83 | const __m128i vb1 = _mm_loadl_epi64((const __m128i*) ((const int8_t*) w + 8)); |
Marat Dukhan | fc188ed | 2021-06-03 12:21:22 -0700 | [diff] [blame] | 84 | const __m128i vxb1 = _mm_cvtepi8_epi16(vb1); |
| 85 | |
| 86 | vacc0x0123 = _mm_add_epi32(vacc0x0123, |
| 87 | _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1)); |
| 88 | vacc1x0123 = _mm_add_epi32(vacc1x0123, |
| 89 | _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(1, 1, 1, 1)), vxb1)); |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 90 | const __m128i vb2 = _mm_loadl_epi64((const __m128i*) ((const int8_t*) w + 16)); |
Marat Dukhan | fc188ed | 2021-06-03 12:21:22 -0700 | [diff] [blame] | 91 | const __m128i vxb2 = _mm_cvtepi8_epi16(vb2); |
| 92 | |
| 93 | vacc0x0123 = _mm_add_epi32(vacc0x0123, |
| 94 | _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2)); |
| 95 | vacc1x0123 = _mm_add_epi32(vacc1x0123, |
| 96 | _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(2, 2, 2, 2)), vxb2)); |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 97 | const __m128i vb3 = _mm_loadl_epi64((const __m128i*) ((const int8_t*) w + 24)); |
Marat Dukhan | fc188ed | 2021-06-03 12:21:22 -0700 | [diff] [blame] | 98 | const __m128i vxb3 = _mm_cvtepi8_epi16(vb3); |
| 99 | |
| 100 | vacc0x0123 = _mm_add_epi32(vacc0x0123, |
| 101 | _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(3, 3, 3, 3)), vxb3)); |
| 102 | vacc1x0123 = _mm_add_epi32(vacc1x0123, |
| 103 | _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(3, 3, 3, 3)), vxb3)); |
| 104 | |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 105 | w = (const void*) ((const int8_t*) w + 32); |
Marat Dukhan | fc188ed | 2021-06-03 12:21:22 -0700 | [diff] [blame] | 106 | k -= 8 * sizeof(int8_t); |
| 107 | } |
| 108 | if (k != 0) { |
| 109 | const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0); |
| 110 | const __m128i vxa0 = _mm_cvtepi8_epi16(va0); |
| 111 | a0 = (const int8_t*) ((uintptr_t) a0 + k); |
| 112 | const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1); |
| 113 | const __m128i vxa1 = _mm_cvtepi8_epi16(va1); |
| 114 | a1 = (const int8_t*) ((uintptr_t) a1 + k); |
| 115 | |
| 116 | const __m128i vb0 = _mm_loadl_epi64((const __m128i*) w); |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 117 | w = (const void*) ((const int8_t*) w + 8); |
Marat Dukhan | cdbe9a3 | 2021-07-01 23:52:04 -0700 | [diff] [blame] | 118 | const __m128i vxb0 = _mm_cvtepi8_epi16(vb0); |
Marat Dukhan | fc188ed | 2021-06-03 12:21:22 -0700 | [diff] [blame] | 119 | |
| 120 | vacc0x0123 = _mm_add_epi32(vacc0x0123, |
| 121 | _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(0, 0, 0, 0)), vxb0)); |
| 122 | vacc1x0123 = _mm_add_epi32(vacc1x0123, |
| 123 | _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(0, 0, 0, 0)), vxb0)); |
| 124 | |
| 125 | if (k > 2 * sizeof(int8_t)) { |
| 126 | const __m128i vb1 = _mm_loadl_epi64((const __m128i*) w); |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 127 | w = (const void*) ((const int8_t*) w + 8); |
Marat Dukhan | cdbe9a3 | 2021-07-01 23:52:04 -0700 | [diff] [blame] | 128 | const __m128i vxb1 = _mm_cvtepi8_epi16(vb1); |
Marat Dukhan | fc188ed | 2021-06-03 12:21:22 -0700 | [diff] [blame] | 129 | |
| 130 | vacc0x0123 = _mm_add_epi32(vacc0x0123, |
| 131 | _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1)); |
| 132 | vacc1x0123 = _mm_add_epi32(vacc1x0123, |
| 133 | _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(1, 1, 1, 1)), vxb1)); |
| 134 | |
| 135 | if (k > 4 * sizeof(int8_t)) { |
| 136 | const __m128i vb2 = _mm_loadl_epi64((const __m128i*) w); |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 137 | w = (const void*) ((const int8_t*) w + 8); |
Marat Dukhan | cdbe9a3 | 2021-07-01 23:52:04 -0700 | [diff] [blame] | 138 | const __m128i vxb2 = _mm_cvtepi8_epi16(vb2); |
Marat Dukhan | fc188ed | 2021-06-03 12:21:22 -0700 | [diff] [blame] | 139 | |
| 140 | vacc0x0123 = _mm_add_epi32(vacc0x0123, |
| 141 | _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2)); |
| 142 | vacc1x0123 = _mm_add_epi32(vacc1x0123, |
| 143 | _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(2, 2, 2, 2)), vxb2)); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | p -= 2 * sizeof(void*); |
| 148 | } while (p != 0); |
| 149 | |
| 150 | __m128 vscaled0x0123 = _mm_cvtepi32_ps(vacc0x0123); |
| 151 | __m128 vscaled1x0123 = _mm_cvtepi32_ps(vacc1x0123); |
| 152 | |
| 153 | const __m128 vscale0123 = _mm_loadu_ps((const float*) w); |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 154 | w = (const void*) ((const float*) w + 4); |
Marat Dukhan | fc188ed | 2021-06-03 12:21:22 -0700 | [diff] [blame] | 155 | vscaled0x0123 = _mm_mul_ps(vscaled0x0123, vscale0123); |
| 156 | vscaled1x0123 = _mm_mul_ps(vscaled1x0123, vscale0123); |
| 157 | |
| 158 | vacc0x0123 = _mm_cvtps_epi32(vscaled0x0123); |
| 159 | vacc1x0123 = _mm_cvtps_epi32(vscaled1x0123); |
| 160 | |
| 161 | const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->sse4.output_zero_point); |
| 162 | __m128i vacc01x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc0x0123, vacc1x0123), voutput_zero_point); |
| 163 | |
| 164 | |
| 165 | __m128i vout = _mm_packs_epi16(vacc01x0123, vacc01x0123); |
| 166 | |
| 167 | vout = _mm_max_epi8(vout, _mm_load_si128((const __m128i*) params->sse4.output_min)); |
| 168 | vout = _mm_min_epi8(vout, _mm_load_si128((const __m128i*) params->sse4.output_max)); |
| 169 | |
| 170 | if (nc >= 4) { |
| 171 | *((uint32_t*) c1) = (uint32_t) _mm_extract_epi32(vout, 1); |
| 172 | c1 = (int8_t*) ((uintptr_t) c1 + cn_stride); |
| 173 | *((uint32_t*) c0) = (uint32_t) _mm_cvtsi128_si32(vout); |
| 174 | c0 = (int8_t*) ((uintptr_t) c0 + cn_stride); |
| 175 | |
| 176 | a = (const int8_t**restrict) ((uintptr_t) a - ks); |
| 177 | |
| 178 | nc -= 4; |
| 179 | } else { |
| 180 | if (nc & 2) { |
| 181 | *((uint16_t*) c1) = (uint16_t) _mm_extract_epi16(vout, 2); |
| 182 | c1 += 2; |
| 183 | *((uint16_t*) c0) = (uint16_t) _mm_extract_epi16(vout, 0); |
| 184 | c0 += 2; |
| 185 | vout = _mm_srli_epi32(vout, 16); |
| 186 | } |
| 187 | if (nc & 1) { |
Marat Dukhan | cdbe9a3 | 2021-07-01 23:52:04 -0700 | [diff] [blame] | 188 | *c1 = (int8_t) _mm_extract_epi8(vout, 4); |
| 189 | *c0 = (int8_t) _mm_extract_epi8(vout, 0); |
Marat Dukhan | fc188ed | 2021-06-03 12:21:22 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | nc = 0; |
| 193 | } |
| 194 | } while (nc != 0); |
| 195 | } |