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