1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-vaddc/sse-mul32-ld32.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 <immintrin.h>
13 
14 #include <xnnpack/intrinsics-polyfill.h>
15 #include <xnnpack/vadd.h>
16 
17 
xnn_qs8_vaddc_minmax_ukernel__sse41_mul32_ld32_x32(size_t n,const int8_t * input_x,const int8_t * input_y,int8_t * output,const union xnn_qs8_add_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_qs8_vaddc_minmax_ukernel__sse41_mul32_ld32_x32(
19     size_t n,
20     const int8_t* input_x,
21     const int8_t* input_y,
22     int8_t* output,
23     const union xnn_qs8_add_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
24 {
25   const __m128i vx_multiplier = _mm_load_si128((const __m128i*) params->sse2.x_multiplier);
26   const __m128i vremainder_mask = _mm_load_si128((const __m128i*) params->sse2.remainder_mask);
27   const __m128i vremainder_threshold = _mm_load_si128((const __m128i*) params->sse2.remainder_threshold);
28   const __m128i vshift = _mm_cvtsi32_si128((int) params->sse2.shift);
29   const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->sse2.output_zero_point);
30   const __m128i voutput_min = _mm_load_si128((const __m128i*) params->sse2.output_min);
31   const __m128i voutput_max = _mm_load_si128((const __m128i*) params->sse2.output_max);
32 
33   __m128i vzero_point_product = _mm_cvtsi32_si128(params->sse2.y_multiplier[0] * (int32_t) *input_y);
34   vzero_point_product = _mm_shuffle_epi32(vzero_point_product, _MM_SHUFFLE(0, 0, 0, 0));
35   vzero_point_product = _mm_add_epi32(vzero_point_product, _mm_load_si128((const __m128i*) params->sse2.zero_point_product));
36   for (; n >= 32 * sizeof(int8_t); n -= 32 * sizeof(int8_t)) {
37     const __m128i vx0123 = _mm_cvtepi8_epi32(_mm_loadu_si32(input_x));
38     const __m128i vx4567 = _mm_cvtepi8_epi32(_mm_loadu_si32(input_x + 4));
39     const __m128i vx89AB = _mm_cvtepi8_epi32(_mm_loadu_si32(input_x + 8));
40     const __m128i vxCDEF = _mm_cvtepi8_epi32(_mm_loadu_si32(input_x + 12));
41     const __m128i vxGHIJ = _mm_cvtepi8_epi32(_mm_loadu_si32(input_x + 16));
42     const __m128i vxKLMN = _mm_cvtepi8_epi32(_mm_loadu_si32(input_x + 20));
43     const __m128i vxOPQR = _mm_cvtepi8_epi32(_mm_loadu_si32(input_x + 24));
44     const __m128i vxSTUV = _mm_cvtepi8_epi32(_mm_loadu_si32(input_x + 28));
45     input_x += 32;
46     input_y += 32;
47 
48     __m128i vacc0123 = _mm_add_epi32(vzero_point_product, _mm_mullo_epi32(vx0123, vx_multiplier));
49     __m128i vacc4567 = _mm_add_epi32(vzero_point_product, _mm_mullo_epi32(vx4567, vx_multiplier));
50     __m128i vacc89AB = _mm_add_epi32(vzero_point_product, _mm_mullo_epi32(vx89AB, vx_multiplier));
51     __m128i vaccCDEF = _mm_add_epi32(vzero_point_product, _mm_mullo_epi32(vxCDEF, vx_multiplier));
52     __m128i vaccGHIJ = _mm_add_epi32(vzero_point_product, _mm_mullo_epi32(vxGHIJ, vx_multiplier));
53     __m128i vaccKLMN = _mm_add_epi32(vzero_point_product, _mm_mullo_epi32(vxKLMN, vx_multiplier));
54     __m128i vaccOPQR = _mm_add_epi32(vzero_point_product, _mm_mullo_epi32(vxOPQR, vx_multiplier));
55     __m128i vaccSTUV = _mm_add_epi32(vzero_point_product, _mm_mullo_epi32(vxSTUV, vx_multiplier));
56 
57     const __m128i vrem0123 = _mm_add_epi32(_mm_and_si128(vacc0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vacc0123));
58     const __m128i vrem4567 = _mm_add_epi32(_mm_and_si128(vacc4567, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vacc4567));
59     const __m128i vrem89AB = _mm_add_epi32(_mm_and_si128(vacc89AB, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vacc89AB));
60     const __m128i vremCDEF = _mm_add_epi32(_mm_and_si128(vaccCDEF, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vaccCDEF));
61     const __m128i vremGHIJ = _mm_add_epi32(_mm_and_si128(vaccGHIJ, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vaccGHIJ));
62     const __m128i vremKLMN = _mm_add_epi32(_mm_and_si128(vaccKLMN, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vaccKLMN));
63     const __m128i vremOPQR = _mm_add_epi32(_mm_and_si128(vaccOPQR, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vaccOPQR));
64     const __m128i vremSTUV = _mm_add_epi32(_mm_and_si128(vaccSTUV, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vaccSTUV));
65 
66     vacc0123 = _mm_sub_epi32(_mm_sra_epi32(vacc0123, vshift), _mm_cmpgt_epi32(vrem0123, vremainder_threshold));
67     vacc4567 = _mm_sub_epi32(_mm_sra_epi32(vacc4567, vshift), _mm_cmpgt_epi32(vrem4567, vremainder_threshold));
68     vacc89AB = _mm_sub_epi32(_mm_sra_epi32(vacc89AB, vshift), _mm_cmpgt_epi32(vrem89AB, vremainder_threshold));
69     vaccCDEF = _mm_sub_epi32(_mm_sra_epi32(vaccCDEF, vshift), _mm_cmpgt_epi32(vremCDEF, vremainder_threshold));
70     vaccGHIJ = _mm_sub_epi32(_mm_sra_epi32(vaccGHIJ, vshift), _mm_cmpgt_epi32(vremGHIJ, vremainder_threshold));
71     vaccKLMN = _mm_sub_epi32(_mm_sra_epi32(vaccKLMN, vshift), _mm_cmpgt_epi32(vremKLMN, vremainder_threshold));
72     vaccOPQR = _mm_sub_epi32(_mm_sra_epi32(vaccOPQR, vshift), _mm_cmpgt_epi32(vremOPQR, vremainder_threshold));
73     vaccSTUV = _mm_sub_epi32(_mm_sra_epi32(vaccSTUV, vshift), _mm_cmpgt_epi32(vremSTUV, vremainder_threshold));
74 
75     __m128i vout01234567 = _mm_adds_epi16(_mm_packs_epi32(vacc0123, vacc4567), voutput_zero_point);
76     __m128i vout89ABCDEF = _mm_adds_epi16(_mm_packs_epi32(vacc89AB, vaccCDEF), voutput_zero_point);
77     __m128i voutGHIJKLMN = _mm_adds_epi16(_mm_packs_epi32(vaccGHIJ, vaccKLMN), voutput_zero_point);
78     __m128i voutOPQRSTUV = _mm_adds_epi16(_mm_packs_epi32(vaccOPQR, vaccSTUV), voutput_zero_point);
79 
80     vout01234567 = _mm_max_epi16(vout01234567, voutput_min);
81     vout89ABCDEF = _mm_max_epi16(vout89ABCDEF, voutput_min);
82     voutGHIJKLMN = _mm_max_epi16(voutGHIJKLMN, voutput_min);
83     voutOPQRSTUV = _mm_max_epi16(voutOPQRSTUV, voutput_min);
84 
85     vout01234567 = _mm_min_epi16(vout01234567, voutput_max);
86     vout89ABCDEF = _mm_min_epi16(vout89ABCDEF, voutput_max);
87     voutGHIJKLMN = _mm_min_epi16(voutGHIJKLMN, voutput_max);
88     voutOPQRSTUV = _mm_min_epi16(voutOPQRSTUV, voutput_max);
89 
90     const __m128i vout0123456789ABCDEF = _mm_packs_epi16(vout01234567, vout89ABCDEF);
91     const __m128i voutGHIJKLMNOPQRSTUV = _mm_packs_epi16(voutGHIJKLMN, voutOPQRSTUV);
92 
93     _mm_storeu_si128((__m128i*) output, vout0123456789ABCDEF);
94     _mm_storeu_si128((__m128i*) (output + 16), voutGHIJKLMNOPQRSTUV);
95     output += 32;
96   }
97   if XNN_UNLIKELY(n != 0) {
98     do {
99       const __m128i vx0123 = _mm_cvtepi8_epi32(_mm_loadu_si32(input_x));
100       const __m128i vx4567 = _mm_cvtepi8_epi32(_mm_loadu_si32(input_x + 4));
101       input_x += 8;
102 
103       __m128i vacc0123 = _mm_add_epi32(vzero_point_product, _mm_mullo_epi32(vx0123, vx_multiplier));
104       __m128i vacc4567 = _mm_add_epi32(vzero_point_product, _mm_mullo_epi32(vx4567, vx_multiplier));
105 
106       const __m128i vrem0123 = _mm_add_epi32(_mm_and_si128(vacc0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vacc0123));
107       const __m128i vrem4567 = _mm_add_epi32(_mm_and_si128(vacc4567, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vacc4567));
108 
109       vacc0123 = _mm_sub_epi32(_mm_sra_epi32(vacc0123, vshift), _mm_cmpgt_epi32(vrem0123, vremainder_threshold));
110       vacc4567 = _mm_sub_epi32(_mm_sra_epi32(vacc4567, vshift), _mm_cmpgt_epi32(vrem4567, vremainder_threshold));
111 
112       __m128i vout01234567 = _mm_adds_epi16(_mm_packs_epi32(vacc0123, vacc4567), voutput_zero_point);
113       vout01234567 = _mm_max_epi16(vout01234567, voutput_min);
114       vout01234567 = _mm_min_epi16(vout01234567, voutput_max);
115 
116       __m128i vout0123456701234567 = _mm_packs_epi16(vout01234567, vout01234567);
117 
118       if XNN_LIKELY(n >= (8 * sizeof(int8_t))) {
119         _mm_storel_epi64((__m128i*) output, vout0123456701234567);
120         output += 8;
121         n -= 8 * sizeof(int8_t);
122       } else {
123         if (n & (4 * sizeof(int8_t))) {
124           *((uint32_t*) output) = (uint32_t) _mm_cvtsi128_si32(vout0123456701234567);
125           vout0123456701234567 = _mm_srli_epi64(vout0123456701234567, 32);
126           output += 4;
127         }
128         if (n & (2 * sizeof(int8_t))) {
129           *((uint16_t*) output) = (uint16_t) _mm_extract_epi16(vout0123456701234567, 0);
130           vout0123456701234567 = _mm_srli_epi32(vout0123456701234567, 16);
131           output += 2;
132         }
133         if (n & (1 * sizeof(int8_t))) {
134           *output = (int8_t) _mm_extract_epi8(vout0123456701234567, 0);
135         }
136         n = 0;
137       }
138     } while (n != 0);
139   }
140 }
141