• 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#include <assert.h>
9
10#include <xmmintrin.h>
11
12#include <xnnpack/igemm.h>
13
14
15void xnn_f32_igemm_minmax_ukernel_${MR}x${NR}__sse_load1(
16    size_t mr,
17    size_t nc,
18    size_t kc,
19    size_t ks,
20    const float**restrict a,
21    const float*restrict w,
22    float*restrict c,
23    size_t cm_stride,
24    size_t cn_stride,
25    size_t a_offset,
26    const float* zero,
27    const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
28{
29  assert(mr != 0);
30  assert(mr <= ${MR});
31  assert(nc != 0);
32  assert(kc != 0);
33  assert(kc % sizeof(float) == 0);
34  assert(ks != 0);
35  assert(ks % (${MR} * sizeof(void*)) == 0);
36  assert(a_offset % sizeof(float) == 0);
37  assert(a != NULL);
38  assert(w != NULL);
39  assert(c != NULL);
40
41  float* c0 = c;
42  $for M in range(1, MR):
43    float* c${M} = (float*) ((uintptr_t) c${M-1} + cm_stride);
44    $if M % 2 == 0:
45      if XNN_UNPREDICTABLE(mr <= ${M}) {
46        c${M} = c${M-1};
47      }
48    $elif M + 1 == MR:
49      if XNN_UNPREDICTABLE(mr != ${M+1}) {
50        c${M} = c${M-1};
51      }
52    $else:
53      if XNN_UNPREDICTABLE(mr < ${M+1}) {
54        c${M} = c${M-1};
55      }
56
57  do {
58    __m128 vacc0x${ABC[0:4]} = _mm_load_ps(w);
59    $for N in range(4, NR, 4):
60      __m128 vacc0x${ABC[N:N+4]} = _mm_load_ps(w + ${N});
61    $for M in range(1, MR):
62      $for N in range(0, NR, 4):
63        __m128 vacc${M}x${ABC[N:N+4]} = vacc0x${ABC[N:N+4]};
64    w += ${NR};
65
66    size_t p = ks;
67    do {
68      $for M in range(MR):
69        const float* restrict a${M} = a[${M}];
70        assert(a${M} != NULL);
71        if XNN_UNPREDICTABLE(a${M} != zero) {
72          a${M} = (const float*) ((uintptr_t) a${M} + a_offset);
73        }
74      a += ${MR};
75
76      size_t k = kc;
77      do {
78        const __m128 vb${ABC[0:4]} = _mm_load_ps(w);
79        $for N in range(4, NR, 4):
80          const __m128 vb${ABC[N:N+4]} = _mm_load_ps(w + ${N});
81        w += ${NR};
82
83        $for M in range(MR):
84          const __m128 va${M} = _mm_load1_ps(a${M});
85          a${M} += 1;
86
87        $for M in range(MR):
88          $for N in range(0, NR, 4):
89            vacc${M}x${ABC[N:N+4]} = _mm_add_ps(vacc${M}x${ABC[N:N+4]}, _mm_mul_ps(va${M}, vb${ABC[N:N+4]}));
90        k -= sizeof(float);
91      } while (k != 0);
92      p -= ${MR} * sizeof(void*);
93    } while (p != 0);
94
95    const __m128 vmax = _mm_load_ps(params->sse.max);
96    $for N in range(0, NR, 4):
97      $for M in range(MR):
98        vacc${M}x${ABC[N:N+4]} = _mm_min_ps(vacc${M}x${ABC[N:N+4]}, vmax);
99
100    const __m128 vmin = _mm_load_ps(params->sse.min);
101    $for N in range(0, NR, 4):
102      $for M in range(MR):
103        vacc${M}x${ABC[N:N+4]} = _mm_max_ps(vacc${M}x${ABC[N:N+4]}, vmin);
104
105    if XNN_LIKELY(nc >= ${NR}) {
106      $for M in reversed(range(MR)):
107        _mm_storeu_ps(c${M}, vacc${M}x${ABC[0:4]});
108        $for N in range(4, NR, 4):
109          _mm_storeu_ps(c${M} + ${N}, vacc${M}x${ABC[N:N+4]});
110        c${M} = (float*) ((uintptr_t) c${M} + cn_stride);
111
112      a = (const float**restrict) ((uintptr_t) a - ks);
113      nc -= ${NR};
114    } else {
115      $for LOG2N in reversed(range(NR.bit_length())):
116        $if NR != 1 << LOG2N:
117          if (nc & ${1 << LOG2N}) {
118            $if LOG2N >= 2:
119              $for M in reversed(range(MR)):
120                _mm_storeu_ps(c${M}, vacc${M}x${ABC[0:4]});
121                $for N in range(4, 1 << LOG2N, 4):
122                  _mm_storeu_ps(c${M} + ${N}, vacc${M}x${ABC[N:N+4]});
123
124              $for M in reversed(range(MR)):
125                $for N in range(0, 1 << (LOG2N - 1), 4):
126                  vacc${M}x${ABC[N:N+4]} = vacc${M}x${ABC[N + (1 << LOG2N):N + (1 << LOG2N)+4]};
127
128              $for M in reversed(range(MR)):
129                c${M} += ${1 << LOG2N};
130            $elif LOG2N == 1:
131              $for M in reversed(range(MR)):
132                _mm_storel_pi((__m64*) c${M}, vacc${M}x${ABC[0:4]});
133
134              $for M in reversed(range(MR)):
135                vacc${M}x${ABC[0:4]} = _mm_movehl_ps(vacc${M}x${ABC[0:4]}, vacc${M}x${ABC[0:4]});
136
137              $for M in reversed(range(MR)):
138                c${M} += 2;
139            $elif LOG2N == 0:
140              $for M in reversed(range(MR)):
141                _mm_store_ss(c${M}, vacc${M}x${ABC[0:4]});
142          }
143
144      nc = 0;
145    }
146  } while (nc != 0);
147}
148