• 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 % 4 == 0
7$assert BATCH_TILE >= 4
8$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
9$assert OP in ["ADD", "DIV", "MAX", "MIN", "MUL", "SUB", "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_F32 = {
20$  "ADD": lambda x, y: "vaddq_f32(%s, %s)" % (x, y),
21$  "DIV": lambda x, y: "vdivq_f32(%s, %s)" % (x, y),
22$  "MAX": lambda x, y: "vmaxq_f32(%s, %s)" % (x, y),
23$  "MIN": lambda x, y: "vminq_f32(%s, %s)" % (x, y),
24$  "MUL": lambda x, y: "vmulq_f32(%s, %s)" % (x, y),
25$  "SUB": lambda x, y: "vsubq_f32(%s, %s)" % (x, y),
26$  "SQRDIFF": lambda x, y: "vsubq_f32(%s, %s)" % (x, y),
27$}[OP]
28$SUFFIX = {"LINEAR": "", "MINMAX": "_minmax"}[ACTIVATION]
29$PARAMS = {"LINEAR": "xnn_f32_default_params", "MINMAX": "xnn_f32_minmax_params"}[ACTIVATION]
30void xnn_f32_v${OP.lower()}${SUFFIX}_ukernel__neon_x${BATCH_TILE}(
31    size_t n,
32    const float* a,
33    const float* b,
34    float* y,
35    const union ${PARAMS} params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
36{
37  assert(n != 0);
38  assert(n % sizeof(float) == 0);
39  assert(a != NULL);
40  assert(b != NULL);
41  assert(y != NULL);
42
43  $if ACTIVATION == "MINMAX":
44    const float32x4_t vy_min = vld1q_dup_f32(&params->scalar.min);
45    const float32x4_t vy_max = vld1q_dup_f32(&params->scalar.max);
46
47  for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
48    $for N in range(0, BATCH_TILE, 4):
49      const float32x4_t va${ABC[N:N+4]} = vld1q_f32(a); a += 4;
50      const float32x4_t vb${ABC[N:N+4]} = vld1q_f32(b); b += 4;
51
52    $for N in range(0, BATCH_TILE, 4):
53      float32x4_t vy${ABC[N:N+4]} = ${VOPQ_F32("va" + ABC[N:N+4], "vb" + ABC[N:N+4])};
54
55    $if OP == "SQRDIFF":
56      $for N in range(0, BATCH_TILE, 4):
57        vy${ABC[N:N+4]} = vmulq_f32(vy${ABC[N:N+4]}, vy${ABC[N:N+4]});
58
59    $if ACTIVATION == "MINMAX":
60      $for N in range(0, BATCH_TILE, 4):
61        vy${ABC[N:N+4]} = vmaxq_f32(vy${ABC[N:N+4]}, vy_min);
62
63      $for N in range(0, BATCH_TILE, 4):
64        vy${ABC[N:N+4]} = vminq_f32(vy${ABC[N:N+4]}, vy_max);
65
66    $for N in range(0, BATCH_TILE, 4):
67      vst1q_f32(y, vy${ABC[N:N+4]}); y += 4;
68  }
69  $if BATCH_TILE > 4:
70    for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
71      const float32x4_t va0123 = vld1q_f32(a); a += 4;
72      const float32x4_t vb0123 = vld1q_f32(b); b += 4;
73
74      float32x4_t vy0123 = ${VOPQ_F32("va0123", "vb0123")};
75      $if OP == "SQRDIFF":
76        vy0123 = vmulq_f32(vy0123, vy0123);
77      $if ACTIVATION == "MINMAX":
78        vy0123 = vmaxq_f32(vy0123, vy_min);
79        vy0123 = vminq_f32(vy0123, vy_max);
80      vst1q_f32(y, vy0123); y += 4;
81    }
82  if XNN_UNLIKELY(n != 0) {
83    const float32x4_t va0123 = vld1q_f32(a);
84    const float32x4_t vb0123 = vld1q_f32(b);
85
86    float32x4_t vy0123 = ${VOPQ_F32("va0123", "vb0123")};
87    $if OP == "SQRDIFF":
88      vy0123 = vmulq_f32(vy0123, vy0123);
89    $if ACTIVATION == "MINMAX":
90      vy0123 = vmaxq_f32(vy0123, vy_min);
91      vy0123 = vminq_f32(vy0123, vy_max);
92
93    float32x2_t vy01 = vget_low_f32(vy0123);
94    if (n & (2 * sizeof(float))) {
95      vst1_f32(y, vy01); y += 2;
96      vy01 = vget_high_f32(vy0123);
97    }
98    if (n & (1 * sizeof(float))) {
99      vst1_lane_f32(y, vy01, 0);
100    }
101  }
102}
103