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_sra(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_sra(
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 int64_t rounding = INT64_C(1) << (shift - 1);
35
36 const __m128i vmultiplier = _mm_set1_epi32(multiplier);
37 const __m128i vzero_point = _mm_set1_epi16((short) zero_point);
38 const __m128i vqmin = _mm_set1_epi8((char) qmin);
39 const __m128i vqmax = _mm_set1_epi8((char) qmax);
40 const __m128i vshift = _mm_cvtsi32_si128((int) (shift - 31));
41 const __m128i vrounding = _mm_set1_epi64x(rounding);
42 for (; n != 0; n -= 16) {
43 const __m128i x = _mm_loadu_si128((const __m128i*) input);
44 const __m128i y = _mm_loadu_si128((const __m128i*) (input + 4));
45 const __m128i z = _mm_loadu_si128((const __m128i*) (input + 8));
46 const __m128i w = _mm_loadu_si128((const __m128i*) (input + 12));
47 input += 16;
48
49 const __m128i x_odd = _mm_shuffle_epi32(x, _MM_SHUFFLE(3, 3, 1, 1));
50 const __m128i y_odd = _mm_shuffle_epi32(y, _MM_SHUFFLE(3, 3, 1, 1));
51 const __m128i z_odd = _mm_shuffle_epi32(z, _MM_SHUFFLE(3, 3, 1, 1));
52 const __m128i w_odd = _mm_shuffle_epi32(w, _MM_SHUFFLE(3, 3, 1, 1));
53
54 const __m128i x_product02 = _mm_mul_epi32(x, vmultiplier);
55 const __m128i y_product02 = _mm_mul_epi32(y, vmultiplier);
56 const __m128i z_product02 = _mm_mul_epi32(z, vmultiplier);
57 const __m128i w_product02 = _mm_mul_epi32(w, vmultiplier);
58
59 const __m128i x_product13 = _mm_mul_epi32(x_odd, vmultiplier);
60 const __m128i y_product13 = _mm_mul_epi32(y_odd, vmultiplier);
61 const __m128i z_product13 = _mm_mul_epi32(z_odd, vmultiplier);
62 const __m128i w_product13 = _mm_mul_epi32(w_odd, vmultiplier);
63
64 const __m128i x_prescaled02 = _mm_srli_epi64(_mm_add_epi64(x_product02, vrounding), 31);
65 const __m128i x_prescaled13 = _mm_slli_epi64(_mm_add_epi64(x_product13, vrounding), 1);
66 const __m128i y_prescaled02 = _mm_srli_epi64(_mm_add_epi64(y_product02, vrounding), 31);
67 const __m128i y_prescaled13 = _mm_slli_epi64(_mm_add_epi64(y_product13, vrounding), 1);
68 const __m128i z_prescaled02 = _mm_srli_epi64(_mm_add_epi64(z_product02, vrounding), 31);
69 const __m128i z_prescaled13 = _mm_slli_epi64(_mm_add_epi64(z_product13, vrounding), 1);
70 const __m128i w_prescaled02 = _mm_srli_epi64(_mm_add_epi64(w_product02, vrounding), 31);
71 const __m128i w_prescaled13 = _mm_slli_epi64(_mm_add_epi64(w_product13, vrounding), 1);
72
73 const __m128i x_prescaled = _mm_blend_epi16(x_prescaled02, x_prescaled13, 0xCC);
74 const __m128i y_prescaled = _mm_blend_epi16(y_prescaled02, y_prescaled13, 0xCC);
75 const __m128i z_prescaled = _mm_blend_epi16(z_prescaled02, z_prescaled13, 0xCC);
76 const __m128i w_prescaled = _mm_blend_epi16(w_prescaled02, w_prescaled13, 0xCC);
77
78 const __m128i x_scaled = _mm_sra_epi32(x_prescaled, vshift);
79 const __m128i y_scaled = _mm_sra_epi32(y_prescaled, vshift);
80 const __m128i z_scaled = _mm_sra_epi32(z_prescaled, vshift);
81 const __m128i w_scaled = _mm_sra_epi32(w_prescaled, vshift);
82
83 const __m128i xy_packed = _mm_adds_epi16(_mm_packs_epi32(x_scaled, y_scaled), vzero_point);
84 const __m128i zw_packed = _mm_adds_epi16(_mm_packs_epi32(z_scaled, w_scaled), vzero_point);
85 const __m128i xyzw_packed = _mm_packs_epi16(xy_packed, zw_packed);
86 const __m128i xyzw_clamped = _mm_max_epi8(_mm_min_epi8(xyzw_packed, vqmax), vqmin);
87
88 // 4x PSHUFD
89 // 8x PMULDQ
90 // 8x PADDQ
91 // 4x PSRLQ
92 // 4x PSLLQ
93 // 4x PBLENDW
94 // 4x PSRAD
95 // 2x PACKSSDW
96 // 2x PADDSW
97 // 1x PACKSSWB
98 // 1x PMAXSB
99 // 1x PMINSB
100 // ---------------------
101 // 43 instructions total
102
103 _mm_storeu_si128((__m128i*) output, xyzw_clamped);
104 output += 16;
105 }
106 }
107