Debian Codegraph @

src/f16-igemm/gen/f16-igemm-4x32-minmax-avx512fp16-broadcast.c

raw symbols SCIP
1 // Auto-generated file. Do not edit!
2 // Template: src/f16-igemm/avx512fp16-broadcast.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/igemm.h"
15 #include "xnnpack/intrinsics-polyfill.h"
16
17
18 void xnn_f16_igemm_minmax_ukernel_4x32__avx512fp16_broadcast(
19 size_t mr,
20 size_t nc,
21 size_t kc,
22 size_t ks,
23 const xnn_float16** restrict a,
24 const xnn_float16* restrict w,
25 xnn_float16* restrict c,
26 size_t cm_stride,
27 size_t cn_stride,
28 size_t a_offset,
29 const xnn_float16* zero,
30 const union xnn_f16_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
31 {
32 assert(mr != 0);
33 assert(mr <= 4);
34 assert(nc != 0);
35 assert(kc != 0);
36 assert(kc % sizeof(uint16_t) == 0);
37 assert(ks != 0);
38 assert(ks % (4 * sizeof(void*)) == 0);
39 assert(a_offset % sizeof(xnn_float16) == 0);
40 assert(a != NULL);
41 assert(w != NULL);
42 assert(c != NULL);
43
44 #if defined(__AVX512FP16__)
45 uint16_t* c0 = (uint16_t*) c;
46 uint16_t* c1 = (uint16_t*) ((uintptr_t) c0 + cm_stride);
47 if XNN_UNPREDICTABLE(mr < 2) {
48 c1 = c0;
49 }
50 uint16_t* c2 = (uint16_t*) ((uintptr_t) c1 + cm_stride);
51 if XNN_UNPREDICTABLE(mr <= 2) {
52 c2 = c1;
53 }
54 uint16_t* c3 = (uint16_t*) ((uintptr_t) c2 + cm_stride);
55 if XNN_UNPREDICTABLE(mr != 4) {
56 c3 = c2;
57 }
58
59 do {
60 __m512h vacc0x0 = _mm512_load_ph(w);
61 __m512h vacc1x0 = vacc0x0;
62 __m512h vacc2x0 = vacc0x0;
63 __m512h vacc3x0 = vacc0x0;
64 w = (const xnn_float16*) w + 32;
65
66 size_t p = ks;
67 do {
68 const uint16_t* restrict a0 = (const uint16_t*) a[0];
69 assert(a0 != NULL);
70 if XNN_UNPREDICTABLE(a0 != (const uint16_t*) zero) {
71 a0 = (const uint16_t*) ((uintptr_t) a0 + a_offset);
72 }
73 const uint16_t* restrict a1 = (const uint16_t*) a[1];
74 assert(a1 != NULL);
75 if XNN_UNPREDICTABLE(a1 != (const uint16_t*) zero) {
76 a1 = (const uint16_t*) ((uintptr_t) a1 + a_offset);
77 }
78 const uint16_t* restrict a2 = (const uint16_t*) a[2];
79 assert(a2 != NULL);
80 if XNN_UNPREDICTABLE(a2 != (const uint16_t*) zero) {
81 a2 = (const uint16_t*) ((uintptr_t) a2 + a_offset);
82 }
83 const uint16_t* restrict a3 = (const uint16_t*) a[3];
84 assert(a3 != NULL);
85 if XNN_UNPREDICTABLE(a3 != (const uint16_t*) zero) {
86 a3 = (const uint16_t*) ((uintptr_t) a3 + a_offset);
87 }
88 a += 4;
89
90 size_t k = kc;
91 do {
92 const __m512h vb0 = _mm512_load_ph(w);
93 w = (const xnn_float16*) w + 32;
94
95 const __m512h va0 = _mm512_castsi512_ph(_mm512_set1_epi16(*a0));
96 vacc0x0 = _mm512_fmadd_ph(va0, vb0, vacc0x0);
97 a0 += 1;
98 const __m512h va1 = _mm512_castsi512_ph(_mm512_set1_epi16(*a1));
99 vacc1x0 = _mm512_fmadd_ph(va1, vb0, vacc1x0);
100 a1 += 1;
101 const __m512h va2 = _mm512_castsi512_ph(_mm512_set1_epi16(*a2));
102 vacc2x0 = _mm512_fmadd_ph(va2, vb0, vacc2x0);
103 a2 += 1;
104 const __m512h va3 = _mm512_castsi512_ph(_mm512_set1_epi16(*a3));
105 vacc3x0 = _mm512_fmadd_ph(va3, vb0, vacc3x0);
106 a3 += 1;
107
108 k -= sizeof(uint16_t);
109 } while (k != 0);
110
111 p -= 4 * sizeof(void*);
112 } while (p != 0);
113
114 const __m512h vmin = _mm512_castsi512_ph(_mm512_set1_epi16(*(const uint16_t*) &params->scalar.min));
115 vacc0x0 = _mm512_max_ph(vmin, vacc0x0);
116 vacc1x0 = _mm512_max_ph(vmin, vacc1x0);
117 vacc2x0 = _mm512_max_ph(vmin, vacc2x0);
118 vacc3x0 = _mm512_max_ph(vmin, vacc3x0);
119
120 const __m512h vmax = _mm512_castsi512_ph(_mm512_set1_epi16(*(const uint16_t*) &params->scalar.max));
121 vacc0x0 = _mm512_min_ph(vmax, vacc0x0);
122 vacc1x0 = _mm512_min_ph(vmax, vacc1x0);
123 vacc2x0 = _mm512_min_ph(vmax, vacc2x0);
124 vacc3x0 = _mm512_min_ph(vmax, vacc3x0);
125
126 if XNN_LIKELY(nc >= 32) {
127 _mm512_storeu_ph(c3, vacc3x0);
128 c3 = (uint16_t*) ((uintptr_t) c3 + cn_stride);
129 _mm512_storeu_ph(c2, vacc2x0);
130 c2 = (uint16_t*) ((uintptr_t) c2 + cn_stride);
131 _mm512_storeu_ph(c1, vacc1x0);
132 c1 = (uint16_t*) ((uintptr_t) c1 + cn_stride);
133 _mm512_storeu_ph(c0, vacc0x0);
134 c0 = (uint16_t*) ((uintptr_t) c0 + cn_stride);
135
136 a = (const xnn_float16**restrict) ((uintptr_t) a - ks);
137 nc -= 32;
138 } else {
139 assert(nc != 0);
140 assert(nc < 32);
141 // Prepare mask for valid 16-bit elements (depends on nc).
142 const __mmask32 vmask = _cvtu32_mask32((uint32_t) (UINT32_C(1) << nc) - UINT32_C(1));
143 _mm512_mask_storeu_epi16(c3, vmask, _mm512_castph_si512(vacc3x0));
144 _mm512_mask_storeu_epi16(c2, vmask, _mm512_castph_si512(vacc2x0));
145 _mm512_mask_storeu_epi16(c1, vmask, _mm512_castph_si512(vacc1x0));
146 _mm512_mask_storeu_epi16(c0, vmask, _mm512_castph_si512(vacc0x0));
147 nc = 0;
148 }
149 } while (nc != 0);
150 #endif // defined(__AVX512FP16__)
151 }