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 <immintrin.h>
10
11 #include <xnnpack/common.h>
12 #include <xnnpack/scalar-utils.h>
13 #include <xnnpack/vadd.h>
14
15
xnn_qu8_vadd_minmax_ukernel__sse2(size_t n,const uint8_t * a,const uint8_t * b,uint8_t * y,const union xnn_qu8_add_params params[restrict XNN_MIN_ELEMENTS (1)])16 void xnn_qu8_vadd_minmax_ukernel__sse2(
17 size_t n,
18 const uint8_t* a,
19 const uint8_t* b,
20 uint8_t* y,
21 const union xnn_qu8_add_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
22 {
23 const __m128i vzero_point_product = _mm_load_si128((const __m128i*) ¶ms->sse2.zero_point_product);
24 const __m128i va_multiplier_lo = _mm_load_si128((const __m128i*) ¶ms->sse2.a_multiplier_lo);
25 const __m128i va_multiplier_hi = _mm_load_si128((const __m128i*) ¶ms->sse2.a_multiplier_hi);
26 const __m128i vb_multiplier_lo = _mm_load_si128((const __m128i*) ¶ms->sse2.b_multiplier_lo);
27 const __m128i vb_multiplier_hi = _mm_load_si128((const __m128i*) ¶ms->sse2.b_multiplier_hi);
28 const __m128i vremainder_mask = _mm_load_si128((const __m128i*) params->sse2.remainder_mask);
29 const __m128i vremainder_threshold = _mm_load_si128((const __m128i*) params->sse2.remainder_threshold);
30 const __m128i vshift = _mm_cvtsi32_si128((int) params->sse2.shift);
31
32 const __m128i vzero = _mm_setzero_si128();
33 for (; n >= 8 * sizeof(uint8_t); n -= 8 * sizeof(uint8_t)) {
34 const __m128i va = _mm_loadl_epi64((const __m128i*) a);
35 a += 8;
36 const __m128i vb = _mm_loadl_epi64((const __m128i*) b);
37 b += 8;
38
39 const __m128i vxa = _mm_unpacklo_epi8(va, vzero);
40 const __m128i vxb = _mm_unpacklo_epi8(vb, vzero);
41
42 // Multiply by factors.
43 const __m128i va_product_lo = _mm_mullo_epi16(vxa, va_multiplier_lo);
44 const __m128i va_product_hi =
45 _mm_add_epi16(_mm_mulhi_epu16(vxa, va_multiplier_lo), _mm_mullo_epi16(vxa, va_multiplier_hi));
46
47 const __m128i vb_product_lo = _mm_mullo_epi16(vxb, vb_multiplier_lo);
48 const __m128i vb_product_hi =
49 _mm_add_epi16(_mm_mulhi_epu16(vxb, vb_multiplier_lo), _mm_mullo_epi16(vxb, vb_multiplier_hi));
50
51 // Accumulate products.
52 __m128i vacc_lo = _mm_add_epi32(vzero_point_product, _mm_unpacklo_epi16(va_product_lo, va_product_hi));
53 __m128i vacc_hi = _mm_add_epi32(vzero_point_product, _mm_unpackhi_epi16(va_product_lo, va_product_hi));
54
55 vacc_lo = _mm_add_epi32(vacc_lo, _mm_unpacklo_epi16(vb_product_lo, vb_product_hi));
56 vacc_hi = _mm_add_epi32(vacc_hi, _mm_unpackhi_epi16(vb_product_lo, vb_product_hi));
57
58 // Shift right and round.
59 const __m128i vrem_lo =
60 _mm_add_epi32(_mm_and_si128(vacc_lo, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vacc_lo));
61 const __m128i vrem_hi =
62 _mm_add_epi32(_mm_and_si128(vacc_hi, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vacc_hi));
63
64 vacc_lo = _mm_sub_epi32(_mm_sra_epi32(vacc_lo, vshift), _mm_cmpgt_epi32(vrem_lo, vremainder_threshold));
65 vacc_hi = _mm_sub_epi32(_mm_sra_epi32(vacc_hi, vshift), _mm_cmpgt_epi32(vrem_hi, vremainder_threshold));
66
67 // Pack, saturate, and add output zero point.
68 const __m128i vy_zero_point = _mm_load_si128((const __m128i*) params->sse2.y_zero_point);
69 const __m128i vacc = _mm_adds_epi16(_mm_packs_epi32(vacc_lo, vacc_hi), vy_zero_point);
70 __m128i vy = _mm_packus_epi16(vacc, vacc);
71 vy = _mm_max_epu8(vy, _mm_load_si128((const __m128i*) params->sse2.y_min));
72 vy = _mm_min_epu8(vy, _mm_load_si128((const __m128i*) params->sse2.y_max));
73
74 _mm_storel_epi64((__m128i*) y, vy);
75 y += 8;
76 }
77 if (n != 0) {
78 const __m128i va = _mm_loadl_epi64((const __m128i*) a);
79 const __m128i vb = _mm_loadl_epi64((const __m128i*) b);
80
81 const __m128i vxa = _mm_unpacklo_epi8(va, vzero);
82 const __m128i vxb = _mm_unpacklo_epi8(vb, vzero);
83
84 // Multiply by factors.
85 const __m128i va_product_lo = _mm_mullo_epi16(vxa, va_multiplier_lo);
86 const __m128i va_product_hi =
87 _mm_add_epi16(_mm_mulhi_epu16(vxa, va_multiplier_lo), _mm_mullo_epi16(vxa, va_multiplier_hi));
88
89 const __m128i vb_product_lo = _mm_mullo_epi16(vxb, vb_multiplier_lo);
90 const __m128i vb_product_hi =
91 _mm_add_epi16(_mm_mulhi_epu16(vxb, vb_multiplier_lo), _mm_mullo_epi16(vxb, vb_multiplier_hi));
92
93 // Accumulate products.
94 __m128i vacc_lo = _mm_add_epi32(vzero_point_product, _mm_unpacklo_epi16(va_product_lo, va_product_hi));
95 __m128i vacc_hi = _mm_add_epi32(vzero_point_product, _mm_unpackhi_epi16(va_product_lo, va_product_hi));
96
97 vacc_lo = _mm_add_epi32(vacc_lo, _mm_unpacklo_epi16(vb_product_lo, vb_product_hi));
98 vacc_hi = _mm_add_epi32(vacc_hi, _mm_unpackhi_epi16(vb_product_lo, vb_product_hi));
99
100 // Shift right and round.
101 const __m128i vrem_lo =
102 _mm_add_epi32(_mm_and_si128(vacc_lo, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vacc_lo));
103 const __m128i vrem_hi =
104 _mm_add_epi32(_mm_and_si128(vacc_hi, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vacc_hi));
105
106 vacc_lo = _mm_sub_epi32(_mm_sra_epi32(vacc_lo, vshift), _mm_cmpgt_epi32(vrem_lo, vremainder_threshold));
107 vacc_hi = _mm_sub_epi32(_mm_sra_epi32(vacc_hi, vshift), _mm_cmpgt_epi32(vrem_hi, vremainder_threshold));
108
109 // Pack, saturate, and add output zero point.
110 const __m128i vy_zero_point = _mm_load_si128((const __m128i*) params->sse2.y_zero_point);
111 const __m128i vacc = _mm_adds_epi16(_mm_packs_epi32(vacc_lo, vacc_hi), vy_zero_point);
112 __m128i vy = _mm_packus_epi16(vacc, vacc);
113 vy = _mm_max_epu8(vy, _mm_load_si128((const __m128i*) params->sse2.y_min));
114 vy = _mm_min_epu8(vy, _mm_load_si128((const __m128i*) params->sse2.y_max));
115
116 if (n & (4 * sizeof(uint8_t))) {
117 *((uint32_t*) y) = (uint32_t) _mm_cvtsi128_si32(vy);
118 vy = _mm_srli_epi64(vy, 32);
119 y += 4;
120 }
121 if (n & (2 * sizeof(uint8_t))) {
122 *((uint16_t*) y) = (uint16_t) _mm_extract_epi16(vy, 0);
123 vy = _mm_srli_epi32(vy, 16);
124 y += 2;
125 }
126 if (n & (1 * sizeof(uint8_t))) {
127 *((uint8_t*) y) = (uint8_t) _mm_cvtsi128_si32(vy);
128 }
129 }
130 }
131