1 /* Copyright (c) 2008-2011 Octasic Inc.
2 2012-2017 Jean-Marc Valin */
3 /*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
19 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifndef THIRD_PARTY_RNNOISE_SRC_RNN_ACTIVATIONS_H_
29 #define THIRD_PARTY_RNNOISE_SRC_RNN_ACTIVATIONS_H_
30
31 #include <cmath>
32
33 namespace rnnoise {
34
TansigApproximated(float x)35 inline float TansigApproximated(float x) {
36 static constexpr float kTansigTable[201] = {
37 0.000000f, 0.039979f, 0.079830f, 0.119427f, 0.158649f, 0.197375f,
38 0.235496f, 0.272905f, 0.309507f, 0.345214f, 0.379949f, 0.413644f,
39 0.446244f, 0.477700f, 0.507977f, 0.537050f, 0.564900f, 0.591519f,
40 0.616909f, 0.641077f, 0.664037f, 0.685809f, 0.706419f, 0.725897f,
41 0.744277f, 0.761594f, 0.777888f, 0.793199f, 0.807569f, 0.821040f,
42 0.833655f, 0.845456f, 0.856485f, 0.866784f, 0.876393f, 0.885352f,
43 0.893698f, 0.901468f, 0.908698f, 0.915420f, 0.921669f, 0.927473f,
44 0.932862f, 0.937863f, 0.942503f, 0.946806f, 0.950795f, 0.954492f,
45 0.957917f, 0.961090f, 0.964028f, 0.966747f, 0.969265f, 0.971594f,
46 0.973749f, 0.975743f, 0.977587f, 0.979293f, 0.980869f, 0.982327f,
47 0.983675f, 0.984921f, 0.986072f, 0.987136f, 0.988119f, 0.989027f,
48 0.989867f, 0.990642f, 0.991359f, 0.992020f, 0.992631f, 0.993196f,
49 0.993718f, 0.994199f, 0.994644f, 0.995055f, 0.995434f, 0.995784f,
50 0.996108f, 0.996407f, 0.996682f, 0.996937f, 0.997172f, 0.997389f,
51 0.997590f, 0.997775f, 0.997946f, 0.998104f, 0.998249f, 0.998384f,
52 0.998508f, 0.998623f, 0.998728f, 0.998826f, 0.998916f, 0.999000f,
53 0.999076f, 0.999147f, 0.999213f, 0.999273f, 0.999329f, 0.999381f,
54 0.999428f, 0.999472f, 0.999513f, 0.999550f, 0.999585f, 0.999617f,
55 0.999646f, 0.999673f, 0.999699f, 0.999722f, 0.999743f, 0.999763f,
56 0.999781f, 0.999798f, 0.999813f, 0.999828f, 0.999841f, 0.999853f,
57 0.999865f, 0.999875f, 0.999885f, 0.999893f, 0.999902f, 0.999909f,
58 0.999916f, 0.999923f, 0.999929f, 0.999934f, 0.999939f, 0.999944f,
59 0.999948f, 0.999952f, 0.999956f, 0.999959f, 0.999962f, 0.999965f,
60 0.999968f, 0.999970f, 0.999973f, 0.999975f, 0.999977f, 0.999978f,
61 0.999980f, 0.999982f, 0.999983f, 0.999984f, 0.999986f, 0.999987f,
62 0.999988f, 0.999989f, 0.999990f, 0.999990f, 0.999991f, 0.999992f,
63 0.999992f, 0.999993f, 0.999994f, 0.999994f, 0.999994f, 0.999995f,
64 0.999995f, 0.999996f, 0.999996f, 0.999996f, 0.999997f, 0.999997f,
65 0.999997f, 0.999997f, 0.999997f, 0.999998f, 0.999998f, 0.999998f,
66 0.999998f, 0.999998f, 0.999998f, 0.999999f, 0.999999f, 0.999999f,
67 0.999999f, 0.999999f, 0.999999f, 0.999999f, 0.999999f, 0.999999f,
68 0.999999f, 0.999999f, 0.999999f, 0.999999f, 1.000000f, 1.000000f,
69 1.000000f, 1.000000f, 1.000000f, 1.000000f, 1.000000f, 1.000000f,
70 1.000000f, 1.000000f, 1.000000f,
71 };
72
73 // Tests are reversed to catch NaNs.
74 if (!(x < 8.f))
75 return 1.f;
76 if (!(x > -8.f))
77 return -1.f;
78 float sign = 1.f;
79 if (x < 0.f) {
80 x = -x;
81 sign = -1.f;
82 }
83 // Look-up.
84 int i = static_cast<int>(std::floor(0.5f + 25 * x));
85 float y = kTansigTable[i];
86 // Map i back to x's scale (undo 25 factor).
87 x -= 0.04f * i;
88 y = y + x * (1.f - y * y) * (1.f - y * x);
89 return sign * y;
90 }
91
SigmoidApproximated(const float x)92 inline float SigmoidApproximated(const float x) {
93 return 0.5f + 0.5f * TansigApproximated(0.5f * x);
94 }
95
RectifiedLinearUnit(const float x)96 inline float RectifiedLinearUnit(const float x) {
97 return x < 0.f ? 0.f : x;
98 }
99
100 } // namespace rnnoise
101
102 #endif // THIRD_PARTY_RNNOISE_SRC_RNN_ACTIVATIONS_H_
103