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_qs8_vcvt_ukernel__sse2_x32(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__sse2_x32(
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)]) 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 vy01234567 = _mm_max_epi16(vy01234567, voutput_min);
84 vy89ABCDEF = _mm_max_epi16(vy89ABCDEF, voutput_min);
85 vyGHIJKLMN = _mm_max_epi16(vyGHIJKLMN, voutput_min);
86 vyOPQRSTUV = _mm_max_epi16(vyOPQRSTUV, voutput_min);
87
88 __m128i vy0123456789ABCDEF = _mm_packs_epi16(vy01234567, vy89ABCDEF);
89 __m128i vyGHIJKLMNOPQRSTUV = _mm_packs_epi16(vyGHIJKLMN, vyOPQRSTUV);
90
91
92 _mm_storeu_si128((__m128i*) y, vy0123456789ABCDEF);
93 _mm_storeu_si128((__m128i*) (y + 16), vyGHIJKLMNOPQRSTUV);
94 y += 32;
95 }
96 for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
97 __m128 vx_lo = _mm_loadu_ps(x);
98 __m128 vx_hi = _mm_loadu_ps(x + 4);
99 x += 8;
100
101 vx_lo = _mm_mul_ps(vx_lo, vscale);
102 vx_hi = _mm_mul_ps(vx_hi, vscale);
103
104 vx_lo = _mm_min_ps(vx_lo, voutput_max_less_zero_point);
105 vx_hi = _mm_min_ps(vx_hi, voutput_max_less_zero_point);
106
107 const __m128i vy_lo = _mm_cvtps_epi32(vx_lo);
108 const __m128i vy_hi = _mm_cvtps_epi32(vx_hi);
109
110 __m128i vy = _mm_packs_epi32(vy_lo, vy_hi);
111 vy = _mm_adds_epi16(vy, voutput_zero_point);
112 vy = _mm_max_epi16(vy, voutput_min);
113 vy = _mm_packs_epi16(vy, vy);
114
115 _mm_storel_epi64((__m128i*) y, vy);
116 y += 8;
117 }
118 if XNN_UNLIKELY(n != 0) {
119 __m128 vx_lo = _mm_loadu_ps(x);
120 const float* x_hi = (const float*) ((uintptr_t) x + (n & (4 * sizeof(float))));
121 __m128 vx_hi = _mm_loadu_ps(x_hi);
122
123 vx_lo = _mm_mul_ps(vx_lo, vscale);
124 vx_hi = _mm_mul_ps(vx_hi, vscale);
125
126 vx_lo = _mm_min_ps(vx_lo, voutput_max_less_zero_point);
127 vx_hi = _mm_min_ps(vx_hi, voutput_max_less_zero_point);
128
129 const __m128i vy_lo = _mm_cvtps_epi32(vx_lo);
130 const __m128i vy_hi = _mm_cvtps_epi32(vx_hi);
131
132 __m128i vy = _mm_packs_epi32(vy_lo, vy_hi);
133 vy = _mm_adds_epi16(vy, voutput_zero_point);
134 vy = _mm_max_epi16(vy, voutput_min);
135 vy = _mm_packs_epi16(vy, vy);
136
137 if (n & (4 * sizeof(float))) {
138 unaligned_store_u32(y, (uint32_t) _mm_cvtsi128_si32(vy));
139 y += 4;
140 vy = _mm_srli_epi64(vy, 32);
141 }
142 {
143 uint32_t vy_lo = (uint32_t) _mm_cvtsi128_si32(vy);
144 if (n & (2 * sizeof(float))) {
145 unaligned_store_u16(y, (uint16_t) vy_lo);
146 y += 2;
147 vy_lo >>= 16;
148 }
149 if (n & (1 * sizeof(float))) {
150 *y = (int8_t) vy_lo;
151 }
152 }
153 }
154 }
155