Elffuss BraidHub

kikocis/ncnn public

master · 3.2 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#ifndef NEON_MATHFUN_TANH_H
16#define NEON_MATHFUN_TANH_H
17
18#include <arm_neon.h>
19
20#define c_tanh_tiny 1e-4f
21#define c_tanh_hi 9.0f
22// The monomial coefficients of the numerator polynomial (odd).
23#define c_tanh_alpha_1 4.89352455891786e-3f
24#define c_tanh_alpha_3 6.37261928875436e-4f
25#define c_tanh_alpha_5 1.48572235717979e-5f
26#define c_tanh_alpha_7 5.12229709037114e-8f
27#define c_tanh_alpha_9 -8.60467152213735e-11f
28#define c_tanh_alpha_11 2.00018790482477e-13f
29#define c_tanh_alpha_13 -2.76076847742355e-16f
30// The monomial coefficients of the denominator polynomial (even).
31#define c_tanh_beta_0 4.89352518554385e-3f
32#define c_tanh_beta_2 2.26843463243900e-3f
33#define c_tanh_beta_4 1.18534705686654e-4f
34#define c_tanh_beta_6 1.19825839466702e-6f
35
36/* Single precision hyperbolic tangent computed for 4 simultaneous float */
37static inline float32x4_t tanh_ps(float32x4_t x)
38{
39 float32x4_t x2 = vabsq_f32(x);
40
41 uint32x4_t tiny_mask = vcgeq_f32(x2, vdupq_n_f32(c_tanh_tiny));
42
43 // clamp the inputs to the range [-9, 9] since anything outside
44 // this range is -/+1.0f in single-precision.
45 x2 = vreinterpretq_f32_u32(vbslq_u32(vcgeq_f32(vdupq_n_f32(c_tanh_hi), x2), vreinterpretq_u32_f32(x2), vreinterpretq_u32_f32(vdupq_n_f32(c_tanh_hi))));
46
47 // since the polynomials are odd/even, we need x**2.
48 float32x4_t z = vmulq_f32(x2, x2);
49
50 // evaluate the numerator polynomial y.
51 float32x4_t y = vdupq_n_f32(c_tanh_alpha_13);
52 y = vmlaq_f32(vdupq_n_f32(c_tanh_alpha_11), y, z);
53 y = vmlaq_f32(vdupq_n_f32(c_tanh_alpha_9), y, z);
54 y = vmlaq_f32(vdupq_n_f32(c_tanh_alpha_7), y, z);
55 y = vmlaq_f32(vdupq_n_f32(c_tanh_alpha_5), y, z);
56 y = vmlaq_f32(vdupq_n_f32(c_tanh_alpha_3), y, z);
57 y = vmlaq_f32(vdupq_n_f32(c_tanh_alpha_1), y, z);
58 y = vmulq_f32(y, x2);
59
60 // evaluate the denominator polynomial w.
61 float32x4_t w = vdupq_n_f32(c_tanh_beta_6);
62 w = vmlaq_f32(vdupq_n_f32(c_tanh_beta_4), w, z);
63 w = vmlaq_f32(vdupq_n_f32(c_tanh_beta_2), w, z);
64 w = vmlaq_f32(vdupq_n_f32(c_tanh_beta_0), w, z);
65
66 // divide the numerator by the denominator.
67#if __aarch64__
68 y = vdivq_f32(y, w);
69#else
70 y = div_ps(y, w);
71#endif
72
73 // reinstate the sign.
74 y = vreinterpretq_f32_u32(vbslq_u32(vdupq_n_u32(1u << 31), vreinterpretq_u32_f32(x), vreinterpretq_u32_f32(y)));
75
76 // when the argument is very small in magnitude it's more accurate to just return it.
77 y = vreinterpretq_f32_u32(vbslq_u32(tiny_mask, vreinterpretq_u32_f32(y), vreinterpretq_u32_f32(x)));
78
79 return y;
80}
81
82#endif // NEON_MATHFUN_TANH_H