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_qs8_vcvt_ukernel__avx2_x48(size_t n,const float * x,int8_t * y,const union xnn_f32_qs8_cvt_params params[restrict XNN_MIN_ELEMENTS (1)])19 void xnn_f32_qs8_vcvt_ukernel__avx2_x48(
20 size_t n,
21 const float* x,
22 int8_t* y,
23 const union xnn_f32_qs8_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 >= 48 * sizeof(float); n -= 48 * 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 __m256 vx89 = _mm256_loadu_ps(x + 32);
42 __m256 vxAB = _mm256_loadu_ps(x + 40);
43 x += 48;
44
45 vx01 = _mm256_mul_ps(vx01, vscale);
46 vx23 = _mm256_mul_ps(vx23, vscale);
47 vx45 = _mm256_mul_ps(vx45, vscale);
48 vx67 = _mm256_mul_ps(vx67, vscale);
49 vx89 = _mm256_mul_ps(vx89, vscale);
50 vxAB = _mm256_mul_ps(vxAB, vscale);
51
52 vx01 = _mm256_min_ps(vx01, voutput_max_less_zero_point);
53 vx23 = _mm256_min_ps(vx23, voutput_max_less_zero_point);
54 vx45 = _mm256_min_ps(vx45, voutput_max_less_zero_point);
55 vx67 = _mm256_min_ps(vx67, voutput_max_less_zero_point);
56 vx89 = _mm256_min_ps(vx89, voutput_max_less_zero_point);
57 vxAB = _mm256_min_ps(vxAB, voutput_max_less_zero_point);
58
59 const __m256i vacc01 = _mm256_cvtps_epi32(vx01);
60 const __m256i vacc23 = _mm256_cvtps_epi32(vx23);
61 const __m256i vacc45 = _mm256_cvtps_epi32(vx45);
62 const __m256i vacc67 = _mm256_cvtps_epi32(vx67);
63 const __m256i vacc89 = _mm256_cvtps_epi32(vx89);
64 const __m256i vaccAB = _mm256_cvtps_epi32(vxAB);
65
66 __m256i vacc0213 = _mm256_packs_epi32(vacc01, vacc23);
67 __m256i vacc4657 = _mm256_packs_epi32(vacc45, vacc67);
68 __m256i vacc8A9B = _mm256_packs_epi32(vacc89, vaccAB);
69
70 vacc0213 = _mm256_adds_epi16(vacc0213, voutput_zero_point);
71 vacc4657 = _mm256_adds_epi16(vacc4657, voutput_zero_point);
72 vacc8A9B = _mm256_adds_epi16(vacc8A9B, voutput_zero_point);
73
74 const __m256i vy02461357 = _mm256_packs_epi16(vacc0213, vacc4657);
75 const __m128i vy8A9B = _mm_packs_epi16(_mm256_castsi256_si128(vacc8A9B), _mm256_extracti128_si256(vacc8A9B, 1));
76
77 __m256i vy01234567 = _mm256_permutevar8x32_epi32(vy02461357, vshuffle_mask);
78 __m128i vy89AB = _mm_shuffle_epi32(vy8A9B, _MM_SHUFFLE(3, 1, 2, 0));
79
80 vy01234567 = _mm256_max_epi8(vy01234567, voutput_min);
81 vy89AB = _mm_max_epi8(vy89AB, _mm256_castsi256_si128(voutput_min));
82
83 _mm256_storeu_si256((__m256i*) y, vy01234567);
84 _mm_storeu_si128((__m128i*) (y + 32), vy89AB);
85 y += 48;
86 }
87 for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
88 __m256 vx = _mm256_loadu_ps(x);
89 vx = _mm256_mul_ps(vx, vscale);
90 vx = _mm256_min_ps(vx, voutput_max_less_zero_point);
91 x += 8;
92
93 const __m256i vacc = _mm256_cvtps_epi32(vx);
94
95 __m128i vy = _mm_packs_epi32(_mm256_castsi256_si128(vacc), _mm256_extracti128_si256(vacc, 1));
96 vy = _mm_adds_epi16(vy, _mm256_castsi256_si128(voutput_zero_point));
97 vy = _mm_packs_epi16(vy, vy);
98 vy = _mm_max_epi8(vy, _mm256_castsi256_si128(voutput_min));
99
100 _mm_storel_epi64((__m128i*) y, vy);
101 y += 8;
102 }
103 if XNN_UNLIKELY(n != 0) {
104 assert(n >= 1 * sizeof(float));
105 assert(n <= 7 * sizeof(float));
106 const __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) ¶ms->avx2.mask_table[7] - n));
107
108 __m256 vx = _mm256_maskload_ps(x, vmask);
109 vx = _mm256_mul_ps(vx, vscale);
110 vx = _mm256_min_ps(vx, voutput_max_less_zero_point);
111
112 const __m256i vacc = _mm256_cvtps_epi32(vx);
113
114 __m128i vy = _mm_packs_epi32(_mm256_castsi256_si128(vacc), _mm256_extracti128_si256(vacc, 1));
115 vy = _mm_adds_epi16(vy, _mm256_castsi256_si128(voutput_zero_point));
116 vy = _mm_packs_epi16(vy, vy);
117 vy = _mm_max_epi8(vy, _mm256_castsi256_si128(voutput_min));
118
119 if (n & (4 * sizeof(float))) {
120 _mm_storeu_si32(y, vy);
121 y += 4;
122 vy = _mm_srli_epi64(vy, 32);
123 }
124 if (n & (2 * sizeof(float))) {
125 _mm_storeu_si16(y, vy);
126 y += 2;
127 vy = _mm_srli_epi32(vy, 16);
128 }
129 if (n & (1 * sizeof(float))) {
130 *y = (int8_t) _mm_extract_epi8(vy, 0);
131 }
132 }
133 }
134