• 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$SIMD_TILE = BATCH_TILE // 8
9#include <assert.h>
10
11#include <immintrin.h>
12
13#include <xnnpack/common.h>
14#include <xnnpack/vunary.h>
15
16
17static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
18
19void xnn_f32_velu_ukernel__avx_rr2_p6_x${BATCH_TILE}(
20    size_t n,
21    const float* x,
22    float* y,
23    const union xnn_f32_elu_params params[restrict XNN_MIN_ELEMENTS(1)])
24{
25  assert(n % sizeof(float) == 0);
26
27  const __m256 vprescale = _mm256_broadcast_ps((const __m128*) params->sse.prescale);
28  const __m256 valpha = _mm256_broadcast_ps((const __m128*) params->sse.alpha);
29  const __m256 vbeta = _mm256_broadcast_ps((const __m128*) params->sse.beta);
30
31  const __m256 vsat_cutoff = _mm256_set1_ps(-0x1.154246p+4f);
32  const __m256 vmagic_bias = _mm256_set1_ps(0x1.8000FEp23f);
33  const __m256 vlog2e = _mm256_set1_ps(0x1.715476p+0f);
34  const __m256 vminus_ln2_hi = _mm256_set1_ps(-0x1.62E440p-1f);
35  const __m256 vminus_ln2_lo = _mm256_set1_ps(0x1.0105C6p-21f);
36  const __m256 vc6 = _mm256_set1_ps(0x1.6b7338p-10f);
37  const __m256 vc5 = _mm256_set1_ps(0x1.12278Ep-7f);
38  const __m256 vc4 = _mm256_set1_ps(0x1.555716p-5f);
39  const __m256 vc3 = _mm256_set1_ps(0x1.5554B0p-3f);
40  const __m256 vc2 = _mm256_set1_ps(0x1.FFFFFEp-2f);
41  const __m256 vone = _mm256_set1_ps(1.0f);
42
43  $if BATCH_TILE > 8:
44    for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
45      __m256 vx0 = _mm256_loadu_ps(x);
46      $for N in range(1, SIMD_TILE):
47        __m256 vx${N} = _mm256_loadu_ps(x + ${N * 8});
48      x += ${BATCH_TILE};
49
50      $for N in range(SIMD_TILE):
51        const __m256 vz${N} = _mm256_max_ps(vsat_cutoff, _mm256_mul_ps(vx${N}, vprescale));
52
53      $for N in range(SIMD_TILE):
54        __m256 vn${N} = _mm256_add_ps(_mm256_mul_ps(vz${N}, vlog2e), vmagic_bias);
55
56      $for N in range(SIMD_TILE):
57        const __m128 vs${N}_lo = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn${N})), 23));
58        const __m128 vs${N}_hi = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn${N}, 1)), 23));
59        vn${N} = _mm256_sub_ps(vn${N}, vmagic_bias);
60
61      $for N in range(SIMD_TILE):
62        __m256 vt${N} = _mm256_add_ps(_mm256_mul_ps(vn${N}, vminus_ln2_hi), vz${N});
63        __m256 vs${N} = _mm256_insertf128_ps(_mm256_castps128_ps256(vs${N}_lo), vs${N}_hi, 1);
64
65      $for N in range(SIMD_TILE):
66        vt${N} = _mm256_add_ps(_mm256_mul_ps(vn${N}, vminus_ln2_lo), vt${N});
67
68      $for N in range(SIMD_TILE):
69        __m256 vp${N} = _mm256_add_ps(_mm256_mul_ps(vc6, vt${N}), vc5);
70
71      $for N in range(SIMD_TILE):
72        vp${N} = _mm256_add_ps(_mm256_mul_ps(vp${N}, vt${N}), vc4);
73
74      $for N in range(SIMD_TILE):
75        vp${N} = _mm256_add_ps(_mm256_mul_ps(vp${N}, vt${N}), vc3);
76
77      $for N in range(SIMD_TILE):
78        vp${N} = _mm256_add_ps(_mm256_mul_ps(vp${N}, vt${N}), vc2);
79
80      $for N in range(SIMD_TILE):
81        vp${N} = _mm256_mul_ps(vp${N}, vt${N});
82
83      $for N in range(SIMD_TILE):
84        vt${N} = _mm256_mul_ps(vt${N}, vs${N});
85        vs${N} = _mm256_sub_ps(vs${N}, vone);
86
87      $for N in range(SIMD_TILE):
88        vp${N} = _mm256_add_ps(_mm256_mul_ps(vp${N}, vt${N}), vt${N});
89
90      $for N in range(SIMD_TILE):
91        const __m256 ve${N} = _mm256_mul_ps(_mm256_add_ps(vp${N}, vs${N}), valpha);
92        vx${N} = _mm256_mul_ps(vx${N}, vbeta);
93
94      $for N in range(SIMD_TILE):
95        const __m256 vy${N} = _mm256_blendv_ps(vx${N}, ve${N}, vx${N});
96
97      _mm256_storeu_ps(y, vy0);
98      $for N in range(1, SIMD_TILE):
99        _mm256_storeu_ps(y + ${N * 8}, vy${N});
100      y += ${BATCH_TILE};
101    }
102  for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
103    __m256 vx = _mm256_loadu_ps(x);
104    x += 8;
105
106    const __m256 vz = _mm256_max_ps(vsat_cutoff, _mm256_mul_ps(vx, vprescale));
107
108    __m256 vn = _mm256_add_ps(_mm256_mul_ps(vz, vlog2e), vmagic_bias);
109    const __m128 vs_lo = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn)), 23));
110    const __m128 vs_hi = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn, 1)), 23));
111    vn = _mm256_sub_ps(vn, vmagic_bias);
112
113    __m256 vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_hi), vz);
114    __m256 vs = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo), vs_hi, 1);
115    vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_lo), vt);
116
117    __m256 vp = _mm256_add_ps(_mm256_mul_ps(vc6, vt), vc5);
118    vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc4);
119    vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc3);
120    vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc2);
121    vp = _mm256_mul_ps(vp, vt);
122
123    vt = _mm256_mul_ps(vt, vs);
124    vs = _mm256_sub_ps(vs, vone);
125    vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vt);
126
127    const __m256 ve = _mm256_mul_ps(_mm256_add_ps(vp, vs), valpha);
128    vx = _mm256_mul_ps(vx, vbeta);
129    const __m256 vy = _mm256_blendv_ps(vx, ve, vx);
130
131    _mm256_storeu_ps(y, vy);
132    y += 8;
133  }
134  if XNN_UNLIKELY(n != 0) {
135    assert(n >= 1 * sizeof(float));
136    assert(n <= 7 * sizeof(float));
137    __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - n));
138
139    __m256 vx = _mm256_maskload_ps(x, vmask);
140
141    const __m256 vz = _mm256_max_ps(vsat_cutoff, _mm256_mul_ps(vx, vprescale));
142
143    __m256 vn = _mm256_add_ps(_mm256_mul_ps(vz, vlog2e), vmagic_bias);
144    const __m128 vs_lo = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_castps256_ps128(vn)), 23));
145    const __m128 vs_hi = _mm_castsi128_ps(_mm_slli_epi32(_mm_castps_si128(_mm256_extractf128_ps(vn, 1)), 23));
146    vn = _mm256_sub_ps(vn, vmagic_bias);
147
148    __m256 vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_hi), vz);
149    __m256 vs = _mm256_insertf128_ps(_mm256_castps128_ps256(vs_lo), vs_hi, 1);
150    vt = _mm256_add_ps(_mm256_mul_ps(vn, vminus_ln2_lo), vt);
151
152    __m256 vp = _mm256_add_ps(_mm256_mul_ps(vc6, vt), vc5);
153    vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc4);
154    vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc3);
155    vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vc2);
156    vp = _mm256_mul_ps(vp, vt);
157
158    vt = _mm256_mul_ps(vt, vs);
159    vs = _mm256_sub_ps(vs, vone);
160    vp = _mm256_add_ps(_mm256_mul_ps(vp, vt), vt);
161
162    const __m256 ve = _mm256_mul_ps(_mm256_add_ps(vp, vs), valpha);
163    vx = _mm256_mul_ps(vx, vbeta);
164    const __m256 vy = _mm256_blendv_ps(vx, ve, vx);
165
166    // _mm256_maskstore_ps(y, vmask, vf) could be used here, but triggers msan failures (probably an msan bug).
167    __m128 vy_lo = _mm256_castps256_ps128(vy);
168    if (n & (4 * sizeof(float))) {
169      _mm_storeu_ps(y, vy_lo);
170      vy_lo = _mm256_extractf128_ps(vy, 1);
171      y += 4;
172    }
173    if (n & (2 * sizeof(float))) {
174      _mm_storel_pi((__m64*) y, vy_lo);
175      vy_lo = _mm_movehl_ps(vy_lo, vy_lo);
176      y += 2;
177    }
178    if (n & (1 * sizeof(float))) {
179      _mm_store_ss(y, vy_lo);
180    }
181  }
182}
183