Marat Dukhan | fc188ed | 2021-06-03 12:21:22 -0700 | [diff] [blame] | 1 | // Auto-generated file. Do not edit! |
| 2 | // Template: src/qs8-gemm/MRx4c8-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/gemm.h> |
| 15 | #include <xnnpack/math.h> |
| 16 | |
| 17 | |
| 18 | void xnn_qc8_gemm_minmax_fp32_ukernel_3x4c8__sse41_ld128( |
| 19 | size_t mr, |
| 20 | size_t nc, |
| 21 | size_t kc, |
| 22 | const int8_t* restrict a, |
| 23 | size_t a_stride, |
| 24 | const void* restrict w, |
| 25 | int8_t* restrict c, |
| 26 | size_t cm_stride, |
| 27 | size_t cn_stride, |
| 28 | const union xnn_qs8_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN XNN_DISABLE_MSAN |
| 29 | { |
| 30 | assert(mr != 0); |
| 31 | assert(mr <= 3); |
| 32 | assert(nc != 0); |
| 33 | assert(kc != 0); |
| 34 | assert(kc % sizeof(int8_t) == 0); |
| 35 | assert(a != NULL); |
| 36 | assert(w != NULL); |
| 37 | assert(c != NULL); |
| 38 | |
| 39 | kc = round_up_po2(kc, 8); |
| 40 | const int8_t* a0 = a; |
| 41 | int8_t* c0 = c; |
| 42 | const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride); |
| 43 | int8_t* c1 = (int8_t*) ((uintptr_t) c0 + cm_stride); |
| 44 | if XNN_UNPREDICTABLE(mr < 2) { |
| 45 | a1 = a0; |
| 46 | c1 = c0; |
| 47 | } |
| 48 | const int8_t* a2 = (const int8_t*) ((uintptr_t) a1 + a_stride); |
| 49 | int8_t* c2 = (int8_t*) ((uintptr_t) c1 + cm_stride); |
| 50 | if XNN_UNPREDICTABLE(mr <= 2) { |
| 51 | a2 = a1; |
| 52 | c2 = c1; |
| 53 | } |
| 54 | |
| 55 | do { |
| 56 | __m128i vacc0x0 = _mm_cvtsi32_si128((int) ((const int32_t*) w)[0]); |
| 57 | __m128i vacc0x1 = _mm_cvtsi32_si128((int) ((const int32_t*) w)[1]); |
| 58 | __m128i vacc0x2 = _mm_cvtsi32_si128((int) ((const int32_t*) w)[2]); |
| 59 | __m128i vacc0x3 = _mm_cvtsi32_si128((int) ((const int32_t*) w)[3]); |
| 60 | __m128i vacc1x0 = vacc0x0; |
| 61 | __m128i vacc1x1 = vacc0x1; |
| 62 | __m128i vacc1x2 = vacc0x2; |
| 63 | __m128i vacc1x3 = vacc0x3; |
| 64 | __m128i vacc2x0 = vacc0x0; |
| 65 | __m128i vacc2x1 = vacc0x1; |
| 66 | __m128i vacc2x2 = vacc0x2; |
| 67 | __m128i vacc2x3 = vacc0x3; |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 68 | w = (const void*) ((const int32_t*) w + 4); |
Marat Dukhan | fc188ed | 2021-06-03 12:21:22 -0700 | [diff] [blame] | 69 | |
| 70 | size_t k = 0; |
| 71 | while (k < kc) { |
| 72 | const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0); |
| 73 | const __m128i vxa0 = _mm_cvtepi8_epi16(va0); |
| 74 | a0 += 8; |
| 75 | const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1); |
| 76 | const __m128i vxa1 = _mm_cvtepi8_epi16(va1); |
| 77 | a1 += 8; |
| 78 | const __m128i va2 = _mm_loadl_epi64((const __m128i*) a2); |
| 79 | const __m128i vxa2 = _mm_cvtepi8_epi16(va2); |
| 80 | a2 += 8; |
| 81 | |
| 82 | const __m128i vb01 = _mm_load_si128((const __m128i*) w); |
Marat Dukhan | 0c2a31e | 2021-08-10 16:24:28 -0700 | [diff] [blame] | 83 | const __m128i vxb0 = _mm_cvtepi8_epi16(vb01); |
| 84 | const __m128i vxb1 = _mm_srai_epi16(_mm_unpackhi_epi8(vb01, vb01), 8); |
Marat Dukhan | fc188ed | 2021-06-03 12:21:22 -0700 | [diff] [blame] | 85 | |
| 86 | vacc0x0 = _mm_add_epi32(vacc0x0, _mm_madd_epi16(vxa0, vxb0)); |
| 87 | vacc0x1 = _mm_add_epi32(vacc0x1, _mm_madd_epi16(vxa0, vxb1)); |
| 88 | vacc1x0 = _mm_add_epi32(vacc1x0, _mm_madd_epi16(vxa1, vxb0)); |
| 89 | vacc1x1 = _mm_add_epi32(vacc1x1, _mm_madd_epi16(vxa1, vxb1)); |
| 90 | vacc2x0 = _mm_add_epi32(vacc2x0, _mm_madd_epi16(vxa2, vxb0)); |
| 91 | vacc2x1 = _mm_add_epi32(vacc2x1, _mm_madd_epi16(vxa2, vxb1)); |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 92 | const __m128i vb23 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 16)); |
Marat Dukhan | 0c2a31e | 2021-08-10 16:24:28 -0700 | [diff] [blame] | 93 | const __m128i vxb2 = _mm_cvtepi8_epi16(vb23); |
| 94 | const __m128i vxb3 = _mm_srai_epi16(_mm_unpackhi_epi8(vb23, vb23), 8); |
Marat Dukhan | fc188ed | 2021-06-03 12:21:22 -0700 | [diff] [blame] | 95 | |
| 96 | vacc0x2 = _mm_add_epi32(vacc0x2, _mm_madd_epi16(vxa0, vxb2)); |
| 97 | vacc0x3 = _mm_add_epi32(vacc0x3, _mm_madd_epi16(vxa0, vxb3)); |
| 98 | vacc1x2 = _mm_add_epi32(vacc1x2, _mm_madd_epi16(vxa1, vxb2)); |
| 99 | vacc1x3 = _mm_add_epi32(vacc1x3, _mm_madd_epi16(vxa1, vxb3)); |
| 100 | vacc2x2 = _mm_add_epi32(vacc2x2, _mm_madd_epi16(vxa2, vxb2)); |
| 101 | vacc2x3 = _mm_add_epi32(vacc2x3, _mm_madd_epi16(vxa2, vxb3)); |
| 102 | |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 103 | w = (const void*) ((const int8_t*) w + 32); |
Marat Dukhan | fc188ed | 2021-06-03 12:21:22 -0700 | [diff] [blame] | 104 | k += 8 * sizeof(int8_t); |
| 105 | } |
| 106 | |
| 107 | const __m128i vacc0x01 = _mm_hadd_epi32(vacc0x0, vacc0x1); |
| 108 | const __m128i vacc0x23 = _mm_hadd_epi32(vacc0x2, vacc0x3); |
| 109 | const __m128i vacc1x01 = _mm_hadd_epi32(vacc1x0, vacc1x1); |
| 110 | const __m128i vacc1x23 = _mm_hadd_epi32(vacc1x2, vacc1x3); |
| 111 | const __m128i vacc2x01 = _mm_hadd_epi32(vacc2x0, vacc2x1); |
| 112 | const __m128i vacc2x23 = _mm_hadd_epi32(vacc2x2, vacc2x3); |
| 113 | |
| 114 | __m128i vacc0x0123 = _mm_hadd_epi32(vacc0x01, vacc0x23); |
| 115 | __m128i vacc1x0123 = _mm_hadd_epi32(vacc1x01, vacc1x23); |
| 116 | __m128i vacc2x0123 = _mm_hadd_epi32(vacc2x01, vacc2x23); |
| 117 | |
| 118 | __m128 vscaled0x0123 = _mm_cvtepi32_ps(vacc0x0123); |
| 119 | __m128 vscaled1x0123 = _mm_cvtepi32_ps(vacc1x0123); |
| 120 | __m128 vscaled2x0123 = _mm_cvtepi32_ps(vacc2x0123); |
| 121 | |
| 122 | const __m128 vscale0123 = _mm_load_ps((const float*) w); |
Marat Dukhan | e5eee46 | 2021-07-01 19:34:39 -0700 | [diff] [blame] | 123 | w = (const void*) ((const float*) w + 4); |
Marat Dukhan | fc188ed | 2021-06-03 12:21:22 -0700 | [diff] [blame] | 124 | vscaled0x0123 = _mm_mul_ps(vscaled0x0123, vscale0123); |
| 125 | vscaled1x0123 = _mm_mul_ps(vscaled1x0123, vscale0123); |
| 126 | vscaled2x0123 = _mm_mul_ps(vscaled2x0123, vscale0123); |
| 127 | |
Marat Dukhan | 13c9f8d | 2021-12-06 02:21:03 -0800 | [diff] [blame] | 128 | const __m128 voutput_max_less_zero_point = _mm_load_ps(params->sse4.output_max_less_zero_point); |
| 129 | vscaled0x0123 = _mm_min_ps(vscaled0x0123, voutput_max_less_zero_point); |
| 130 | vscaled1x0123 = _mm_min_ps(vscaled1x0123, voutput_max_less_zero_point); |
| 131 | vscaled2x0123 = _mm_min_ps(vscaled2x0123, voutput_max_less_zero_point); |
| 132 | |
Marat Dukhan | fc188ed | 2021-06-03 12:21:22 -0700 | [diff] [blame] | 133 | vacc0x0123 = _mm_cvtps_epi32(vscaled0x0123); |
| 134 | vacc1x0123 = _mm_cvtps_epi32(vscaled1x0123); |
| 135 | vacc2x0123 = _mm_cvtps_epi32(vscaled2x0123); |
| 136 | |
| 137 | const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->sse4.output_zero_point); |
| 138 | __m128i vacc01x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc0x0123, vacc1x0123), voutput_zero_point); |
| 139 | __m128i vacc22x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc2x0123, vacc2x0123), voutput_zero_point); |
| 140 | |
| 141 | |
| 142 | __m128i vout = _mm_packs_epi16(vacc01x0123, vacc22x0123); |
| 143 | |
| 144 | vout = _mm_max_epi8(vout, _mm_load_si128((const __m128i*) params->sse4.output_min)); |
Marat Dukhan | fc188ed | 2021-06-03 12:21:22 -0700 | [diff] [blame] | 145 | |
| 146 | if (nc >= 4) { |
| 147 | *((uint32_t*) c0) = (uint32_t) _mm_cvtsi128_si32(vout); |
| 148 | *((uint32_t*) c1) = (uint32_t) _mm_extract_epi32(vout, 1); |
| 149 | *((uint32_t*) c2) = (uint32_t) _mm_extract_epi32(vout, 2); |
| 150 | |
| 151 | c0 = (int8_t*) ((uintptr_t) c0 + cn_stride); |
| 152 | c1 = (int8_t*) ((uintptr_t) c1 + cn_stride); |
| 153 | c2 = (int8_t*) ((uintptr_t) c2 + cn_stride); |
| 154 | |
| 155 | a0 = (const int8_t*) ((uintptr_t) a0 - kc); |
| 156 | a1 = (const int8_t*) ((uintptr_t) a1 - kc); |
| 157 | a2 = (const int8_t*) ((uintptr_t) a2 - kc); |
| 158 | |
| 159 | nc -= 4; |
| 160 | } else { |
| 161 | if (nc & 2) { |
| 162 | *((uint16_t*) c0) = (uint16_t) _mm_extract_epi16(vout, 0); |
| 163 | c0 += 2; |
| 164 | *((uint16_t*) c1) = (uint16_t) _mm_extract_epi16(vout, 2); |
| 165 | c1 += 2; |
| 166 | *((uint16_t*) c2) = (uint16_t) _mm_extract_epi16(vout, 4); |
| 167 | c2 += 2; |
| 168 | vout = _mm_srli_epi32(vout, 16); |
| 169 | } |
| 170 | if (nc & 1) { |
Marat Dukhan | cdbe9a3 | 2021-07-01 23:52:04 -0700 | [diff] [blame] | 171 | *c0 = (int8_t) _mm_extract_epi8(vout, 0); |
| 172 | *c1 = (int8_t) _mm_extract_epi8(vout, 4); |
| 173 | *c2 = (int8_t) _mm_extract_epi8(vout, 8); |
Marat Dukhan | fc188ed | 2021-06-03 12:21:22 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | nc = 0; |
| 177 | } |
| 178 | } while (nc != 0); |
| 179 | } |