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/common.h>
12 #include <xnnpack/math-stubs.h>
13
14
15 // Table of exp2(k / 64) values, k = 0..63
16 extern XNN_INTERNAL const float xnn_table_exp2_k_over_64[64];
17
xnn_math_f32_sigmoid__avx512f_rr2_lut64_p2_gather_scalef_div(size_t n,const float * input,float * output)18 void xnn_math_f32_sigmoid__avx512f_rr2_lut64_p2_gather_scalef_div(
19 size_t n,
20 const float* input,
21 float* output)
22 {
23 assert(n % (16 * sizeof(float)) == 0);
24
25 // Floating-point mask with only the sign bit set
26 const __m512i vsign_mask = _mm512_set1_epi32(0x80000000);
27 // Large number such that ulp(magic bias) == exp2(-6)
28 const __m512 vmagic_bias = _mm512_set1_ps(0x1.800000p17f);
29 const __m512 vlog2e = _mm512_set1_ps(0x1.715476p0f);
30 // Mask for the lowest 6 bits
31 const __m512i vindex_mask = _mm512_set1_epi32(INT32_C(0x3F));
32 const __m512 vminus_ln2_hi = _mm512_set1_ps(-0x1.62e43p-1f);
33 const __m512 vminus_ln2_lo = _mm512_set1_ps(0x1.05c61p-29f);
34 // Coefficient of polynomial approximation of exp(t) ~ 1 + t * (1 + t * c2) on [-log(2)/128, log(2)/128]
35 const __m512 vc2 = _mm512_set1_ps(0x1.FFFF0Ap-2f);
36 const __m512 vone = _mm512_set1_ps(1.0f);
37
38 for (; n != 0; n -= 16 * sizeof(float)) {
39 const __m512 vx = _mm512_loadu_ps(input);
40
41 // General structure of the algorithm:
42 //
43 // / exp(x) / (1 + exp(x)) if x <= 0
44 // f[x] :=
45 // \ 1 - f[-x] if x >= 0
46 //
47 // First we compute f[z] := exp(z) / (1 + exp(z)) where z = -abs(x), then replace result with 1 - f[z] if x >= 0.
48 const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask));
49
50 // Compute reduced argument n := round(z / log(2), 6).
51 // We do it by adding a large number (magic bias), which cause rounding of the result to 6 fractional bits, then
52 // subtracing the large number back. The addition is combined with multiplication by log2e into a single FMA
53 // instruction. The trick with adding large number is valid only within certain bounds (|z / log(2)| <= 2**16, i.e.
54 // |z| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x outside of [-87.336544, 17.328678]
55 // (i.e. z outsize [87.336544, 0]) underflow or saturate sigmoidf(x). We fixup the result for such inputs at the
56 // very end of the algorithm.
57 __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias);
58
59 // Use the low 6 bits of n (as integer) for table lookup.
60 const __m512i vidx = _mm512_and_epi32(_mm512_castps_si512(vn), vindex_mask);
61 const __m512 vl = _mm512_i32gather_ps(vidx, xnn_table_exp2_k_over_64, sizeof(float));
62
63 // Subtract the large number back to get the final n := round(z / log(2), 6) as a floating-point number.
64 vn = _mm512_sub_ps(vn, vmagic_bias);
65
66 // Compute reduced argument t := z - n * log(2).
67 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
68 __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vz);
69 vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt);
70
71 // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128].
72 // P(t) = 1 + t * (1 + t * c2) = 1 + (t + t * (t * c2))
73 // p = l * P(t)
74 // = l + l * (t + t * (t * c2))
75 __m512 vp = _mm512_mul_ps(vt, vc2);
76 vp = _mm512_fmadd_ps(vt, vp, vt);
77 vp = _mm512_fmadd_ps(vl, vp, vl);
78
79 // Reconstruct the exp(z) value: e = exp2(floor(n)) * p.
80 const __m512 ve = _mm512_scalef_ps(vp, vn);
81
82 // Denominator of the sigmoid fraction: 1.0 + exp(z)
83 const __m512 vd = _mm512_add_ps(ve, vone);
84
85 // Reconstruct sigmoid(z) = exp(z) / (1.0 + exp(z))
86 __m512 vf = _mm512_div_ps(ve, vd);
87
88 // Reconstruct sigmoid(x) = x < 0 ? sigmoid(z) : 1.0 - sigmoid(z)
89 vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf);
90
91 _mm512_storeu_ps(output, vf);
92
93 input += 16;
94 output += 16;
95 }
96 }
97