• 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 % 16 == 0
7$assert BATCH_TILE >= 16
8$assert RR_STEPS in [1, 2]
9$assert DIV_ALGO in ["div", "nr1fma", "nr1fma1adj"]
10$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
11$SIMD_TILE = BATCH_TILE // 16
12#include <assert.h>
13
14#include <immintrin.h>
15
16#include <xnnpack/common.h>
17#include <xnnpack/intrinsics-polyfill.h>
18#include <xnnpack/vunary.h>
19
20
21void xnn_f32_sigmoid_ukernel__avx512f_rr${RR_STEPS}_lut16_p3_perm_scalef_${DIV_ALGO}_x${BATCH_TILE}(
22    size_t n,
23    const float* x,
24    float* y,
25    const void* params)
26{
27  assert(n % sizeof(float) == 0);
28
29  const __m512i vsign_mask = _mm512_set1_epi32(0x80000000);
30  const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p19f);
31  const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f);
32  const __m512 vtable = _mm512_set_ps(
33    0x1.EA4AFAp+0f, 0x1.D5818Ep+0f, 0x1.C199BEp+0f, 0x1.AE89FAp+0f,
34    0x1.9C4918p+0f, 0x1.8ACE54p+0f, 0x1.7A1148p+0f, 0x1.6A09E6p+0f,
35    0x1.5AB07Ep+0f, 0x1.4BFDAEp+0f, 0x1.3DEA64p+0f, 0x1.306FE0p+0f,
36    0x1.2387A6p+0f, 0x1.172B84p+0f, 0x1.0B5586p+0f, 0x1.000000p+0f);
37  $if RR_STEPS == 1:
38    const __m512 vminus_ln2 = _mm512_set1_ps(-0x1.62E43p-1f);
39  $else:
40    const __m512 vminus_ln2_hi = _mm512_set1_ps(-0x1.62E43p-1f);
41    const __m512 vminus_ln2_lo = _mm512_set1_ps(0x1.05C61p-29f);
42  const __m512 vc3 = _mm512_set1_ps(0x1.55559Ap-3f);
43  const __m512 vc2 = _mm512_set1_ps(0x1.00021Ep-1f);
44  const __m512 vone = _mm512_set1_ps(1.0f);
45
46  $if BATCH_TILE > 16:
47    for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
48      const __m512 vx${ABC[0]} = _mm512_loadu_ps(x);
49      $for N in range(1, SIMD_TILE):
50        const __m512 vx${ABC[N]} = _mm512_loadu_ps(x + ${N * 16});
51      x += ${BATCH_TILE};
52
53      $for N in range(SIMD_TILE):
54        const __m512 vz${ABC[N]} = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx${ABC[N]}), vsign_mask));
55
56      $for N in range(SIMD_TILE):
57        __m512 vn${ABC[N]} = _mm512_fmadd_ps(vz${ABC[N]}, vlog2e, vmagic_bias);
58
59      $for N in range(SIMD_TILE):
60        const __m512 vl${ABC[N]} = _mm512_permutexvar_ps(_mm512_castps_si512(vn${ABC[N]}), vtable);
61
62      $for N in range(SIMD_TILE):
63        vn${ABC[N]} = _mm512_sub_ps(vn${ABC[N]}, vmagic_bias);
64
65      $if RR_STEPS == 1:
66        $for N in range(SIMD_TILE):
67          __m512 vt${ABC[N]} = _mm512_fmadd_ps(vn${ABC[N]}, vminus_ln2, vz${ABC[N]});
68      $else:
69        $for N in range(SIMD_TILE):
70          __m512 vt${ABC[N]} = _mm512_fmadd_ps(vn${ABC[N]}, vminus_ln2_hi, vz${ABC[N]});
71
72        $for N in range(SIMD_TILE):
73          vt${ABC[N]} = _mm512_fmadd_ps(vn${ABC[N]}, vminus_ln2_lo, vt${ABC[N]});
74
75      $for N in range(SIMD_TILE):
76        __m512 vp${ABC[N]} = _mm512_fmadd_ps(vt${ABC[N]}, vc3, vc2);
77
78      $for N in range(SIMD_TILE):
79        vp${ABC[N]} = _mm512_mul_ps(vp${ABC[N]}, vt${ABC[N]});
80
81      $for N in range(SIMD_TILE):
82        vp${ABC[N]} = _mm512_fmadd_ps(vt${ABC[N]}, vp${ABC[N]}, vt${ABC[N]});
83
84      $for N in range(SIMD_TILE):
85        vp${ABC[N]} = _mm512_fmadd_ps(vl${ABC[N]}, vp${ABC[N]}, vl${ABC[N]});
86
87      $for N in range(SIMD_TILE):
88        const __m512 ve${ABC[N]} = _mm512_scalef_ps(vp${ABC[N]}, vn${ABC[N]});
89
90      $for N in range(SIMD_TILE):
91        const __m512 vd${ABC[N]} = _mm512_add_ps(ve${ABC[N]}, vone);
92
93      $if DIV_ALGO == "div":
94        $for N in range(SIMD_TILE):
95          __m512 vf${ABC[N]} = _mm512_div_ps(ve${ABC[N]}, vd${ABC[N]});
96      $else:
97        $for N in range(SIMD_TILE):
98          __m512 vr${ABC[N]} = _mm512_rcp14_ps(vd${ABC[N]});
99
100        $for N in range(SIMD_TILE):
101          vr${ABC[N]} = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr${ABC[N]}, vd${ABC[N]}, vone), vr${ABC[N]}, vr${ABC[N]});
102
103        $for N in range(SIMD_TILE):
104          __m512 vf${ABC[N]} = _mm512_mul_ps(ve${ABC[N]}, vr${ABC[N]});
105
106        $if DIV_ALGO == "nr1fma1adj":
107          $for N in range(SIMD_TILE):
108            vf${ABC[N]} = _mm512_fmadd_ps(_mm512_fnmadd_ps(vf${ABC[N]}, vd${ABC[N]}, ve${ABC[N]}), vr${ABC[N]}, vf${ABC[N]});
109
110      $for N in range(SIMD_TILE):
111        vf${ABC[N]} = _mm512_mask_sub_ps(vf${ABC[N]}, _mm512_testn_epi32_mask(_mm512_castps_si512(vx${ABC[N]}), vsign_mask), vone, vf${ABC[N]});
112
113      _mm512_storeu_ps(y, vf${ABC[0]});
114      $for N in range(1, SIMD_TILE):
115        _mm512_storeu_ps(y + ${N * 16}, vf${ABC[N]});
116      y += ${BATCH_TILE};
117    }
118  for (; n >= 16 * sizeof(float); n -= 16 * sizeof(float)) {
119    const __m512 vx = _mm512_loadu_ps(x);
120    x += 16;
121
122    const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask));
123
124    __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias);
125    const __m512 vl = _mm512_permutexvar_ps(_mm512_castps_si512(vn), vtable);
126    vn = _mm512_sub_ps(vn, vmagic_bias);
127
128    $if RR_STEPS == 1:
129      __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz);
130    $else:
131      __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vz);
132      vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt);
133
134    __m512 vp = _mm512_fmadd_ps(vt, vc3, vc2);
135    vp = _mm512_mul_ps(vp, vt);
136    vp = _mm512_fmadd_ps(vt, vp, vt);
137    vp = _mm512_fmadd_ps(vl, vp, vl);
138
139    const __m512 ve = _mm512_scalef_ps(vp, vn);
140    const __m512 vd = _mm512_add_ps(ve, vone);
141
142    $if DIV_ALGO == "div":
143      __m512 vf = _mm512_div_ps(ve, vd);
144    $else:
145      __m512 vr = _mm512_rcp14_ps(vd);
146      vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr);
147
148      __m512 vf = _mm512_mul_ps(ve, vr);
149      $if DIV_ALGO == "nr1fma1adj":
150        vf = _mm512_fmadd_ps(_mm512_fnmadd_ps(vf, vd, ve), vr, vf);
151
152    vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf);
153
154    _mm512_storeu_ps(y, vf);
155    y += 16;
156  }
157  if XNN_UNLIKELY(n != 0) {
158    assert(n >= 1 * sizeof(float));
159    assert(n <= 15 * sizeof(float));
160
161    // Prepare mask for valid 32-bit elements (depends on n).
162    n >>= 2 /* log2(sizeof(float)) */;
163    const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << n) - UINT32_C(1)));
164
165    const __m512 vx = _mm512_maskz_loadu_ps(vmask, x);
166    const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask));
167
168    __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias);
169    const __m512 vl = _mm512_permutexvar_ps(_mm512_castps_si512(vn), vtable);
170    vn = _mm512_sub_ps(vn, vmagic_bias);
171
172    $if RR_STEPS == 1:
173      __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz);
174    $else:
175      __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vz);
176      vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt);
177
178    __m512 vp = _mm512_fmadd_ps(vt, vc3, vc2);
179    vp = _mm512_mul_ps(vp, vt);
180    vp = _mm512_fmadd_ps(vt, vp, vt);
181    vp = _mm512_fmadd_ps(vl, vp, vl);
182
183    const __m512 ve = _mm512_scalef_ps(vp, vn);
184    const __m512 vd = _mm512_add_ps(ve, vone);
185
186    $if DIV_ALGO == "div":
187      __m512 vf = _mm512_div_ps(ve, vd);
188    $else:
189      __m512 vr = _mm512_rcp14_ps(vd);
190      vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr);
191
192      __m512 vf = _mm512_mul_ps(ve, vr);
193      $if DIV_ALGO == "nr1fma1adj":
194        vf = _mm512_fmadd_ps(_mm512_fnmadd_ps(vf, vd, ve), vr, vf);
195
196    vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf);
197
198    _mm512_mask_storeu_ps(y, vmask, vf);
199  }
200}
201