• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f16-gemm/avx2-broadcast.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2022 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/gemm.h>
15 #include <xnnpack/intrinsics-polyfill.h>
16 
17 
xnn_f16_gemm_minmax_ukernel_3x16__avx2_broadcast(size_t mr,size_t nc,size_t kc,const void * restrict a,size_t a_stride,const void * restrict w,void * restrict c,size_t cm_stride,size_t cn_stride,const union xnn_f16_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_f16_gemm_minmax_ukernel_3x16__avx2_broadcast(
19     size_t mr,
20     size_t nc,
21     size_t kc,
22     const void*restrict a,
23     size_t a_stride,
24     const void*restrict w,
25     void*restrict c,
26     size_t cm_stride,
27     size_t cn_stride,
28     const union xnn_f16_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
29 {
30   assert(mr != 0);
31   assert(mr <= 3);
32   assert(nc != 0);
33   assert(kc != 0);
34   assert(kc % sizeof(uint16_t) == 0);
35   assert(a != NULL);
36   assert(w != NULL);
37   assert(c != NULL);
38 
39   const uint16_t* a0 = a;
40   uint16_t* c0 = c;
41   const uint16_t* a1 = (const uint16_t*) ((uintptr_t) a0 + a_stride);
42   uint16_t* c1 = (uint16_t*) ((uintptr_t) c0 + cm_stride);
43   if XNN_UNPREDICTABLE(mr < 2) {
44     a1 = a0;
45     c1 = c0;
46   }
47   const uint16_t* a2 = (const uint16_t*) ((uintptr_t) a1 + a_stride);
48   uint16_t* c2 = (uint16_t*) ((uintptr_t) c1 + cm_stride);
49   if XNN_UNPREDICTABLE(mr <= 2) {
50     a2 = a1;
51     c2 = c1;
52   }
53 
54   do {
55     __m256 vacc0x01234567 = _mm256_cvtph_ps(_mm_load_si128((const __m128i*) w));
56     __m256 vacc0x89ABCDEF = _mm256_cvtph_ps(_mm_load_si128((const __m128i*) ((const uint16_t*) w + 8)));
57     __m256 vacc1x01234567 = vacc0x01234567;
58     __m256 vacc1x89ABCDEF = vacc0x89ABCDEF;
59     __m256 vacc2x01234567 = vacc0x01234567;
60     __m256 vacc2x89ABCDEF = vacc0x89ABCDEF;
61     w = (const uint16_t*) w + 16;
62 
63     size_t k = kc;
64     do {
65       const __m256 va0 = _mm256_cvtph_ps(_mm_set1_epi16((short) *a0));
66       a0 += 1;
67       const __m256 va1 = _mm256_cvtph_ps(_mm_set1_epi16((short) *a1));
68       a1 += 1;
69       const __m256 va2 = _mm256_cvtph_ps(_mm_set1_epi16((short) *a2));
70       a2 += 1;
71 
72       const __m256 vb01234567 = _mm256_cvtph_ps(_mm_load_si128((const __m128i*) w));
73       const __m256 vb89ABCDEF = _mm256_cvtph_ps(_mm_load_si128((const __m128i*) ((const uint16_t*) w + 8)));
74       w = (const uint16_t*) w + 16;
75 
76       vacc0x01234567 = _mm256_cvtph_ps(_mm256_cvtps_ph(_mm256_fmadd_ps(va0, vb01234567, vacc0x01234567), _MM_FROUND_NO_EXC));
77       vacc1x01234567 = _mm256_cvtph_ps(_mm256_cvtps_ph(_mm256_fmadd_ps(va1, vb01234567, vacc1x01234567), _MM_FROUND_NO_EXC));
78       vacc2x01234567 = _mm256_cvtph_ps(_mm256_cvtps_ph(_mm256_fmadd_ps(va2, vb01234567, vacc2x01234567), _MM_FROUND_NO_EXC));
79       vacc0x89ABCDEF = _mm256_cvtph_ps(_mm256_cvtps_ph(_mm256_fmadd_ps(va0, vb89ABCDEF, vacc0x89ABCDEF), _MM_FROUND_NO_EXC));
80       vacc1x89ABCDEF = _mm256_cvtph_ps(_mm256_cvtps_ph(_mm256_fmadd_ps(va1, vb89ABCDEF, vacc1x89ABCDEF), _MM_FROUND_NO_EXC));
81       vacc2x89ABCDEF = _mm256_cvtph_ps(_mm256_cvtps_ph(_mm256_fmadd_ps(va2, vb89ABCDEF, vacc2x89ABCDEF), _MM_FROUND_NO_EXC));
82 
83       k -= sizeof(uint16_t);
84     } while (k != 0);
85 
86     const __m256 vmin = _mm256_load_ps(params->avx.min);
87     vacc0x01234567 = _mm256_max_ps(vacc0x01234567, vmin);
88     vacc1x01234567 = _mm256_max_ps(vacc1x01234567, vmin);
89     vacc2x01234567 = _mm256_max_ps(vacc2x01234567, vmin);
90     vacc0x89ABCDEF = _mm256_max_ps(vacc0x89ABCDEF, vmin);
91     vacc1x89ABCDEF = _mm256_max_ps(vacc1x89ABCDEF, vmin);
92     vacc2x89ABCDEF = _mm256_max_ps(vacc2x89ABCDEF, vmin);
93 
94     const __m256 vmax = _mm256_load_ps(params->avx.max);
95     vacc0x01234567 = _mm256_min_ps(vacc0x01234567, vmax);
96     vacc1x01234567 = _mm256_min_ps(vacc1x01234567, vmax);
97     vacc2x01234567 = _mm256_min_ps(vacc2x01234567, vmax);
98     vacc0x89ABCDEF = _mm256_min_ps(vacc0x89ABCDEF, vmax);
99     vacc1x89ABCDEF = _mm256_min_ps(vacc1x89ABCDEF, vmax);
100     vacc2x89ABCDEF = _mm256_min_ps(vacc2x89ABCDEF, vmax);
101 
102     if XNN_LIKELY(nc >= 16) {
103       _mm_storeu_si128((__m128i*) c0, _mm256_cvtps_ph(vacc0x01234567, _MM_FROUND_NO_EXC));
104       _mm_storeu_si128((__m128i*) (c0 + 8), _mm256_cvtps_ph(vacc0x89ABCDEF, _MM_FROUND_NO_EXC));
105       c0 = (uint16_t*) ((uintptr_t) c0 + cn_stride);
106       _mm_storeu_si128((__m128i*) c1, _mm256_cvtps_ph(vacc1x01234567, _MM_FROUND_NO_EXC));
107       _mm_storeu_si128((__m128i*) (c1 + 8), _mm256_cvtps_ph(vacc1x89ABCDEF, _MM_FROUND_NO_EXC));
108       c1 = (uint16_t*) ((uintptr_t) c1 + cn_stride);
109       _mm_storeu_si128((__m128i*) c2, _mm256_cvtps_ph(vacc2x01234567, _MM_FROUND_NO_EXC));
110       _mm_storeu_si128((__m128i*) (c2 + 8), _mm256_cvtps_ph(vacc2x89ABCDEF, _MM_FROUND_NO_EXC));
111       c2 = (uint16_t*) ((uintptr_t) c2 + cn_stride);
112 
113       a0 = (const uint16_t*) ((uintptr_t) a0 - kc);
114       a1 = (const uint16_t*) ((uintptr_t) a1 - kc);
115       a2 = (const uint16_t*) ((uintptr_t) a2 - kc);
116 
117       nc -= 16;
118     } else {
119       __m128i vh0x01234567 = _mm256_cvtps_ph(vacc0x01234567, _MM_FROUND_NO_EXC);
120       __m128i vh1x01234567 = _mm256_cvtps_ph(vacc1x01234567, _MM_FROUND_NO_EXC);
121       __m128i vh2x01234567 = _mm256_cvtps_ph(vacc2x01234567, _MM_FROUND_NO_EXC);
122       if (nc & 8) {
123         _mm_storeu_si128((__m128i*) c0, vh0x01234567);
124         _mm_storeu_si128((__m128i*) c1, vh1x01234567);
125         _mm_storeu_si128((__m128i*) c2, vh2x01234567);
126 
127         vh0x01234567 = _mm256_cvtps_ph(vacc0x89ABCDEF, _MM_FROUND_NO_EXC);
128         vh1x01234567 = _mm256_cvtps_ph(vacc1x89ABCDEF, _MM_FROUND_NO_EXC);
129         vh2x01234567 = _mm256_cvtps_ph(vacc2x89ABCDEF, _MM_FROUND_NO_EXC);
130 
131         c0 += 8;
132         c1 += 8;
133         c2 += 8;
134       }
135       if (nc & 4) {
136         _mm_storel_epi64((__m128i*) c0, vh0x01234567);
137         _mm_storel_epi64((__m128i*) c1, vh1x01234567);
138         _mm_storel_epi64((__m128i*) c2, vh2x01234567);
139 
140         vh0x01234567 = _mm_unpackhi_epi64(vh0x01234567, vh0x01234567);
141         vh1x01234567 = _mm_unpackhi_epi64(vh1x01234567, vh1x01234567);
142         vh2x01234567 = _mm_unpackhi_epi64(vh2x01234567, vh2x01234567);
143 
144         c0 += 4;
145         c1 += 4;
146         c2 += 4;
147       }
148       if (nc & 2) {
149         _mm_storeu_si32(c0, vh0x01234567);
150         _mm_storeu_si32(c1, vh1x01234567);
151         _mm_storeu_si32(c2, vh2x01234567);
152 
153         vh0x01234567 = _mm_srli_epi64(vh0x01234567, 32);
154         vh1x01234567 = _mm_srli_epi64(vh1x01234567, 32);
155         vh2x01234567 = _mm_srli_epi64(vh2x01234567, 32);
156 
157         c0 += 2;
158         c1 += 2;
159         c2 += 2;
160       }
161       if (nc & 1) {
162         *c0 = (uint16_t) _mm_extract_epi16(vh0x01234567, 0);
163         *c1 = (uint16_t) _mm_extract_epi16(vh1x01234567, 0);
164         *c2 = (uint16_t) _mm_extract_epi16(vh2x01234567, 0);
165       }
166 
167       nc = 0;
168     }
169   } while (nc != 0);
170 }
171