• 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"}_ld64(
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 >= 2 * sizeof(float); k -= 2 * sizeof(float)) {
79        $for M in range(MR):
80          const float32x2_t va${M} = vld1_f32(a${M}); a${M} += 2;
81
82        $for L in range(2):
83          $for N in range(0, NR, 4):
84            const float32x4_t vb${ABC[N:N+4]}c${L} = vld1q_f32(w); w += 4;
85
86          $if DUP:
87            $for M in range(MR):
88              const float32x4_t va${M}c${L} = vdupq_lane_f32(va${M}, ${L});
89            $for N in range(0, NR, 4):
90              $for M in range(MR):
91                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});
92          $else:
93             $for N in range(0, NR, 4):
94               $for M in range(MR):
95                 vacc${M}x${ABC[N:N+4]} = ${VMULADDQ_LANE_F32}(vacc${M}x${ABC[N:N+4]}, vb${ABC[  N:N+4]}c${L}, va${M}, ${L});
96
97      }
98      if XNN_UNLIKELY(k != 0) {
99        $for M in range(MR):
100          const float32x4_t va${M} = vld1q_dup_f32(a${M});
101
102        $for N in range(0, NR, 4):
103          const float32x4_t vb${ABC[N:N+4]} = vld1q_f32(w); w += 4;
104
105        $for N in range(0, NR, 4):
106          $for M in range(MR):
107            vacc${M}x${ABC[N:N+4]} = ${VMULADDQ_F32}(vacc${M}x${ABC[N:N+4]}, va${M}, vb${ABC[N:N+4]});
108      }
109      p -= ${MR} * sizeof(void*);
110    } while (p != 0);
111
112    const float32x4_t vmax = vld1q_dup_f32(&params->scalar.max);
113    $for N in range(0, NR, 4):
114      $for M in range(MR):
115        vacc${M}x${ABC[N:N+4]} = vminq_f32(vacc${M}x${ABC[N:N+4]}, vmax);
116
117    const float32x4_t vmin = vld1q_dup_f32(&params->scalar.min);
118    $for N in range(0, NR, 4):
119      $for M in range(MR):
120        vacc${M}x${ABC[N:N+4]} = vmaxq_f32(vacc${M}x${ABC[N:N+4]}, vmin);
121
122    if XNN_LIKELY(nc >= ${NR}) {
123      $for M in reversed(range(MR)):
124        vst1q_f32(c${M}, vacc${M}x${ABC[0:4]});
125        $for N in range(4, NR, 4):
126          vst1q_f32(c${M} + ${N}, vacc${M}x${ABC[N:N+4]});
127        c${M} = (float*) ((uintptr_t) c${M} + cn_stride);
128
129      a = (const float**restrict) ((uintptr_t) a - ks);
130      nc -= ${NR};
131    } else {
132      $for LOG2N in reversed(range(NR.bit_length())):
133        $if LOG2N == 1:
134          $for M in reversed(range(MR)):
135            float32x2_t vacc${M}x${ABC[0:2]} = vget_low_f32(vacc${M}x${ABC[0:4]});
136        $if 1 << LOG2N != NR:
137          if (nc & ${1 << LOG2N}) {
138            $if LOG2N >= 2:
139              $for N in range(0, 1 << LOG2N, 4):
140                $for M in reversed(range(MR)):
141                  vst1q_f32(c${M}, vacc${M}x${ABC[N:N+4]}); c${M} += 4;
142
143              $for M in reversed(range(MR)):
144                $for N in range(0, 1 << (LOG2N - 1), 4):
145                  vacc${M}x${ABC[N:N+4]} = vacc${M}x${ABC[N + (1 << LOG2N):N + (1 << LOG2N)+4]};
146            $elif LOG2N == 1:
147              $for M in reversed(range(MR)):
148                vst1_f32(c${M}, vacc${M}x${ABC[0:2]}); c${M} += 2;
149
150              $for M in reversed(range(MR)):
151                vacc${M}x${ABC[0:2]} = vget_high_f32(vacc${M}x${ABC[0:4]});
152            $elif LOG2N == 0:
153              $for M in reversed(range(MR)):
154                vst1_lane_f32(c${M}, vacc${M}x${ABC[0:2]}, 0);
155          }
156
157      nc = 0;
158    }
159  } while (nc != 0);
160}
161