xref: /aosp_15_r20/external/XNNPACK/src/qs8-requantization/rndnu-sse4-srl.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
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 <xnnpack/math.h>
13 #include <xnnpack/requantization-stubs.h>
14 
15 
xnn_qs8_requantize_rndnu__sse4_srl(size_t n,const int32_t * input,float scale,int8_t zero_point,int8_t qmin,int8_t qmax,int8_t * output)16 void xnn_qs8_requantize_rndnu__sse4_srl(
17     size_t n,
18     const int32_t* input,
19     float scale,
20     int8_t zero_point,
21     int8_t qmin,
22     int8_t qmax,
23     int8_t* output)
24 {
25   assert(n % 16 == 0);
26   assert(scale < 1.0f);
27   assert(scale >= 0x1.0p-32f);
28 
29   const uint32_t scale_bits = float_as_uint32(scale);
30   const int32_t multiplier = ((int32_t) (scale_bits << 7) & INT32_C(0x3FFFFF80)) | INT32_C(0x40000000);
31   const uint32_t shift = 127 + 30 - (scale_bits >> 23);
32   assert(shift >= 31);
33   assert(shift < 63);
34   const uint64_t rounding = UINT64_C(1) << (shift - 1);
35   const uint64_t pre_shift_offset = UINT64_C(0x8000000000000000) | rounding;
36   const uint32_t post_shift_offset = (uint32_t) (UINT64_C(0x8000000000000000) >> shift);
37 
38   const __m128i vmultiplier = _mm_set1_epi32(multiplier);
39   const __m128i vzero_point = _mm_set1_epi16((short) zero_point);
40   const __m128i vqmin = _mm_set1_epi8((char) qmin);
41   const __m128i vqmax = _mm_set1_epi8((char) qmax);
42   const __m128i vshift = _mm_cvtsi32_si128((int) shift);
43   const __m128i vpre_offset = _mm_set1_epi64x((long long) pre_shift_offset);
44   const __m128i vpost_offset = _mm_set1_epi32((int) post_shift_offset);
45   for (; n != 0; n -= 16) {
46     const __m128i x = _mm_loadu_si128((const __m128i*) input);
47     const __m128i y = _mm_loadu_si128((const __m128i*) (input + 4));
48     const __m128i z = _mm_loadu_si128((const __m128i*) (input + 8));
49     const __m128i w = _mm_loadu_si128((const __m128i*) (input + 12));
50     input += 16;
51 
52     const __m128i x_odd = _mm_shuffle_epi32(x, _MM_SHUFFLE(3, 3, 1, 1));
53     const __m128i y_odd = _mm_shuffle_epi32(y, _MM_SHUFFLE(3, 3, 1, 1));
54     const __m128i z_odd = _mm_shuffle_epi32(z, _MM_SHUFFLE(3, 3, 1, 1));
55     const __m128i w_odd = _mm_shuffle_epi32(w, _MM_SHUFFLE(3, 3, 1, 1));
56 
57     const __m128i x_product02 = _mm_mul_epi32(x, vmultiplier);
58     const __m128i y_product02 = _mm_mul_epi32(y, vmultiplier);
59     const __m128i z_product02 = _mm_mul_epi32(z, vmultiplier);
60     const __m128i w_product02 = _mm_mul_epi32(w, vmultiplier);
61 
62     const __m128i x_product13 = _mm_mul_epi32(x_odd, vmultiplier);
63     const __m128i y_product13 = _mm_mul_epi32(y_odd, vmultiplier);
64     const __m128i z_product13 = _mm_mul_epi32(z_odd, vmultiplier);
65     const __m128i w_product13 = _mm_mul_epi32(w_odd, vmultiplier);
66 
67     const __m128i x_scaled02 = _mm_srl_epi64(_mm_add_epi64(x_product02, vpre_offset), vshift);
68     const __m128i x_scaled13 = _mm_srl_epi64(_mm_add_epi64(x_product13, vpre_offset), vshift);
69     const __m128i y_scaled02 = _mm_srl_epi64(_mm_add_epi64(y_product02, vpre_offset), vshift);
70     const __m128i y_scaled13 = _mm_srl_epi64(_mm_add_epi64(y_product13, vpre_offset), vshift);
71     const __m128i z_scaled02 = _mm_srl_epi64(_mm_add_epi64(z_product02, vpre_offset), vshift);
72     const __m128i z_scaled13 = _mm_srl_epi64(_mm_add_epi64(z_product13, vpre_offset), vshift);
73     const __m128i w_scaled02 = _mm_srl_epi64(_mm_add_epi64(w_product02, vpre_offset), vshift);
74     const __m128i w_scaled13 = _mm_srl_epi64(_mm_add_epi64(w_product13, vpre_offset), vshift);
75 
76     const __m128i x_scaled = _mm_sub_epi32(_mm_blend_epi16(x_scaled02, _mm_slli_epi64(x_scaled13, 32), 0xCC), vpost_offset);
77     const __m128i y_scaled = _mm_sub_epi32(_mm_blend_epi16(y_scaled02, _mm_slli_epi64(y_scaled13, 32), 0xCC), vpost_offset);
78     const __m128i z_scaled = _mm_sub_epi32(_mm_blend_epi16(z_scaled02, _mm_slli_epi64(z_scaled13, 32), 0xCC), vpost_offset);
79     const __m128i w_scaled = _mm_sub_epi32(_mm_blend_epi16(w_scaled02, _mm_slli_epi64(w_scaled13, 32), 0xCC), vpost_offset);
80 
81     const __m128i xy_packed = _mm_adds_epi16(_mm_packs_epi32(x_scaled, y_scaled), vzero_point);
82     const __m128i zw_packed = _mm_adds_epi16(_mm_packs_epi32(z_scaled, w_scaled), vzero_point);
83     const __m128i xyzw_packed = _mm_packs_epi16(xy_packed, zw_packed);
84     const __m128i xyzw_clamped = _mm_max_epi8(_mm_min_epi8(xyzw_packed, vqmax), vqmin);
85 
86     // 4x PSHUFD
87     // 8x PMULDQ
88     // 8x PADDQ
89     // 8x PSRLQ
90     // 4x PSLLD
91     // 4x PBLENDW
92     // 4x PSUBD
93     // 2x PACKSSDW
94     // 2x PADDSW
95     // 1x PACKSSWB
96     // 1x PMAXSB
97     // 1x PMINSB
98     // ---------------------
99     // 47 instructions total
100 
101     _mm_storeu_si128((__m128i*) output, xyzw_clamped);
102     output += 16;
103   }
104 }
105