| 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/common.h" |
| 15 |
#include "xnnpack/gemm.h" |
| 16 |
#include "xnnpack/math.h" |
| 17 |
|
| 18 |
|
| 19 |
void xnn_qd8_f32_qc8w_gemm_minmax_ukernel_3x4c8__sse41_ld64( |
| 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 |
float* restrict c, |
| 27 |
size_t cm_stride, |
| 28 |
size_t cn_stride, |
| 29 |
const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)], |
| 30 |
const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS |
| 31 |
{ |
| 32 |
assert(mr != 0); |
| 33 |
assert(mr <= 3); |
| 34 |
assert(nc != 0); |
| 35 |
assert(kc != 0); |
| 36 |
assert(kc % sizeof(int8_t) == 0); |
| 37 |
assert(a != NULL); |
| 38 |
assert(w != NULL); |
| 39 |
assert(c != NULL); |
| 40 |
|
| 41 |
kc = round_up_po2(kc, 8 * sizeof(int8_t)); |
| 42 |
const int8_t* a0 = a; |
| 43 |
float* c0 = c; |
| 44 |
const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride); |
| 45 |
float* c1 = (float*) ((uintptr_t) c0 + cm_stride); |
| 46 |
if XNN_UNPREDICTABLE(mr < 2) { |
| 47 |
a1 = a0; |
| 48 |
c1 = c0; |
| 49 |
} |
| 50 |
const int8_t* a2 = (const int8_t*) ((uintptr_t) a1 + a_stride); |
| 51 |
float* c2 = (float*) ((uintptr_t) c1 + cm_stride); |
| 52 |
if XNN_UNPREDICTABLE(mr <= 2) { |
| 53 |
a2 = a1; |
| 54 |
c2 = c1; |
| 55 |
} |
| 56 |
|
| 57 |
const __m128 vmin = _mm_set1_ps(params->scalar.min); |
| 58 |
const __m128 vmax = _mm_set1_ps(params->scalar.max); |
| 59 |
XNN_FORCE_REALIZATION(vmin); |
| 60 |
XNN_FORCE_REALIZATION(vmax); |
| 61 |
|
| 62 |
|
| 63 |
do { |
| 64 |
const __m128i vksum = _mm_load_si128((const __m128i*) w); |
| 65 |
const __m128i vinput_zero_point01 = _mm_loadu_si128((const __m128i*) &quantization_params[0]); |
| 66 |
const __m128i vinput_zero_point0 = _mm_shuffle_epi32(vinput_zero_point01, _MM_SHUFFLE(0, 0, 0, 0)); |
| 67 |
const __m128i vinput_zero_point1 = _mm_shuffle_epi32(vinput_zero_point01, _MM_SHUFFLE(2, 2, 2, 2)); |
| 68 |
__m128i vinput_zero_point2 = _mm_cvtsi32_si128(*((const int*) &quantization_params[2].zero_point)); |
| 69 |
vinput_zero_point2 = _mm_shuffle_epi32(vinput_zero_point2, _MM_SHUFFLE(0, 0, 0, 0)); |
| 70 |
const __m128i vzero = _mm_setzero_si128(); |
| 71 |
const __m128i vinit0 = _mm_mullo_epi32(vksum, vinput_zero_point0); |
| 72 |
const __m128i vinit1 = _mm_mullo_epi32(vksum, vinput_zero_point1); |
| 73 |
const __m128i vinit2 = _mm_mullo_epi32(vksum, vinput_zero_point2); |
| 74 |
__m128i vacc0x0 = _mm_blend_epi16(vinit0, vzero, 0xFC); |
| 75 |
__m128i vacc0x1 = _mm_blend_epi16(vinit0, vzero, 0xF3); |
| 76 |
__m128i vacc0x2 = _mm_blend_epi16(vinit0, vzero, 0xCF); |
| 77 |
__m128i vacc0x3 = _mm_blend_epi16(vinit0, vzero, 0x3F); |
| 78 |
__m128i vacc1x0 = _mm_blend_epi16(vinit1, vzero, 0xFC); |
| 79 |
__m128i vacc1x1 = _mm_blend_epi16(vinit1, vzero, 0xF3); |
| 80 |
__m128i vacc1x2 = _mm_blend_epi16(vinit1, vzero, 0xCF); |
| 81 |
__m128i vacc1x3 = _mm_blend_epi16(vinit1, vzero, 0x3F); |
| 82 |
__m128i vacc2x0 = _mm_blend_epi16(vinit2, vzero, 0xFC); |
| 83 |
__m128i vacc2x1 = _mm_blend_epi16(vinit2, vzero, 0xF3); |
| 84 |
__m128i vacc2x2 = _mm_blend_epi16(vinit2, vzero, 0xCF); |
| 85 |
__m128i vacc2x3 = _mm_blend_epi16(vinit2, vzero, 0x3F); |
| 86 |
w = (const int32_t*) w + 4; |
| 87 |
|
| 88 |
size_t k = kc; |
| 89 |
|
| 90 |
|
| 91 |
while (k >= 8 * sizeof(int8_t)) { |
| 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 |
|
| 104 |
const __m128i vxb0 = _mm_cvtepi8_epi16(vb0); |
| 105 |
|
| 106 |
vacc0x0 = _mm_add_epi32(vacc0x0, _mm_madd_epi16(vxa0, vxb0)); |
| 107 |
vacc1x0 = _mm_add_epi32(vacc1x0, _mm_madd_epi16(vxa1, vxb0)); |
| 108 |
vacc2x0 = _mm_add_epi32(vacc2x0, _mm_madd_epi16(vxa2, vxb0)); |
| 109 |
const __m128i vb1 = _mm_loadl_epi64((const __m128i*) ((const int8_t*) w + 8)); |
| 110 |
|
| 111 |
const __m128i vxb1 = _mm_cvtepi8_epi16(vb1); |
| 112 |
|
| 113 |
vacc0x1 = _mm_add_epi32(vacc0x1, _mm_madd_epi16(vxa0, vxb1)); |
| 114 |
vacc1x1 = _mm_add_epi32(vacc1x1, _mm_madd_epi16(vxa1, vxb1)); |
| 115 |
vacc2x1 = _mm_add_epi32(vacc2x1, _mm_madd_epi16(vxa2, vxb1)); |
| 116 |
const __m128i vb2 = _mm_loadl_epi64((const __m128i*) ((const int8_t*) w + 16)); |
| 117 |
|
| 118 |
const __m128i vxb2 = _mm_cvtepi8_epi16(vb2); |
| 119 |
|
| 120 |
vacc0x2 = _mm_add_epi32(vacc0x2, _mm_madd_epi16(vxa0, vxb2)); |
| 121 |
vacc1x2 = _mm_add_epi32(vacc1x2, _mm_madd_epi16(vxa1, vxb2)); |
| 122 |
vacc2x2 = _mm_add_epi32(vacc2x2, _mm_madd_epi16(vxa2, vxb2)); |
| 123 |
const __m128i vb3 = _mm_loadl_epi64((const __m128i*) ((const int8_t*) w + 24)); |
| 124 |
|
| 125 |
const __m128i vxb3 = _mm_cvtepi8_epi16(vb3); |
| 126 |
|
| 127 |
vacc0x3 = _mm_add_epi32(vacc0x3, _mm_madd_epi16(vxa0, vxb3)); |
| 128 |
vacc1x3 = _mm_add_epi32(vacc1x3, _mm_madd_epi16(vxa1, vxb3)); |
| 129 |
vacc2x3 = _mm_add_epi32(vacc2x3, _mm_madd_epi16(vxa2, vxb3)); |
| 130 |
|
| 131 |
w = (const int8_t*) w + 32; |
| 132 |
k -= 8 * sizeof(int8_t); |
| 133 |
} |
| 134 |
|
| 135 |
const __m128i vacc0x01 = _mm_hadd_epi32(vacc0x0, vacc0x1); |
| 136 |
const __m128i vacc0x23 = _mm_hadd_epi32(vacc0x2, vacc0x3); |
| 137 |
const __m128i vacc1x01 = _mm_hadd_epi32(vacc1x0, vacc1x1); |
| 138 |
const __m128i vacc1x23 = _mm_hadd_epi32(vacc1x2, vacc1x3); |
| 139 |
const __m128i vacc2x01 = _mm_hadd_epi32(vacc2x0, vacc2x1); |
| 140 |
const __m128i vacc2x23 = _mm_hadd_epi32(vacc2x2, vacc2x3); |
| 141 |
|
| 142 |
__m128i vacc0x0123 = _mm_hadd_epi32(vacc0x01, vacc0x23); |
| 143 |
__m128i vacc1x0123 = _mm_hadd_epi32(vacc1x01, vacc1x23); |
| 144 |
__m128i vacc2x0123 = _mm_hadd_epi32(vacc2x01, vacc2x23); |
| 145 |
|
| 146 |
__m128 vout0x0123 = _mm_cvtepi32_ps(vacc0x0123); |
| 147 |
__m128 vout1x0123 = _mm_cvtepi32_ps(vacc1x0123); |
| 148 |
__m128 vout2x0123 = _mm_cvtepi32_ps(vacc2x0123); |
| 149 |
|
| 150 |
const __m128i vinput_scale01 = _mm_loadu_si128((const __m128i*) &quantization_params[0]); |
| 151 |
const __m128 vinput_scale0 = _mm_castsi128_ps(_mm_shuffle_epi32(vinput_scale01, _MM_SHUFFLE(1, 1, 1, 1))); |
| 152 |
const __m128 vinput_scale1 = _mm_castsi128_ps(_mm_shuffle_epi32(vinput_scale01, _MM_SHUFFLE(3, 3, 3, 3))); |
| 153 |
const __m128 vinput_scale2 = _mm_load1_ps(&quantization_params[2].inv_scale); |
| 154 |
|
| 155 |
vout0x0123 = _mm_mul_ps(vout0x0123, vinput_scale0); |
| 156 |
vout1x0123 = _mm_mul_ps(vout1x0123, vinput_scale1); |
| 157 |
vout2x0123 = _mm_mul_ps(vout2x0123, vinput_scale2); |
| 158 |
|
| 159 |
const __m128 vfilter_output_scale0123 = _mm_load_ps((const float*) w); |
| 160 |
vout0x0123 = _mm_mul_ps(vout0x0123, vfilter_output_scale0123); |
| 161 |
vout1x0123 = _mm_mul_ps(vout1x0123, vfilter_output_scale0123); |
| 162 |
vout2x0123 = _mm_mul_ps(vout2x0123, vfilter_output_scale0123); |
| 163 |
|
| 164 |
const __m128 vbias0123 = _mm_load_ps((const float*) w + 4); |
| 165 |
w = (const float*) w + 8; |
| 166 |
vout0x0123 = _mm_add_ps(vout0x0123, vbias0123); |
| 167 |
vout1x0123 = _mm_add_ps(vout1x0123, vbias0123); |
| 168 |
vout2x0123 = _mm_add_ps(vout2x0123, vbias0123); |
| 169 |
|
| 170 |
vout0x0123 = _mm_max_ps(vout0x0123, vmin); |
| 171 |
vout1x0123 = _mm_max_ps(vout1x0123, vmin); |
| 172 |
vout2x0123 = _mm_max_ps(vout2x0123, vmin); |
| 173 |
|
| 174 |
vout0x0123 = _mm_min_ps(vout0x0123, vmax); |
| 175 |
vout1x0123 = _mm_min_ps(vout1x0123, vmax); |
| 176 |
vout2x0123 = _mm_min_ps(vout2x0123, vmax); |
| 177 |
|
| 178 |
if XNN_LIKELY(nc >= 4) { |
| 179 |
_mm_storeu_ps(c0, vout0x0123); |
| 180 |
_mm_storeu_ps(c1, vout1x0123); |
| 181 |
_mm_storeu_ps(c2, vout2x0123); |
| 182 |
|
| 183 |
a0 = (const int8_t*) ((uintptr_t) a0 - kc); |
| 184 |
a1 = (const int8_t*) ((uintptr_t) a1 - kc); |
| 185 |
a2 = (const int8_t*) ((uintptr_t) a2 - kc); |
| 186 |
|
| 187 |
c0 = (float*) ((uintptr_t) c0 + cn_stride); |
| 188 |
c1 = (float*) ((uintptr_t) c1 + cn_stride); |
| 189 |
c2 = (float*) ((uintptr_t) c2 + cn_stride); |
| 190 |
|
| 191 |
nc -= 4; |
| 192 |
} else { |
| 193 |
if (nc & 2) { |
| 194 |
_mm_storel_pi((__m64*) c0, vout0x0123); |
| 195 |
vout0x0123 = _mm_unpackhi_ps(vout0x0123, vout0x0123); |
| 196 |
c0 += 2; |
| 197 |
_mm_storel_pi((__m64*) c1, vout1x0123); |
| 198 |
vout1x0123 = _mm_unpackhi_ps(vout1x0123, vout1x0123); |
| 199 |
c1 += 2; |
| 200 |
_mm_storel_pi((__m64*) c2, vout2x0123); |
| 201 |
vout2x0123 = _mm_unpackhi_ps(vout2x0123, vout2x0123); |
| 202 |
c2 += 2; |
| 203 |
} |
| 204 |
if (nc & 1) { |
| 205 |
_mm_store_ss(c0, vout0x0123); |
| 206 |
_mm_store_ss(c1, vout1x0123); |
| 207 |
_mm_store_ss(c2, vout2x0123); |
| 208 |
} |
| 209 |
nc = 0; |
| 210 |
} |
| 211 |
} while (nc != 0); |
| 212 |
} |