xref: /aosp_15_r20/external/XNNPACK/src/qu8-vcvt/gen/vcvt-avx2-x64.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-vcvt/avx2.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__avx2_x64(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__avx2_x64(
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 __m256i vinput_zero_point = _mm256_load_si256((const __m256i*) params->avx2.input_zero_point);
31   const __m256i vmultiplier = _mm256_load_si256((const __m256i*) params->avx2.multiplier);
32   const __m256i voutput_zero_point = _mm256_load_si256((const __m256i*) params->avx2.output_zero_point);
33   for (; n >= 64 * sizeof(uint8_t); n -= 64 * sizeof(uint8_t)) {
34     __m256i vacc0 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i*) x));
35     __m256i vacc1 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i*) (x + 16)));
36     __m256i vacc2 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i*) (x + 32)));
37     __m256i vacc3 = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i*) (x + 48)));
38     x += 64;
39 
40     vacc0 = _mm256_sub_epi16(vinput_zero_point, vacc0);
41     vacc1 = _mm256_sub_epi16(vinput_zero_point, vacc1);
42     vacc2 = _mm256_sub_epi16(vinput_zero_point, vacc2);
43     vacc3 = _mm256_sub_epi16(vinput_zero_point, vacc3);
44 
45     vacc0 = _mm256_slli_epi16(vacc0, 7);
46     vacc1 = _mm256_slli_epi16(vacc1, 7);
47     vacc2 = _mm256_slli_epi16(vacc2, 7);
48     vacc3 = _mm256_slli_epi16(vacc3, 7);
49 
50     vacc0 = _mm256_mulhrs_epi16(vacc0, vmultiplier);
51     vacc1 = _mm256_mulhrs_epi16(vacc1, vmultiplier);
52     vacc2 = _mm256_mulhrs_epi16(vacc2, vmultiplier);
53     vacc3 = _mm256_mulhrs_epi16(vacc3, vmultiplier);
54 
55     vacc0 = _mm256_adds_epi16(vacc0, voutput_zero_point);
56     vacc1 = _mm256_adds_epi16(vacc1, voutput_zero_point);
57     vacc2 = _mm256_adds_epi16(vacc2, voutput_zero_point);
58     vacc3 = _mm256_adds_epi16(vacc3, voutput_zero_point);
59 
60     __m256i vy0 = _mm256_packus_epi16(vacc0, vacc1);
61     __m256i vy1 = _mm256_packus_epi16(vacc2, vacc3);
62 
63     vy0 = _mm256_permute4x64_epi64(vy0, _MM_SHUFFLE(3, 1, 2, 0));
64     vy1 = _mm256_permute4x64_epi64(vy1, _MM_SHUFFLE(3, 1, 2, 0));
65 
66     _mm256_storeu_si256((__m256i*) y, vy0);
67     _mm256_storeu_si256((__m256i*) (y + 32), vy1);
68     y += 64;
69   }
70   for (; n >= 16 * sizeof(uint8_t); n -= 16 * sizeof(uint8_t)) {
71     __m256i vacc = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i*) x));
72     vacc = _mm256_sub_epi16(vinput_zero_point, vacc);
73     vacc = _mm256_slli_epi16(vacc, 7);
74     vacc = _mm256_mulhrs_epi16(vacc, vmultiplier);
75     vacc = _mm256_adds_epi16(vacc, voutput_zero_point);
76     x += 16;
77 
78     const __m128i vacc_hi = _mm256_extracti128_si256(vacc, 1);
79     const __m128i vy = _mm_packus_epi16(_mm256_castsi256_si128(vacc), vacc_hi);
80     _mm_storeu_si128((__m128i*) y, vy);
81     y += 16;
82   }
83   if XNN_UNLIKELY(n != 0) {
84     assert(n >= 1 * sizeof(uint8_t));
85     assert(n <= 15 * sizeof(uint8_t));
86 
87     __m256i vacc = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i*) x));
88     vacc = _mm256_sub_epi16(vinput_zero_point, vacc);
89     vacc = _mm256_slli_epi16(vacc, 7);
90     vacc = _mm256_mulhrs_epi16(vacc, vmultiplier);
91     vacc = _mm256_adds_epi16(vacc, voutput_zero_point);
92 
93     const __m128i vacc_hi = _mm256_extracti128_si256(vacc, 1);
94     __m128i vy = _mm_packus_epi16(_mm256_castsi256_si128(vacc), vacc_hi);
95     if (n & (8 * sizeof(uint8_t))) {
96       _mm_storel_epi64((__m128i*) y, vy);
97       vy = _mm_unpackhi_epi64(vy, vy);
98       y += 8;
99     }
100     if (n & (4 * sizeof(uint8_t))) {
101       _mm_storeu_si32(y, vy);
102       vy = _mm_srli_epi64(vy, 32);
103       y += 4;
104     }
105     if (n & (2 * sizeof(uint8_t))) {
106       _mm_storeu_si16(y, vy);
107       vy = _mm_srli_epi32(vy, 16);
108       y += 2;
109     }
110     if (n & (1 * sizeof(uint8_t))) {
111       *y = (uint8_t) _mm_extract_epi8(vy, 0);
112     }
113   }
114 }
115