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