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}_p5_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 vlog2e = _mm512_set1_ps(0x1.715476p+0f);
31  $if RR_STEPS == 1:
32    const __m512 vminus_ln2 = _mm512_set1_ps(-0x1.62E43p-1f);
33  $else:
34    const __m512 vminus_ln2_hi = _mm512_set1_ps(-0x1.62E43p-1f);
35    const __m512 vminus_ln2_lo = _mm512_set1_ps(0x1.05C61p-29f);
36  const __m512 vc5 = _mm512_set1_ps(0x1.0F9F9Cp-7f);
37  const __m512 vc4 = _mm512_set1_ps(0x1.573A1Ap-5f);
38  const __m512 vc3 = _mm512_set1_ps(0x1.555A80p-3f);
39  const __m512 vc2 = _mm512_set1_ps(0x1.FFFDC6p-2f);
40  const __m512 vc1 = _mm512_set1_ps(0x1.FFFFF6p-1f);
41  const __m512 vone = _mm512_set1_ps(1.0f);
42
43  $if BATCH_TILE > 16:
44    for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
45      const __m512 vx${ABC[0]} = _mm512_loadu_ps(x);
46      $for N in range(1, SIMD_TILE):
47        const __m512 vx${ABC[N]} = _mm512_loadu_ps(x + ${N * 16});
48      x += ${BATCH_TILE};
49
50      $for N in range(SIMD_TILE):
51        const __m512 vz${ABC[N]} = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx${ABC[N]}), vsign_mask));
52
53      $for N in range(SIMD_TILE):
54        __m512 vn${ABC[N]} = _mm512_mul_ps(vz${ABC[N]}, vlog2e);
55
56      $for N in range(SIMD_TILE):
57        vn${ABC[N]} = _mm512_roundscale_ps(vn${ABC[N]}, 0);
58
59      $if RR_STEPS == 1:
60        $for N in range(SIMD_TILE):
61          __m512 vt${ABC[N]} = _mm512_fmadd_ps(vn${ABC[N]}, vminus_ln2, vz${ABC[N]});
62      $else:
63        $for N in range(SIMD_TILE):
64          __m512 vt${ABC[N]} = _mm512_fmadd_ps(vn${ABC[N]}, vminus_ln2_hi, vz${ABC[N]});
65
66        $for N in range(SIMD_TILE):
67          vt${ABC[N]} = _mm512_fmadd_ps(vn${ABC[N]}, vminus_ln2_lo, vt${ABC[N]});
68
69      $for N in range(SIMD_TILE):
70        __m512 vp${ABC[N]} = _mm512_fmadd_ps(vc5, vt${ABC[N]}, vc4);
71
72      $for N in range(SIMD_TILE):
73        vp${ABC[N]} = _mm512_fmadd_ps(vp${ABC[N]}, vt${ABC[N]}, vc3);
74
75      $for N in range(SIMD_TILE):
76        vp${ABC[N]} = _mm512_fmadd_ps(vp${ABC[N]}, vt${ABC[N]}, vc2);
77
78      $for N in range(SIMD_TILE):
79        vp${ABC[N]} = _mm512_fmadd_ps(vp${ABC[N]}, vt${ABC[N]}, vc1);
80
81      $for N in range(SIMD_TILE):
82        vp${ABC[N]} = _mm512_fmadd_ps(vp${ABC[N]}, vt${ABC[N]}, vone);
83
84      $for N in range(SIMD_TILE):
85        const __m512 ve${ABC[N]} = _mm512_scalef_ps(vp${ABC[N]}, vn${ABC[N]});
86
87      $for N in range(SIMD_TILE):
88        const __m512 vd${ABC[N]} = _mm512_add_ps(ve${ABC[N]}, vone);
89
90      $if DIV_ALGO == "div":
91        $for N in range(SIMD_TILE):
92          __m512 vf${ABC[N]} = _mm512_div_ps(ve${ABC[N]}, vd${ABC[N]});
93      $else:
94        $for N in range(SIMD_TILE):
95          __m512 vr${ABC[N]} = _mm512_rcp14_ps(vd${ABC[N]});
96
97        $for N in range(SIMD_TILE):
98          vr${ABC[N]} = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr${ABC[N]}, vd${ABC[N]}, vone), vr${ABC[N]}, vr${ABC[N]});
99
100        $for N in range(SIMD_TILE):
101          __m512 vf${ABC[N]} = _mm512_mul_ps(ve${ABC[N]}, vr${ABC[N]});
102
103        $if DIV_ALGO == "nr1fma1adj":
104          $for N in range(SIMD_TILE):
105            vf${ABC[N]} = _mm512_fmadd_ps(_mm512_fnmadd_ps(vf${ABC[N]}, vd${ABC[N]}, ve${ABC[N]}), vr${ABC[N]}, vf${ABC[N]});
106
107      $for N in range(SIMD_TILE):
108        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]});
109
110      _mm512_storeu_ps(y, vf${ABC[0]});
111      $for N in range(1, SIMD_TILE):
112        _mm512_storeu_ps(y + ${N * 16}, vf${ABC[N]});
113      y += ${BATCH_TILE};
114    }
115  for (; n >= 16 * sizeof(float); n -= 16 * sizeof(float)) {
116    const __m512 vx = _mm512_loadu_ps(x);
117    x += 16;
118
119    const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask));
120
121    const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vz, vlog2e), 0);
122
123    $if RR_STEPS == 1:
124      __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz);
125    $else:
126      __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vz);
127      vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt);
128
129    __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4);
130    vp = _mm512_fmadd_ps(vp, vt, vc3);
131    vp = _mm512_fmadd_ps(vp, vt, vc2);
132    vp = _mm512_fmadd_ps(vp, vt, vc1);
133    vp = _mm512_fmadd_ps(vp, vt, vone);
134
135    const __m512 ve = _mm512_scalef_ps(vp, vn);
136    const __m512 vd = _mm512_add_ps(ve, vone);
137
138    $if DIV_ALGO == "div":
139      __m512 vf = _mm512_div_ps(ve, vd);
140    $else:
141      __m512 vr = _mm512_rcp14_ps(vd);
142      vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr);
143
144      __m512 vf = _mm512_mul_ps(ve, vr);
145      $if DIV_ALGO == "nr1fma1adj":
146        vf = _mm512_fmadd_ps(_mm512_fnmadd_ps(vf, vd, ve), vr, vf);
147
148    vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf);
149
150    _mm512_storeu_ps(y, vf);
151    y += 16;
152  }
153  if XNN_UNLIKELY(n != 0) {
154    assert(n >= 1 * sizeof(float));
155    assert(n <= 15 * sizeof(float));
156
157    // Prepare mask for valid 32-bit elements (depends on n).
158    n >>= 2 /* log2(sizeof(float)) */;
159    const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << n) - UINT32_C(1)));
160
161    const __m512 vx = _mm512_maskz_loadu_ps(vmask, x);
162    const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask));
163
164    const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vz, vlog2e), 0);
165
166    $if RR_STEPS == 1:
167      __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz);
168    $else:
169      __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vz);
170      vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt);
171
172    __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4);
173    vp = _mm512_fmadd_ps(vp, vt, vc3);
174    vp = _mm512_fmadd_ps(vp, vt, vc2);
175    vp = _mm512_fmadd_ps(vp, vt, vc1);
176    vp = _mm512_fmadd_ps(vp, vt, vone);
177
178    const __m512 ve = _mm512_scalef_ps(vp, vn);
179    const __m512 vd = _mm512_add_ps(ve, vone);
180
181    $if DIV_ALGO == "div":
182      __m512 vf = _mm512_div_ps(ve, vd);
183    $else:
184      __m512 vr = _mm512_rcp14_ps(vd);
185      vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr);
186
187      __m512 vf = _mm512_mul_ps(ve, vr);
188      $if DIV_ALGO == "nr1fma1adj":
189        vf = _mm512_fmadd_ps(_mm512_fnmadd_ps(vf, vd, ve), vr, vf);
190
191    vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf);
192
193    _mm512_mask_storeu_ps(y, vmask, vf);
194  }
195}
196