• 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 BATCH_TILE % 16 == 0
7$assert BATCH_TILE >= 16
8$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
9$assert OP in ["ADD", "DIV", "MAX", "MIN", "MUL", "SUB"]
10#include <assert.h>
11
12#include <immintrin.h>
13
14#include <xnnpack/common.h>
15#include <xnnpack/intrinsics-polyfill.h>
16#include <xnnpack/vbinary.h>
17
18
19$_MM512_OP_PS = {
20$  "ADD": lambda x, y: "_mm512_add_ps(%s, %s)" % (x, y),
21$  "DIV": lambda x, y: "_mm512_div_ps(%s, %s)" % (x, y),
22$  "MAX": lambda x, y: "_mm512_max_ps(%s, %s)" % (x, y),
23$  "MIN": lambda x, y: "_mm512_min_ps(%s, %s)" % (x, y),
24$  "MUL": lambda x, y: "_mm512_mul_ps(%s, %s)" % (x, y),
25$  "SUB": lambda x, y: "_mm512_sub_ps(%s, %s)" % (x, y),
26$}[OP]
27void xnn_f32_v${OP.lower()}_ukernel__avx512f_x${BATCH_TILE}(
28    size_t n,
29    const float* a,
30    const float* b,
31    float* y,
32    const union xnn_f32_output_params params[restrict static 1])
33{
34  assert(n != 0);
35  assert(n % sizeof(float) == 0);
36
37  const __m512 vy_min = _mm512_broadcast_f32x4(_mm_load_ps(params->sse.min));
38  const __m512 vy_max = _mm512_broadcast_f32x4(_mm_load_ps(params->sse.max));
39
40  for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
41    const __m512 va${ABC[0:16]} = _mm512_loadu_ps(a);
42    $for N in range(16, BATCH_TILE, 16):
43      const __m512 va${ABC[N:N+16]} = _mm512_loadu_ps(a + ${N});
44    a += ${BATCH_TILE};
45
46    const __m512 vb${ABC[0:16]} = _mm512_loadu_ps(b);
47    $for N in range(16, BATCH_TILE, 16):
48      const __m512 vb${ABC[N:N+16]} = _mm512_loadu_ps(b + ${N});
49    b += ${BATCH_TILE};
50
51    $for N in range(0, BATCH_TILE, 16):
52      __m512 vy${ABC[N:N+16]} = ${_MM512_OP_PS("va" + ABC[N:N+16], "vb" + ABC[N:N+16])};
53
54    $for N in range(0, BATCH_TILE, 16):
55      vy${ABC[N:N+16]} = _mm512_max_ps(vy${ABC[N:N+16]}, vy_min);
56
57    $for N in range(0, BATCH_TILE, 16):
58      vy${ABC[N:N+16]} = _mm512_min_ps(vy${ABC[N:N+16]}, vy_max);
59
60    _mm512_storeu_ps(y, vy${ABC[0:16]});
61    $for N in range(16, BATCH_TILE, 16):
62      _mm512_storeu_ps(y + ${N}, vy${ABC[N:N+16]});
63    y += ${BATCH_TILE};
64  }
65  $if BATCH_TILE >= 16:
66    for (; n >= 16 * sizeof(float); n -= 16 * sizeof(float)) {
67      const __m512 va = _mm512_loadu_ps(a);
68      a += 16;
69
70      const __m512 vb = _mm512_loadu_ps(b);
71      b += 16;
72
73      __m512 vy = ${_MM512_OP_PS("va", "vb")};
74      vy = _mm512_max_ps(vy, vy_min);
75      vy = _mm512_min_ps(vy, vy_max);
76      _mm512_storeu_ps(y, vy);
77      y += 16;
78    }
79  if XNN_UNLIKELY(n != 0) {
80    assert(n >= 1 * sizeof(float));
81    assert(n <= 15 * sizeof(float));
82    // Prepare mask for valid 32-bit elements (depends on n).
83    n >>= 2 /* log2(sizeof(float)) */;
84    const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << n) - UINT32_C(1)));
85
86    const __m512 va = _mm512_maskz_loadu_ps(vmask, a);
87    const __m512 vb = _mm512_maskz_loadu_ps(vmask, b);
88
89    __m512 vy = ${_MM512_OP_PS("va", "vb")};
90    vy = _mm512_max_ps(vy, vy_min);
91    vy = _mm512_min_ps(vy, vy_max);
92    _mm512_mask_storeu_ps(y, vmask, vy);
93  }
94}
95