Marat Dukhan | 3c35f7a | 2021-07-08 18:55:42 -0700 | [diff] [blame] | 1 | // Auto-generated file. Do not edit! |
| 2 | // Template: src/qs8-dwconv/unipass-sse-mul32.c.in |
| 3 | // Generator: tools/xngen |
| 4 | // |
| 5 | // Copyright 2020 Google LLC |
| 6 | // |
| 7 | // This source code is licensed under the BSD-style license found in the |
| 8 | // LICENSE file in the root directory of this source tree. |
| 9 | |
| 10 | #include <assert.h> |
| 11 | |
| 12 | #if defined(__GNUC__) || defined(__clang__) |
| 13 | #include <x86intrin.h> |
| 14 | #else |
| 15 | #include <immintrin.h> |
| 16 | #include <ammintrin.h> |
| 17 | #endif |
| 18 | |
| 19 | #include <xnnpack/dwconv.h> |
| 20 | #include <xnnpack/intrinsics-polyfill.h> |
| 21 | |
| 22 | |
| 23 | void xnn_qu8_dwconv_minmax_fp32_ukernel_up8x9__xop_mul32( |
| 24 | size_t channels, |
| 25 | size_t output_width, |
| 26 | const uint8_t** input, |
| 27 | const void* weights, |
| 28 | uint8_t* output, |
| 29 | size_t input_stride, |
| 30 | size_t output_increment, |
| 31 | size_t input_offset, |
| 32 | const uint8_t* zero, |
Marat Dukhan | 7be427a | 2021-12-13 23:38:20 -0800 | [diff] [blame] | 33 | const union xnn_qu8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS |
Marat Dukhan | 3c35f7a | 2021-07-08 18:55:42 -0700 | [diff] [blame] | 34 | { |
| 35 | assert(channels != 0); |
| 36 | assert(output_width != 0); |
| 37 | |
| 38 | const __m128i vk_zero_point = _mm_cvtepu16_epi32(_mm_loadl_epi64((const __m128i*) params->fp32_sse2.kernel_zero_point)); |
| 39 | do { |
| 40 | const uint8_t* i0 = input[0]; |
| 41 | assert(i0 != NULL); |
| 42 | if XNN_UNPREDICTABLE(i0 != zero) { |
| 43 | i0 = (const uint8_t*) ((uintptr_t) i0 + input_offset); |
| 44 | } |
| 45 | const uint8_t* i1 = input[1]; |
| 46 | assert(i1 != NULL); |
| 47 | if XNN_UNPREDICTABLE(i1 != zero) { |
| 48 | i1 = (const uint8_t*) ((uintptr_t) i1 + input_offset); |
| 49 | } |
| 50 | const uint8_t* i2 = input[2]; |
| 51 | assert(i2 != NULL); |
| 52 | if XNN_UNPREDICTABLE(i2 != zero) { |
| 53 | i2 = (const uint8_t*) ((uintptr_t) i2 + input_offset); |
| 54 | } |
| 55 | const uint8_t* i3 = input[3]; |
| 56 | assert(i3 != NULL); |
| 57 | if XNN_UNPREDICTABLE(i3 != zero) { |
| 58 | i3 = (const uint8_t*) ((uintptr_t) i3 + input_offset); |
| 59 | } |
| 60 | const uint8_t* i4 = input[4]; |
| 61 | assert(i4 != NULL); |
| 62 | if XNN_UNPREDICTABLE(i4 != zero) { |
| 63 | i4 = (const uint8_t*) ((uintptr_t) i4 + input_offset); |
| 64 | } |
| 65 | const uint8_t* i5 = input[5]; |
| 66 | assert(i5 != NULL); |
| 67 | if XNN_UNPREDICTABLE(i5 != zero) { |
| 68 | i5 = (const uint8_t*) ((uintptr_t) i5 + input_offset); |
| 69 | } |
| 70 | const uint8_t* i6 = input[6]; |
| 71 | assert(i6 != NULL); |
| 72 | if XNN_UNPREDICTABLE(i6 != zero) { |
| 73 | i6 = (const uint8_t*) ((uintptr_t) i6 + input_offset); |
| 74 | } |
| 75 | const uint8_t* i7 = input[7]; |
| 76 | assert(i7 != NULL); |
| 77 | if XNN_UNPREDICTABLE(i7 != zero) { |
| 78 | i7 = (const uint8_t*) ((uintptr_t) i7 + input_offset); |
| 79 | } |
| 80 | const uint8_t* i8 = input[8]; |
| 81 | assert(i8 != NULL); |
| 82 | if XNN_UNPREDICTABLE(i8 != zero) { |
| 83 | i8 = (const uint8_t*) ((uintptr_t) i8 + input_offset); |
| 84 | } |
| 85 | input = (const uint8_t**) ((uintptr_t) input + input_stride); |
| 86 | |
| 87 | size_t c = channels; |
| 88 | const void* w = weights; |
| 89 | for (; c >= 8; c -= 8) { |
| 90 | __m128i vacc0123 = _mm_loadu_si128((const __m128i*) w); |
| 91 | __m128i vacc4567 = _mm_loadu_si128((const __m128i*) ((const int32_t*) w + 4)); |
| 92 | |
| 93 | |
| 94 | const __m128i vi0x0123 = _mm_cvtepu8_epi32(_mm_loadu_si32(i0)); |
| 95 | const __m128i vk0x0123 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 0 * sizeof(uint8_t)))), vk_zero_point); |
| 96 | const __m128i vi0x4567 = _mm_cvtepu8_epi32(_mm_loadu_si32(i0 + 4)); |
| 97 | const __m128i vk0x4567 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 4 * sizeof(uint8_t)))), vk_zero_point); |
| 98 | i0 += 8; |
| 99 | |
| 100 | vacc0123 = _mm_macc_epi32(vi0x0123, vk0x0123, vacc0123); |
| 101 | vacc4567 = _mm_macc_epi32(vi0x4567, vk0x4567, vacc4567); |
| 102 | |
| 103 | const __m128i vi1x0123 = _mm_cvtepu8_epi32(_mm_loadu_si32(i1)); |
| 104 | const __m128i vk1x0123 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 8 * sizeof(uint8_t)))), vk_zero_point); |
| 105 | const __m128i vi1x4567 = _mm_cvtepu8_epi32(_mm_loadu_si32(i1 + 4)); |
| 106 | const __m128i vk1x4567 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 12 * sizeof(uint8_t)))), vk_zero_point); |
| 107 | i1 += 8; |
| 108 | |
| 109 | vacc0123 = _mm_macc_epi32(vi1x0123, vk1x0123, vacc0123); |
| 110 | vacc4567 = _mm_macc_epi32(vi1x4567, vk1x4567, vacc4567); |
| 111 | |
| 112 | const __m128i vi2x0123 = _mm_cvtepu8_epi32(_mm_loadu_si32(i2)); |
| 113 | const __m128i vk2x0123 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 16 * sizeof(uint8_t)))), vk_zero_point); |
| 114 | const __m128i vi2x4567 = _mm_cvtepu8_epi32(_mm_loadu_si32(i2 + 4)); |
| 115 | const __m128i vk2x4567 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 20 * sizeof(uint8_t)))), vk_zero_point); |
| 116 | i2 += 8; |
| 117 | |
| 118 | vacc0123 = _mm_macc_epi32(vi2x0123, vk2x0123, vacc0123); |
| 119 | vacc4567 = _mm_macc_epi32(vi2x4567, vk2x4567, vacc4567); |
| 120 | |
| 121 | const __m128i vi3x0123 = _mm_cvtepu8_epi32(_mm_loadu_si32(i3)); |
| 122 | const __m128i vk3x0123 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 24 * sizeof(uint8_t)))), vk_zero_point); |
| 123 | const __m128i vi3x4567 = _mm_cvtepu8_epi32(_mm_loadu_si32(i3 + 4)); |
| 124 | const __m128i vk3x4567 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 28 * sizeof(uint8_t)))), vk_zero_point); |
| 125 | i3 += 8; |
| 126 | |
| 127 | vacc0123 = _mm_macc_epi32(vi3x0123, vk3x0123, vacc0123); |
| 128 | vacc4567 = _mm_macc_epi32(vi3x4567, vk3x4567, vacc4567); |
| 129 | |
| 130 | const __m128i vi4x0123 = _mm_cvtepu8_epi32(_mm_loadu_si32(i4)); |
| 131 | const __m128i vk4x0123 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 32 * sizeof(uint8_t)))), vk_zero_point); |
| 132 | const __m128i vi4x4567 = _mm_cvtepu8_epi32(_mm_loadu_si32(i4 + 4)); |
| 133 | const __m128i vk4x4567 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 36 * sizeof(uint8_t)))), vk_zero_point); |
| 134 | i4 += 8; |
| 135 | |
| 136 | vacc0123 = _mm_macc_epi32(vi4x0123, vk4x0123, vacc0123); |
| 137 | vacc4567 = _mm_macc_epi32(vi4x4567, vk4x4567, vacc4567); |
| 138 | |
| 139 | const __m128i vi5x0123 = _mm_cvtepu8_epi32(_mm_loadu_si32(i5)); |
| 140 | const __m128i vk5x0123 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 40 * sizeof(uint8_t)))), vk_zero_point); |
| 141 | const __m128i vi5x4567 = _mm_cvtepu8_epi32(_mm_loadu_si32(i5 + 4)); |
| 142 | const __m128i vk5x4567 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 44 * sizeof(uint8_t)))), vk_zero_point); |
| 143 | i5 += 8; |
| 144 | |
| 145 | vacc0123 = _mm_macc_epi32(vi5x0123, vk5x0123, vacc0123); |
| 146 | vacc4567 = _mm_macc_epi32(vi5x4567, vk5x4567, vacc4567); |
| 147 | |
| 148 | const __m128i vi6x0123 = _mm_cvtepu8_epi32(_mm_loadu_si32(i6)); |
| 149 | const __m128i vk6x0123 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 48 * sizeof(uint8_t)))), vk_zero_point); |
| 150 | const __m128i vi6x4567 = _mm_cvtepu8_epi32(_mm_loadu_si32(i6 + 4)); |
| 151 | const __m128i vk6x4567 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 52 * sizeof(uint8_t)))), vk_zero_point); |
| 152 | i6 += 8; |
| 153 | |
| 154 | vacc0123 = _mm_macc_epi32(vi6x0123, vk6x0123, vacc0123); |
| 155 | vacc4567 = _mm_macc_epi32(vi6x4567, vk6x4567, vacc4567); |
| 156 | |
| 157 | const __m128i vi7x0123 = _mm_cvtepu8_epi32(_mm_loadu_si32(i7)); |
| 158 | const __m128i vk7x0123 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 56 * sizeof(uint8_t)))), vk_zero_point); |
| 159 | const __m128i vi7x4567 = _mm_cvtepu8_epi32(_mm_loadu_si32(i7 + 4)); |
| 160 | const __m128i vk7x4567 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 60 * sizeof(uint8_t)))), vk_zero_point); |
| 161 | i7 += 8; |
| 162 | |
| 163 | vacc0123 = _mm_macc_epi32(vi7x0123, vk7x0123, vacc0123); |
| 164 | vacc4567 = _mm_macc_epi32(vi7x4567, vk7x4567, vacc4567); |
| 165 | |
| 166 | const __m128i vi8x0123 = _mm_cvtepu8_epi32(_mm_loadu_si32(i8)); |
| 167 | const __m128i vk8x0123 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 64 * sizeof(uint8_t)))), vk_zero_point); |
| 168 | const __m128i vi8x4567 = _mm_cvtepu8_epi32(_mm_loadu_si32(i8 + 4)); |
| 169 | const __m128i vk8x4567 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 68 * sizeof(uint8_t)))), vk_zero_point); |
| 170 | i8 += 8; |
| 171 | |
| 172 | vacc0123 = _mm_macc_epi32(vi8x0123, vk8x0123, vacc0123); |
| 173 | vacc4567 = _mm_macc_epi32(vi8x4567, vk8x4567, vacc4567); |
| 174 | |
| 175 | w = (const void*) ((uintptr_t) w + 8 * sizeof(int32_t) + 72 * sizeof(uint8_t)); |
| 176 | |
| 177 | __m128 vscaled0123 = _mm_cvtepi32_ps(vacc0123); |
| 178 | __m128 vscaled4567 = _mm_cvtepi32_ps(vacc4567); |
| 179 | |
| 180 | const __m128 vscale = _mm_load_ps(params->fp32_sse2.scale); |
| 181 | vscaled0123 = _mm_mul_ps(vscaled0123, vscale); |
| 182 | vscaled4567 = _mm_mul_ps(vscaled4567, vscale); |
| 183 | |
Marat Dukhan | 13c9f8d | 2021-12-06 02:21:03 -0800 | [diff] [blame] | 184 | const __m128 voutput_max_less_zero_point = _mm_load_ps(params->fp32_sse2.output_max_less_zero_point); |
| 185 | vscaled0123 = _mm_min_ps(vscaled0123, voutput_max_less_zero_point); |
| 186 | vscaled4567 = _mm_min_ps(vscaled4567, voutput_max_less_zero_point); |
| 187 | |
Marat Dukhan | 3c35f7a | 2021-07-08 18:55:42 -0700 | [diff] [blame] | 188 | vacc0123 = _mm_cvtps_epi32(vscaled0123); |
| 189 | vacc4567 = _mm_cvtps_epi32(vscaled4567); |
| 190 | |
| 191 | const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->fp32_sse2.output_zero_point); |
| 192 | __m128i vout01234567 = _mm_adds_epi16(_mm_packs_epi32(vacc0123, vacc4567), voutput_zero_point); |
| 193 | |
| 194 | const __m128i voutput_min = _mm_load_si128((const __m128i*) params->fp32_sse2.output_min); |
Marat Dukhan | 3c35f7a | 2021-07-08 18:55:42 -0700 | [diff] [blame] | 195 | __m128i vout0123456701234567 = _mm_packus_epi16(vout01234567, vout01234567); |
| 196 | vout0123456701234567 = _mm_max_epu8(vout0123456701234567, voutput_min); |
Marat Dukhan | 3c35f7a | 2021-07-08 18:55:42 -0700 | [diff] [blame] | 197 | |
| 198 | _mm_storel_epi64((__m128i*) output, vout0123456701234567); |
| 199 | output += 8; |
| 200 | } |
| 201 | if XNN_UNLIKELY(c != 0) { |
| 202 | const uint8_t* k = (const uint8_t*) ((const int32_t*) w + 8); |
| 203 | do { |
| 204 | __m128i vacc0123 = _mm_loadu_si128((const __m128i*) w); |
| 205 | |
| 206 | const __m128i vi0x0123 = _mm_cvtepu8_epi32(_mm_loadu_si32(i0)); |
| 207 | const __m128i vk0x0123 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32(k)), vk_zero_point); |
| 208 | i0 += 4; |
| 209 | |
| 210 | vacc0123 = _mm_macc_epi32(vi0x0123, vk0x0123, vacc0123); |
| 211 | const __m128i vi1x0123 = _mm_cvtepu8_epi32(_mm_loadu_si32(i1)); |
| 212 | const __m128i vk1x0123 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) (k + 8))), vk_zero_point); |
| 213 | i1 += 4; |
| 214 | |
| 215 | vacc0123 = _mm_macc_epi32(vi1x0123, vk1x0123, vacc0123); |
| 216 | const __m128i vi2x0123 = _mm_cvtepu8_epi32(_mm_loadu_si32(i2)); |
| 217 | const __m128i vk2x0123 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) (k + 16))), vk_zero_point); |
| 218 | i2 += 4; |
| 219 | |
| 220 | vacc0123 = _mm_macc_epi32(vi2x0123, vk2x0123, vacc0123); |
| 221 | const __m128i vi3x0123 = _mm_cvtepu8_epi32(_mm_loadu_si32(i3)); |
| 222 | const __m128i vk3x0123 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) (k + 24))), vk_zero_point); |
| 223 | i3 += 4; |
| 224 | |
| 225 | vacc0123 = _mm_macc_epi32(vi3x0123, vk3x0123, vacc0123); |
| 226 | const __m128i vi4x0123 = _mm_cvtepu8_epi32(_mm_loadu_si32(i4)); |
| 227 | const __m128i vk4x0123 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) (k + 32))), vk_zero_point); |
| 228 | i4 += 4; |
| 229 | |
| 230 | vacc0123 = _mm_macc_epi32(vi4x0123, vk4x0123, vacc0123); |
| 231 | const __m128i vi5x0123 = _mm_cvtepu8_epi32(_mm_loadu_si32(i5)); |
| 232 | const __m128i vk5x0123 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) (k + 40))), vk_zero_point); |
| 233 | i5 += 4; |
| 234 | |
| 235 | vacc0123 = _mm_macc_epi32(vi5x0123, vk5x0123, vacc0123); |
| 236 | const __m128i vi6x0123 = _mm_cvtepu8_epi32(_mm_loadu_si32(i6)); |
| 237 | const __m128i vk6x0123 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) (k + 48))), vk_zero_point); |
| 238 | i6 += 4; |
| 239 | |
| 240 | vacc0123 = _mm_macc_epi32(vi6x0123, vk6x0123, vacc0123); |
| 241 | const __m128i vi7x0123 = _mm_cvtepu8_epi32(_mm_loadu_si32(i7)); |
| 242 | const __m128i vk7x0123 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) (k + 56))), vk_zero_point); |
| 243 | i7 += 4; |
| 244 | |
| 245 | vacc0123 = _mm_macc_epi32(vi7x0123, vk7x0123, vacc0123); |
| 246 | const __m128i vi8x0123 = _mm_cvtepu8_epi32(_mm_loadu_si32(i8)); |
| 247 | const __m128i vk8x0123 = _mm_sub_epi32(_mm_cvtepu8_epi32(_mm_loadu_si32((const void*) (k + 64))), vk_zero_point); |
| 248 | i8 += 4; |
| 249 | |
| 250 | vacc0123 = _mm_macc_epi32(vi8x0123, vk8x0123, vacc0123); |
| 251 | |
| 252 | k += 4; |
| 253 | |
| 254 | __m128 vscaled0123 = _mm_cvtepi32_ps(vacc0123); |
| 255 | vscaled0123 = _mm_mul_ps(vscaled0123, _mm_load_ps(params->fp32_sse2.scale)); |
Marat Dukhan | 13c9f8d | 2021-12-06 02:21:03 -0800 | [diff] [blame] | 256 | vscaled0123 = _mm_min_ps(vscaled0123, _mm_load_ps(params->fp32_sse2.output_max_less_zero_point)); |
Marat Dukhan | 3c35f7a | 2021-07-08 18:55:42 -0700 | [diff] [blame] | 257 | vacc0123 = _mm_cvtps_epi32(vscaled0123); |
| 258 | |
| 259 | w = (const void*) ((const int32_t*) w + 4); |
| 260 | |
| 261 | const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->fp32_sse2.output_zero_point); |
| 262 | __m128i vout0123 = _mm_adds_epi16(_mm_packs_epi32(vacc0123, vacc0123), voutput_zero_point); |
| 263 | |
| 264 | vout0123 = _mm_packus_epi16(vout0123, vout0123); |
| 265 | vout0123 = _mm_max_epu8(vout0123, _mm_load_si128((const __m128i*) params->fp32_sse2.output_min)); |
Marat Dukhan | 3c35f7a | 2021-07-08 18:55:42 -0700 | [diff] [blame] | 266 | |
| 267 | if XNN_LIKELY(c >= 4) { |
| 268 | _mm_storeu_si32(output, vout0123); |
| 269 | output += 4; |
| 270 | c -= 4; |
| 271 | } else { |
| 272 | if (c & 2) { |
| 273 | *((uint16_t*) output) = (uint16_t) _mm_extract_epi16(vout0123, 0); |
| 274 | vout0123 = _mm_srli_epi32(vout0123, 16); |
| 275 | output += 2; |
| 276 | } |
| 277 | if (c & 1) { |
| 278 | *output = (uint8_t) _mm_extract_epi8(vout0123, 0); |
| 279 | output += 1; |
| 280 | } |
| 281 | c = 0; |
| 282 | } |
| 283 | } while (c != 0); |
| 284 | } |
| 285 | |
| 286 | output = (uint8_t*) ((uintptr_t) output + output_increment); |
| 287 | } while (--output_width != 0); |
| 288 | } |