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 BATCH_TILE % 16 == 0
7$assert BATCH_TILE >= 16
8$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
9#include <assert.h>
10
11#include <immintrin.h>
12
13#include <xnnpack/vunary.h>
14#include <xnnpack/common.h>
15#include <xnnpack/intrinsics-polyfill.h>
16
17
18void xnn_f32_relu_ukernel__avx512f_x${BATCH_TILE}(
19    size_t n,
20    const float* x,
21    float* y,
22    const union xnn_f32_relu_params params[restrict XNN_MIN_ELEMENTS(1)])
23{
24  assert(n != 0);
25  assert(n % sizeof(float) == 0);
26  assert(x != NULL);
27  assert(y != NULL);
28
29  const __m512 vzero = _mm512_setzero_ps();
30
31  for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
32    __m512 vacc${ABC[0:16]} = _mm512_loadu_ps(x);
33    $for N in range(16, BATCH_TILE, 16):
34      __m512 vacc${ABC[N:N+16]} = _mm512_loadu_ps(x + ${N});
35    x += ${BATCH_TILE};
36
37    $for N in range(0, BATCH_TILE, 16):
38      vacc${ABC[N:N+16]} = _mm512_max_ps(vacc${ABC[N:N+16]}, vzero);
39
40    _mm512_storeu_ps(y, vacc${ABC[0:16]});
41    $for N in range(16, BATCH_TILE, 16):
42      _mm512_storeu_ps(y + ${N}, vacc${ABC[N:N+16]});
43    y += ${BATCH_TILE};
44  }
45  $if BATCH_TILE > 16:
46    for (; n >= 16 * sizeof(float); n -= 16 * sizeof(float)) {
47      __m512 vacc = _mm512_loadu_ps(x);
48      x += 16;
49
50      vacc = _mm512_max_ps(vacc, vzero);
51
52      _mm512_storeu_ps(y, vacc);
53      y += 16;
54    }
55  if XNN_UNLIKELY(n != 0) {
56    assert(n >= 1 * sizeof(float));
57    assert(n <= 15 * sizeof(float));
58    // Prepare mask for valid 32-bit elements (depends on n).
59    n >>= 2 /* log2(sizeof(float)) */;
60    const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << n) - UINT32_C(1)));
61
62    __m512 vacc = _mm512_maskz_loadu_ps(vmask, x);
63    vacc = _mm512_max_ps(vacc, vzero);
64    _mm512_mask_storeu_ps(y, vmask, vacc);
65  }
66}
67