xref: /aosp_15_r20/external/XNNPACK/src/qu8-vcvt/gen/vcvt-sse41-x32.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-vcvt/sse4.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2022 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/common.h>
15 #include <xnnpack/intrinsics-polyfill.h>
16 #include <xnnpack/vcvt.h>
17 
18 
xnn_qu8_vcvt_ukernel__sse41_x32(size_t n,const uint8_t * x,uint8_t * y,const union xnn_qu8_cvt_params params[restrict XNN_MIN_ELEMENTS (1)])19 void xnn_qu8_vcvt_ukernel__sse41_x32(
20     size_t n,
21     const uint8_t* x,
22     uint8_t* y,
23     const union xnn_qu8_cvt_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
24 {
25   assert(n != 0);
26   assert(n % sizeof(uint8_t) == 0);
27   assert(x != NULL);
28   assert(y != NULL);
29 
30   const __m128i vinput_zero_point = _mm_load_si128((const __m128i*) params->ssse3.input_zero_point);
31   const __m128i vmultiplier = _mm_load_si128((const __m128i*) params->ssse3.multiplier);
32   const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->ssse3.output_zero_point);
33   for (; n >= 32 * sizeof(uint8_t); n -= 32 * sizeof(uint8_t)) {
34     __m128i vacc0 = _mm_cvtepu8_epi16(_mm_loadl_epi64((const __m128i*) x));
35     __m128i vacc1 = _mm_cvtepu8_epi16(_mm_loadl_epi64((const __m128i*) (x + 8)));
36     __m128i vacc2 = _mm_cvtepu8_epi16(_mm_loadl_epi64((const __m128i*) (x + 16)));
37     __m128i vacc3 = _mm_cvtepu8_epi16(_mm_loadl_epi64((const __m128i*) (x + 24)));
38     x += 32;
39 
40     vacc0 = _mm_sub_epi16(vinput_zero_point, vacc0);
41     vacc1 = _mm_sub_epi16(vinput_zero_point, vacc1);
42     vacc2 = _mm_sub_epi16(vinput_zero_point, vacc2);
43     vacc3 = _mm_sub_epi16(vinput_zero_point, vacc3);
44 
45     vacc0 = _mm_slli_epi16(vacc0, 7);
46     vacc1 = _mm_slli_epi16(vacc1, 7);
47     vacc2 = _mm_slli_epi16(vacc2, 7);
48     vacc3 = _mm_slli_epi16(vacc3, 7);
49 
50     vacc0 = _mm_mulhrs_epi16(vacc0, vmultiplier);
51     vacc1 = _mm_mulhrs_epi16(vacc1, vmultiplier);
52     vacc2 = _mm_mulhrs_epi16(vacc2, vmultiplier);
53     vacc3 = _mm_mulhrs_epi16(vacc3, vmultiplier);
54 
55     vacc0 = _mm_adds_epi16(vacc0, voutput_zero_point);
56     vacc1 = _mm_adds_epi16(vacc1, voutput_zero_point);
57     vacc2 = _mm_adds_epi16(vacc2, voutput_zero_point);
58     vacc3 = _mm_adds_epi16(vacc3, voutput_zero_point);
59 
60     const __m128i vy0 = _mm_packus_epi16(vacc0, vacc1);
61     const __m128i vy1 = _mm_packus_epi16(vacc2, vacc3);
62 
63     _mm_storeu_si128((__m128i*) y, vy0);
64     _mm_storeu_si128((__m128i*) (y + 16), vy1);
65     y += 32;
66   }
67   for (; n >= 8 * sizeof(uint8_t); n -= 8 * sizeof(uint8_t)) {
68     __m128i vacc = _mm_cvtepu8_epi16(_mm_loadl_epi64((const __m128i*) x));
69     vacc = _mm_sub_epi16(vinput_zero_point, vacc);
70     vacc = _mm_slli_epi16(vacc, 7);
71     vacc = _mm_mulhrs_epi16(vacc, vmultiplier);
72     vacc = _mm_adds_epi16(vacc, voutput_zero_point);
73     x += 8;
74 
75     const __m128i vy = _mm_packus_epi16(vacc, vacc);
76     _mm_storel_epi64((__m128i*) y, vy);
77     y += 8;
78   }
79   if XNN_UNLIKELY(n != 0) {
80     assert(n >= 1 * sizeof(uint8_t));
81     assert(n <= 7 * sizeof(uint8_t));
82 
83     __m128i vacc = _mm_cvtepu8_epi16(_mm_loadl_epi64((const __m128i*) x));
84     vacc = _mm_sub_epi16(vinput_zero_point, vacc);
85     vacc = _mm_slli_epi16(vacc, 7);
86     vacc = _mm_mulhrs_epi16(vacc, vmultiplier);
87     vacc = _mm_adds_epi16(vacc, voutput_zero_point);
88 
89     __m128i vy = _mm_packus_epi16(vacc, vacc);
90     if (n & (4 * sizeof(uint8_t))) {
91       _mm_storeu_si32(y, vy);
92       vy = _mm_srli_epi64(vy, 32);
93       y += 4;
94     }
95     if (n & (2 * sizeof(uint8_t))) {
96       _mm_storeu_si16(y, vy);
97       vy = _mm_srli_epi32(vy, 16);
98       y += 2;
99     }
100     if (n & (1 * sizeof(uint8_t))) {
101       *y = (uint8_t) _mm_extract_epi8(vy, 0);
102     }
103   }
104 }
105