• 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_qs8_vcvt_ukernel__sse2_x8(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_x8(
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 >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
36     __m128 vx_lo = _mm_loadu_ps(x);
37     __m128 vx_hi = _mm_loadu_ps(x + 4);
38     x += 8;
39 
40     vx_lo = _mm_mul_ps(vx_lo, vscale);
41     vx_hi = _mm_mul_ps(vx_hi, vscale);
42 
43     vx_lo = _mm_min_ps(vx_lo, voutput_max_less_zero_point);
44     vx_hi = _mm_min_ps(vx_hi, voutput_max_less_zero_point);
45 
46     const __m128i vy_lo = _mm_cvtps_epi32(vx_lo);
47     const __m128i vy_hi = _mm_cvtps_epi32(vx_hi);
48 
49     __m128i vy = _mm_packs_epi32(vy_lo, vy_hi);
50     vy = _mm_adds_epi16(vy, voutput_zero_point);
51     vy = _mm_max_epi16(vy, voutput_min);
52     vy = _mm_packs_epi16(vy, vy);
53 
54     _mm_storel_epi64((__m128i*) y, vy);
55     y += 8;
56   }
57   if XNN_UNLIKELY(n != 0) {
58     __m128 vx_lo = _mm_loadu_ps(x);
59     const float* x_hi = (const float*) ((uintptr_t) x + (n & (4 * sizeof(float))));
60     __m128 vx_hi = _mm_loadu_ps(x_hi);
61 
62     vx_lo = _mm_mul_ps(vx_lo, vscale);
63     vx_hi = _mm_mul_ps(vx_hi, vscale);
64 
65     vx_lo = _mm_min_ps(vx_lo, voutput_max_less_zero_point);
66     vx_hi = _mm_min_ps(vx_hi, voutput_max_less_zero_point);
67 
68     const __m128i vy_lo = _mm_cvtps_epi32(vx_lo);
69     const __m128i vy_hi = _mm_cvtps_epi32(vx_hi);
70 
71     __m128i vy = _mm_packs_epi32(vy_lo, vy_hi);
72     vy = _mm_adds_epi16(vy, voutput_zero_point);
73     vy = _mm_max_epi16(vy, voutput_min);
74     vy = _mm_packs_epi16(vy, vy);
75 
76     if (n & (4 * sizeof(float))) {
77       unaligned_store_u32(y, (uint32_t) _mm_cvtsi128_si32(vy));
78       y += 4;
79       vy = _mm_srli_epi64(vy, 32);
80     }
81     {
82       uint32_t vy_lo = (uint32_t) _mm_cvtsi128_si32(vy);
83       if (n & (2 * sizeof(float))) {
84         unaligned_store_u16(y, (uint16_t) vy_lo);
85         y += 2;
86         vy_lo >>= 16;
87       }
88       if (n & (1 * sizeof(float))) {
89         *y = (int8_t) vy_lo;
90       }
91     }
92   }
93 }
94