• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 Google LLC
2 //
3 // This source code is licensed under the BSD-style license found in the
4 // LICENSE file in the root directory of this source tree.
5 
6 #include <assert.h>
7 #include <stdint.h>
8 #include <stddef.h>
9 
10 #include <smmintrin.h>
11 
12 #include <fp16/bitcasts.h>
13 
14 #include <xnnpack/requantization-stubs.h>
15 
16 
xnn_qs8_requantize_rndnu__sse4_sra(size_t n,const int32_t * input,float scale,int8_t zero_point,int8_t qmin,int8_t qmax,int8_t * output)17 void xnn_qs8_requantize_rndnu__sse4_sra(
18     size_t n,
19     const int32_t* input,
20     float scale,
21     int8_t zero_point,
22     int8_t qmin,
23     int8_t qmax,
24     int8_t* output)
25 {
26   assert(n % 16 == 0);
27   assert(scale < 1.0f);
28   assert(scale >= 0x1.0p-32f);
29 
30   const uint32_t scale_bits = fp32_to_bits(scale);
31   const int32_t multiplier = ((int32_t) (scale_bits << 7) & INT32_C(0x3FFFFF80)) | INT32_C(0x40000000);
32   const uint32_t shift = 127 + 30 - (scale_bits >> 23);
33   assert(shift >= 31);
34   assert(shift < 63);
35   const int64_t rounding = INT64_C(1) << (shift - 1);
36 
37   const __m128i vmultiplier = _mm_set1_epi32(multiplier);
38   const __m128i vzero_point = _mm_set1_epi16((short) zero_point);
39   const __m128i vqmin = _mm_set1_epi8((char) qmin);
40   const __m128i vqmax = _mm_set1_epi8((char) qmax);
41   const __m128i vshift = _mm_cvtsi32_si128((int) (shift - 31));
42   const __m128i vrounding = _mm_set1_epi64x(rounding);
43   for (; n != 0; n -= 16) {
44     const __m128i x = _mm_loadu_si128((const __m128i*) input);
45     const __m128i y = _mm_loadu_si128((const __m128i*) (input + 4));
46     const __m128i z = _mm_loadu_si128((const __m128i*) (input + 8));
47     const __m128i w = _mm_loadu_si128((const __m128i*) (input + 12));
48     input += 16;
49 
50     const __m128i x_odd = _mm_shuffle_epi32(x, _MM_SHUFFLE(3, 3, 1, 1));
51     const __m128i y_odd = _mm_shuffle_epi32(y, _MM_SHUFFLE(3, 3, 1, 1));
52     const __m128i z_odd = _mm_shuffle_epi32(z, _MM_SHUFFLE(3, 3, 1, 1));
53     const __m128i w_odd = _mm_shuffle_epi32(w, _MM_SHUFFLE(3, 3, 1, 1));
54 
55     const __m128i x_product02 = _mm_mul_epi32(x, vmultiplier);
56     const __m128i y_product02 = _mm_mul_epi32(y, vmultiplier);
57     const __m128i z_product02 = _mm_mul_epi32(z, vmultiplier);
58     const __m128i w_product02 = _mm_mul_epi32(w, vmultiplier);
59 
60     const __m128i x_product13 = _mm_mul_epi32(x_odd, vmultiplier);
61     const __m128i y_product13 = _mm_mul_epi32(y_odd, vmultiplier);
62     const __m128i z_product13 = _mm_mul_epi32(z_odd, vmultiplier);
63     const __m128i w_product13 = _mm_mul_epi32(w_odd, vmultiplier);
64 
65     const __m128i x_prescaled02 = _mm_srli_epi64(_mm_add_epi64(x_product02, vrounding), 31);
66     const __m128i x_prescaled13 = _mm_slli_epi64(_mm_add_epi64(x_product13, vrounding), 1);
67     const __m128i y_prescaled02 = _mm_srli_epi64(_mm_add_epi64(y_product02, vrounding), 31);
68     const __m128i y_prescaled13 = _mm_slli_epi64(_mm_add_epi64(y_product13, vrounding), 1);
69     const __m128i z_prescaled02 = _mm_srli_epi64(_mm_add_epi64(z_product02, vrounding), 31);
70     const __m128i z_prescaled13 = _mm_slli_epi64(_mm_add_epi64(z_product13, vrounding), 1);
71     const __m128i w_prescaled02 = _mm_srli_epi64(_mm_add_epi64(w_product02, vrounding), 31);
72     const __m128i w_prescaled13 = _mm_slli_epi64(_mm_add_epi64(w_product13, vrounding), 1);
73 
74     const __m128i x_prescaled = _mm_blend_epi16(x_prescaled02, x_prescaled13, 0xCC);
75     const __m128i y_prescaled = _mm_blend_epi16(y_prescaled02, y_prescaled13, 0xCC);
76     const __m128i z_prescaled = _mm_blend_epi16(z_prescaled02, z_prescaled13, 0xCC);
77     const __m128i w_prescaled = _mm_blend_epi16(w_prescaled02, w_prescaled13, 0xCC);
78 
79     const __m128i x_scaled = _mm_sra_epi32(x_prescaled, vshift);
80     const __m128i y_scaled = _mm_sra_epi32(y_prescaled, vshift);
81     const __m128i z_scaled = _mm_sra_epi32(z_prescaled, vshift);
82     const __m128i w_scaled = _mm_sra_epi32(w_prescaled, vshift);
83 
84     const __m128i xy_packed = _mm_adds_epi16(_mm_packs_epi32(x_scaled, y_scaled), vzero_point);
85     const __m128i zw_packed = _mm_adds_epi16(_mm_packs_epi32(z_scaled, w_scaled), vzero_point);
86     const __m128i xyzw_packed = _mm_packs_epi16(xy_packed, zw_packed);
87     const __m128i xyzw_clamped = _mm_max_epi8(_mm_min_epi8(xyzw_packed, vqmax), vqmin);
88 
89     // 4x PSHUFD
90     // 8x PMULDQ
91     // 8x PADDQ
92     // 4x PSRLQ
93     // 4x PSLLQ
94     // 4x PBLENDW
95     // 4x PSRAD
96     // 2x PACKSSDW
97     // 2x PADDSW
98     // 1x PACKSSWB
99     // 1x PMAXSB
100     // 1x PMINSB
101     // ---------------------
102     // 43 instructions total
103 
104     _mm_storeu_si128((__m128i*) output, xyzw_clamped);
105     output += 16;
106   }
107 }
108