xref: /aosp_15_r20/external/XNNPACK/src/f32-gemm/avx512-broadcast.c.in (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1// Copyright 2019 Google LLC
2//
3// This source code is licensed under the BSD-style license found in the
4// LICENSE file in the root directory of this source tree.
5
6$assert NR % 16 == 0
7$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8#include <assert.h>
9
10#include <immintrin.h>
11
12#include <xnnpack/gemm.h>
13#include <xnnpack/intrinsics-polyfill.h>
14
15
16void xnn_f32_gemm${"inc" if INC else ""}_minmax_ukernel_${MR}x${NR}__avx512f_broadcast(
17    size_t mr,
18    size_t nc,
19    size_t kc,
20    const float*restrict a,
21    size_t a_stride,
22    const float*restrict w,
23    float*restrict c,
24    size_t cm_stride,
25    size_t cn_stride,
26    $if INC:
27      const float*restrict acc,
28    const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
29{
30  assert(mr != 0);
31  assert(mr <= ${MR});
32  assert(nc != 0);
33  assert(kc != 0);
34  assert(kc % sizeof(float) == 0);
35  assert(a != NULL);
36  assert(w != NULL);
37  assert(c != NULL);
38  $if INC:
39    assert(acc != NULL);
40
41  const float* a0 = a;
42  float* c0 = c;
43  $for M in range(1, MR):
44    const float* a${M} = (const float*) ((uintptr_t) a${M-1} + a_stride);
45    float* c${M} = (float*) ((uintptr_t) c${M-1} + cm_stride);
46    $if M % 2 == 0:
47      if XNN_UNPREDICTABLE(mr <= ${M}) {
48        a${M} = a${M-1};
49        c${M} = c${M-1};
50      }
51    $elif M + 1 == MR:
52      if XNN_UNPREDICTABLE(mr != ${M+1}) {
53        a${M} = a${M-1};
54        c${M} = c${M-1};
55      }
56    $else:
57      if XNN_UNPREDICTABLE(mr < ${M+1}) {
58        a${M} = a${M-1};
59        c${M} = c${M-1};
60      }
61
62  do {
63    $if INC:
64      $for M in range(MR):
65        $for N in range(0, NR, 16):
66          __m512 vacc${M}x${ABC[N:N+16]} = _mm512_load_ps(acc + ${M*NR+N});
67      acc += ${MR*NR};
68    $else:
69      __m512 vacc0x${ABC[0:16]} = _mm512_load_ps(w);
70      $for N in range(16, NR, 16):
71        __m512 vacc0x${ABC[N:N+16]} = _mm512_load_ps(w + ${N});
72      $for M in range(1, MR):
73        $for N in range(0, NR, 16):
74          __m512 vacc${M}x${ABC[N:N+16]} = vacc0x${ABC[N:N+16]};
75      w += ${NR};
76
77    size_t k = kc;
78    do {
79      const __m512 vb${ABC[0:16]} = _mm512_load_ps(w);
80      $for N in range(16, NR, 16):
81        const __m512 vb${ABC[N:N+16]} = _mm512_load_ps(w + ${N});
82      w += ${NR};
83
84      $for M in range(MR):
85        const __m512 va${M} = _mm512_set1_ps(*a${M});
86        $for N in range(0, NR, 16):
87          vacc${M}x${ABC[N:N+16]} = _mm512_fmadd_ps(va${M}, vb${ABC[N:N+16]}, vacc${M}x${ABC[N:N+16]});
88
89      $for M in range(MR):
90        a${M} += 1;
91
92      k -= sizeof(float);
93    } while (k != 0);
94
95    const __m512 vmin = _mm512_set1_ps(params->scalar.min);
96    $for N in range(0, NR, 16):
97      $for M in range(MR):
98        vacc${M}x${ABC[N:N+16]} = _mm512_max_ps(vacc${M}x${ABC[N:N+16]}, vmin);
99
100    const __m512 vmax = _mm512_set1_ps(params->scalar.max);
101    $for N in range(0, NR, 16):
102      $for M in range(MR):
103        vacc${M}x${ABC[N:N+16]} = _mm512_min_ps(vacc${M}x${ABC[N:N+16]}, vmax);
104
105    if XNN_LIKELY(nc >= ${NR}) {
106      $for M in reversed(range(MR)):
107        _mm512_storeu_ps(c${M}, vacc${M}x${ABC[0:16]});
108        $for N in range(16, NR, 16):
109          _mm512_storeu_ps(c${M} + ${N}, vacc${M}x${ABC[N:N+16]});
110        c${M} = (float*) ((uintptr_t) c${M} + cn_stride);
111
112      $for M in reversed(range(MR)):
113        a${M} = (const float*) ((uintptr_t) a${M} - kc);
114
115      nc -= ${NR};
116    } else {
117      $for LOG2N in reversed(range(4, NR.bit_length())):
118        $if NR != 1 << LOG2N:
119          if (nc & ${1 << LOG2N}) {
120            $if LOG2N >= 4:
121              $for M in reversed(range(MR)):
122                _mm512_storeu_ps(c${M}, vacc${M}x${ABC[0:16]});
123                $for N in range(16, 1 << LOG2N, 16):
124                  _mm512_storeu_ps(c${M} + ${N}, vacc${M}x${ABC[N:N+16]});
125
126              $for M in reversed(range(MR)):
127                $for N in range(0, 1 << (LOG2N - 1), 16):
128                  vacc${M}x${ABC[N:N+16]} = vacc${M}x${ABC[N + (1 << LOG2N):N + (1 << LOG2N)+16]};
129
130              $for M in reversed(range(MR)):
131                c${M} += ${1 << LOG2N};
132          }
133        $if LOG2N == 4:
134          if (nc & 15) {
135            // Prepare mask for valid 32-bit elements (depends on nc).
136            const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << nc) - UINT32_C(1)));
137
138            $for M in reversed(range(MR)):
139              _mm512_mask_storeu_ps(c${M}, vmask, vacc${M}x${ABC[0:16]});
140          }
141
142      nc = 0;
143    }
144  } while (nc != 0);
145}
146