xref: /aosp_15_r20/external/XNNPACK/src/qu8-vcvt/gen/vcvt-sse41-x16.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_x16(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_x16(
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 >= 16 * sizeof(uint8_t); n -= 16 * 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     x += 16;
37 
38     vacc0 = _mm_sub_epi16(vinput_zero_point, vacc0);
39     vacc1 = _mm_sub_epi16(vinput_zero_point, vacc1);
40 
41     vacc0 = _mm_slli_epi16(vacc0, 7);
42     vacc1 = _mm_slli_epi16(vacc1, 7);
43 
44     vacc0 = _mm_mulhrs_epi16(vacc0, vmultiplier);
45     vacc1 = _mm_mulhrs_epi16(vacc1, vmultiplier);
46 
47     vacc0 = _mm_adds_epi16(vacc0, voutput_zero_point);
48     vacc1 = _mm_adds_epi16(vacc1, voutput_zero_point);
49 
50     const __m128i vy0 = _mm_packus_epi16(vacc0, vacc1);
51 
52     _mm_storeu_si128((__m128i*) y, vy0);
53     y += 16;
54   }
55   for (; n >= 8 * sizeof(uint8_t); n -= 8 * sizeof(uint8_t)) {
56     __m128i vacc = _mm_cvtepu8_epi16(_mm_loadl_epi64((const __m128i*) x));
57     vacc = _mm_sub_epi16(vinput_zero_point, vacc);
58     vacc = _mm_slli_epi16(vacc, 7);
59     vacc = _mm_mulhrs_epi16(vacc, vmultiplier);
60     vacc = _mm_adds_epi16(vacc, voutput_zero_point);
61     x += 8;
62 
63     const __m128i vy = _mm_packus_epi16(vacc, vacc);
64     _mm_storel_epi64((__m128i*) y, vy);
65     y += 8;
66   }
67   if XNN_UNLIKELY(n != 0) {
68     assert(n >= 1 * sizeof(uint8_t));
69     assert(n <= 7 * sizeof(uint8_t));
70 
71     __m128i vacc = _mm_cvtepu8_epi16(_mm_loadl_epi64((const __m128i*) x));
72     vacc = _mm_sub_epi16(vinput_zero_point, vacc);
73     vacc = _mm_slli_epi16(vacc, 7);
74     vacc = _mm_mulhrs_epi16(vacc, vmultiplier);
75     vacc = _mm_adds_epi16(vacc, voutput_zero_point);
76 
77     __m128i vy = _mm_packus_epi16(vacc, vacc);
78     if (n & (4 * sizeof(uint8_t))) {
79       _mm_storeu_si32(y, vy);
80       vy = _mm_srli_epi64(vy, 32);
81       y += 4;
82     }
83     if (n & (2 * sizeof(uint8_t))) {
84       _mm_storeu_si16(y, vy);
85       vy = _mm_srli_epi32(vy, 16);
86       y += 2;
87     }
88     if (n & (1 * sizeof(uint8_t))) {
89       *y = (uint8_t) _mm_extract_epi8(vy, 0);
90     }
91   }
92 }
93