| 1 | /* LOONGARCH implementation of exp |
| 2 | * |
| 3 | * Inspired by Intel Approximate Math library, and based on the |
| 4 | * corresponding algorithms of the cephes math library |
| 5 | * Copyright (C) 2022 yala <zhaojunchao@loongson.cn>;<junchao82@qq.com>. All rights reserved. |
| 6 | */ |
| 7 | |
| 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 LSX_MATHFUN_H |
| 29 | #define LSX_MATHFUN_H |
| 30 | |
| 31 | #include "loongarch_usability.h" |
| 32 | |
| 33 | #include <lsxintrin.h> |
| 34 | |
| 35 | _LOONGARCH_FLOAT_CONST(c_1, 1.0f); |
| 36 | _LOONGARCH_FLOAT_CONST(c_2, 2.0f); |
| 37 | _LOONGARCH_FLOAT_CONST(c_n1, -1.0f); |
| 38 | _LOONGARCH_FLOAT_CONST(c_0p5, 0.5f); |
| 39 | |
| 40 | #define c_inv_mant_mask ~0x7f800000u |
| 41 | _LOONGARCH_FLOAT_CONST(c_cephes_SQRTHF, 0.707106781186547524); |
| 42 | _LOONGARCH_FLOAT_CONST(c_cephes_log_p0, 7.0376836292E-2); |
| 43 | _LOONGARCH_FLOAT_CONST(c_cephes_log_p1, -1.1514610310E-1); |
| 44 | _LOONGARCH_FLOAT_CONST(c_cephes_log_p2, 1.1676998740E-1); |
| 45 | _LOONGARCH_FLOAT_CONST(c_cephes_log_p3, -1.2420140846E-1); |
| 46 | _LOONGARCH_FLOAT_CONST(c_cephes_log_p4, +1.4249322787E-1); |
| 47 | _LOONGARCH_FLOAT_CONST(c_cephes_log_p5, -1.6668057665E-1); |
| 48 | _LOONGARCH_FLOAT_CONST(c_cephes_log_p6, +2.0000714765E-1); |
| 49 | _LOONGARCH_FLOAT_CONST(c_cephes_log_p7, -2.4999993993E-1); |
| 50 | _LOONGARCH_FLOAT_CONST(c_cephes_log_p8, +3.3333331174E-1); |
| 51 | _LOONGARCH_FLOAT_CONST(c_cephes_log_q1, -2.12194440e-4); |
| 52 | _LOONGARCH_FLOAT_CONST(c_cephes_log_q2, 0.693359375); |
| 53 | |
| 54 | /* natural logarithm computed for 4 simultaneous float |
| 55 | * return NaN for x <= 0 |
| 56 | */ |
| 57 | static inline __m128 log_ps(__m128 x) |
| 58 | { |
| 59 | __m128 one = (__m128)__lsx_vreplgr2vr_w(c_1.i); |
| 60 | |
| 61 | x = __lsx_vfmax_s(x, (__m128)__lsx_vreplgr2vr_w(0)); /* force flush to zero on denormal values */ |
| 62 | __m128i invalid_mask = __lsx_vfcmp_cle_s(x, (__m128)__lsx_vreplgr2vr_w(0)); |
| 63 | |
| 64 | __m128i ux = (__m128i)(x); |
| 65 | |
| 66 | __m128i emm0 = __lsx_vsrl_w(ux, __lsx_vreplgr2vr_w(23)); |
| 67 | |
| 68 | /* keep only the fractional part */ |
| 69 | ux = __lsx_vand_v(ux, __lsx_vreplgr2vr_w(c_inv_mant_mask)); |
| 70 | ux = __lsx_vor_v(ux, __lsx_vreplgr2vr_w(c_0p5.i)); |
| 71 | x = (__m128)(ux); |
| 72 | |
| 73 | emm0 = __lsx_vsub_w(emm0, __lsx_vreplgr2vr_w(0x7f)); |
| 74 | __m128 e = __lsx_vffint_s_w(emm0); |
| 75 | |
| 76 | e = __lsx_vfadd_s(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 | __m128i mask = __lsx_vfcmp_clt_s((__m128)x, (__m128)__lsx_vreplgr2vr_w(c_cephes_SQRTHF.i)); |
| 85 | __m128 tmp = (__m128)(__lsx_vand_v((__m128i)(x), (__m128i)mask)); |
| 86 | x = __lsx_vfsub_s(x, one); |
| 87 | e = __lsx_vfsub_s(e, (__m128)(__lsx_vand_v((__m128i)(one), (__m128i)mask))); |
| 88 | x = __lsx_vfadd_s(x, tmp); |
| 89 | |
| 90 | __m128 z = __lsx_vfmul_s(x, x); |
| 91 | |
| 92 | __m128 y = (__m128)__lsx_vreplgr2vr_w(c_cephes_log_p0.i); |
| 93 | |
| 94 | y = __lsx_vfmadd_s(x, y, (__m128)__lsx_vreplgr2vr_w(c_cephes_log_p1.i)); |
| 95 | y = __lsx_vfmadd_s(x, y, (__m128)__lsx_vreplgr2vr_w(c_cephes_log_p2.i)); |
| 96 | y = __lsx_vfmadd_s(x, y, (__m128)__lsx_vreplgr2vr_w(c_cephes_log_p3.i)); |
| 97 | y = __lsx_vfmadd_s(x, y, (__m128)__lsx_vreplgr2vr_w(c_cephes_log_p4.i)); |
| 98 | y = __lsx_vfmadd_s(x, y, (__m128)__lsx_vreplgr2vr_w(c_cephes_log_p5.i)); |
| 99 | y = __lsx_vfmadd_s(x, y, (__m128)__lsx_vreplgr2vr_w(c_cephes_log_p6.i)); |
| 100 | y = __lsx_vfmadd_s(x, y, (__m128)__lsx_vreplgr2vr_w(c_cephes_log_p7.i)); |
| 101 | y = __lsx_vfmadd_s(x, y, (__m128)__lsx_vreplgr2vr_w(c_cephes_log_p8.i)); |
| 102 | y = __lsx_vfmul_s(y, x); |
| 103 | |
| 104 | y = __lsx_vfmul_s(y, z); |
| 105 | |
| 106 | tmp = __lsx_vfmul_s(e, (__m128)__lsx_vreplgr2vr_w(c_cephes_log_q1.i)); |
| 107 | y = __lsx_vfadd_s(y, tmp); |
| 108 | |
| 109 | tmp = __lsx_vfmul_s(z, (__m128)__lsx_vreplgr2vr_w(c_0p5.i)); |
| 110 | y = __lsx_vfsub_s(y, tmp); |
| 111 | |
| 112 | tmp = __lsx_vfmul_s(e, (__m128)__lsx_vreplgr2vr_w(c_cephes_log_q2.i)); |
| 113 | x = __lsx_vfadd_s(x, y); |
| 114 | x = __lsx_vfadd_s(x, tmp); |
| 115 | x = (__m128)(__lsx_vor_v((__m128i)(x), (__m128i)invalid_mask)); // negative arg will be NAN |
| 116 | return x; |
| 117 | } |
| 118 | |
| 119 | _LOONGARCH_FLOAT_CONST(c_exp_hi, 88.3762626647949f); |
| 120 | _LOONGARCH_FLOAT_CONST(c_exp_lo, -88.3762626647949f); |
| 121 | |
| 122 | _LOONGARCH_FLOAT_CONST(c_cephes_LOG2EF, 1.44269504088896341); |
| 123 | _LOONGARCH_FLOAT_CONST(c_cephes_exp_C1, 0.693359375); |
| 124 | _LOONGARCH_FLOAT_CONST(c_cephes_exp_C2, -2.12194440e-4); |
| 125 | |
| 126 | _LOONGARCH_FLOAT_CONST(c_cephes_exp_p0, 1.9875691500E-4); |
| 127 | _LOONGARCH_FLOAT_CONST(c_cephes_exp_p1, 1.3981999507E-3); |
| 128 | _LOONGARCH_FLOAT_CONST(c_cephes_exp_p2, 8.3334519073E-3); |
| 129 | _LOONGARCH_FLOAT_CONST(c_cephes_exp_p3, 4.1665795894E-2); |
| 130 | _LOONGARCH_FLOAT_CONST(c_cephes_exp_p4, 1.6666665459E-1); |
| 131 | _LOONGARCH_FLOAT_CONST(c_cephes_exp_p5, 5.0000001201E-1); |
| 132 | |
| 133 | /* exp() computed for 4 float at once */ |
| 134 | static inline __m128 exp_ps(__m128 x) |
| 135 | { |
| 136 | __m128 tmp, fx; |
| 137 | |
| 138 | __m128 one = (__m128)__lsx_vreplgr2vr_w(c_1.i); |
| 139 | x = __lsx_vfmin_s(x, (__m128)__lsx_vreplgr2vr_w(c_exp_hi.i)); |
| 140 | x = __lsx_vfmax_s(x, (__m128)__lsx_vreplgr2vr_w(c_exp_lo.i)); |
| 141 | |
| 142 | /* express exp(x) as exp(g + n*log(2)) */ |
| 143 | fx = __lsx_vfmul_s(x, (__m128)__lsx_vreplgr2vr_w(c_cephes_LOG2EF.i)); |
| 144 | fx = __lsx_vfadd_s(fx, (__m128)__lsx_vreplgr2vr_w(c_0p5.i)); |
| 145 | |
| 146 | /* perform a floorf */ |
| 147 | tmp = __lsx_vffint_s_w(__lsx_vftint_w_s(fx)); |
| 148 | |
| 149 | /* if greater, substract 1 */ |
| 150 | __m128i mask = __lsx_vfcmp_clt_s(fx, tmp); |
| 151 | mask = __lsx_vand_v(mask, (__m128i)one); |
| 152 | |
| 153 | fx = __lsx_vfsub_s(tmp, (__m128)mask); |
| 154 | |
| 155 | tmp = __lsx_vfmul_s(fx, (__m128)__lsx_vreplgr2vr_w(c_cephes_exp_C1.i)); |
| 156 | __m128 z = __lsx_vfmul_s(fx, (__m128)__lsx_vreplgr2vr_w(c_cephes_exp_C2.i)); |
| 157 | x = __lsx_vfsub_s(x, tmp); |
| 158 | x = __lsx_vfsub_s(x, z); |
| 159 | |
| 160 | __m128 y = (__m128)__lsx_vreplgr2vr_w(c_cephes_exp_p0.i); |
| 161 | |
| 162 | z = __lsx_vfmul_s(x, x); |
| 163 | |
| 164 | y = __lsx_vfmadd_s(x, y, (__m128)__lsx_vreplgr2vr_w(c_cephes_exp_p1.i)); |
| 165 | y = __lsx_vfmadd_s(x, y, (__m128)__lsx_vreplgr2vr_w(c_cephes_exp_p2.i)); |
| 166 | y = __lsx_vfmadd_s(x, y, (__m128)__lsx_vreplgr2vr_w(c_cephes_exp_p3.i)); |
| 167 | y = __lsx_vfmadd_s(x, y, (__m128)__lsx_vreplgr2vr_w(c_cephes_exp_p4.i)); |
| 168 | y = __lsx_vfmadd_s(x, y, (__m128)__lsx_vreplgr2vr_w(c_cephes_exp_p5.i)); |
| 169 | |
| 170 | y = __lsx_vfmul_s(y, z); |
| 171 | y = __lsx_vfadd_s(y, x); |
| 172 | y = __lsx_vfadd_s(y, one); |
| 173 | |
| 174 | /* build 2^n */ |
| 175 | __m128i mm; |
| 176 | mm = __lsx_vftintrz_w_s(fx); |
| 177 | mm = __lsx_vadd_w(mm, __lsx_vreplgr2vr_w(0x7f)); |
| 178 | mm = __lsx_vsll_w(mm, __lsx_vreplgr2vr_w(23)); |
| 179 | |
| 180 | y = __lsx_vfmul_s(y, (__m128)mm); |
| 181 | return y; |
| 182 | } |
| 183 | |
| 184 | _LOONGARCH_FLOAT_CONST(c_tanh_tiny, 1e-4f); |
| 185 | _LOONGARCH_FLOAT_CONST(c_tanh_hi, 9.0f); |
| 186 | // The monomial coefficients of the numerator polynomial (odd). |
| 187 | _LOONGARCH_FLOAT_CONST(c_tanh_alpha_1, 4.89352455891786e-3f); |
| 188 | _LOONGARCH_FLOAT_CONST(c_tanh_alpha_3, 6.37261928875436e-4f); |
| 189 | _LOONGARCH_FLOAT_CONST(c_tanh_alpha_5, 1.48572235717979e-5f); |
| 190 | _LOONGARCH_FLOAT_CONST(c_tanh_alpha_7, 5.12229709037114e-8f); |
| 191 | _LOONGARCH_FLOAT_CONST(c_tanh_alpha_9, -8.60467152213735e-11f); |
| 192 | _LOONGARCH_FLOAT_CONST(c_tanh_alpha_11, 2.00018790482477e-13f); |
| 193 | _LOONGARCH_FLOAT_CONST(c_tanh_alpha_13, -2.76076847742355e-16f); |
| 194 | // The monomial coefficients of the denominator polynomial (even). |
| 195 | _LOONGARCH_FLOAT_CONST(c_tanh_beta_0, 4.89352518554385e-3f); |
| 196 | _LOONGARCH_FLOAT_CONST(c_tanh_beta_2, 2.26843463243900e-3f); |
| 197 | _LOONGARCH_FLOAT_CONST(c_tanh_beta_4, 1.18534705686654e-4f); |
| 198 | _LOONGARCH_FLOAT_CONST(c_tanh_beta_6, 1.19825839466702e-6f); |
| 199 | |
| 200 | /* tanh() computed for 4 float at once */ |
| 201 | static inline __m128 tanh_ps(__m128 x) |
| 202 | { |
| 203 | __m128 x2 = (__m128)__lsx_vbitclri_w((__m128i)x, 31); |
| 204 | __m128i tiny_mask = __lsx_vfcmp_clt_s((__m128)x2, (__m128)(__m128)__lsx_vreplgr2vr_w(c_tanh_tiny.i)); |
| 205 | __m128i sig_mask = __lsx_vreplgr2vr_w(1 << 31); |
| 206 | __m128i sig_save = __lsx_vand_v((__m128i)x, sig_mask); |
| 207 | |
| 208 | // clamp the inputs to the range [-9, 9] since anything outside |
| 209 | // this range is -/+1.0f in single-precision. |
| 210 | x2 = (__m128)__lsx_vbitsel_v((__m128i)x2, (__m128i)__lsx_vreplgr2vr_w(c_tanh_hi.i), (__m128i)__lsx_vfcmp_clt_s((__m128)__lsx_vreplgr2vr_w(c_tanh_hi.i), (__m128)x2)); |
| 211 | |
| 212 | // since the polynomials are odd/even, we need x**2. |
| 213 | __m128 z = __lsx_vfmul_s(x2, x2); |
| 214 | |
| 215 | // evaluate the numerator polynomial y. |
| 216 | __m128 y = (__m128)__lsx_vreplgr2vr_w(c_tanh_alpha_13.i); |
| 217 | y = __lsx_vfmadd_s(z, y, (__m128)__lsx_vreplgr2vr_w(c_tanh_alpha_11.i)); |
| 218 | y = __lsx_vfmadd_s(z, y, (__m128)__lsx_vreplgr2vr_w(c_tanh_alpha_9.i)); |
| 219 | y = __lsx_vfmadd_s(z, y, (__m128)__lsx_vreplgr2vr_w(c_tanh_alpha_7.i)); |
| 220 | y = __lsx_vfmadd_s(z, y, (__m128)__lsx_vreplgr2vr_w(c_tanh_alpha_5.i)); |
| 221 | y = __lsx_vfmadd_s(z, y, (__m128)__lsx_vreplgr2vr_w(c_tanh_alpha_3.i)); |
| 222 | y = __lsx_vfmadd_s(z, y, (__m128)__lsx_vreplgr2vr_w(c_tanh_alpha_1.i)); |
| 223 | y = __lsx_vfmul_s(y, x2); |
| 224 | |
| 225 | // evaluate the denominator polynomial w. |
| 226 | __m128 w = (__m128)__lsx_vreplgr2vr_w(c_tanh_beta_6.i); |
| 227 | w = __lsx_vfmadd_s(z, w, (__m128)__lsx_vreplgr2vr_w(c_tanh_beta_4.i)); |
| 228 | w = __lsx_vfmadd_s(z, w, (__m128)__lsx_vreplgr2vr_w(c_tanh_beta_2.i)); |
| 229 | w = __lsx_vfmadd_s(z, w, (__m128)__lsx_vreplgr2vr_w(c_tanh_beta_0.i)); |
| 230 | |
| 231 | // divide the numerator by the denominator. |
| 232 | y = __lsx_vfdiv_s(y, w); |
| 233 | |
| 234 | // reinstate the sign. |
| 235 | y = (__m128)__lsx_vor_v((__m128i)y, sig_save); |
| 236 | |
| 237 | // when the argument is very small in magnitude it's more accurate to just return it. |
| 238 | y = (__m128)__lsx_vbitsel_v((__m128i)y, (__m128i)x, (__m128i)tiny_mask); |
| 239 | |
| 240 | return y; |
| 241 | } |
| 242 | |
| 243 | static inline __m128 pow_ps(__m128 a, __m128 b) |
| 244 | { |
| 245 | // pow(x, m) = exp(m * log(x)) |
| 246 | return exp_ps(__lsx_vfmul_s(b, log_ps(a))); |
| 247 | } |
| 248 | |
| 249 | static inline __m128 sigmoid_ps(__m128 _v) |
| 250 | { |
| 251 | __m128 _one = __lsx_vreplfr2vr_s(1.f); |
| 252 | _v = (__m128)__lsx_vbitrevi_w((__m128i)_v, 31); |
| 253 | _v = exp_ps(_v); |
| 254 | _v = __lsx_vfadd_s(_v, _one); |
| 255 | return __lsx_vfdiv_s(_one, _v); |
| 256 | } |
| 257 | |
| 258 | static inline __m128 atan2_ps(__m128 a, __m128 b) |
| 259 | { |
| 260 | //TODO lsx optimize |
| 261 | float tmpx[4]; |
| 262 | float tmpy[4]; |
| 263 | __lsx_vst(a, tmpx, 0); |
| 264 | __lsx_vst(b, tmpy, 0); |
| 265 | tmpx[0] = atan2(tmpx[0], tmpy[0]); |
| 266 | tmpx[1] = atan2(tmpx[1], tmpy[1]); |
| 267 | tmpx[2] = atan2(tmpx[2], tmpy[2]); |
| 268 | tmpx[3] = atan2(tmpx[3], tmpy[3]); |
| 269 | return (__m128)__lsx_vld(tmpx, 0); |
| 270 | } |
| 271 | |
| 272 | #endif // LSX_MATHFUN_H |