• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-qs8-vcvt/avx2.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2021 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_f32_qu8_vcvt_ukernel__avx2_x32(size_t n,const float * x,uint8_t * y,const union xnn_f32_qu8_cvt_params params[restrict XNN_MIN_ELEMENTS (1)])19 void xnn_f32_qu8_vcvt_ukernel__avx2_x32(
20     size_t n,
21     const float* x,
22     uint8_t* y,
23     const union xnn_f32_qu8_cvt_params params[restrict XNN_MIN_ELEMENTS(1)])
24 {
25   assert(n != 0);
26   assert(n % sizeof(float) == 0);
27   assert(x != NULL);
28   assert(y != NULL);
29 
30   const __m256 vscale = _mm256_load_ps(params->avx2.scale);
31   const __m256 voutput_max_less_zero_point = _mm256_load_ps(params->avx2.output_max_less_zero_point);
32   const __m256i voutput_zero_point = _mm256_load_si256((const __m256i*) params->avx2.output_zero_point);
33   const __m256i vshuffle_mask = _mm256_load_si256((const __m256i*) params->avx2.shuffle_mask);
34   const __m256i voutput_min = _mm256_load_si256((const __m256i*) params->avx2.output_min);
35 
36   for (; n >= 32 * sizeof(float); n -= 32 * sizeof(float)) {
37     __m256 vx01 = _mm256_loadu_ps(x);
38     __m256 vx23 = _mm256_loadu_ps(x + 8);
39     __m256 vx45 = _mm256_loadu_ps(x + 16);
40     __m256 vx67 = _mm256_loadu_ps(x + 24);
41     x += 32;
42 
43     vx01 = _mm256_mul_ps(vx01, vscale);
44     vx23 = _mm256_mul_ps(vx23, vscale);
45     vx45 = _mm256_mul_ps(vx45, vscale);
46     vx67 = _mm256_mul_ps(vx67, vscale);
47 
48     vx01 = _mm256_min_ps(vx01, voutput_max_less_zero_point);
49     vx23 = _mm256_min_ps(vx23, voutput_max_less_zero_point);
50     vx45 = _mm256_min_ps(vx45, voutput_max_less_zero_point);
51     vx67 = _mm256_min_ps(vx67, voutput_max_less_zero_point);
52 
53     const __m256i vacc01 = _mm256_cvtps_epi32(vx01);
54     const __m256i vacc23 = _mm256_cvtps_epi32(vx23);
55     const __m256i vacc45 = _mm256_cvtps_epi32(vx45);
56     const __m256i vacc67 = _mm256_cvtps_epi32(vx67);
57 
58     __m256i vacc0213 = _mm256_packs_epi32(vacc01, vacc23);
59     __m256i vacc4657 = _mm256_packs_epi32(vacc45, vacc67);
60 
61     vacc0213 = _mm256_adds_epi16(vacc0213, voutput_zero_point);
62     vacc4657 = _mm256_adds_epi16(vacc4657, voutput_zero_point);
63 
64     const __m256i vy02461357 = _mm256_packus_epi16(vacc0213, vacc4657);
65 
66     __m256i vy01234567 = _mm256_permutevar8x32_epi32(vy02461357, vshuffle_mask);
67 
68     vy01234567 = _mm256_max_epu8(vy01234567, voutput_min);
69 
70     _mm256_storeu_si256((__m256i*) y, vy01234567);
71     y += 32;
72   }
73   for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
74     __m256 vx = _mm256_loadu_ps(x);
75     vx = _mm256_mul_ps(vx, vscale);
76     vx = _mm256_min_ps(vx, voutput_max_less_zero_point);
77     x += 8;
78 
79     const __m256i vacc = _mm256_cvtps_epi32(vx);
80 
81     __m128i vy = _mm_packs_epi32(_mm256_castsi256_si128(vacc), _mm256_extracti128_si256(vacc, 1));
82     vy = _mm_adds_epi16(vy, _mm256_castsi256_si128(voutput_zero_point));
83     vy = _mm_packus_epi16(vy, vy);
84     vy = _mm_max_epu8(vy, _mm256_castsi256_si128(voutput_min));
85 
86     _mm_storel_epi64((__m128i*) y, vy);
87     y += 8;
88   }
89   if XNN_UNLIKELY(n != 0) {
90     assert(n >= 1 * sizeof(float));
91     assert(n <= 7 * sizeof(float));
92     const __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &params->avx2.mask_table[7] - n));
93 
94     __m256 vx = _mm256_maskload_ps(x, vmask);
95     vx = _mm256_mul_ps(vx, vscale);
96     vx = _mm256_min_ps(vx, voutput_max_less_zero_point);
97 
98     const __m256i vacc = _mm256_cvtps_epi32(vx);
99 
100     __m128i vy = _mm_packs_epi32(_mm256_castsi256_si128(vacc), _mm256_extracti128_si256(vacc, 1));
101     vy = _mm_adds_epi16(vy, _mm256_castsi256_si128(voutput_zero_point));
102     vy = _mm_packus_epi16(vy, vy);
103     vy = _mm_max_epu8(vy, _mm256_castsi256_si128(voutput_min));
104 
105     if (n & (4 * sizeof(float))) {
106       _mm_storeu_si32(y, vy);
107       y += 4;
108       vy = _mm_srli_epi64(vy, 32);
109     }
110     if (n & (2 * sizeof(float))) {
111       _mm_storeu_si16(y, vy);
112       y += 2;
113       vy = _mm_srli_epi32(vy, 16);
114     }
115     if (n & (1 * sizeof(float))) {
116       *y = (uint8_t) _mm_extract_epi8(vy, 0);
117     }
118   }
119 }
120