xref: /aosp_15_r20/external/XNNPACK/src/bf16-gemm/gen/1x4c8-minmax-neonbf16-bfdot.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
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_1x4c8__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_1x4c8__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 <= 1);
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 
42   const bfloat16_t* w = (const bfloat16_t*) w_ptr;
43   do {
44     float32x4_t vacc0x0 = vcvt_f32_bf16(vld1_lane_bf16(w, vreinterpret_bf16_u16(vdup_n_u16(0)), 0)); w += 1;
45     float32x4_t vacc0x1 = vcvt_f32_bf16(vld1_lane_bf16(w, vreinterpret_bf16_u16(vdup_n_u16(0)), 0)); w += 1;
46     float32x4_t vacc0x2 = vcvt_f32_bf16(vld1_lane_bf16(w, vreinterpret_bf16_u16(vdup_n_u16(0)), 0)); w += 1;
47     float32x4_t vacc0x3 = vcvt_f32_bf16(vld1_lane_bf16(w, vreinterpret_bf16_u16(vdup_n_u16(0)), 0)); w += 1;
48 
49     size_t k = kc;
50     for (; k >= 8 * sizeof(bfloat16_t); k -= 8 * sizeof(bfloat16_t)) {
51       const bfloat16x8_t va0 = vld1q_bf16(a0); a0 += 8;
52 
53       const bfloat16x8_t vb0 = vld1q_bf16(w); w += 8;
54       const bfloat16x8_t vb1 = vld1q_bf16(w); w += 8;
55       const bfloat16x8_t vb2 = vld1q_bf16(w); w += 8;
56       const bfloat16x8_t vb3 = vld1q_bf16(w); w += 8;
57 
58       vacc0x0 = vbfdotq_f32(vacc0x0, va0, vb0);
59       vacc0x1 = vbfdotq_f32(vacc0x1, va0, vb1);
60       vacc0x2 = vbfdotq_f32(vacc0x2, va0, vb2);
61       vacc0x3 = vbfdotq_f32(vacc0x3, va0, vb3);
62     }
63     if XNN_UNLIKELY(k != 0) {
64       const bfloat16x8_t va0 = vld1q_bf16(a0); a0 = (const bfloat16_t*) ((uintptr_t) a0 + k);
65 
66       const bfloat16x8_t vb0 = vld1q_bf16(w); w += 8;
67       const bfloat16x8_t vb1 = vld1q_bf16(w); w += 8;
68       const bfloat16x8_t vb2 = vld1q_bf16(w); w += 8;
69       const bfloat16x8_t vb3 = vld1q_bf16(w); w += 8;
70 
71       const uint16x8_t vm0 = vceqq_u16(vreinterpretq_u16_bf16(vb0), vmovq_n_u16(0));
72       const uint16x8_t vm1 = vceqq_u16(vreinterpretq_u16_bf16(vb1), vmovq_n_u16(0));
73       const uint16x8_t vm2 = vceqq_u16(vreinterpretq_u16_bf16(vb2), vmovq_n_u16(0));
74       const uint16x8_t vm3 = vceqq_u16(vreinterpretq_u16_bf16(vb3), vmovq_n_u16(0));
75 
76       const bfloat16x8_t va0x0 = vreinterpretq_bf16_u16(vbicq_u16(vreinterpretq_u16_bf16(va0), vm0));
77       vacc0x0 = vbfdotq_f32(vacc0x0, va0x0, vb0);
78       const bfloat16x8_t va0x1 = vreinterpretq_bf16_u16(vbicq_u16(vreinterpretq_u16_bf16(va0), vm1));
79       vacc0x1 = vbfdotq_f32(vacc0x1, va0x1, vb1);
80       const bfloat16x8_t va0x2 = vreinterpretq_bf16_u16(vbicq_u16(vreinterpretq_u16_bf16(va0), vm2));
81       vacc0x2 = vbfdotq_f32(vacc0x2, va0x2, vb2);
82       const bfloat16x8_t va0x3 = vreinterpretq_bf16_u16(vbicq_u16(vreinterpretq_u16_bf16(va0), vm3));
83       vacc0x3 = vbfdotq_f32(vacc0x3, va0x3, vb3);
84     }
85 
86 #if XNN_ARCH_ARM64
87     const float32x4_t vacc0x01 = vpaddq_f32(vacc0x0, vacc0x1);
88     const float32x4_t vacc0x23 = vpaddq_f32(vacc0x2, vacc0x3);
89 
90     float32x4_t vacc0x0123 = vpaddq_f32(vacc0x01, vacc0x23);
91 #else
92     const float32x2_t vsum0x0 = vadd_f32(vget_low_f32(vacc0x0), vget_high_f32(vacc0x0));
93     const float32x2_t vsum0x1 = vadd_f32(vget_low_f32(vacc0x1), vget_high_f32(vacc0x1));
94     const float32x2_t vsum0x2 = vadd_f32(vget_low_f32(vacc0x2), vget_high_f32(vacc0x2));
95     const float32x2_t vsum0x3 = vadd_f32(vget_low_f32(vacc0x3), vget_high_f32(vacc0x3));
96 
97     float32x4_t vacc0x0123 = vcombine_f32(vpadd_f32(vsum0x0, vsum0x1), vpadd_f32(vsum0x2, vsum0x3));
98 #endif
99 
100     const float32x4_t vmax = vld1q_dup_f32(&params->scalar.max);
101     vacc0x0123 = vminq_f32(vacc0x0123, vmax);
102 
103     const float32x4_t vmin = vld1q_dup_f32(&params->scalar.min);
104     vacc0x0123 = vmaxq_f32(vacc0x0123, vmin);
105 
106     bfloat16x4_t vout0x0123 = vcvt_bf16_f32(vacc0x0123);
107 
108     if XNN_LIKELY(nc >= 4) {
109       vst1_bf16(c0, vout0x0123);
110       c0 = (bfloat16_t*) ((uintptr_t) c0 + cn_stride);
111 
112       a0 = (const bfloat16_t*) ((uintptr_t) a0 - kc);
113 
114       nc -= 4;
115     } else {
116       if (nc & 2) {
117         vst1_lane_u32((void*) c0, vreinterpret_u32_bf16(vout0x0123), 0); c0 += 2;
118 
119         vout0x0123 = vreinterpret_bf16_u16(vext_u16(vreinterpret_u16_bf16(vout0x0123), vreinterpret_u16_bf16(vout0x0123), 2));
120       }
121       if (nc & 1) {
122         vst1_lane_bf16(c0, vout0x0123, 0);
123       }
124 
125       nc = 0;
126     }
127   } while (nc != 0);
128 }
129