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#include <assert.h> 10 11#include <arm_neon.h> 12 13#include <xnnpack/common.h> 14#include <xnnpack/vunary.h> 15 16 17void xnn_f32_vhswish_ukernel__neon_x${BATCH_TILE}( 18 size_t n, 19 const float* x, 20 float* y, 21 const union xnn_f32_hswish_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS 22{ 23 assert(n != 0); 24 assert(n % sizeof(float) == 0); 25 26 const float32x4_t vsixth = vld1q_dup_f32(¶ms->scalar.sixth); 27 const float32x4_t vthree = vld1q_dup_f32(¶ms->scalar.three); 28 const int32x4_t vsix = vreinterpretq_s32_f32(vld1q_dup_f32(¶ms->scalar.six)); 29 const int32x4_t vzero = vdupq_n_s32(0); 30 31 $if BATCH_TILE > 4: 32 for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) { 33 $for N in range(0, BATCH_TILE, 4): 34 float32x4_t vx${ABC[N:N+4]} = vld1q_f32(x); x += 4; 35 36 $for N in range(0, BATCH_TILE, 4): 37 float32x4_t vacc${ABC[N:N+4]} = vaddq_f32(vx${ABC[N:N+4]}, vthree); 38 vx${ABC[N:N+4]} = vmulq_f32(vx${ABC[N:N+4]}, vsixth); 39 40 $for N in range(0, BATCH_TILE, 4): 41 vacc${ABC[N:N+4]} = vreinterpretq_f32_s32(vmaxq_s32(vreinterpretq_s32_f32(vacc${ABC[N:N+4]}), vzero)); 42 43 $for N in range(0, BATCH_TILE, 4): 44 vacc${ABC[N:N+4]} = vreinterpretq_f32_s32(vminq_s32(vreinterpretq_s32_f32(vacc${ABC[N:N+4]}), vsix)); 45 46 $for N in range(0, BATCH_TILE, 4): 47 vacc${ABC[N:N+4]} = vmulq_f32(vacc${ABC[N:N+4]}, vx${ABC[N:N+4]}); 48 49 $for N in range(0, BATCH_TILE, 4): 50 vst1q_f32(y, vacc${ABC[N:N+4]}); y += 4; 51 } 52 for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) { 53 float32x4_t vx = vld1q_f32(x); x += 4; 54 float32x4_t vacc = vaddq_f32(vx, vthree); 55 vx = vmulq_f32(vx, vsixth); 56 vacc = vreinterpretq_f32_s32(vmaxq_s32(vreinterpretq_s32_f32(vacc), vzero)); 57 vacc = vreinterpretq_f32_s32(vminq_s32(vreinterpretq_s32_f32(vacc), vsix)); 58 vacc = vmulq_f32(vacc, vx); 59 vst1q_f32(y, vacc); y += 4; 60 } 61 if XNN_UNLIKELY(n != 0) { 62 float32x4_t vx = vld1q_f32(x); 63 float32x4_t vacc = vaddq_f32(vx, vthree); 64 vx = vmulq_f32(vx, vsixth); 65 vacc = vreinterpretq_f32_s32(vmaxq_s32(vreinterpretq_s32_f32(vacc), vzero)); 66 vacc = vreinterpretq_f32_s32(vminq_s32(vreinterpretq_s32_f32(vacc), vsix)); 67 vacc = vmulq_f32(vacc, vx); 68 69 float32x2_t vacc_lo = vget_low_f32(vacc); 70 if (n & (2 * sizeof(float))) { 71 vst1_f32(y, vacc_lo); y += 2; 72 vacc_lo = vget_high_f32(vacc); 73 } 74 if (n & (1 * sizeof(float))) { 75 vst1_lane_f32(y, vacc_lo, 0); 76 } 77 } 78} 79