xref: /aosp_15_r20/external/XNNPACK/src/f16-vhswish/neonfp16arith.c.in (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
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 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
9#include <assert.h>
10
11#include <arm_neon.h>
12
13#include <xnnpack/common.h>
14#include <xnnpack/vunary.h>
15
16
17void xnn_f16_vhswish_ukernel__neonfp16arith_x${BATCH_TILE}(
18    size_t n,
19    const void* restrict x_ptr,
20    void* restrict y_ptr,
21    const union xnn_f16_hswish_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
22{
23  assert(n != 0);
24  assert(n % sizeof(__fp16) == 0);
25
26  const __fp16* x = (const __fp16*) x_ptr;
27  __fp16* y = (__fp16*) y_ptr;
28
29  const float16x8_t vsixth = vreinterpretq_f16_u16(vld1q_dup_u16(&params->neon.sixth));
30  const float16x8_t vthree = vreinterpretq_f16_u16(vld1q_dup_u16(&params->neon.three));
31  const int16x8_t vsix = vreinterpretq_s16_u16(vld1q_dup_u16(&params->neon.six));
32  const int16x8_t vzero = vdupq_n_s16(0);
33
34  $if BATCH_TILE > 8:
35    for (; n >= ${BATCH_TILE} * sizeof(__fp16); n -= ${BATCH_TILE} * sizeof(__fp16)) {
36      $for N in range(0, BATCH_TILE, 8):
37        float16x8_t vx${ABC[N:N+8]} = vld1q_f16(x); x += 8;
38
39      $for N in range(0, BATCH_TILE, 8):
40        float16x8_t vacc${ABC[N:N+8]} = vaddq_f16(vx${ABC[N:N+8]}, vthree);
41        vx${ABC[N:N+8]} = vmulq_f16(vx${ABC[N:N+8]}, vsixth);
42
43      $for N in range(0, BATCH_TILE, 8):
44        vacc${ABC[N:N+8]} = vreinterpretq_f16_s16(vmaxq_s16(vreinterpretq_s16_f16(vacc${ABC[N:N+8]}), vzero));
45
46      $for N in range(0, BATCH_TILE, 8):
47        vacc${ABC[N:N+8]} = vreinterpretq_f16_s16(vminq_s16(vreinterpretq_s16_f16(vacc${ABC[N:N+8]}), vsix));
48
49      $for N in range(0, BATCH_TILE, 8):
50        vacc${ABC[N:N+8]} = vmulq_f16(vacc${ABC[N:N+8]}, vx${ABC[N:N+8]});
51
52      $for N in range(0, BATCH_TILE, 8):
53        vst1q_f16(y, vacc${ABC[N:N+8]}); y += 8;
54    }
55  for (; n >= 8 * sizeof(__fp16); n -= 8 * sizeof(__fp16)) {
56    float16x8_t vx = vld1q_f16(x); x += 8;
57    float16x8_t vacc = vaddq_f16(vx, vthree);
58    vx = vmulq_f16(vx, vsixth);
59    vacc = vreinterpretq_f16_s16(vmaxq_s16(vreinterpretq_s16_f16(vacc), vzero));
60    vacc = vreinterpretq_f16_s16(vminq_s16(vreinterpretq_s16_f16(vacc), vsix));
61    vacc = vmulq_f16(vacc, vx);
62    vst1q_f16(y, vacc); y += 8;
63  }
64  if XNN_UNLIKELY(n != 0) {
65    float16x8_t vx = vld1q_f16(x);
66    float16x8_t vacc = vaddq_f16(vx, vthree);
67    vx = vmulq_f16(vx, vsixth);
68    vacc = vreinterpretq_f16_s16(vmaxq_s16(vreinterpretq_s16_f16(vacc), vzero));
69    vacc = vreinterpretq_f16_s16(vminq_s16(vreinterpretq_s16_f16(vacc), vsix));
70    vacc = vmulq_f16(vacc, vx);
71
72    float16x4_t vacc_lo = vget_low_f16(vacc);
73    if (n & (4 * sizeof(__fp16))) {
74      vst1_f16(y, vacc_lo); y += 4;
75      vacc_lo = vget_high_f16(vacc);
76    }
77    if (n & (2 * sizeof(__fp16))) {
78      vst1_lane_u32((void*) y, vreinterpret_u32_f16(vacc_lo), 0); y += 2;
79      vacc_lo = vext_f16(vacc_lo, vacc_lo, 2);
80    }
81    if (n & (1 * sizeof(__fp16))) {
82      vst1_lane_f16(y, vacc_lo, 0);
83    }
84  }
85}
86