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 #include <assert.h>
7 #include <stddef.h>
8
9 #include <immintrin.h>
10
11 #include <xnnpack/math-stubs.h>
12
13
xnn_math_f32_sigmoid__avx512f_rr2_p5_scalef_nr1fma(size_t n,const float * input,float * output)14 void xnn_math_f32_sigmoid__avx512f_rr2_p5_scalef_nr1fma(
15 size_t n,
16 const float* input,
17 float* output)
18 {
19 assert(n % (16 * sizeof(float)) == 0);
20
21 // Floating-point mask with only the sign bit set
22 const __m512i vsign_mask = _mm512_set1_epi32(0x80000000);
23 const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f);
24 const __m512 vminus_ln2_hi = _mm512_set1_ps(-0x1.62E43p-1f);
25 const __m512 vminus_ln2_lo = _mm512_set1_ps(0x1.05C61p-29f);
26 // Coefficient of polynomial approximation of
27 // exp(t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2]
28 const __m512 vc5 = _mm512_set1_ps(0x1.0F9F9Cp-7f);
29 const __m512 vc4 = _mm512_set1_ps(0x1.573A1Ap-5f);
30 const __m512 vc3 = _mm512_set1_ps(0x1.555A80p-3f);
31 const __m512 vc2 = _mm512_set1_ps(0x1.FFFDC6p-2f);
32 const __m512 vc1 = _mm512_set1_ps(0x1.FFFFF6p-1f);
33 const __m512 vone = _mm512_set1_ps(1.0f);
34
35 for (; n != 0; n -= 16 * sizeof(float)) {
36 const __m512 vx = _mm512_loadu_ps(input);
37
38 // General structure of the algorithm:
39 //
40 // / exp(x) / (1 + exp(x)) if x <= 0
41 // f[x] :=
42 // \ 1 - f[-x] if x >= 0
43 //
44 // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0.
45 const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask));
46
47 // Compute reduced argument n := round(z / log(2)).
48 const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vz, vlog2e), 0);
49
50 // Compute reduced argument t := z - n * log(2).
51 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
52 __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vz);
53 vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt);
54
55 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
56 // P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = p
57 __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4);
58 vp = _mm512_fmadd_ps(vp, vt, vc3);
59 vp = _mm512_fmadd_ps(vp, vt, vc2);
60 vp = _mm512_fmadd_ps(vp, vt, vc1);
61 vp = _mm512_fmadd_ps(vp, vt, vone);
62
63 // Reconstruct the exp(z) value: e = exp2(n) * p.
64 const __m512 ve = _mm512_scalef_ps(vp, vn);
65
66 // Denominator of the sigmoid fraction: 1.0 + exp(z)
67 const __m512 vd = _mm512_add_ps(ve, vone);
68
69 // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator.
70 // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
71 // Thus the reciprocal of the denominator never overflows.
72 __m512 vr = _mm512_rcp14_ps(vd);
73 vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr);
74
75 // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
76 __m512 vf = _mm512_mul_ps(ve, vr);
77
78 // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
79 vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf);
80
81 _mm512_storeu_ps(output, vf);
82
83 input += 16;
84 output += 16;
85 }
86 }
87