• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-qs8-vcvt/avx.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__avx_x8(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__avx_x8(
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->avx.scale);
31   const __m256 voutput_max_less_zero_point = _mm256_load_ps(params->avx.output_max_less_zero_point);
32   const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->avx.output_zero_point);
33   const __m128i voutput_min = _mm_load_si128((const __m128i*) params->avx.output_min);
34 
35   for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
36     __m256 vx = _mm256_loadu_ps(x);
37     vx = _mm256_mul_ps(vx, vscale);
38     vx = _mm256_min_ps(vx, voutput_max_less_zero_point);
39     x += 8;
40 
41     const __m256i vacc = _mm256_cvtps_epi32(vx);
42 
43     __m128i vy = _mm_packs_epi32(_mm256_castsi256_si128(vacc), _mm256_extractf128_si256(vacc, 1));
44     vy = _mm_adds_epi16(vy, voutput_zero_point);
45     vy = _mm_packus_epi16(vy, vy);
46     vy = _mm_max_epu8(vy, voutput_min);
47 
48     _mm_storel_epi64((__m128i*) y, vy);
49     y += 8;
50   }
51   if XNN_UNLIKELY(n != 0) {
52     assert(n >= 1 * sizeof(float));
53     assert(n <= 7 * sizeof(float));
54     const __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &params->avx.mask_table[7] - n));
55 
56     __m256 vx = _mm256_maskload_ps(x, vmask);
57     vx = _mm256_mul_ps(vx, vscale);
58     vx = _mm256_min_ps(vx, voutput_max_less_zero_point);
59 
60     const __m256i vacc = _mm256_cvtps_epi32(vx);
61 
62     __m128i vy = _mm_packs_epi32(_mm256_castsi256_si128(vacc), _mm256_extractf128_si256(vacc, 1));
63     vy = _mm_adds_epi16(vy, voutput_zero_point);
64     vy = _mm_packus_epi16(vy, vy);
65     vy = _mm_max_epu8(vy, voutput_min);
66 
67     if (n & (4 * sizeof(float))) {
68       _mm_storeu_si32(y, vy);
69       y += 4;
70       vy = _mm_srli_epi64(vy, 32);
71     }
72     if (n & (2 * sizeof(float))) {
73       _mm_storeu_si16(y, vy);
74       y += 2;
75       vy = _mm_srli_epi32(vy, 16);
76     }
77     if (n & (1 * sizeof(float))) {
78       *y = (uint8_t) _mm_extract_epi8(vy, 0);
79     }
80   }
81 }
82