• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 % 8 == 0
7$assert BATCH_TILE >= 8
8$ABC = "01234567456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
9$assert OP in ["ADD", "DIV", "RDIV", "MAX", "MIN", "MUL", "SUB", "RSUB", "SQRDIFF"]
10$assert ACTIVATION in ["LINEAR", "MINMAX"]
11#include <assert.h>
12
13#include <arm_neon.h>
14
15#include <xnnpack/common.h>
16#include <xnnpack/vbinary.h>
17
18
19$VOPQ_f16 = {
20$  "ADD": lambda x: "vaddq_f16(%s, vb)" % x,
21$  "DIV": lambda x: "vdivq_f16(%s, vb)" % x,
22$  "RDIV": lambda x: "vdivq_f16(vb, %s)" % x,
23$  "MAX": lambda x: "vmaxq_f16(%s, vb)" % x,
24$  "MIN": lambda x: "vminq_f16(%s, vb)" % x,
25$  "MUL": lambda x: "vmulq_f16(%s, vb)" % x,
26$  "SUB": lambda x: "vsubq_f16(%s, vb)" % x,
27$  "RSUB": lambda x: "vsubq_f16(vb, %s)" % x,
28$  "SQRDIFF": lambda x: "vsubq_f16(%s, vb)" % x,
29$}[OP]
30$SUFFIX = {"LINEAR": "", "MINMAX": "_minmax"}[ACTIVATION]
31$PARAMS = {"LINEAR": "xnn_f16_default_params", "MINMAX": "xnn_f16_minmax_params"}[ACTIVATION]
32void xnn_f16_v${OP.lower()}c${SUFFIX}_ukernel__neonfp16arith_x${BATCH_TILE}(
33    size_t n,
34    const void* restrict a_ptr,
35    const void* restrict b_ptr,
36    void* restrict y_ptr,
37    const struct ${PARAMS} params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
38{
39  assert(n != 0);
40  assert(n % sizeof(__fp16) == 0);
41  assert(a_ptr != NULL);
42  assert(b_ptr != NULL);
43  assert(y_ptr != NULL);
44
45  const __fp16* a = (const __fp16*) a_ptr;
46  const __fp16* b = (const __fp16*) b_ptr;
47  __fp16* y = (__fp16*) y_ptr;
48
49  $if ACTIVATION == "MINMAX":
50    const float16x8_t vy_min = vld1q_dup_f16(&params->min);
51    const float16x8_t vy_max = vld1q_dup_f16(&params->max);
52
53  const float16x8_t vb = vld1q_dup_f16(b);
54  for (; n >= ${BATCH_TILE} * sizeof(__fp16); n -= ${BATCH_TILE} * sizeof(__fp16)) {
55    $for N in range(0, BATCH_TILE, 8):
56      const float16x8_t va${ABC[N:N+8]} = vld1q_f16(a); a += 8;
57
58    $for N in range(0, BATCH_TILE, 8):
59      float16x8_t vy${ABC[N:N+8]} = ${VOPQ_f16("va" + ABC[N:N+8])};
60
61    $if OP == "SQRDIFF":
62      $for N in range(0, BATCH_TILE, 8):
63        vy${ABC[N:N+8]} = vmulq_f16(vy${ABC[N:N+8]}, vy${ABC[N:N+8]});
64
65    $if ACTIVATION == "MINMAX":
66      $for N in range(0, BATCH_TILE, 8):
67        vy${ABC[N:N+8]} = vmaxq_f16(vy${ABC[N:N+8]}, vy_min);
68
69      $for N in range(0, BATCH_TILE, 8):
70        vy${ABC[N:N+8]} = vminq_f16(vy${ABC[N:N+8]}, vy_max);
71
72    $for N in range(0, BATCH_TILE, 8):
73      vst1q_f16(y, vy${ABC[N:N+8]}); y += 8;
74  }
75  $if BATCH_TILE > 8:
76    for (; n >= 8 * sizeof(__fp16); n -= 8 * sizeof(__fp16)) {
77      const float16x8_t va01234567 = vld1q_f16(a); a += 8;
78
79      float16x8_t vy01234567 = ${VOPQ_f16("va01234567")};
80      $if OP == "SQRDIFF":
81        vy01234567 = vmulq_f16(vy01234567, vy01234567);
82      $if ACTIVATION == "MINMAX":
83        vy01234567 = vmaxq_f16(vy01234567, vy_min);
84        vy01234567 = vminq_f16(vy01234567, vy_max);
85      vst1q_f16(y, vy01234567); y += 8;
86    }
87  if XNN_UNLIKELY(n != 0) {
88    const float16x8_t va01234567 = vld1q_f16(a);
89
90    float16x8_t vy01234567 = ${VOPQ_f16("va01234567")};
91    $if OP == "SQRDIFF":
92      vy01234567 = vmulq_f16(vy01234567, vy01234567);
93    $if ACTIVATION == "MINMAX":
94      vy01234567 = vmaxq_f16(vy01234567, vy_min);
95      vy01234567 = vminq_f16(vy01234567, vy_max);
96
97    float16x4_t vy0123 = vget_low_f16(vy01234567);
98    if (n & (4 * sizeof(__fp16))) {
99      vst1_f16(y, vy0123); y += 4;
100      vy0123 = vget_high_f16(vy01234567);
101    }
102
103    if (n & (2 * sizeof(__fp16))) {
104      vst1_lane_u32(__builtin_assume_aligned(y, 1), vreinterpret_u32_f16(vy0123), 0); y += 2;
105      vy0123 = vext_f16(vy0123, vy0123, 2);
106    }
107
108    if (n & (1 * sizeof(__fp16))) {
109      vst1_lane_f16(y, vy0123, 0);
110    }
111  }
112}
113