• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/igemm.h>
13#include <xnnpack/intrinsics-polyfill.h>
14
15
16void xnn_f32_igemm_ukernel_${MR}x${NR}__avx512f_broadcast(
17    size_t mr,
18    size_t nc,
19    size_t kc,
20    size_t ks,
21    const float**restrict a,
22    const float*restrict w,
23    float*restrict c,
24    size_t cm_stride,
25    size_t cn_stride,
26    size_t a_offset,
27    const float* zero,
28    const union xnn_f32_output_params params[restrict static 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(ks != 0);
36  assert(ks % (${MR} * sizeof(void*)) == 0);
37  assert(a_offset % sizeof(float) == 0);
38  assert(a != NULL);
39  assert(w != NULL);
40  assert(c != NULL);
41
42  float* c0 = c;
43  $for M in range(1, MR):
44    float* c${M} = (float*) ((uintptr_t) c${M-1} + cm_stride);
45    $if M % 2 == 0:
46      if XNN_UNPREDICTABLE(mr <= ${M}) {
47        c${M} = c${M-1};
48      }
49    $elif M + 1 == MR:
50      if XNN_UNPREDICTABLE(mr != ${M+1}) {
51        c${M} = c${M-1};
52      }
53    $else:
54      if XNN_UNPREDICTABLE(mr < ${M+1}) {
55        c${M} = c${M-1};
56      }
57
58  do {
59    __m512 vacc0x${ABC[0:16]} = _mm512_load_ps(w);
60    $for N in range(16, NR, 16):
61      __m512 vacc0x${ABC[N:N+16]} = _mm512_load_ps(w + ${N});
62    $for M in range(1, MR):
63      $for N in range(0, NR, 16):
64        __m512 vacc${M}x${ABC[N:N+16]} = vacc0x${ABC[N:N+16]};
65    w += ${NR};
66
67    size_t p = ks;
68    do {
69      $for M in range(MR):
70        const float* restrict a${M} = a[${M}];
71        assert(a${M} != NULL);
72        if XNN_UNPREDICTABLE(a${M} != zero) {
73          a${M} = (const float*) ((uintptr_t) a${M} + a_offset);
74        }
75      a += ${MR};
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          $for N in range(0, NR, 16):
86            vacc${M}x${ABC[N:N+16]} = _mm512_fmadd_ps(_mm512_set1_ps(*a${M}), vb${ABC[N:N+16]}, vacc${M}x${ABC[N:N+16]});
87
88        $for M in range(MR):
89          a${M} += 1;
90
91        k -= sizeof(float);
92      } while (k != 0);
93      p -= ${MR} * sizeof(void*);
94    } while (p != 0);
95
96    const __m512 vmax = _mm512_broadcast_f32x4(_mm_load_ps(params->sse.max));
97    $for N in range(0, NR, 16):
98      $for M in range(MR):
99        vacc${M}x${ABC[N:N+16]} = _mm512_min_ps(vacc${M}x${ABC[N:N+16]}, vmax);
100
101    const __m512 vmin = _mm512_broadcast_f32x4(_mm_load_ps(params->sse.min));
102    $for N in range(0, NR, 16):
103      $for M in range(MR):
104        vacc${M}x${ABC[N:N+16]} = _mm512_max_ps(vacc${M}x${ABC[N:N+16]}, vmin);
105
106    if XNN_LIKELY(nc >= ${NR}) {
107      $for M in reversed(range(MR)):
108        _mm512_storeu_ps(c${M}, vacc${M}x${ABC[0:16]});
109        $for N in range(16, NR, 16):
110          _mm512_storeu_ps(c${M} + ${N}, vacc${M}x${ABC[N:N+16]});
111        c${M} = (float*) ((uintptr_t) c${M} + cn_stride);
112
113      a = (const float**restrict) ((uintptr_t) a - ks);
114      nc -= ${NR};
115    } else {
116      $for LOG2N in reversed(range(4, NR.bit_length())):
117        $if NR != 1 << LOG2N:
118          if (nc & ${1 << LOG2N}) {
119            $if LOG2N >= 4:
120              $for M in reversed(range(MR)):
121                _mm512_storeu_ps(c${M}, vacc${M}x${ABC[0:16]});
122                $for N in range(16, 1 << LOG2N, 16):
123                  _mm512_storeu_ps(c${M} + ${N}, vacc${M}x${ABC[N:N+16]});
124
125              $for M in reversed(range(MR)):
126                $for N in range(0, 1 << (LOG2N - 1), 16):
127                  vacc${M}x${ABC[N:N+16]} = vacc${M}x${ABC[N + (1 << LOG2N):N + (1 << LOG2N)+16]};
128
129              $for M in reversed(range(MR)):
130                c${M} += ${1 << LOG2N};
131          }
132        $if LOG2N == 4:
133          if (nc & 15) {
134            // Prepare mask for valid 32-bit elements (depends on nc).
135            const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << nc) - UINT32_C(1)));
136
137            $for M in reversed(range(MR)):
138              _mm512_mask_storeu_ps(c${M}, vmask, vacc${M}x${ABC[0:16]});
139          }
140
141      nc = 0;
142    }
143  } while (nc != 0);
144}
145