• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/bf16-gemm/c8-neon.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 
11 #include <assert.h>
12 
13 #include <arm_neon.h>
14 
15 #include <xnnpack/gemm.h>
16 
17 
xnn_bf16_gemm_minmax_ukernel_2x4c8__neonfma_zip(size_t mr,size_t nc,size_t kc,const void * restrict a,size_t a_stride,const void * restrict w_ptr,void * restrict c,size_t cm_stride,size_t cn_stride,const union xnn_bf16_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_bf16_gemm_minmax_ukernel_2x4c8__neonfma_zip(
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_ptr,
25     void* restrict c,
26     size_t cm_stride,
27     size_t cn_stride,
28     const union xnn_bf16_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
29 {
30   assert(mr != 0);
31   assert(mr <= 2);
32   assert(nc != 0);
33   assert(kc != 0);
34   assert(kc % sizeof(uint16_t) == 0);
35   assert(a != NULL);
36   assert(w_ptr != NULL);
37   assert(c != NULL);
38 
39   const uint16_t* a0 = (const uint16_t*) a;
40   uint16_t* c0 = (uint16_t*) 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 
48   const uint16_t* w = (const uint16_t*) w_ptr;
49   const uint16x8_t vzero = vmovq_n_u16(0);
50   do {
51     float32x4_t vacc0x0 = vreinterpretq_f32_u32(vshll_n_u16(vld1_lane_u16(w, vdup_n_u16(0), 0), 16)); w += 1;
52     float32x4_t vacc0x1 = vreinterpretq_f32_u32(vshll_n_u16(vld1_lane_u16(w, vdup_n_u16(0), 0), 16)); w += 1;
53     float32x4_t vacc0x2 = vreinterpretq_f32_u32(vshll_n_u16(vld1_lane_u16(w, vdup_n_u16(0), 0), 16)); w += 1;
54     float32x4_t vacc0x3 = vreinterpretq_f32_u32(vshll_n_u16(vld1_lane_u16(w, vdup_n_u16(0), 0), 16)); w += 1;
55     float32x4_t vacc1x0 = vacc0x0;
56     float32x4_t vacc1x1 = vacc0x1;
57     float32x4_t vacc1x2 = vacc0x2;
58     float32x4_t vacc1x3 = vacc0x3;
59 
60     size_t k = kc;
61     for (; k >= 8 * sizeof(uint16_t); k -= 8 * sizeof(uint16_t)) {
62       const uint16x8_t va0 = vld1q_u16(a0); a0 += 8;
63       const uint16x8_t va1 = vld1q_u16(a1); a1 += 8;
64 
65       const uint16x8_t vb0 = vld1q_u16(w); w += 8;
66       const uint16x8_t vb1 = vld1q_u16(w); w += 8;
67       const uint16x8_t vb2 = vld1q_u16(w); w += 8;
68       const uint16x8_t vb3 = vld1q_u16(w); w += 8;
69 
70       const float32x4_t va0e = vreinterpretq_f32_u16(vzip1q_u16(vzero, va0));
71       const float32x4_t va1e = vreinterpretq_f32_u16(vzip1q_u16(vzero, va1));
72 
73       const float32x4_t vb0e = vreinterpretq_f32_u16(vzip1q_u16(vzero, vb0));
74       const float32x4_t vb1e = vreinterpretq_f32_u16(vzip1q_u16(vzero, vb1));
75       const float32x4_t vb2e = vreinterpretq_f32_u16(vzip1q_u16(vzero, vb2));
76       const float32x4_t vb3e = vreinterpretq_f32_u16(vzip1q_u16(vzero, vb3));
77 
78       vacc0x0 = vfmaq_f32(vacc0x0, va0e, vb0e);
79       vacc1x0 = vfmaq_f32(vacc1x0, va1e, vb0e);
80       vacc0x1 = vfmaq_f32(vacc0x1, va0e, vb1e);
81       vacc1x1 = vfmaq_f32(vacc1x1, va1e, vb1e);
82       vacc0x2 = vfmaq_f32(vacc0x2, va0e, vb2e);
83       vacc1x2 = vfmaq_f32(vacc1x2, va1e, vb2e);
84       vacc0x3 = vfmaq_f32(vacc0x3, va0e, vb3e);
85       vacc1x3 = vfmaq_f32(vacc1x3, va1e, vb3e);
86 
87       const float32x4_t va0o = vreinterpretq_f32_u16(vzip2q_u16(vzero, va0));
88       const float32x4_t va1o = vreinterpretq_f32_u16(vzip2q_u16(vzero, va1));
89 
90       const float32x4_t vb0o = vreinterpretq_f32_u16(vzip2q_u16(vzero, vb0));
91       const float32x4_t vb1o = vreinterpretq_f32_u16(vzip2q_u16(vzero, vb1));
92       const float32x4_t vb2o = vreinterpretq_f32_u16(vzip2q_u16(vzero, vb2));
93       const float32x4_t vb3o = vreinterpretq_f32_u16(vzip2q_u16(vzero, vb3));
94 
95       vacc0x0 = vfmaq_f32(vacc0x0, va0o, vb0o);
96       vacc1x0 = vfmaq_f32(vacc1x0, va1o, vb0o);
97       vacc0x1 = vfmaq_f32(vacc0x1, va0o, vb1o);
98       vacc1x1 = vfmaq_f32(vacc1x1, va1o, vb1o);
99       vacc0x2 = vfmaq_f32(vacc0x2, va0o, vb2o);
100       vacc1x2 = vfmaq_f32(vacc1x2, va1o, vb2o);
101       vacc0x3 = vfmaq_f32(vacc0x3, va0o, vb3o);
102       vacc1x3 = vfmaq_f32(vacc1x3, va1o, vb3o);
103     }
104     if XNN_UNLIKELY(k != 0) {
105       const uint16x8_t va0 = vld1q_u16(a0); a0 = (const uint16_t*) ((uintptr_t) a0 + k);
106       const uint16x8_t va1 = vld1q_u16(a1); a1 = (const uint16_t*) ((uintptr_t) a1 + k);
107 
108       const uint16x8_t vb0 = vld1q_u16(w); w += 8;
109       const uint16x8_t vb1 = vld1q_u16(w); w += 8;
110       const uint16x8_t vb2 = vld1q_u16(w); w += 8;
111       const uint16x8_t vb3 = vld1q_u16(w); w += 8;
112 
113       const uint16x8_t vm0 = vceqq_u16(vb0, vmovq_n_u16(0));
114       const uint16x8_t vm1 = vceqq_u16(vb1, vmovq_n_u16(0));
115       const uint16x8_t vm2 = vceqq_u16(vb2, vmovq_n_u16(0));
116       const uint16x8_t vm3 = vceqq_u16(vb3, vmovq_n_u16(0));
117 
118       const float32x4_t vb0e = vreinterpretq_f32_u16(vzip1q_u16(vzero, vb0));
119       const float32x4_t vb1e = vreinterpretq_f32_u16(vzip1q_u16(vzero, vb1));
120       const float32x4_t vb2e = vreinterpretq_f32_u16(vzip1q_u16(vzero, vb2));
121       const float32x4_t vb3e = vreinterpretq_f32_u16(vzip1q_u16(vzero, vb3));
122 
123       const uint16x8_t va0x0 = vbicq_u16(va0, vm0);
124       const uint16x8_t va1x0 = vbicq_u16(va1, vm0);
125       const uint16x8_t va0x1 = vbicq_u16(va0, vm1);
126       const uint16x8_t va1x1 = vbicq_u16(va1, vm1);
127       const uint16x8_t va0x2 = vbicq_u16(va0, vm2);
128       const uint16x8_t va1x2 = vbicq_u16(va1, vm2);
129       const uint16x8_t va0x3 = vbicq_u16(va0, vm3);
130       const uint16x8_t va1x3 = vbicq_u16(va1, vm3);
131 
132       const float32x4_t va0x0e = vreinterpretq_f32_u16(vzip1q_u16(vzero, va0x0));
133       const float32x4_t va1x0e = vreinterpretq_f32_u16(vzip1q_u16(vzero, va1x0));
134       const float32x4_t va0x1e = vreinterpretq_f32_u16(vzip1q_u16(vzero, va0x1));
135       const float32x4_t va1x1e = vreinterpretq_f32_u16(vzip1q_u16(vzero, va1x1));
136       const float32x4_t va0x2e = vreinterpretq_f32_u16(vzip1q_u16(vzero, va0x2));
137       const float32x4_t va1x2e = vreinterpretq_f32_u16(vzip1q_u16(vzero, va1x2));
138       const float32x4_t va0x3e = vreinterpretq_f32_u16(vzip1q_u16(vzero, va0x3));
139       const float32x4_t va1x3e = vreinterpretq_f32_u16(vzip1q_u16(vzero, va1x3));
140 
141       vacc0x0 = vfmaq_f32(vacc0x0, va0x0e, vb0e);
142       vacc1x0 = vfmaq_f32(vacc1x0, va1x0e, vb0e);
143       vacc0x1 = vfmaq_f32(vacc0x1, va0x1e, vb1e);
144       vacc1x1 = vfmaq_f32(vacc1x1, va1x1e, vb1e);
145       vacc0x2 = vfmaq_f32(vacc0x2, va0x2e, vb2e);
146       vacc1x2 = vfmaq_f32(vacc1x2, va1x2e, vb2e);
147       vacc0x3 = vfmaq_f32(vacc0x3, va0x3e, vb3e);
148       vacc1x3 = vfmaq_f32(vacc1x3, va1x3e, vb3e);
149 
150       const float32x4_t vb0o = vreinterpretq_f32_u16(vzip2q_u16(vzero, vb0));
151       const float32x4_t vb1o = vreinterpretq_f32_u16(vzip2q_u16(vzero, vb1));
152       const float32x4_t vb2o = vreinterpretq_f32_u16(vzip2q_u16(vzero, vb2));
153       const float32x4_t vb3o = vreinterpretq_f32_u16(vzip2q_u16(vzero, vb3));
154 
155       const float32x4_t va0x0o = vreinterpretq_f32_u16(vzip2q_u16(vzero, va0x0));
156       const float32x4_t va1x0o = vreinterpretq_f32_u16(vzip2q_u16(vzero, va1x0));
157       const float32x4_t va0x1o = vreinterpretq_f32_u16(vzip2q_u16(vzero, va0x1));
158       const float32x4_t va1x1o = vreinterpretq_f32_u16(vzip2q_u16(vzero, va1x1));
159       const float32x4_t va0x2o = vreinterpretq_f32_u16(vzip2q_u16(vzero, va0x2));
160       const float32x4_t va1x2o = vreinterpretq_f32_u16(vzip2q_u16(vzero, va1x2));
161       const float32x4_t va0x3o = vreinterpretq_f32_u16(vzip2q_u16(vzero, va0x3));
162       const float32x4_t va1x3o = vreinterpretq_f32_u16(vzip2q_u16(vzero, va1x3));
163 
164       vacc0x0 = vfmaq_f32(vacc0x0, va0x0o, vb0o);
165       vacc1x0 = vfmaq_f32(vacc1x0, va1x0o, vb0o);
166       vacc0x1 = vfmaq_f32(vacc0x1, va0x1o, vb1o);
167       vacc1x1 = vfmaq_f32(vacc1x1, va1x1o, vb1o);
168       vacc0x2 = vfmaq_f32(vacc0x2, va0x2o, vb2o);
169       vacc1x2 = vfmaq_f32(vacc1x2, va1x2o, vb2o);
170       vacc0x3 = vfmaq_f32(vacc0x3, va0x3o, vb3o);
171       vacc1x3 = vfmaq_f32(vacc1x3, va1x3o, vb3o);
172     }
173 
174 #if XNN_ARCH_ARM64
175     const float32x4_t vacc0x01 = vpaddq_f32(vacc0x0, vacc0x1);
176     const float32x4_t vacc1x01 = vpaddq_f32(vacc1x0, vacc1x1);
177     const float32x4_t vacc0x23 = vpaddq_f32(vacc0x2, vacc0x3);
178     const float32x4_t vacc1x23 = vpaddq_f32(vacc1x2, vacc1x3);
179 
180     float32x4_t vacc0x0123 = vpaddq_f32(vacc0x01, vacc0x23);
181     float32x4_t vacc1x0123 = vpaddq_f32(vacc1x01, vacc1x23);
182 #else
183     const float32x2_t vsum0x0 = vadd_f32(vget_low_f32(vacc0x0), vget_high_f32(vacc0x0));
184     const float32x2_t vsum1x0 = vadd_f32(vget_low_f32(vacc1x0), vget_high_f32(vacc1x0));
185     const float32x2_t vsum0x1 = vadd_f32(vget_low_f32(vacc0x1), vget_high_f32(vacc0x1));
186     const float32x2_t vsum1x1 = vadd_f32(vget_low_f32(vacc1x1), vget_high_f32(vacc1x1));
187     const float32x2_t vsum0x2 = vadd_f32(vget_low_f32(vacc0x2), vget_high_f32(vacc0x2));
188     const float32x2_t vsum1x2 = vadd_f32(vget_low_f32(vacc1x2), vget_high_f32(vacc1x2));
189     const float32x2_t vsum0x3 = vadd_f32(vget_low_f32(vacc0x3), vget_high_f32(vacc0x3));
190     const float32x2_t vsum1x3 = vadd_f32(vget_low_f32(vacc1x3), vget_high_f32(vacc1x3));
191 
192     float32x4_t vacc0x0123 = vcombine_f32(vpadd_f32(vsum0x0, vsum0x1), vpadd_f32(vsum0x2, vsum0x3));
193     float32x4_t vacc1x0123 = vcombine_f32(vpadd_f32(vsum1x0, vsum1x1), vpadd_f32(vsum1x2, vsum1x3));
194 #endif
195 
196     const float32x4_t vmax = vld1q_dup_f32(&params->scalar.max);
197     vacc0x0123 = vminq_f32(vacc0x0123, vmax);
198     vacc1x0123 = vminq_f32(vacc1x0123, vmax);
199 
200     const float32x4_t vmin = vld1q_dup_f32(&params->scalar.min);
201     vacc0x0123 = vmaxq_f32(vacc0x0123, vmin);
202     vacc1x0123 = vmaxq_f32(vacc1x0123, vmin);
203 
204     uint16x4_t vout0x0123 = vshrn_n_u32(vreinterpretq_u32_f32(vacc0x0123), 16);
205     uint16x4_t vout1x0123 = vshrn_n_u32(vreinterpretq_u32_f32(vacc1x0123), 16);
206 
207     if XNN_LIKELY(nc >= 4) {
208       vst1_u16(c0, vout0x0123);
209       c0 = (uint16_t*) ((uintptr_t) c0 + cn_stride);
210       vst1_u16(c1, vout1x0123);
211       c1 = (uint16_t*) ((uintptr_t) c1 + cn_stride);
212 
213       a0 = (const uint16_t*) ((uintptr_t) a0 - kc);
214       a1 = (const uint16_t*) ((uintptr_t) a1 - kc);
215 
216       nc -= 4;
217     } else {
218       if (nc & 2) {
219         vst1_lane_u32((void*) c0, vreinterpret_u32_u16(vout0x0123), 0); c0 += 2;
220         vst1_lane_u32((void*) c1, vreinterpret_u32_u16(vout1x0123), 0); c1 += 2;
221 
222         vout0x0123 = vext_u16(vout0x0123, vout0x0123, 2);
223         vout1x0123 = vext_u16(vout1x0123, vout1x0123, 2);
224       }
225       if (nc & 1) {
226         vst1_lane_u16(c0, vout0x0123, 0);
227         vst1_lane_u16(c1, vout1x0123, 0);
228       }
229 
230       nc = 0;
231     }
232   } while (nc != 0);
233 }
234