• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/bf16-gemm/c8-neonbf16.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__neonbf16_bfdot(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__neonbf16_bfdot(
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(bfloat16_t) == 0);
35   assert(a != NULL);
36   assert(w_ptr != NULL);
37   assert(c != NULL);
38 
39   const bfloat16_t* a0 = (const bfloat16_t*) a;
40   bfloat16_t* c0 = (bfloat16_t*) c;
41   const bfloat16_t* a1 = (const bfloat16_t*) ((uintptr_t) a0 + a_stride);
42   bfloat16_t* c1 = (bfloat16_t*) ((uintptr_t) c0 + cm_stride);
43   if XNN_UNPREDICTABLE(mr != 2) {
44     a1 = a0;
45     c1 = c0;
46   }
47 
48   const bfloat16_t* w = (const bfloat16_t*) w_ptr;
49   do {
50     float32x4_t vacc0x0 = vcvt_f32_bf16(vld1_lane_bf16(w, vreinterpret_bf16_u16(vdup_n_u16(0)), 0)); w += 1;
51     float32x4_t vacc0x1 = vcvt_f32_bf16(vld1_lane_bf16(w, vreinterpret_bf16_u16(vdup_n_u16(0)), 0)); w += 1;
52     float32x4_t vacc0x2 = vcvt_f32_bf16(vld1_lane_bf16(w, vreinterpret_bf16_u16(vdup_n_u16(0)), 0)); w += 1;
53     float32x4_t vacc0x3 = vcvt_f32_bf16(vld1_lane_bf16(w, vreinterpret_bf16_u16(vdup_n_u16(0)), 0)); w += 1;
54     float32x4_t vacc1x0 = vacc0x0;
55     float32x4_t vacc1x1 = vacc0x1;
56     float32x4_t vacc1x2 = vacc0x2;
57     float32x4_t vacc1x3 = vacc0x3;
58 
59     size_t k = kc;
60     for (; k >= 8 * sizeof(bfloat16_t); k -= 8 * sizeof(bfloat16_t)) {
61       const bfloat16x8_t va0 = vld1q_bf16(a0); a0 += 8;
62       const bfloat16x8_t va1 = vld1q_bf16(a1); a1 += 8;
63 
64       const bfloat16x8_t vb0 = vld1q_bf16(w); w += 8;
65       const bfloat16x8_t vb1 = vld1q_bf16(w); w += 8;
66       const bfloat16x8_t vb2 = vld1q_bf16(w); w += 8;
67       const bfloat16x8_t vb3 = vld1q_bf16(w); w += 8;
68 
69       vacc0x0 = vbfdotq_f32(vacc0x0, va0, vb0);
70       vacc1x0 = vbfdotq_f32(vacc1x0, va1, vb0);
71       vacc0x1 = vbfdotq_f32(vacc0x1, va0, vb1);
72       vacc1x1 = vbfdotq_f32(vacc1x1, va1, vb1);
73       vacc0x2 = vbfdotq_f32(vacc0x2, va0, vb2);
74       vacc1x2 = vbfdotq_f32(vacc1x2, va1, vb2);
75       vacc0x3 = vbfdotq_f32(vacc0x3, va0, vb3);
76       vacc1x3 = vbfdotq_f32(vacc1x3, va1, vb3);
77     }
78     if XNN_UNLIKELY(k != 0) {
79       const bfloat16x8_t va0 = vld1q_bf16(a0); a0 = (const bfloat16_t*) ((uintptr_t) a0 + k);
80       const bfloat16x8_t va1 = vld1q_bf16(a1); a1 = (const bfloat16_t*) ((uintptr_t) a1 + k);
81 
82       const bfloat16x8_t vb0 = vld1q_bf16(w); w += 8;
83       const bfloat16x8_t vb1 = vld1q_bf16(w); w += 8;
84       const bfloat16x8_t vb2 = vld1q_bf16(w); w += 8;
85       const bfloat16x8_t vb3 = vld1q_bf16(w); w += 8;
86 
87       const uint16x8_t vm0 = vceqq_u16(vreinterpretq_u16_bf16(vb0), vmovq_n_u16(0));
88       const uint16x8_t vm1 = vceqq_u16(vreinterpretq_u16_bf16(vb1), vmovq_n_u16(0));
89       const uint16x8_t vm2 = vceqq_u16(vreinterpretq_u16_bf16(vb2), vmovq_n_u16(0));
90       const uint16x8_t vm3 = vceqq_u16(vreinterpretq_u16_bf16(vb3), vmovq_n_u16(0));
91 
92       const bfloat16x8_t va0x0 = vreinterpretq_bf16_u16(vbicq_u16(vreinterpretq_u16_bf16(va0), vm0));
93       vacc0x0 = vbfdotq_f32(vacc0x0, va0x0, vb0);
94       const bfloat16x8_t va1x0 = vreinterpretq_bf16_u16(vbicq_u16(vreinterpretq_u16_bf16(va1), vm0));
95       vacc1x0 = vbfdotq_f32(vacc1x0, va1x0, vb0);
96       const bfloat16x8_t va0x1 = vreinterpretq_bf16_u16(vbicq_u16(vreinterpretq_u16_bf16(va0), vm1));
97       vacc0x1 = vbfdotq_f32(vacc0x1, va0x1, vb1);
98       const bfloat16x8_t va1x1 = vreinterpretq_bf16_u16(vbicq_u16(vreinterpretq_u16_bf16(va1), vm1));
99       vacc1x1 = vbfdotq_f32(vacc1x1, va1x1, vb1);
100       const bfloat16x8_t va0x2 = vreinterpretq_bf16_u16(vbicq_u16(vreinterpretq_u16_bf16(va0), vm2));
101       vacc0x2 = vbfdotq_f32(vacc0x2, va0x2, vb2);
102       const bfloat16x8_t va1x2 = vreinterpretq_bf16_u16(vbicq_u16(vreinterpretq_u16_bf16(va1), vm2));
103       vacc1x2 = vbfdotq_f32(vacc1x2, va1x2, vb2);
104       const bfloat16x8_t va0x3 = vreinterpretq_bf16_u16(vbicq_u16(vreinterpretq_u16_bf16(va0), vm3));
105       vacc0x3 = vbfdotq_f32(vacc0x3, va0x3, vb3);
106       const bfloat16x8_t va1x3 = vreinterpretq_bf16_u16(vbicq_u16(vreinterpretq_u16_bf16(va1), vm3));
107       vacc1x3 = vbfdotq_f32(vacc1x3, va1x3, vb3);
108     }
109 
110 #if XNN_ARCH_ARM64
111     const float32x4_t vacc0x01 = vpaddq_f32(vacc0x0, vacc0x1);
112     const float32x4_t vacc1x01 = vpaddq_f32(vacc1x0, vacc1x1);
113     const float32x4_t vacc0x23 = vpaddq_f32(vacc0x2, vacc0x3);
114     const float32x4_t vacc1x23 = vpaddq_f32(vacc1x2, vacc1x3);
115 
116     float32x4_t vacc0x0123 = vpaddq_f32(vacc0x01, vacc0x23);
117     float32x4_t vacc1x0123 = vpaddq_f32(vacc1x01, vacc1x23);
118 #else
119     const float32x2_t vsum0x0 = vadd_f32(vget_low_f32(vacc0x0), vget_high_f32(vacc0x0));
120     const float32x2_t vsum1x0 = vadd_f32(vget_low_f32(vacc1x0), vget_high_f32(vacc1x0));
121     const float32x2_t vsum0x1 = vadd_f32(vget_low_f32(vacc0x1), vget_high_f32(vacc0x1));
122     const float32x2_t vsum1x1 = vadd_f32(vget_low_f32(vacc1x1), vget_high_f32(vacc1x1));
123     const float32x2_t vsum0x2 = vadd_f32(vget_low_f32(vacc0x2), vget_high_f32(vacc0x2));
124     const float32x2_t vsum1x2 = vadd_f32(vget_low_f32(vacc1x2), vget_high_f32(vacc1x2));
125     const float32x2_t vsum0x3 = vadd_f32(vget_low_f32(vacc0x3), vget_high_f32(vacc0x3));
126     const float32x2_t vsum1x3 = vadd_f32(vget_low_f32(vacc1x3), vget_high_f32(vacc1x3));
127 
128     float32x4_t vacc0x0123 = vcombine_f32(vpadd_f32(vsum0x0, vsum0x1), vpadd_f32(vsum0x2, vsum0x3));
129     float32x4_t vacc1x0123 = vcombine_f32(vpadd_f32(vsum1x0, vsum1x1), vpadd_f32(vsum1x2, vsum1x3));
130 #endif
131 
132     const float32x4_t vmax = vld1q_dup_f32(&params->scalar.max);
133     vacc0x0123 = vminq_f32(vacc0x0123, vmax);
134     vacc1x0123 = vminq_f32(vacc1x0123, vmax);
135 
136     const float32x4_t vmin = vld1q_dup_f32(&params->scalar.min);
137     vacc0x0123 = vmaxq_f32(vacc0x0123, vmin);
138     vacc1x0123 = vmaxq_f32(vacc1x0123, vmin);
139 
140     bfloat16x4_t vout0x0123 = vcvt_bf16_f32(vacc0x0123);
141     bfloat16x4_t vout1x0123 = vcvt_bf16_f32(vacc1x0123);
142 
143     if XNN_LIKELY(nc >= 4) {
144       vst1_bf16(c0, vout0x0123);
145       c0 = (bfloat16_t*) ((uintptr_t) c0 + cn_stride);
146       vst1_bf16(c1, vout1x0123);
147       c1 = (bfloat16_t*) ((uintptr_t) c1 + cn_stride);
148 
149       a0 = (const bfloat16_t*) ((uintptr_t) a0 - kc);
150       a1 = (const bfloat16_t*) ((uintptr_t) a1 - kc);
151 
152       nc -= 4;
153     } else {
154       if (nc & 2) {
155         vst1_lane_u32((void*) c0, vreinterpret_u32_bf16(vout0x0123), 0); c0 += 2;
156         vst1_lane_u32((void*) c1, vreinterpret_u32_bf16(vout1x0123), 0); c1 += 2;
157 
158         vout0x0123 = vreinterpret_bf16_u16(vext_u16(vreinterpret_u16_bf16(vout0x0123), vreinterpret_u16_bf16(vout0x0123), 2));
159         vout1x0123 = vreinterpret_bf16_u16(vext_u16(vreinterpret_u16_bf16(vout1x0123), vreinterpret_u16_bf16(vout1x0123), 2));
160       }
161       if (nc & 1) {
162         vst1_lane_bf16(c0, vout0x0123, 0);
163         vst1_lane_bf16(c1, vout1x0123, 0);
164       }
165 
166       nc = 0;
167     }
168   } while (nc != 0);
169 }
170