xref: /aosp_15_r20/external/XNNPACK/src/qu8-vcvt/gen/vcvt-avx2-x16.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_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__avx2_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 __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 >= 16 * sizeof(uint8_t); n -= 16 * sizeof(uint8_t)) {
34     __m256i vacc = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i*) x));
35     vacc = _mm256_sub_epi16(vinput_zero_point, vacc);
36     vacc = _mm256_slli_epi16(vacc, 7);
37     vacc = _mm256_mulhrs_epi16(vacc, vmultiplier);
38     vacc = _mm256_adds_epi16(vacc, voutput_zero_point);
39     x += 16;
40 
41     const __m128i vacc_hi = _mm256_extracti128_si256(vacc, 1);
42     const __m128i vy = _mm_packus_epi16(_mm256_castsi256_si128(vacc), vacc_hi);
43     _mm_storeu_si128((__m128i*) y, vy);
44     y += 16;
45   }
46   if XNN_UNLIKELY(n != 0) {
47     assert(n >= 1 * sizeof(uint8_t));
48     assert(n <= 15 * sizeof(uint8_t));
49 
50     __m256i vacc = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i*) x));
51     vacc = _mm256_sub_epi16(vinput_zero_point, vacc);
52     vacc = _mm256_slli_epi16(vacc, 7);
53     vacc = _mm256_mulhrs_epi16(vacc, vmultiplier);
54     vacc = _mm256_adds_epi16(vacc, voutput_zero_point);
55 
56     const __m128i vacc_hi = _mm256_extracti128_si256(vacc, 1);
57     __m128i vy = _mm_packus_epi16(_mm256_castsi256_si128(vacc), vacc_hi);
58     if (n & (8 * sizeof(uint8_t))) {
59       _mm_storel_epi64((__m128i*) y, vy);
60       vy = _mm_unpackhi_epi64(vy, vy);
61       y += 8;
62     }
63     if (n & (4 * sizeof(uint8_t))) {
64       _mm_storeu_si32(y, vy);
65       vy = _mm_srli_epi64(vy, 32);
66       y += 4;
67     }
68     if (n & (2 * sizeof(uint8_t))) {
69       _mm_storeu_si16(y, vy);
70       vy = _mm_srli_epi32(vy, 16);
71       y += 2;
72     }
73     if (n & (1 * sizeof(uint8_t))) {
74       *y = (uint8_t) _mm_extract_epi8(vy, 0);
75     }
76   }
77 }
78