• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-qs8-vcvt/sse.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 <emmintrin.h>
13 
14 #include <xnnpack/common.h>
15 #include <xnnpack/unaligned.h>
16 #include <xnnpack/vcvt.h>
17 
18 
xnn_f32_qu8_vcvt_ukernel__sse2_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__sse2_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)]) XNN_OOB_READS
24 {
25   assert(n != 0);
26   assert(n % sizeof(float) == 0);
27   assert(x != NULL);
28   assert(y != NULL);
29 
30   const __m128 vscale = _mm_load_ps(params->sse2.scale);
31   const __m128 voutput_max_less_zero_point = _mm_load_ps(params->sse2.output_max_less_zero_point);
32   const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->sse2.output_zero_point);
33   const __m128i voutput_min = _mm_load_si128((const __m128i*) params->sse2.output_min);
34 
35   for (; n >= 32 * sizeof(float); n -= 32 * sizeof(float)) {
36     __m128 vx0123 = _mm_loadu_ps(x);
37     __m128 vx4567 = _mm_loadu_ps(x + 4);
38     __m128 vx89AB = _mm_loadu_ps(x + 8);
39     __m128 vxCDEF = _mm_loadu_ps(x + 12);
40     __m128 vxGHIJ = _mm_loadu_ps(x + 16);
41     __m128 vxKLMN = _mm_loadu_ps(x + 20);
42     __m128 vxOPQR = _mm_loadu_ps(x + 24);
43     __m128 vxSTUV = _mm_loadu_ps(x + 28);
44     x += 32;
45 
46     vx0123 = _mm_mul_ps(vx0123, vscale);
47     vx4567 = _mm_mul_ps(vx4567, vscale);
48     vx89AB = _mm_mul_ps(vx89AB, vscale);
49     vxCDEF = _mm_mul_ps(vxCDEF, vscale);
50     vxGHIJ = _mm_mul_ps(vxGHIJ, vscale);
51     vxKLMN = _mm_mul_ps(vxKLMN, vscale);
52     vxOPQR = _mm_mul_ps(vxOPQR, vscale);
53     vxSTUV = _mm_mul_ps(vxSTUV, vscale);
54 
55     vx0123 = _mm_min_ps(vx0123, voutput_max_less_zero_point);
56     vx4567 = _mm_min_ps(vx4567, voutput_max_less_zero_point);
57     vx89AB = _mm_min_ps(vx89AB, voutput_max_less_zero_point);
58     vxCDEF = _mm_min_ps(vxCDEF, voutput_max_less_zero_point);
59     vxGHIJ = _mm_min_ps(vxGHIJ, voutput_max_less_zero_point);
60     vxKLMN = _mm_min_ps(vxKLMN, voutput_max_less_zero_point);
61     vxOPQR = _mm_min_ps(vxOPQR, voutput_max_less_zero_point);
62     vxSTUV = _mm_min_ps(vxSTUV, voutput_max_less_zero_point);
63 
64     const __m128i vy0123 = _mm_cvtps_epi32(vx0123);
65     const __m128i vy4567 = _mm_cvtps_epi32(vx4567);
66     const __m128i vy89AB = _mm_cvtps_epi32(vx89AB);
67     const __m128i vyCDEF = _mm_cvtps_epi32(vxCDEF);
68     const __m128i vyGHIJ = _mm_cvtps_epi32(vxGHIJ);
69     const __m128i vyKLMN = _mm_cvtps_epi32(vxKLMN);
70     const __m128i vyOPQR = _mm_cvtps_epi32(vxOPQR);
71     const __m128i vySTUV = _mm_cvtps_epi32(vxSTUV);
72 
73     __m128i vy01234567 = _mm_packs_epi32(vy0123, vy4567);
74     __m128i vy89ABCDEF = _mm_packs_epi32(vy89AB, vyCDEF);
75     __m128i vyGHIJKLMN = _mm_packs_epi32(vyGHIJ, vyKLMN);
76     __m128i vyOPQRSTUV = _mm_packs_epi32(vyOPQR, vySTUV);
77 
78     vy01234567 = _mm_adds_epi16(vy01234567, voutput_zero_point);
79     vy89ABCDEF = _mm_adds_epi16(vy89ABCDEF, voutput_zero_point);
80     vyGHIJKLMN = _mm_adds_epi16(vyGHIJKLMN, voutput_zero_point);
81     vyOPQRSTUV = _mm_adds_epi16(vyOPQRSTUV, voutput_zero_point);
82 
83 
84     __m128i vy0123456789ABCDEF = _mm_packus_epi16(vy01234567, vy89ABCDEF);
85     __m128i vyGHIJKLMNOPQRSTUV = _mm_packus_epi16(vyGHIJKLMN, vyOPQRSTUV);
86 
87     vy0123456789ABCDEF = _mm_max_epu8(vy0123456789ABCDEF, voutput_min);
88     vyGHIJKLMNOPQRSTUV = _mm_max_epu8(vyGHIJKLMNOPQRSTUV, voutput_min);
89 
90     _mm_storeu_si128((__m128i*) y, vy0123456789ABCDEF);
91     _mm_storeu_si128((__m128i*) (y + 16), vyGHIJKLMNOPQRSTUV);
92     y += 32;
93   }
94   for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
95     __m128 vx_lo = _mm_loadu_ps(x);
96     __m128 vx_hi = _mm_loadu_ps(x + 4);
97     x += 8;
98 
99     vx_lo = _mm_mul_ps(vx_lo, vscale);
100     vx_hi = _mm_mul_ps(vx_hi, vscale);
101 
102     vx_lo = _mm_min_ps(vx_lo, voutput_max_less_zero_point);
103     vx_hi = _mm_min_ps(vx_hi, voutput_max_less_zero_point);
104 
105     const __m128i vy_lo = _mm_cvtps_epi32(vx_lo);
106     const __m128i vy_hi = _mm_cvtps_epi32(vx_hi);
107 
108     __m128i vy = _mm_packs_epi32(vy_lo, vy_hi);
109     vy = _mm_adds_epi16(vy, voutput_zero_point);
110     vy = _mm_packus_epi16(vy, vy);
111     vy = _mm_max_epu8(vy, voutput_min);
112 
113     _mm_storel_epi64((__m128i*) y, vy);
114     y += 8;
115   }
116   if XNN_UNLIKELY(n != 0) {
117     __m128 vx_lo = _mm_loadu_ps(x);
118     const float* x_hi = (const float*) ((uintptr_t) x + (n & (4 * sizeof(float))));
119     __m128 vx_hi = _mm_loadu_ps(x_hi);
120 
121     vx_lo = _mm_mul_ps(vx_lo, vscale);
122     vx_hi = _mm_mul_ps(vx_hi, vscale);
123 
124     vx_lo = _mm_min_ps(vx_lo, voutput_max_less_zero_point);
125     vx_hi = _mm_min_ps(vx_hi, voutput_max_less_zero_point);
126 
127     const __m128i vy_lo = _mm_cvtps_epi32(vx_lo);
128     const __m128i vy_hi = _mm_cvtps_epi32(vx_hi);
129 
130     __m128i vy = _mm_packs_epi32(vy_lo, vy_hi);
131     vy = _mm_adds_epi16(vy, voutput_zero_point);
132     vy = _mm_packus_epi16(vy, vy);
133     vy = _mm_max_epu8(vy, voutput_min);
134 
135     if (n & (4 * sizeof(float))) {
136       unaligned_store_u32(y, (uint32_t) _mm_cvtsi128_si32(vy));
137       y += 4;
138       vy = _mm_srli_epi64(vy, 32);
139     }
140     {
141       uint32_t vy_lo = (uint32_t) _mm_cvtsi128_si32(vy);
142       if (n & (2 * sizeof(float))) {
143         unaligned_store_u16(y, (uint16_t) vy_lo);
144         y += 2;
145         vy_lo >>= 16;
146       }
147       if (n & (1 * sizeof(float))) {
148         *y = (uint8_t) vy_lo;
149       }
150     }
151   }
152 }
153