• 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 #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_rr1_p5_scalef_nr1fma(size_t n,const float * input,float * output)14 void xnn_math_f32_sigmoid__avx512f_rr1_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 = _mm512_set1_ps(-0x1.62E43p-1f);
25   // Coefficient of polynomial approximation of
26   // exp(t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2]
27   const __m512 vc5 = _mm512_set1_ps(0x1.0F9F9Cp-7f);
28   const __m512 vc4 = _mm512_set1_ps(0x1.573A1Ap-5f);
29   const __m512 vc3 = _mm512_set1_ps(0x1.555A80p-3f);
30   const __m512 vc2 = _mm512_set1_ps(0x1.FFFDC6p-2f);
31   const __m512 vc1 = _mm512_set1_ps(0x1.FFFFF6p-1f);
32   const __m512 vone = _mm512_set1_ps(1.0f);
33 
34   for (; n != 0; n -= 16 * sizeof(float)) {
35     const __m512 vx = _mm512_loadu_ps(input);
36 
37     // General structure of the algorithm:
38     //
39     //           / exp(x) / (1 + exp(x)) if x <= 0
40     //   f[x] :=
41     //           \ 1 - f[-x] if x >= 0
42     //
43     // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0.
44     const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask));
45 
46     // Compute reduced argument n := round(z / log(2)).
47     const __m512 vn = _mm512_roundscale_ps(_mm512_mul_ps(vz, vlog2e), 0);
48 
49     // Compute reduced argument t := z - n * log(2).
50     __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2, vz);
51 
52     // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
53     //   P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = p
54     __m512 vp = _mm512_fmadd_ps(vc5, vt, vc4);
55     vp = _mm512_fmadd_ps(vp, vt, vc3);
56     vp = _mm512_fmadd_ps(vp, vt, vc2);
57     vp = _mm512_fmadd_ps(vp, vt, vc1);
58     vp = _mm512_fmadd_ps(vp, vt, vone);
59 
60     // Reconstruct the exp(z) value: e = exp2(n) * p.
61     const __m512 ve = _mm512_scalef_ps(vp, vn);
62 
63     // Denominator of the sigmoid fraction: 1.0 + exp(z)
64     const __m512 vd = _mm512_add_ps(ve, vone);
65 
66     // Use Newton-Raphson method (1 iteration) to compute reciprocal of denominator.
67     // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
68     // Thus the reciprocal of the denominator never overflows.
69     __m512 vr = _mm512_rcp14_ps(vd);
70     vr = _mm512_fmadd_ps(_mm512_fnmadd_ps(vr, vd, vone), vr, vr);
71 
72     // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
73     __m512 vf = _mm512_mul_ps(ve, vr);
74 
75     // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
76     vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf);
77 
78     _mm512_storeu_ps(output, vf);
79 
80     input += 16;
81     output += 16;
82   }
83 }
84