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