1 // Auto-generated file. Do not edit!
2 // Template: src/f32-sigmoid/scalar-lut2048-p1-div.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2019 Google LLC
6 //
7 // This source code is licensed under the BSD-style license found in the
8 // LICENSE file in the root directory of this source tree.
9
10 #include <assert.h>
11 #include <math.h>
12
13 #include <xnnpack/common.h>
14 #include <xnnpack/vunary.h>
15
16 #include <fp16/bitcasts.h>
17
18
19 // Note redefine as uint32[] to avoid redundant bitcasts.
20 extern XNN_INTERNAL const uint32_t xnn_table_exp2_k_over_2048[2048];
21
xnn_f32_sigmoid_ukernel__scalar_lut2048_p1_div_x1(size_t n,const float * x,float * y,const void * params)22 void xnn_f32_sigmoid_ukernel__scalar_lut2048_p1_div_x1(
23 size_t n,
24 const float* x,
25 float* y,
26 const void* params)
27 {
28 assert(n % sizeof(float) == 0);
29
30 const float vmagic_bias = 0x1.800000p23f;
31 // The largest z for which sigmoidf(-z) is normalized.
32 // This number is also the largest z for which expf(-z) is normalized.
33 const float vdenorm_cutoff = 0x1.5D589Ep+6f;
34 const float vminus_log2e_x2048 = -0x1.715476p11f;
35 // Last 18 bits are zeroes
36 const float vln2_o2048_hi = 0x1.600000p-12f;
37 const float vln2_o2048_lo = 0x1.7217F8p-19f;
38 const float vone = 1.0f;
39
40 const float vc1 = -0x1.FFFFFEp-1f;
41
42 const uint32_t vindex_mask = UINT32_C(0x7FF);
43
44 do {
45 const float vx = *x++;
46
47 // General structure of the algorithm:
48 // / exp(x) / (1 + exp(x)) if x <= 0
49 // f[x] :=
50 // \ 1 - f[-x] if x >= 0
51 //
52 // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x),
53 // then replace result with 1 - f[-z] if x >= 0.
54 const float vz = fabsf(vx);
55
56 // Compute reduced argument n := round(-z * 2048 / log(2)).
57 // We do it by adding a large number (magic bias), which cause rounding of the result to an integer, then subtracing
58 // the large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
59 // The trick with adding large number is valid only within certain bounds (|z * 2048 / log(2)| <= 2**22, i.e.
60 // |z| <= 0x1.62E43p+10 = 1419.5654296875), but that is acceptable, because inputs x outside of
61 // [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup the result
62 // for such inputs at the very end of the algorithm.
63 float vn = vz * vminus_log2e_x2048 + vmagic_bias;
64
65 // Create a floating-point number s (scale) such that s := 2**(n / 2048) for such inputs that sigmoidf(-z) is
66 // normalized, i.e. 0 <= z <= 87.33642. As n has 11 fractional bits, we split s == 2**(n / 2048) =
67 // = 2**e * 2**(n / 2048 - e), where e := int(n / 2048). We create s in two steps:
68 // 1. Fetch 2**(n / 2048 - e) = 2**(n % 2048) from table using the 6 low bits of n, as integer.
69 // Note that the fetched values are in the [1.0, 2.0) range, i.e. their floating-point exponent is 0.
70 // 2. Adjust fecthed value by addition of e to its floating-point exponent. The result is always a normalized
71 // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(-z) is normalized) we have -126 <= e <= 0,
72 // and thus the adjusted exponent is not lower than -126.
73 //
74 // Extract e from bits 11:19 of n and shift it into bits 23:31 (position of floating-point exponent).
75 const uint32_t ve = (fp32_to_bits(vn) & ~vindex_mask) << 12;
76
77 // Use bits 0:11 bits of n, as integer, as an index for table lookup of l := 2**(n % 2048).
78 const uint32_t vidx = fp32_to_bits(vn) & vindex_mask;
79 // Adjust exponent of the value l fetched from the table to get the final s value.
80 const float vs = fp32_from_bits(xnn_table_exp2_k_over_2048[vidx] + ve);
81
82 // Subtract the large number back to get the final n := round(-z * 2048 / log(2)) as a floating-point number.
83 vn -= vmagic_bias;
84
85 // Compute reduced argument t := (z + n * log(2) / 2048). Note that -t = -z - n * log(2) / 2048.
86 // Use Cody-Waite range reduction method (note two constants to represent log(2) / 2048) to improve accuracy.
87 float vt = vn * vln2_o2048_hi + vz;
88 vt = vn * vln2_o2048_lo + vt;
89
90 // Compute degree-1 polynomial approximation for exp(-t) on [-log(2)/4096, log(2)/4096]:
91 // P1(t) = 1 + t * c1
92 const float vp = vt * vc1;
93
94 // Reconstruct the exp(-z) value:
95 // y = s * (1 + t * c1)
96 // = s + s * (t * c1))
97 // = s + s * p
98 const float vy = vp * vs + vs;
99
100 // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z))
101 float vf = vy / (vy + vone);
102
103 // For inputs above denormal cutoff, replace output with +0.0f.
104 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
105 if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) {
106 vf = 0.0f;
107 }
108
109 // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z)
110 if XNN_UNPREDICTABLE(vx > 0.0f) {
111 vf = vone - vf;
112 }
113
114 *y++ = vf;
115
116 n -= sizeof(float);
117 } while (n != 0);
118 }
119