1// Copyright 2020 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 CHANNEL_TILE % 16 == 0
7$assert CHANNEL_TILE >= 16
8$assert ROW_TILE >= 1
9$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
10#include <assert.h>
11
12#include <immintrin.h>
13
14#include <xnnpack/intrinsics-polyfill.h>
15#include <xnnpack/math.h>
16#include <xnnpack/prelu.h>
17
18
19void xnn_f32_prelu_ukernel__avx512f_${ROW_TILE}x${CHANNEL_TILE}(
20    size_t rows,
21    size_t channels,
22    const float*restrict input,
23    size_t input_stride,
24    const float*restrict weights,
25    float*restrict output,
26    size_t output_stride)
27{
28  assert(rows != 0);
29  assert(channels != 0);
30  assert(channels % sizeof(float) == 0);
31
32  const float* i0 = input;
33  float* o0 = output;
34  $for M in range(1, ROW_TILE):
35    const float* i${M} = (const float*) ((uintptr_t) i${M-1} + input_stride);
36    float* o${M} = (float*) ((uintptr_t) o${M-1} + output_stride);
37    $if M % 2 == 0:
38      if XNN_UNPREDICTABLE(rows <= ${M}) {
39        i${M} = i${M-1};
40        o${M} = o${M-1};
41      }
42    $else:
43      if XNN_UNPREDICTABLE(rows < ${M+1}) {
44        i${M} = i${M-1};
45        o${M} = o${M-1};
46      }
47
48  const size_t input_increment = input_stride * ${ROW_TILE} - channels;
49  const size_t output_increment = output_stride * ${ROW_TILE} - channels;
50
51  const __m512 vzero = _mm512_setzero_ps();
52  do {
53    const float* w = weights;
54    size_t c = channels;
55    for (; c >= ${CHANNEL_TILE} * sizeof(float); c -= ${CHANNEL_TILE} * sizeof(float)) {
56      const __m512 vw${ABC[0:16]} = _mm512_load_ps(w);
57      $for C in range(16, CHANNEL_TILE, 16):
58        const __m512 vw${ABC[C:C+16]} = _mm512_load_ps(w + ${C});
59      w += ${CHANNEL_TILE};
60
61      $for M in range(ROW_TILE):
62        const __m512 vi${M}x${ABC[0:16]} = _mm512_loadu_ps(i${M});
63        $for C in range(16, CHANNEL_TILE, 16):
64          const __m512 vi${M}x${ABC[C:C+16]} = _mm512_loadu_ps(i${M} + ${C});
65        i${M} += ${CHANNEL_TILE};
66
67      $for M in range(ROW_TILE):
68        $for C in range(0, CHANNEL_TILE, 16):
69          const __mmask16 vsign${M}x${ABC[C:C+16]} = _mm512_cmp_ps_mask(vi${M}x${ABC[C:C+16]}, vzero, _CMP_LT_OQ);
70          const __m512 vacc${M}x${ABC[C:C+16]} = _mm512_mask_mul_ps(vi${M}x${ABC[C:C+16]}, vsign${M}x${ABC[C:C+16]}, vi${M}x${ABC[C:C+16]}, vw${ABC[C:C+16]});
71
72      $for M in range(ROW_TILE):
73        _mm512_storeu_ps(o${M}, vacc${M}x${ABC[0:16]});
74        $for C in range(16, CHANNEL_TILE, 16):
75          _mm512_storeu_ps(o${M} + ${C}, vacc${M}x${ABC[C:C+16]});
76        o${M} += ${CHANNEL_TILE};
77    }
78    $if CHANNEL_TILE > 16:
79      for (; c >= 16 * sizeof(float); c -= 16 * sizeof(float)) {
80        const __m512 vw = _mm512_load_ps(w);
81        w += 16;
82
83        $for M in range(ROW_TILE):
84          const __m512 vi${M} = _mm512_loadu_ps(i${M});
85          i${M} += 16;
86
87        $for M in range(ROW_TILE):
88          const __mmask16 vsign${M} = _mm512_cmp_ps_mask(vi${M}, vzero, _CMP_LT_OQ);
89          const __m512 vacc${M} = _mm512_mask_mul_ps(vi${M}, vsign${M}, vi${M}, vw);
90
91        $for M in range(ROW_TILE):
92          _mm512_storeu_ps(o${M}, vacc${M});
93          o${M} += 16;
94      }
95    if XNN_UNLIKELY(c != 0) {
96      assert(c >= 1 * sizeof(float));
97      assert(c <= 15 * sizeof(float));
98      // Prepare mask for valid 32-bit elements (depends on c).
99      const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << (c >> 2 /* log2(sizeof(float))*/)) - UINT32_C(1)));
100
101      const __m512 vw = _mm512_maskz_loadu_ps(vmask, w);
102
103      $for M in range(ROW_TILE):
104        const __m512 vi${M} = _mm512_maskz_loadu_ps(vmask, i${M});
105        i${M} = (const float*) ((uintptr_t) i${M} + c);
106
107      $for M in range(ROW_TILE):
108        const __mmask16 vsign${M} = _mm512_cmp_ps_mask(vi${M}, vzero, _CMP_LT_OQ);
109        const __m512 vacc${M} = _mm512_mask_mul_ps(vi${M}, vsign${M}, vi${M}, vw);
110
111      $for M in range(ROW_TILE):
112        _mm512_mask_storeu_ps(o${M}, vmask, vacc${M});
113        o${M} = (float*) ((uintptr_t) o${M} + c);
114    }
115    $for M in range(ROW_TILE):
116      i${M} = (const float*) ((uintptr_t) i${M} + input_increment);
117      o${M} = (float*) ((uintptr_t) o${M} + output_increment);
118      $if M % 2 == 1:
119        if XNN_UNPREDICTABLE(rows < ${ROW_TILE + M+1}) {
120          i${M} = i${M-1};
121          o${M} = o${M-1};
122        }
123      $elif M != 0:
124        if XNN_UNPREDICTABLE(rows <= ${ROW_TILE + M}) {
125          i${M} = i${M-1};
126          o${M} = o${M-1};
127        }
128    rows = doz(rows, ${ROW_TILE});
129  } while (rows != 0);
130}
131