Elffuss BraidHub

kikocis/ncnn public

master · 9.6 KiBRawPermalink
1/* MIPS implementation of exp
2 *
3 * Inspired by Intel Approximate Math library, and based on the
4 * corresponding algorithms of the cephes math library
5 */
6
7/* Copyright (C) 2020 Leo <leo@nullptr.com.cn>. All rights reserved.
8 *
9 * This software is provided 'as-is', without any express or implied
10 * warranty. In no event will the authors be held liable for any damages
11 * arising from the use of this software.
12 *
13 * Permission is granted to anyone to use this software for any purpose,
14 * including commercial applications, and to alter it and redistribute it
15 * freely, subject to the following restrictions:
16 *
17 * 1. The origin of this software must not be misrepresented; you must not
18 * claim that you wrote the original software. If you use this software
19 * in a product, an acknowledgment in the product documentation would be
20 * appreciated but is not required.
21 * 2. Altered source versions must be plainly marked as such, and must not be
22 * misrepresented as being the original software.
23 * 3. This notice may not be removed or altered from any source distribution.
24 *
25 * (this is the zlib license)
26 */
27
28#ifndef MSA_MATHFUN_H
29#define MSA_MATHFUN_H
30
31#include "mips_usability.h"
32
33#include <msa.h>
34
35_MIPS_FLOAT_CONST(c_1, 1.0f);
36_MIPS_FLOAT_CONST(c_2, 2.0f);
37_MIPS_FLOAT_CONST(c_n1, -1.0f);
38_MIPS_FLOAT_CONST(c_0p5, 0.5f);
39
40#define c_inv_mant_mask ~0x7f800000u
41_MIPS_FLOAT_CONST(c_cephes_SQRTHF, 0.707106781186547524);
42_MIPS_FLOAT_CONST(c_cephes_log_p0, 7.0376836292E-2);
43_MIPS_FLOAT_CONST(c_cephes_log_p1, -1.1514610310E-1);
44_MIPS_FLOAT_CONST(c_cephes_log_p2, 1.1676998740E-1);
45_MIPS_FLOAT_CONST(c_cephes_log_p3, -1.2420140846E-1);
46_MIPS_FLOAT_CONST(c_cephes_log_p4, +1.4249322787E-1);
47_MIPS_FLOAT_CONST(c_cephes_log_p5, -1.6668057665E-1);
48_MIPS_FLOAT_CONST(c_cephes_log_p6, +2.0000714765E-1);
49_MIPS_FLOAT_CONST(c_cephes_log_p7, -2.4999993993E-1);
50_MIPS_FLOAT_CONST(c_cephes_log_p8, +3.3333331174E-1);
51_MIPS_FLOAT_CONST(c_cephes_log_q1, -2.12194440e-4);
52_MIPS_FLOAT_CONST(c_cephes_log_q2, 0.693359375);
53
54/* natural logarithm computed for 4 simultaneous float
55 * return NaN for x <= 0
56 */
57static inline v4f32 log_ps(v4f32 x)
58{
59 v4f32 one = (v4f32)__msa_fill_w(c_1.i);
60
61 x = __msa_fmax_w(x, (v4f32)__msa_fill_w(0)); /* force flush to zero on denormal values */
62 v4i32 invalid_mask = __msa_fcle_w(x, (v4f32)__msa_fill_w(0));
63
64 v4i32 ux = (v4i32)(x);
65
66 v4i32 emm0 = __msa_srl_w(ux, (v4i32)__msa_fill_w(23));
67
68 /* keep only the fractional part */
69 ux = (v4i32)__msa_and_v((v16u8)ux, (v16u8)__msa_fill_w(c_inv_mant_mask));
70 ux = (v4i32)__msa_or_v((v16u8)ux, (v16u8)__msa_fill_w(c_0p5.i));
71 x = (v4f32)(ux);
72
73 emm0 = __msa_subv_w(emm0, (v4i32)__msa_fill_w(0x7f));
74 v4f32 e = __msa_ffint_s_w(emm0);
75
76 e = __msa_fadd_w(e, one);
77
78 /* part2:
79 * if( x < SQRTHF ) {
80 * e -= 1;
81 * x = x + x - 1.0;
82 * } else { x = x - 1.0; }
83 */
84 v4i32 mask = __msa_fclt_w(x, (v4f32)__msa_fill_w(c_cephes_SQRTHF.i));
85 v4f32 tmp = (v4f32)(__msa_and_v((v16u8)(x), (v16u8)mask));
86 x = __msa_fsub_w(x, one);
87 e = __msa_fsub_w(e, (v4f32)(__msa_and_v((v16u8)(one), (v16u8)mask)));
88 x = __msa_fadd_w(x, tmp);
89
90 v4f32 z = __msa_fmul_w(x, x);
91
92 v4f32 y = (v4f32)__msa_fill_w(c_cephes_log_p0.i);
93
94 y = __msa_fmadd_w((v4f32)__msa_fill_w(c_cephes_log_p1.i), y, x);
95 y = __msa_fmadd_w((v4f32)__msa_fill_w(c_cephes_log_p2.i), y, x);
96 y = __msa_fmadd_w((v4f32)__msa_fill_w(c_cephes_log_p3.i), y, x);
97 y = __msa_fmadd_w((v4f32)__msa_fill_w(c_cephes_log_p4.i), y, x);
98 y = __msa_fmadd_w((v4f32)__msa_fill_w(c_cephes_log_p5.i), y, x);
99 y = __msa_fmadd_w((v4f32)__msa_fill_w(c_cephes_log_p6.i), y, x);
100 y = __msa_fmadd_w((v4f32)__msa_fill_w(c_cephes_log_p7.i), y, x);
101 y = __msa_fmadd_w((v4f32)__msa_fill_w(c_cephes_log_p8.i), y, x);
102 y = __msa_fmul_w(y, x);
103
104 y = __msa_fmul_w(y, z);
105
106 tmp = __msa_fmul_w(e, (v4f32)__msa_fill_w(c_cephes_log_q1.i));
107 y = __msa_fadd_w(y, tmp);
108
109 tmp = __msa_fmul_w(z, (v4f32)__msa_fill_w(c_0p5.i));
110 y = __msa_fsub_w(y, tmp);
111
112 tmp = __msa_fmul_w(e, (v4f32)__msa_fill_w(c_cephes_log_q2.i));
113 x = __msa_fadd_w(x, y);
114 x = __msa_fadd_w(x, tmp);
115 x = (v4f32)(__msa_or_v((v16u8)(x), (v16u8)invalid_mask)); // negative arg will be NAN
116 return x;
117}
118
119_MIPS_FLOAT_CONST(c_exp_hi, 88.3762626647949f);
120_MIPS_FLOAT_CONST(c_exp_lo, -88.3762626647949f);
121
122_MIPS_FLOAT_CONST(c_cephes_LOG2EF, 1.44269504088896341);
123_MIPS_FLOAT_CONST(c_cephes_exp_C1, 0.693359375);
124_MIPS_FLOAT_CONST(c_cephes_exp_C2, -2.12194440e-4);
125
126_MIPS_FLOAT_CONST(c_cephes_exp_p0, 1.9875691500E-4);
127_MIPS_FLOAT_CONST(c_cephes_exp_p1, 1.3981999507E-3);
128_MIPS_FLOAT_CONST(c_cephes_exp_p2, 8.3334519073E-3);
129_MIPS_FLOAT_CONST(c_cephes_exp_p3, 4.1665795894E-2);
130_MIPS_FLOAT_CONST(c_cephes_exp_p4, 1.6666665459E-1);
131_MIPS_FLOAT_CONST(c_cephes_exp_p5, 5.0000001201E-1);
132
133/* exp() computed for 4 float at once */
134static inline v4f32 exp_ps(v4f32 x)
135{
136 v4f32 tmp, fx;
137
138 v4f32 one = (v4f32)__msa_fill_w(c_1.i);
139 x = __msa_fmin_w(x, (v4f32)__msa_fill_w(c_exp_hi.i));
140 x = __msa_fmax_w(x, (v4f32)__msa_fill_w(c_exp_lo.i));
141
142 /* express exp(x) as exp(g + n*log(2)) */
143 fx = __msa_fmul_w(x, (v4f32)__msa_fill_w(c_cephes_LOG2EF.i));
144 fx = __msa_fadd_w(fx, (v4f32)__msa_fill_w(c_0p5.i));
145
146 /* perform a floorf */
147 tmp = __msa_ffint_s_w(__msa_ftint_s_w(fx));
148
149 /* if greater, substract 1 */
150 v4i32_w mask = __msa_fclt_w(fx, tmp);
151 mask = (v4i32_w)__msa_and_v((v16u8)mask, (v16u8)one);
152
153 fx = __msa_fsub_w(tmp, (v4f32)mask);
154
155 tmp = __msa_fmul_w(fx, (v4f32)__msa_fill_w(c_cephes_exp_C1.i));
156 v4f32 z = __msa_fmul_w(fx, (v4f32)__msa_fill_w(c_cephes_exp_C2.i));
157 x = __msa_fsub_w(x, tmp);
158 x = __msa_fsub_w(x, z);
159
160 v4f32 y = (v4f32)__msa_fill_w(c_cephes_exp_p0.i);
161
162 z = __msa_fmul_w(x, x);
163
164 y = __msa_fmadd_w((v4f32)__msa_fill_w(c_cephes_exp_p1.i), y, x);
165 y = __msa_fmadd_w((v4f32)__msa_fill_w(c_cephes_exp_p2.i), y, x);
166 y = __msa_fmadd_w((v4f32)__msa_fill_w(c_cephes_exp_p3.i), y, x);
167 y = __msa_fmadd_w((v4f32)__msa_fill_w(c_cephes_exp_p4.i), y, x);
168 y = __msa_fmadd_w((v4f32)__msa_fill_w(c_cephes_exp_p5.i), y, x);
169
170 y = __msa_fmul_w(y, z);
171 y = __msa_fadd_w(y, x);
172 y = __msa_fadd_w(y, one);
173
174 /* build 2^n */
175 v4i32 mm;
176 mm = __msa_ftrunc_s_w(fx);
177 mm = __msa_addv_w(mm, __msa_fill_w(0x7f));
178 mm = __msa_sll_w(mm, __msa_fill_w(23));
179
180 y = __msa_fmul_w(y, (v4f32)mm);
181 return y;
182}
183
184_MIPS_FLOAT_CONST(c_tanh_tiny, 1e-4f);
185_MIPS_FLOAT_CONST(c_tanh_hi, 9.0f);
186// The monomial coefficients of the numerator polynomial (odd).
187_MIPS_FLOAT_CONST(c_tanh_alpha_1, 4.89352455891786e-3f);
188_MIPS_FLOAT_CONST(c_tanh_alpha_3, 6.37261928875436e-4f);
189_MIPS_FLOAT_CONST(c_tanh_alpha_5, 1.48572235717979e-5f);
190_MIPS_FLOAT_CONST(c_tanh_alpha_7, 5.12229709037114e-8f);
191_MIPS_FLOAT_CONST(c_tanh_alpha_9, -8.60467152213735e-11f);
192_MIPS_FLOAT_CONST(c_tanh_alpha_11, 2.00018790482477e-13f);
193_MIPS_FLOAT_CONST(c_tanh_alpha_13, -2.76076847742355e-16f);
194// The monomial coefficients of the denominator polynomial (even).
195_MIPS_FLOAT_CONST(c_tanh_beta_0, 4.89352518554385e-3f);
196_MIPS_FLOAT_CONST(c_tanh_beta_2, 2.26843463243900e-3f);
197_MIPS_FLOAT_CONST(c_tanh_beta_4, 1.18534705686654e-4f);
198_MIPS_FLOAT_CONST(c_tanh_beta_6, 1.19825839466702e-6f);
199
200/* tanh() computed for 4 float at once */
201static inline v4f32 tanh_ps(v4f32 x)
202{
203 v4f32 x2 = (v4f32)__msa_bclri_w((v4u32)x, 31);
204 v4i32 tiny_mask = __msa_fclt_w(x2, (v4f32)__msa_fill_w(c_tanh_tiny.i));
205
206 // clamp the inputs to the range [-9, 9] since anything outside
207 // this range is -/+1.0f in single-precision.
208 x2 = (v4f32)__msa_bsel_v((v16u8)__msa_fclt_w((v4f32)__msa_fill_w(c_tanh_hi.i), x2), (v16u8)x2, (v16u8)__msa_fill_w(c_tanh_hi.i));
209
210 // since the polynomials are odd/even, we need x**2.
211 v4f32 z = __msa_fmul_w(x2, x2);
212
213 // evaluate the numerator polynomial y.
214 v4f32 y = (v4f32)__msa_fill_w(c_tanh_alpha_13.i);
215 y = __msa_fmadd_w((v4f32)__msa_fill_w(c_tanh_alpha_11.i), y, z);
216 y = __msa_fmadd_w((v4f32)__msa_fill_w(c_tanh_alpha_9.i), y, z);
217 y = __msa_fmadd_w((v4f32)__msa_fill_w(c_tanh_alpha_7.i), y, z);
218 y = __msa_fmadd_w((v4f32)__msa_fill_w(c_tanh_alpha_5.i), y, z);
219 y = __msa_fmadd_w((v4f32)__msa_fill_w(c_tanh_alpha_3.i), y, z);
220 y = __msa_fmadd_w((v4f32)__msa_fill_w(c_tanh_alpha_1.i), y, z);
221 y = __msa_fmul_w(y, x2);
222
223 // evaluate the denominator polynomial w.
224 v4f32 w = (v4f32)__msa_fill_w(c_tanh_beta_6.i);
225 w = __msa_fmadd_w((v4f32)__msa_fill_w(c_tanh_beta_4.i), w, z);
226 w = __msa_fmadd_w((v4f32)__msa_fill_w(c_tanh_beta_2.i), w, z);
227 w = __msa_fmadd_w((v4f32)__msa_fill_w(c_tanh_beta_0.i), w, z);
228
229 // divide the numerator by the denominator.
230 y = __msa_fdiv_w(y, w);
231
232 // reinstate the sign.
233 y = (v4f32)__msa_binsli_w((v4u32)y, (v4u32)x, 0);
234
235 // when the argument is very small in magnitude it's more accurate to just return it.
236 y = (v4f32)__msa_bsel_v((v16u8)tiny_mask, (v16u8)y, (v16u8)x);
237
238 return y;
239}
240
241static inline v4f32 pow_ps(v4f32 a, v4f32 b)
242{
243 // pow(x, m) = exp(m * log(x))
244 return exp_ps(__msa_fmul_w(b, log_ps(a)));
245}
246
247static inline v4f32 sigmoid_ps(v4f32 _v)
248{
249 v4f32 _one = __msa_fill_w_f32(1.f);
250 _v = (v4f32)__msa_bnegi_w((v4u32)_v, 31);
251 _v = exp_ps(_v);
252 _v = __msa_fadd_w(_v, _one);
253 return __msa_fdiv_w(_one, _v);
254}
255
256static inline v4f32 atan2_ps(v4f32 a, v4f32 b)
257{
258 //TODO msa optimize
259 float tmpx[4];
260 float tmpy[4];
261 __msa_st_w((v4i32)a, tmpx, 0);
262 __msa_st_w((v4i32)b, tmpy, 0);
263 tmpx[0] = atan2(tmpx[0], tmpy[0]);
264 tmpx[1] = atan2(tmpx[1], tmpy[1]);
265 tmpx[2] = atan2(tmpx[2], tmpy[2]);
266 tmpx[3] = atan2(tmpx[3], tmpy[3]);
267 return (v4f32)__msa_ld_w(tmpx, 0);
268}
269
270#endif // MSA_MATHFUN_H