1// Copyright 2020 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$assert BATCH_TILE % 8 == 0
7$assert BATCH_TILE >= 8
8$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
9#include <assert.h>
10
11$if SSE == 5:
12  #ifdef __GNUC__
13    #include <x86intrin.h>
14  #else
15    #include <immintrin.h>
16    #include <ammintrin.h>
17  #endif
18$else:
19  #include <immintrin.h>
20
21#include <xnnpack/intrinsics-polyfill.h>
22#include <xnnpack/vadd.h>
23
24
25$ISA = {4: "sse41", 5: "xop"}[SSE]
26void xnn_qs8_vaddc_minmax_ukernel__${ISA}_mul32_ld32_x${BATCH_TILE}(
27    size_t n,
28    const int8_t* input_x,
29    const int8_t* input_y,
30    int8_t* output,
31    const union xnn_qs8_add_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
32{
33  const __m128i vx_multiplier = _mm_load_si128((const __m128i*) params->sse2.x_multiplier);
34  const __m128i vremainder_mask = _mm_load_si128((const __m128i*) params->sse2.remainder_mask);
35  const __m128i vremainder_threshold = _mm_load_si128((const __m128i*) params->sse2.remainder_threshold);
36  const __m128i vshift = _mm_cvtsi32_si128((int) params->sse2.shift);
37  const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->sse2.output_zero_point);
38  const __m128i voutput_min = _mm_load_si128((const __m128i*) params->sse2.output_min);
39  const __m128i voutput_max = _mm_load_si128((const __m128i*) params->sse2.output_max);
40
41  __m128i vzero_point_product = _mm_cvtsi32_si128(params->sse2.y_multiplier[0] * (int32_t) *input_y);
42  vzero_point_product = _mm_shuffle_epi32(vzero_point_product, _MM_SHUFFLE(0, 0, 0, 0));
43  vzero_point_product = _mm_add_epi32(vzero_point_product, _mm_load_si128((const __m128i*) params->sse2.zero_point_product));
44  for (; n >= ${BATCH_TILE} * sizeof(int8_t); n -= ${BATCH_TILE} * sizeof(int8_t)) {
45    const __m128i vx${ABC[0:4]} = _mm_cvtepi8_epi32(_mm_loadu_si32(input_x));
46    $for N in range(4, BATCH_TILE, 4):
47      const __m128i vx${ABC[N:N+4]} = _mm_cvtepi8_epi32(_mm_loadu_si32(input_x + ${N}));
48    input_x += ${BATCH_TILE};
49    input_y += ${BATCH_TILE};
50
51    $if SSE == 5:
52      $for N in range(0, BATCH_TILE, 4):
53        __m128i vacc${ABC[N:N+4]} = _mm_macc_epi32(vx${ABC[N:N+4]}, vx_multiplier, vzero_point_product);
54    $else:
55      $for N in range(0, BATCH_TILE, 4):
56        __m128i vacc${ABC[N:N+4]} = _mm_add_epi32(vzero_point_product, _mm_mullo_epi32(vx${ABC[N:N+4]}, vx_multiplier));
57
58    $for N in range(0, BATCH_TILE, 4):
59      const __m128i vrem${ABC[N:N+4]} = _mm_add_epi32(_mm_and_si128(vacc${ABC[N:N+4]}, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vacc${ABC[N:N+4]}));
60
61    $for N in range(0, BATCH_TILE, 4):
62      vacc${ABC[N:N+4]} = _mm_sub_epi32(_mm_sra_epi32(vacc${ABC[N:N+4]}, vshift), _mm_cmpgt_epi32(vrem${ABC[N:N+4]}, vremainder_threshold));
63
64    $for N in range(0, BATCH_TILE, 8):
65      __m128i vout${ABC[N:N+8]} = _mm_adds_epi16(_mm_packs_epi32(vacc${ABC[N:N+4]}, vacc${ABC[N+4:N+8]}), voutput_zero_point);
66
67    $for N in range(0, BATCH_TILE, 8):
68      vout${ABC[N:N+8]} = _mm_max_epi16(vout${ABC[N:N+8]}, voutput_min);
69
70    $for N in range(0, BATCH_TILE, 8):
71      vout${ABC[N:N+8]} = _mm_min_epi16(vout${ABC[N:N+8]}, voutput_max);
72
73    $for N in range(0, BATCH_TILE, 16):
74      $if N + 8 < BATCH_TILE:
75        const __m128i vout${ABC[N:N+16]} = _mm_packs_epi16(vout${ABC[N:N+8]}, vout${ABC[N+8:N+16]});
76      $else:
77        const __m128i vout${ABC[N:N+8]}${ABC[N:N+8]} = _mm_packs_epi16(vout${ABC[N:N+8]}, vout${ABC[N:N+8]});
78
79    $if BATCH_TILE >= 16:
80      _mm_storeu_si128((__m128i*) output, vout${ABC[0:16]});
81    $else:
82      _mm_storel_epi64((__m128i*) output, vout${ABC[0:8]}${ABC[0:8]});
83    $for N in range(16, BATCH_TILE, 16):
84      $if N + 8 < BATCH_TILE:
85        _mm_storeu_si128((__m128i*) (output + ${N}), vout${ABC[N:N+16]});
86      $else:
87        _mm_storel_epi64((__m128i*) (output + ${N}), vout${ABC[N:N+8]}${ABC[N:N+8]});
88    output += ${BATCH_TILE};
89  }
90  if XNN_UNLIKELY(n != 0) {
91    ${"do " if BATCH_TILE > 8 else ""}{
92      const __m128i vx${ABC[0:4]} = _mm_cvtepi8_epi32(_mm_loadu_si32(input_x));
93      const __m128i vx${ABC[4:8]} = _mm_cvtepi8_epi32(_mm_loadu_si32(input_x + 4));
94      $if BATCH_TILE > 8:
95        input_x += 8;
96
97      $if SSE == 5:
98        __m128i vacc${ABC[0:4]} = _mm_macc_epi32(vx${ABC[0:4]}, vx_multiplier, vzero_point_product);
99        __m128i vacc${ABC[4:8]} = _mm_macc_epi32(vx${ABC[4:8]}, vx_multiplier, vzero_point_product);
100      $else:
101        __m128i vacc${ABC[0:4]} = _mm_add_epi32(vzero_point_product, _mm_mullo_epi32(vx${ABC[0:4]}, vx_multiplier));
102        __m128i vacc${ABC[4:8]} = _mm_add_epi32(vzero_point_product, _mm_mullo_epi32(vx${ABC[4:8]}, vx_multiplier));
103
104      const __m128i vrem${ABC[0:4]} = _mm_add_epi32(_mm_and_si128(vacc${ABC[0:4]}, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vacc${ABC[0:4]}));
105      const __m128i vrem${ABC[4:8]} = _mm_add_epi32(_mm_and_si128(vacc${ABC[4:8]}, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vacc${ABC[4:8]}));
106
107      vacc${ABC[0:4]} = _mm_sub_epi32(_mm_sra_epi32(vacc${ABC[0:4]}, vshift), _mm_cmpgt_epi32(vrem${ABC[0:4]}, vremainder_threshold));
108      vacc${ABC[4:8]} = _mm_sub_epi32(_mm_sra_epi32(vacc${ABC[4:8]}, vshift), _mm_cmpgt_epi32(vrem${ABC[4:8]}, vremainder_threshold));
109
110      __m128i vout${ABC[0:8]} = _mm_adds_epi16(_mm_packs_epi32(vacc${ABC[0:4]}, vacc${ABC[4:8]}), voutput_zero_point);
111      vout${ABC[0:8]} = _mm_max_epi16(vout${ABC[0:8]}, voutput_min);
112      vout${ABC[0:8]} = _mm_min_epi16(vout${ABC[0:8]}, voutput_max);
113
114      __m128i vout${ABC[0:8]}${ABC[0:8]} = _mm_packs_epi16(vout${ABC[0:8]}, vout${ABC[0:8]});
115
116      $if BATCH_TILE > 8:
117        if XNN_LIKELY(n >= (8 * sizeof(int8_t))) {
118          _mm_storel_epi64((__m128i*) output, vout${ABC[0:8]}${ABC[0:8]});
119          output += 8;
120          n -= 8 * sizeof(int8_t);
121        } else {
122          if (n & (4 * sizeof(int8_t))) {
123            *((uint32_t*) output) = (uint32_t) _mm_cvtsi128_si32(vout${ABC[0:8]}${ABC[0:8]});
124            vout${ABC[0:8]}${ABC[0:8]} = _mm_srli_epi64(vout${ABC[0:8]}${ABC[0:8]}, 32);
125            output += 4;
126          }
127          if (n & (2 * sizeof(int8_t))) {
128            *((uint16_t*) output) = (uint16_t) _mm_extract_epi16(vout${ABC[0:8]}${ABC[0:8]}, 0);
129            vout${ABC[0:8]}${ABC[0:8]} = _mm_srli_epi32(vout${ABC[0:8]}${ABC[0:8]}, 16);
130            output += 2;
131          }
132          if (n & (1 * sizeof(int8_t))) {
133            $if SSE >= 4:
134              *output = (int8_t) _mm_extract_epi8(vout${ABC[0:8]}${ABC[0:8]}, 0);
135            $else:
136              *output = (int32_t) _mm_cvtsi128_si32(vout${ABC[0:8]}${ABC[0:8]});
137          }
138          n = 0;
139        }
140      $else:
141        if (n & (4 * sizeof(int8_t))) {
142          *((uint32_t*) output) = (uint32_t) _mm_cvtsi128_si32(vout${ABC[0:8]}${ABC[0:8]});
143          vout${ABC[0:8]}${ABC[0:8]} = _mm_srli_epi64(vout${ABC[0:8]}${ABC[0:8]}, 32);
144          output += 4;
145        }
146        if (n & (2 * sizeof(int8_t))) {
147          *((uint16_t*) output) = (uint16_t) _mm_extract_epi16(vout${ABC[0:8]}${ABC[0:8]}, 0);
148          vout${ABC[0:8]}${ABC[0:8]} = _mm_srli_epi32(vout${ABC[0:8]}${ABC[0:8]}, 16);
149          output += 2;
150        }
151        if (n & (1 * sizeof(int8_t))) {
152          $if SSE >= 4:
153            *output = (int8_t) _mm_extract_epi8(vout${ABC[0:8]}${ABC[0:8]}, 0);
154          $else:
155            *output = (int32_t) _mm_cvtsi128_si32(vout${ABC[0:8]}${ABC[0:8]});
156        }
157    }${" while (n != 0);" if BATCH_TILE > 8 else ""}
158  }
159}
160