• 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 <wasm_simd128.h>
10 
11 #include <xnnpack/common.h>
12 #include <xnnpack/math-stubs.h>
13 
14 
xnn_math_f32_sigmoid__wasmsimd_rr2_p5_div(size_t n,const float * input,float * output)15 void xnn_math_f32_sigmoid__wasmsimd_rr2_p5_div(
16     size_t n,
17     const float* input,
18     float* output)
19 {
20   assert(n % (4 * sizeof(float)) == 0);
21 
22   // Large number such that ulp(magic bias) == 1 and magic bias === 127 mod 2**22.
23   const v128_t vmagic_bias = wasm_f32x4_splat(0x1.8000FEp23f);
24   const v128_t vminus_log2e = wasm_f32x4_splat(-0x1.715476p+0f);
25   // Last 7 bits are zeroes
26   const v128_t vln2_hi = wasm_f32x4_splat(0x1.62E400p-1f);
27   const v128_t vln2_lo = wasm_f32x4_splat(0x1.7F7D1Cp-20f);
28   // Coefficient of polynomial approximation of
29   // exp(-t) ~ 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) on [-log(2)/2, log(2)/2]
30   const v128_t vc5 = wasm_f32x4_splat(-0x1.0F9F9Cp-7f);
31   const v128_t vc4 = wasm_f32x4_splat( 0x1.573A1Ap-5f);
32   const v128_t vc3 = wasm_f32x4_splat(-0x1.555A80p-3f);
33   const v128_t vc2 = wasm_f32x4_splat( 0x1.FFFDC6p-2f);
34   const v128_t vc1 = wasm_f32x4_splat(-0x1.FFFFF6p-1f);
35   const v128_t vone = wasm_f32x4_splat(1.0f);
36   // The largest z for which sigmoidf(-z) is normalized.
37   // This number is also the largest z for which expf(-z) is normalized.
38   const v128_t vdenorm_cutoff = wasm_f32x4_splat(0x1.5D589Ep+6f);
39 
40   for (; n != 0; n -= 4 * sizeof(float)) {
41     const v128_t vx = wasm_v128_load(input);
42     input += 4;
43 
44     // General structure of the algorithm:
45     //
46     //           / exp(x) / (1 + exp(x)) if x <= 0
47     //   f[x] :=
48     //           \ 1 - f[-x] if x >= 0
49     //
50     // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x),
51     // then replace result with 1 - f[-z] if x >= 0.
52     const v128_t vz = wasm_f32x4_abs(vx);
53 
54     // Compute reduced argument n := round(-z / log(2)).
55     // We do it by adding a large number (magic bias), which cause rounding of the result to integer, then subtracing
56     // the large number back. The trick with adding large number is valid only within certain bounds
57     // (|-z / log(2)| <= 2**22, i.e. |z| <= 0x1.62E43p+21 = 2907270.0), but that is acceptable, because inputs x
58     // outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup
59     // the result for such inputs at the very end of the algorithm.
60     v128_t vn = wasm_f32x4_add(vmagic_bias, wasm_f32x4_mul(vz, vminus_log2e));
61 
62     // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
63     // -87.336544 <= -z <= 0.0, and -126 <= n <= 0 accordingly.
64     const v128_t vs = wasm_i32x4_shl(vn, 23);
65 
66     // Subtract the large number back to get the final n := round(-z / log(2)) as a floating-point number.
67     vn = wasm_f32x4_sub(vn, vmagic_bias);
68 
69     // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2).
70     // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
71     v128_t vt = wasm_f32x4_add(vz, wasm_f32x4_mul(vn, vln2_hi));
72     vt = wasm_f32x4_add(vt, wasm_f32x4_mul(vn, vln2_lo));
73 
74     // Compute degree-5 polynomial approximation for exp(-t) on [-log(2)/2, log(2)/2]:
75     //   P(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) = 1 + t * p
76     v128_t vp = wasm_f32x4_add(vc4, wasm_f32x4_mul(vt, vc5));
77     vp = wasm_f32x4_add(vc3, wasm_f32x4_mul(vt, vp));
78     vp = wasm_f32x4_add(vc2, wasm_f32x4_mul(vt, vp));
79     vp = wasm_f32x4_add(vc1, wasm_f32x4_mul(vt, vp));
80 
81     // Reconstruct the exp(-z) value:
82     //   e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
83     //     = s * (1 + t * p)
84     //     = s + (t * s) * p
85     vt = wasm_f32x4_mul(vt, vs);
86     const v128_t ve = wasm_f32x4_add(vs, wasm_f32x4_mul(vt, vp));
87 
88     // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z))
89     v128_t vf = wasm_f32x4_div(ve, wasm_f32x4_add(ve, vone));
90 
91     // For inputs below denormal cutoff, replace output with +0.0f.
92     // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
93     vf = wasm_v128_andnot(vf, wasm_f32x4_gt(vz, vdenorm_cutoff));
94 
95     // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z)
96     vf = wasm_v128_bitselect(vf, wasm_f32x4_sub(vone, vf), wasm_i32x4_shr(vx, 31));
97 
98     wasm_v128_store(output, vf);
99     output += 4;
100   }
101 }
102