1 // Copyright (c) Facebook, Inc. and its affiliates.
2 // All rights reserved.
3 //
4 // Copyright 2019 Google LLC
5 //
6 // This source code is licensed under the BSD-style license found in the
7 // LICENSE file in the root directory of this source tree.
8
9 #include <assert.h>
10 #include <stdint.h>
11 #include <stddef.h>
12
13 #include <smmintrin.h>
14
15 #include <fp16/bitcasts.h>
16
17 #include <xnnpack/requantization-stubs.h>
18
19
xnn_qu8_requantize_precise__sse4(size_t n,const int32_t * input,float scale,uint8_t zero_point,uint8_t qmin,uint8_t qmax,uint8_t * output)20 void xnn_qu8_requantize_precise__sse4(
21 size_t n,
22 const int32_t* input,
23 float scale,
24 uint8_t zero_point,
25 uint8_t qmin,
26 uint8_t qmax,
27 uint8_t* output)
28 {
29 assert(n % 16 == 0);
30 assert(scale < 1.0f);
31 assert(scale >= 0x1.0p-32f);
32
33 const uint32_t scale_bits = fp32_to_bits(scale);
34 const uint32_t multiplier = (scale_bits << 8) | UINT32_C(0x80000000);
35 const uint32_t shift = 127 + 31 - (scale_bits >> 23);
36 assert(shift >= 32);
37 assert(shift < 64);
38 const uint64_t rounding = UINT64_C(1) << (shift - 1);
39
40 const __m128i vmultiplier = _mm_set1_epi32(multiplier);
41 const __m128i vzero_point = _mm_set1_epi16((short) (uint16_t) zero_point);
42 const __m128i vqmin = _mm_set1_epi8((char) qmin);
43 const __m128i vqmax = _mm_set1_epi8((char) qmax);
44 const __m128i vshiftlo = _mm_cvtsi32_si128((int) shift);
45 const __m128i vshifthi = _mm_cvtsi32_si128((int) shift - 32);
46 const __m128i vrounding = _mm_set1_epi64x(rounding);
47 for (; n != 0; n -= 16) {
48 const __m128i x = _mm_loadu_si128((const __m128i*) input);
49 const __m128i y = _mm_loadu_si128((const __m128i*) (input + 4));
50 const __m128i z = _mm_loadu_si128((const __m128i*) (input + 8));
51 const __m128i w = _mm_loadu_si128((const __m128i*) (input + 12));
52 input += 16;
53
54 const __m128i x_abs0123 = _mm_abs_epi32(x);
55 const __m128i y_abs0123 = _mm_abs_epi32(y);
56 const __m128i z_abs0123 = _mm_abs_epi32(z);
57 const __m128i w_abs0123 = _mm_abs_epi32(w);
58
59 const __m128i x_abs1032 = _mm_shuffle_epi32(x_abs0123, _MM_SHUFFLE(2, 3, 0, 1));
60 const __m128i y_abs1032 = _mm_shuffle_epi32(y_abs0123, _MM_SHUFFLE(2, 3, 0, 1));
61 const __m128i z_abs1032 = _mm_shuffle_epi32(z_abs0123, _MM_SHUFFLE(2, 3, 0, 1));
62 const __m128i w_abs1032 = _mm_shuffle_epi32(w_abs0123, _MM_SHUFFLE(2, 3, 0, 1));
63
64 const __m128i x_absmul02 = _mm_mul_epu32(x_abs0123, vmultiplier);
65 const __m128i y_absmul02 = _mm_mul_epu32(y_abs0123, vmultiplier);
66 const __m128i z_absmul02 = _mm_mul_epu32(z_abs0123, vmultiplier);
67 const __m128i w_absmul02 = _mm_mul_epu32(w_abs0123, vmultiplier);
68
69 const __m128i x_absmul13 = _mm_mul_epu32(x_abs1032, vmultiplier);
70 const __m128i y_absmul13 = _mm_mul_epu32(y_abs1032, vmultiplier);
71 const __m128i z_absmul13 = _mm_mul_epu32(z_abs1032, vmultiplier);
72 const __m128i w_absmul13 = _mm_mul_epu32(w_abs1032, vmultiplier);
73
74 const __m128i x_abs_scaled02 = _mm_srl_epi64(_mm_add_epi64(x_absmul02, vrounding), vshiftlo);
75 const __m128i x_abs_scaled13 = _mm_srl_epi32(_mm_add_epi64(x_absmul13, vrounding), vshifthi);
76 const __m128i y_abs_scaled02 = _mm_srl_epi64(_mm_add_epi64(y_absmul02, vrounding), vshiftlo);
77 const __m128i y_abs_scaled13 = _mm_srl_epi32(_mm_add_epi64(y_absmul13, vrounding), vshifthi);
78 const __m128i z_abs_scaled02 = _mm_srl_epi64(_mm_add_epi64(z_absmul02, vrounding), vshiftlo);
79 const __m128i z_abs_scaled13 = _mm_srl_epi32(_mm_add_epi64(z_absmul13, vrounding), vshifthi);
80 const __m128i w_abs_scaled02 = _mm_srl_epi64(_mm_add_epi64(w_absmul02, vrounding), vshiftlo);
81 const __m128i w_abs_scaled13 = _mm_srl_epi32(_mm_add_epi64(w_absmul13, vrounding), vshifthi);
82
83 const __m128i x_abs_scaled = _mm_blend_epi16(x_abs_scaled02, x_abs_scaled13, 0xCC);
84 const __m128i y_abs_scaled = _mm_blend_epi16(y_abs_scaled02, y_abs_scaled13, 0xCC);
85 const __m128i z_abs_scaled = _mm_blend_epi16(z_abs_scaled02, z_abs_scaled13, 0xCC);
86 const __m128i w_abs_scaled = _mm_blend_epi16(w_abs_scaled02, w_abs_scaled13, 0xCC);
87
88 const __m128i x_scaled = _mm_sign_epi32(x_abs_scaled, x);
89 const __m128i y_scaled = _mm_sign_epi32(y_abs_scaled, y);
90 const __m128i z_scaled = _mm_sign_epi32(z_abs_scaled, z);
91 const __m128i w_scaled = _mm_sign_epi32(w_abs_scaled, w);
92
93 const __m128i xy_packed = _mm_adds_epi16(_mm_packs_epi32(x_scaled, y_scaled), vzero_point);
94 const __m128i zw_packed = _mm_adds_epi16(_mm_packs_epi32(z_scaled, w_scaled), vzero_point);
95 const __m128i xyzw_packed = _mm_packus_epi16(xy_packed, zw_packed);
96 const __m128i xyzw_clamped = _mm_max_epu8(_mm_min_epu8(xyzw_packed, vqmax), vqmin);
97
98 // 4x PABSD
99 // 4x PSHUFD
100 // 8x PMULUDQ
101 // 4x PSRLQ
102 // 4x PSRLD
103 // 8x PADDQ
104 // 4x PBLENDW
105 // 4x PSIGND
106 // 2x PACKSSDW
107 // 1x PACKUSWB
108 // 2x PADDW
109 // 1x PMAXUB
110 // 1x PMINUB
111 // ---------------------
112 // 47 instructions total
113
114 _mm_storeu_si128((__m128i*) output, xyzw_clamped);
115 output += 16;
116 }
117 }
118