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 % 16 == 0
7$assert BATCH_TILE >= 16
8$SIMD_TILE = BATCH_TILE // 16
9#include <assert.h>
10
11#include <immintrin.h>
12
13#include <xnnpack/common.h>
14#include <xnnpack/intrinsics-polyfill.h>
15#include <xnnpack/vunary.h>
16
17
18void xnn_f32_velu_ukernel__avx512f_rr1_p6_x${BATCH_TILE}(
19    size_t n,
20    const float* x,
21    float* y,
22    const union xnn_f32_elu_params params[restrict XNN_MIN_ELEMENTS(1)])
23{
24  assert(n != 0);
25  assert(n % sizeof(float) == 0);
26
27  const __m512 vprescale = _mm512_broadcast_f32x4(_mm_load_ps(params->sse.prescale));
28  const __m512 valpha = _mm512_broadcast_f32x4(_mm_load_ps(params->sse.alpha));
29  const __m512 vbeta = _mm512_broadcast_f32x4(_mm_load_ps(params->sse.beta));
30
31  const __m512 vsat_cutoff = _mm512_set1_ps(-0x1.154246p+4f);
32  const __m512 vmagic_bias = _mm512_set1_ps(0x1.8000FEp23f);
33  const __m512 vlog2e = _mm512_set1_ps(0x1.715476p+0f);
34  const __m512 vminus_ln2 = _mm512_set1_ps(-0x1.62E43p-1f);
35  const __m512 vc6 = _mm512_set1_ps(0x1.6b7338p-10f);
36  const __m512 vc5 = _mm512_set1_ps(0x1.12278Ep-7f);
37  const __m512 vc4 = _mm512_set1_ps(0x1.555716p-5f);
38  const __m512 vc3 = _mm512_set1_ps(0x1.5554B0p-3f);
39  const __m512 vc2 = _mm512_set1_ps(0x1.FFFFFEp-2f);
40
41  $if BATCH_TILE > 16:
42    for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
43      __m512 vx0 = _mm512_loadu_ps(x);
44      $for N in range(1, SIMD_TILE):
45        __m512 vx${N} = _mm512_loadu_ps(x + ${N * 16});
46      x += ${BATCH_TILE};
47
48      $for N in range(SIMD_TILE):
49        const __m512 vz${N} = _mm512_max_ps(vsat_cutoff, _mm512_mul_ps(vx${N}, vprescale));
50
51      $for N in range(SIMD_TILE):
52        __m512 vn${N} = _mm512_fmadd_ps(vz${N}, vlog2e, vmagic_bias);
53
54      $for N in range(SIMD_TILE):
55        __m512 vs${N} = _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_castps_si512(vn${N}), 23));
56        vn${N} = _mm512_sub_ps(vn${N}, vmagic_bias);
57
58      $for N in range(SIMD_TILE):
59        __m512 vt${N} = _mm512_fmadd_ps(vn${N}, vminus_ln2, vz${N});
60
61      $for N in range(SIMD_TILE):
62        __m512 vp${N} = _mm512_fmadd_ps(vc6, vt${N}, vc5);
63
64      $for N in range(SIMD_TILE):
65        vp${N} = _mm512_fmadd_ps(vp${N}, vt${N}, vc4);
66
67      $for N in range(SIMD_TILE):
68        vp${N} = _mm512_fmadd_ps(vp${N}, vt${N}, vc3);
69
70      $for N in range(SIMD_TILE):
71        vp${N} = _mm512_fmadd_ps(vp${N}, vt${N}, vc2);
72
73      $for N in range(SIMD_TILE):
74        vp${N} = _mm512_mul_ps(vp${N}, vt${N});
75        vt${N} = _mm512_mul_ps(vt${N}, vs${N});
76
77      $for N in range(SIMD_TILE):
78        vs${N} = _mm512_fmsub_ps(vs${N}, valpha, valpha);
79
80      $for N in range(SIMD_TILE):
81        vp${N} = _mm512_fmadd_ps(vp${N}, vt${N}, vt${N});
82
83      const __m512 vzero = _mm512_setzero_ps();
84      $for N in range(SIMD_TILE):
85        __m512 vy${N} = _mm512_fmadd_ps(vp${N}, valpha, vs${N});
86        const __mmask16 vsign${N} = _mm512_cmp_ps_mask(vx${N}, vzero, _CMP_NLT_US);
87
88      $for N in range(SIMD_TILE):
89        vy${N} = _mm512_mask_mul_ps(vy${N}, vsign${N}, vx${N}, vbeta);
90
91      _mm512_storeu_ps(y, vy0);
92      $for N in range(1, SIMD_TILE):
93        _mm512_storeu_ps(y + ${N * 16}, vy${N});
94      y += ${BATCH_TILE};
95    }
96  for (; n >= 16 * sizeof(float); n -= 16 * sizeof(float)) {
97    __m512 vx = _mm512_loadu_ps(x);
98    x += 16;
99
100    const __m512 vz = _mm512_max_ps(vsat_cutoff, _mm512_mul_ps(vx, vprescale));
101    const __mmask16 vsign = _mm512_cmp_ps_mask(vx, _mm512_setzero_ps(), _CMP_NLT_US);
102
103    __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias);
104    __m512 vs = _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_castps_si512(vn), 23));
105    vn = _mm512_sub_ps(vn, vmagic_bias);
106
107    __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz);
108
109    __m512 vp = _mm512_fmadd_ps(vc6, vt, vc5);
110    vp = _mm512_fmadd_ps(vp, vt, vc4);
111    vp = _mm512_fmadd_ps(vp, vt, vc3);
112    vp = _mm512_fmadd_ps(vp, vt, vc2);
113    vp = _mm512_mul_ps(vp, vt);
114
115    vt = _mm512_mul_ps(vt, vs);
116    vs = _mm512_fmsub_ps(vs, valpha, valpha);
117    vp = _mm512_fmadd_ps(vp, vt, vt);
118    __m512 vy = _mm512_fmadd_ps(vp, valpha, vs);
119
120    vy = _mm512_mask_mul_ps(vy, vsign, vx, vbeta);
121
122    _mm512_storeu_ps(y, vy);
123    y += 16;
124  }
125  if XNN_UNLIKELY(n != 0) {
126    assert(n >= 1 * sizeof(float));
127    assert(n <= 15 * sizeof(float));
128    // Prepare mask for valid 32-bit elements (depends on n).
129    n >>= 2 /* log2(sizeof(float)) */;
130    const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << n) - UINT32_C(1)));
131
132    __m512 vx = _mm512_maskz_loadu_ps(vmask, x);
133
134    const __m512 vz = _mm512_max_ps(vsat_cutoff, _mm512_mul_ps(vx, vprescale));
135    const __mmask16 vsign = _mm512_cmp_ps_mask(vx, _mm512_setzero_ps(), _CMP_NLT_US);
136
137    __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias);
138    __m512 vs = _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_castps_si512(vn), 23));
139    vn = _mm512_sub_ps(vn, vmagic_bias);
140
141    __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz);
142
143    __m512 vp = _mm512_fmadd_ps(vc6, vt, vc5);
144    vp = _mm512_fmadd_ps(vp, vt, vc4);
145    vp = _mm512_fmadd_ps(vp, vt, vc3);
146    vp = _mm512_fmadd_ps(vp, vt, vc2);
147    vp = _mm512_mul_ps(vp, vt);
148
149    vt = _mm512_mul_ps(vt, vs);
150    vs = _mm512_fmsub_ps(vs, valpha, valpha);
151    vp = _mm512_fmadd_ps(vp, vt, vt);
152    __m512 vy = _mm512_fmadd_ps(vp, valpha, vs);
153
154    vy = _mm512_mask_mul_ps(vy, vsign, vx, vbeta);
155
156    _mm512_mask_storeu_ps(y, vmask, vy);
157  }
158}
159