Debian Codegraph @

src/f16-rminmax/gen/f16-rminmax-avx512fp16-u32.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/f16-rminmax/avx512fp16.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2024 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 #include <immintrin.h>
13
14 #include "xnnpack/common.h"
15 #include "xnnpack/reduce.h"
16
17
18 void xnn_f16_rminmax_ukernel__avx512fp16_u32(
19 size_t batch,
20 const xnn_float16* input,
21 xnn_float16* output,
22 const struct xnn_f16_default_params params[restrict XNN_MIN_ELEMENTS(1)])
23 {
24 assert(batch != 0);
25 assert(batch % sizeof(uint16_t) == 0);
26 assert(input != NULL);
27 assert(output != NULL);
28
29 #if defined(__AVX512FP16__)
30 const uint16_t* i = (const uint16_t*) input;
31 __m512h vmin0 = _mm512_castsi512_ph(_mm512_set1_epi16(*i));
32 __m512h vmax0 = vmin0;
33 for (; batch >= 32 * sizeof(uint16_t); batch -= 32 * sizeof(uint16_t)) {
34 const __m512h vt = _mm512_loadu_ph(i);
35 i += 32;
36
37 vmin0 = _mm512_min_ph(vmin0, vt);
38 vmax0 = _mm512_max_ph(vmax0, vt);
39 }
40 if XNN_UNLIKELY(batch != 0) {
41 assert(batch >= 1 * sizeof(uint16_t));
42 assert(batch <= 31 * sizeof(uint16_t));
43
44 // Prepare mask for valid elements (depends on batch).
45 batch >>= XNN_LOG2_SIZEOF_HALF;
46 const __mmask32 vmask = _cvtu32_mask32((uint32_t) ((UINT32_C(1) << batch) - UINT32_C(1)));
47
48 const __m512h vt = _mm512_castsi512_ph(_mm512_maskz_loadu_epi16(vmask, i));
49
50 vmin0 = _mm512_mask_min_ph(vmin0, vmask, vmin0, vt);
51 vmax0 = _mm512_mask_max_ph(vmax0, vmask, vmax0, vt);
52 }
53 __m256h vmin256 = _mm256_min_ph(_mm512_castph512_ph256(vmin0), _mm256_castpd_ph(_mm512_extractf64x4_pd(_mm512_castph_pd(vmin0), 1)));
54 __m256h vmax256 = _mm256_max_ph(_mm512_castph512_ph256(vmax0), _mm256_castpd_ph(_mm512_extractf64x4_pd(_mm512_castph_pd(vmax0), 1)));
55 __m128h vmin = _mm_min_ph(_mm256_castph256_ph128(vmin256), _mm_castps_ph(_mm256_extractf128_ps(_mm256_castph_ps(vmin256), 1)));
56 __m128h vmax = _mm_max_ph(_mm256_castph256_ph128(vmax256), _mm_castps_ph(_mm256_extractf128_ps(_mm256_castph_ps(vmax256), 1)));
57 vmin = _mm_min_ph(vmin, _mm_castps_ph(_mm_movehl_ps(_mm_castph_ps(vmin), _mm_castph_ps(vmin))));
58 vmax = _mm_max_ph(vmax, _mm_castps_ph(_mm_movehl_ps(_mm_castph_ps(vmax), _mm_castph_ps(vmax))));
59 vmin = _mm_min_ph(vmin, _mm_castps_ph(_mm_movehdup_ps(_mm_castph_ps(vmin))));
60 vmax = _mm_max_ph(vmax, _mm_castps_ph(_mm_movehdup_ps(_mm_castph_ps(vmax))));
61 vmin = _mm_min_sh(vmin, _mm_castsi128_ph(_mm_srli_epi32(_mm_castph_si128(vmin), 16)));
62 vmax = _mm_max_sh(vmax, _mm_castsi128_ph(_mm_srli_epi32(_mm_castph_si128(vmax), 16)));
63
64 *((uint16_t*) output) = (uint16_t) _mm_extract_epi16(_mm_castph_si128(vmin), 0);
65 *((uint16_t*) output + 1) = (uint16_t) _mm_extract_epi16(_mm_castph_si128(vmax), 0);
66 #endif // defined(__AVX512FP16__)
67 }