• 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 % 4 == 0
7$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8$VMULADDQ_F32 = "vfmaq_f32" if FMA else "vmlaq_f32"
9$VMULADDQ_LANE_F32 = "vfmaq_lane_f32" if FMA else "vmlaq_lane_f32"
10
11#include <assert.h>
12
13#include <arm_neon.h>
14
15#include <xnnpack/igemm.h>
16
17
18void xnn_f32_igemm_minmax_ukernel_${MR}x${NR}__${"neonfma" if FMA else "neon"}_${"dup" if DUP else "lane"}_ld128(
19    size_t mr,
20    size_t nc,
21    size_t kc,
22    size_t ks,
23    const float**restrict a,
24    const float*restrict w,
25    float*restrict c,
26    size_t cm_stride,
27    size_t cn_stride,
28    size_t a_offset,
29    const float* zero,
30    const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
31{
32  assert(mr != 0);
33  assert(mr <= ${MR});
34  assert(nc != 0);
35  assert(kc != 0);
36  assert(kc % sizeof(float) == 0);
37  assert(ks != 0);
38  assert(ks % (${MR} * sizeof(void*)) == 0);
39  assert(a_offset % sizeof(float) == 0);
40  assert(a != NULL);
41  assert(w != NULL);
42  assert(c != NULL);
43
44  float* c0 = c;
45  $for M in range(1, MR):
46    float* c${M} = (float*) ((uintptr_t) c${M-1} + cm_stride);
47    $if M % 2 == 0:
48      if XNN_UNPREDICTABLE(mr <= ${M}) {
49        c${M} = c${M-1};
50      }
51    $elif M + 1 == MR:
52      if XNN_UNPREDICTABLE(mr != ${M+1}) {
53        c${M} = c${M-1};
54      }
55    $else:
56      if XNN_UNPREDICTABLE(mr < ${M+1}) {
57        c${M} = c${M-1};
58      }
59
60  do {
61    $for N in range(0, NR, 4):
62      float32x4_t vacc0x${ABC[N:N+4]} = vld1q_f32(w); w += 4;
63    $for M in range(1, MR):
64      $for N in range(0, NR, 4):
65        float32x4_t vacc${M}x${ABC[N:N+4]} = vacc0x${ABC[N:N+4]};
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      for (; k >= 4 * sizeof(float); k -= 4 * sizeof(float)) {
79        $for M in range(MR):
80          const float32x4_t va${M} = vld1q_f32(a${M}); a${M} += 4;
81
82        $for L in range(4):
83          $VGET_PART_F32 = "vget_low_f32" if L < 2 else "vget_high_f32"
84
85          $for N in range(0, NR, 4):
86            const float32x4_t vb${ABC[N:N+4]}c${L} = vld1q_f32(w); w += 4;
87
88          $if DUP:
89            $for M in range(MR):
90              const float32x4_t va${M}c${L} = vdupq_lane_f32(${VGET_PART_F32}(va${M}), ${L % 2});
91            $for N in range(0, NR, 4):
92              $for M in range(MR):
93                vacc${M}x${ABC[N:N+4]} = ${VMULADDQ_F32}(vacc${M}x${ABC[N:N+4]}, va${M}c${L}, vb${ABC[N:N+4]}c${L});
94          $else:
95            $for N in range(0, NR, 4):
96              $for M in range(MR):
97                vacc${M}x${ABC[N:N+4]} = ${VMULADDQ_LANE_F32}(vacc${M}x${ABC[N:N+4]}, vb${ABC[N:N+4]}c${L}, ${VGET_PART_F32}(va${M}), ${L % 2});
98      }
99      if XNN_UNLIKELY(k != 0) {
100        do {
101          $for M in range(MR):
102            const float32x4_t va${M} = vld1q_dup_f32(a${M}); a${M} += 1;
103
104          $for N in range(0, NR, 4):
105            const float32x4_t vb${ABC[N:N+4]} = vld1q_f32(w); w += 4;
106
107          $for N in range(0, NR, 4):
108            $for M in range(MR):
109              vacc${M}x${ABC[N:N+4]} = ${VMULADDQ_F32}(vacc${M}x${ABC[N:N+4]}, va${M}, vb${ABC[N:N+4]});
110
111          k -= sizeof(float);
112        } while (k != 0);
113      }
114
115      p -= ${MR} * sizeof(void*);
116    } while (p != 0);
117
118    const float32x4_t vmax = vld1q_dup_f32(&params->scalar.max);
119    $for N in range(0, NR, 4):
120      $for M in range(MR):
121        vacc${M}x${ABC[N:N+4]} = vminq_f32(vacc${M}x${ABC[N:N+4]}, vmax);
122
123    const float32x4_t vmin = vld1q_dup_f32(&params->scalar.min);
124    $for N in range(0, NR, 4):
125      $for M in range(MR):
126        vacc${M}x${ABC[N:N+4]} = vmaxq_f32(vacc${M}x${ABC[N:N+4]}, vmin);
127
128    if XNN_LIKELY(nc >= ${NR}) {
129      $for M in reversed(range(MR)):
130        vst1q_f32(c${M}, vacc${M}x${ABC[0:4]});
131        $for N in range(4, NR, 4):
132          vst1q_f32(c${M} + ${N}, vacc${M}x${ABC[N:N+4]});
133        c${M} = (float*) ((uintptr_t) c${M} + cn_stride);
134
135      a = (const float**restrict) ((uintptr_t) a - ks);
136      nc -= ${NR};
137    } else {
138      $for LOG2N in reversed(range(NR.bit_length())):
139        $if LOG2N == 1:
140          $for M in reversed(range(MR)):
141            float32x2_t vacc${M}x${ABC[0:2]} = vget_low_f32(vacc${M}x${ABC[0:4]});
142        $if 1 << LOG2N != NR:
143          if (nc & ${1 << LOG2N}) {
144            $if LOG2N >= 2:
145              $for N in range(0, 1 << LOG2N, 4):
146                $for M in reversed(range(MR)):
147                  vst1q_f32(c${M}, vacc${M}x${ABC[N:N+4]}); c${M} += 4;
148
149              $for M in reversed(range(MR)):
150                $for N in range(0, 1 << (LOG2N - 1), 4):
151                  vacc${M}x${ABC[N:N+4]} = vacc${M}x${ABC[N + (1 << LOG2N):N + (1 << LOG2N)+4]};
152            $elif LOG2N == 1:
153              $for M in reversed(range(MR)):
154                vst1_f32(c${M}, vacc${M}x${ABC[0:2]}); c${M} += 2;
155
156              $for M in reversed(range(MR)):
157                vacc${M}x${ABC[0:2]} = vget_high_f32(vacc${M}x${ABC[0:4]});
158            $elif LOG2N == 0:
159              $for M in reversed(range(MR)):
160                vst1_lane_f32(c${M}, vacc${M}x${ABC[0:2]}, 0);
161          }
162
163      nc = 0;
164    }
165  } while (nc != 0);
166}
167