xref: /aosp_15_r20/external/XNNPACK/src/f32-vsigmoid/avx512f-rr1-lut16-p3-perm-scalef.c.in (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
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 DIV_ALGO in ["div", "nr1fma", "nr1fma1adj"]
9$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
10$SIMD_TILE = BATCH_TILE // 16
11#include <assert.h>
12
13#include <immintrin.h>
14
15#include <xnnpack/common.h>
16#include <xnnpack/intrinsics-polyfill.h>
17#include <xnnpack/vunary.h>
18
19
20void xnn_f32_vsigmoid_ukernel__avx512f_rr1_lut16_p3_perm_scalef_${DIV_ALGO}_x${BATCH_TILE}(
21    size_t n,
22    const float* x,
23    float* y,
24    const union xnn_f32_sigmoid_params params[restrict XNN_MIN_ELEMENTS(1)])
25{
26  assert(n % sizeof(float) == 0);
27
28  const __m512i vsign_mask = _mm512_set1_epi32((int) params->avx512_rr1_lut16_p3.sign_mask);
29  const __m512 vmagic_bias = _mm512_set1_ps(params->avx512_rr1_lut16_p3.magic_bias);
30  const __m512 vlog2e = _mm512_set1_ps(params->avx512_rr1_lut16_p3.log2e);
31  const __m512 vtable = _mm512_load_ps(params->avx512_rr1_lut16_p3.table);
32  const __m512 vminus_ln2 = _mm512_set1_ps(params->avx512_rr1_lut16_p3.minus_ln2);
33  const __m512 vc3 = _mm512_set1_ps(params->avx512_rr1_lut16_p3.c3);
34  const __m512 vc2 = _mm512_set1_ps(params->avx512_rr1_lut16_p3.c2);
35  const __m512 vone = _mm512_set1_ps(params->avx512_rr1_lut16_p3.one);
36
37  $if BATCH_TILE > 16:
38    for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
39      const __m512 vx${ABC[0]} = _mm512_loadu_ps(x);
40      $for N in range(1, SIMD_TILE):
41        const __m512 vx${ABC[N]} = _mm512_loadu_ps(x + ${N * 16});
42      x += ${BATCH_TILE};
43
44      $for N in range(SIMD_TILE):
45        const __m512 vz${ABC[N]} = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx${ABC[N]}), vsign_mask));
46
47      $for N in range(SIMD_TILE):
48        __m512 vn${ABC[N]} = _mm512_fmadd_ps(vz${ABC[N]}, vlog2e, vmagic_bias);
49
50      $for N in range(SIMD_TILE):
51        const __m512 vl${ABC[N]} = _mm512_permutexvar_ps(_mm512_castps_si512(vn${ABC[N]}), vtable);
52
53      $for N in range(SIMD_TILE):
54        vn${ABC[N]} = _mm512_sub_ps(vn${ABC[N]}, vmagic_bias);
55
56      $for N in range(SIMD_TILE):
57        __m512 vt${ABC[N]} = _mm512_fmadd_ps(vn${ABC[N]}, vminus_ln2, vz${ABC[N]});
58
59      $for N in range(SIMD_TILE):
60        __m512 vp${ABC[N]} = _mm512_fmadd_ps(vt${ABC[N]}, vc3, vc2);
61
62      $for N in range(SIMD_TILE):
63        vp${ABC[N]} = _mm512_mul_ps(vp${ABC[N]}, vt${ABC[N]});
64
65      $for N in range(SIMD_TILE):
66        vp${ABC[N]} = _mm512_fmadd_ps(vt${ABC[N]}, vp${ABC[N]}, vt${ABC[N]});
67
68      $for N in range(SIMD_TILE):
69        vp${ABC[N]} = _mm512_fmadd_ps(vl${ABC[N]}, vp${ABC[N]}, vl${ABC[N]});
70
71      $for N in range(SIMD_TILE):
72        const __m512 ve${ABC[N]} = _mm512_scalef_ps(vp${ABC[N]}, vn${ABC[N]});
73
74      $for N in range(SIMD_TILE):
75        const __m512 vd${ABC[N]} = _mm512_add_ps(ve${ABC[N]}, vone);
76
77      $if DIV_ALGO == "div":
78        $for N in range(SIMD_TILE):
79          __m512 vf${ABC[N]} = _mm512_div_ps(ve${ABC[N]}, vd${ABC[N]});
80      $else:
81        $for N in range(SIMD_TILE):
82          __m512 vr${ABC[N]} = _mm512_rcp14_ps(vd${ABC[N]});
83
84        $for N in range(SIMD_TILE):
85          vr${ABC[N]} = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr${ABC[N]}, vd${ABC[N]}, vone), vr${ABC[N]}, vr${ABC[N]});
86
87        $for N in range(SIMD_TILE):
88          __m512 vf${ABC[N]} = _mm512_mul_ps(ve${ABC[N]}, vr${ABC[N]});
89
90        $if DIV_ALGO == "nr1fma1adj":
91          $for N in range(SIMD_TILE):
92            vf${ABC[N]} = _mm512_fmadd_ps(_mm512_fnmadd_ps(vf${ABC[N]}, vd${ABC[N]}, ve${ABC[N]}), vr${ABC[N]}, vf${ABC[N]});
93
94      $for N in range(SIMD_TILE):
95        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]});
96
97      _mm512_storeu_ps(y, vf${ABC[0]});
98      $for N in range(1, SIMD_TILE):
99        _mm512_storeu_ps(y + ${N * 16}, vf${ABC[N]});
100      y += ${BATCH_TILE};
101    }
102  for (; n >= 16 * sizeof(float); n -= 16 * sizeof(float)) {
103    const __m512 vx = _mm512_loadu_ps(x);
104    x += 16;
105
106    const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask));
107
108    __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias);
109    const __m512 vl = _mm512_permutexvar_ps(_mm512_castps_si512(vn), vtable);
110    vn = _mm512_sub_ps(vn, vmagic_bias);
111
112    __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz);
113
114    __m512 vp = _mm512_fmadd_ps(vt, vc3, vc2);
115    vp = _mm512_mul_ps(vp, vt);
116    vp = _mm512_fmadd_ps(vt, vp, vt);
117    vp = _mm512_fmadd_ps(vl, vp, vl);
118
119    const __m512 ve = _mm512_scalef_ps(vp, vn);
120    const __m512 vd = _mm512_add_ps(ve, vone);
121
122    $if DIV_ALGO == "div":
123      __m512 vf = _mm512_div_ps(ve, vd);
124    $else:
125      __m512 vr = _mm512_rcp14_ps(vd);
126      vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr);
127
128      __m512 vf = _mm512_mul_ps(ve, vr);
129      $if DIV_ALGO == "nr1fma1adj":
130        vf = _mm512_fmadd_ps(_mm512_fnmadd_ps(vf, vd, ve), vr, vf);
131
132    vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf);
133
134    _mm512_storeu_ps(y, vf);
135    y += 16;
136  }
137  if XNN_UNLIKELY(n != 0) {
138    assert(n >= 1 * sizeof(float));
139    assert(n <= 15 * sizeof(float));
140
141    // Prepare mask for valid 32-bit elements (depends on n).
142    n >>= 2 /* log2(sizeof(float)) */;
143    const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << n) - UINT32_C(1)));
144
145    const __m512 vx = _mm512_maskz_loadu_ps(vmask, x);
146    const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask));
147
148    __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias);
149    const __m512 vl = _mm512_permutexvar_ps(_mm512_castps_si512(vn), vtable);
150    vn = _mm512_sub_ps(vn, vmagic_bias);
151
152    __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz);
153
154    __m512 vp = _mm512_fmadd_ps(vt, vc3, vc2);
155    vp = _mm512_mul_ps(vp, vt);
156    vp = _mm512_fmadd_ps(vt, vp, vt);
157    vp = _mm512_fmadd_ps(vl, vp, vl);
158
159    const __m512 ve = _mm512_scalef_ps(vp, vn);
160    const __m512 vd = _mm512_add_ps(ve, vone);
161
162    $if DIV_ALGO == "div":
163      __m512 vf = _mm512_div_ps(ve, vd);
164    $else:
165      __m512 vr = _mm512_rcp14_ps(vd);
166      vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr);
167
168      __m512 vf = _mm512_mul_ps(ve, vr);
169      $if DIV_ALGO == "nr1fma1adj":
170        vf = _mm512_fmadd_ps(_mm512_fnmadd_ps(vf, vd, ve), vr, vf);
171
172    vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf);
173
174    _mm512_mask_storeu_ps(y, vmask, vf);
175  }
176}
177