| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2020 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 | /* NEON implementation of sin, cos, exp and log |
| 16 | * |
| 17 | * Inspired by Intel Approximate Math library, and based on the |
| 18 | * corresponding algorithms of the cephes math library |
| 19 | */ |
| 20 | |
| 21 | /* Copyright (C) 2011 Julien Pommier |
| 22 | * |
| 23 | * This software is provided 'as-is', without any express or implied |
| 24 | * warranty. In no event will the authors be held liable for any damages |
| 25 | * arising from the use of this software. |
| 26 | * |
| 27 | * Permission is granted to anyone to use this software for any purpose, |
| 28 | * including commercial applications, and to alter it and redistribute it |
| 29 | * freely, subject to the following restrictions: |
| 30 | * |
| 31 | * 1. The origin of this software must not be misrepresented; you must not |
| 32 | * claim that you wrote the original software. If you use this software |
| 33 | * in a product, an acknowledgment in the product documentation would be |
| 34 | * appreciated but is not required. |
| 35 | * 2. Altered source versions must be plainly marked as such, and must not be |
| 36 | * misrepresented as being the original software. |
| 37 | * 3. This notice may not be removed or altered from any source distribution. |
| 38 | * |
| 39 | * (this is the zlib license) |
| 40 | */ |
| 41 | |
| 42 | #ifndef NEON_MATHFUN_FP16S_H |
| 43 | #define NEON_MATHFUN_FP16S_H |
| 44 | |
| 45 | #include <arm_neon.h> |
| 46 | |
| 47 | #define c_inv_mant_mask_f16 -31745 // ~0x7c00u |
| 48 | #define c_cephes_SQRTHF 0.707106781186547524 |
| 49 | #define c_cephes_log_p0 7.0376836292E-2 |
| 50 | #define c_cephes_log_p1 -1.1514610310E-1 |
| 51 | #define c_cephes_log_p2 1.1676998740E-1 |
| 52 | #define c_cephes_log_p3 -1.2420140846E-1 |
| 53 | #define c_cephes_log_p4 +1.4249322787E-1 |
| 54 | #define c_cephes_log_p5 -1.6668057665E-1 |
| 55 | #define c_cephes_log_p6 +2.0000714765E-1 |
| 56 | #define c_cephes_log_p7 -2.4999993993E-1 |
| 57 | #define c_cephes_log_p8 +3.3333331174E-1 |
| 58 | #define c_cephes_log_q1 -2.12194440e-4 |
| 59 | #define c_cephes_log_q2 0.693359375 |
| 60 | |
| 61 | /* natural logarithm computed for 4 simultaneous float |
| 62 | * return NaN for x <= 0 |
| 63 | */ |
| 64 | static inline float16x4_t log_ps_f16(float16x4_t x) |
| 65 | { |
| 66 | float16x4_t one = vdup_n_f16(1); |
| 67 | |
| 68 | x = vmax_f16(x, vdup_n_f16(0)); /* force flush to zero on denormal values */ |
| 69 | uint16x4_t invalid_mask = vcle_f16(x, vdup_n_f16(0)); |
| 70 | |
| 71 | int16x4_t ux = vreinterpret_s16_f16(x); |
| 72 | |
| 73 | int16x4_t emm0 = vshr_n_s16(ux, 10); |
| 74 | |
| 75 | /* keep only the fractional part */ |
| 76 | ux = vand_s16(ux, vdup_n_s16(c_inv_mant_mask_f16)); |
| 77 | ux = vorr_s16(ux, vreinterpret_s16_f16(vdup_n_f16(0.5f))); |
| 78 | x = vreinterpret_f16_s16(ux); |
| 79 | |
| 80 | emm0 = vsub_s16(emm0, vdup_n_s16(0xf)); |
| 81 | float16x4_t e = vcvt_f16_s16(emm0); |
| 82 | |
| 83 | e = vadd_f16(e, one); |
| 84 | |
| 85 | /* part2: |
| 86 | * if( x < SQRTHF ) { |
| 87 | * e -= 1; |
| 88 | * x = x + x - 1.0; |
| 89 | * } else { x = x - 1.0; } |
| 90 | */ |
| 91 | uint16x4_t mask = vclt_f16(x, vdup_n_f16(c_cephes_SQRTHF)); |
| 92 | float16x4_t tmp = (float16x4_t)(vand_u16((uint16x4_t)(x), mask)); |
| 93 | x = vsub_f16(x, one); |
| 94 | e = vsub_f16(e, (float16x4_t)(vand_u16((uint16x4_t)(one), mask))); |
| 95 | x = vadd_f16(x, tmp); |
| 96 | |
| 97 | float16x4_t z = vmul_f16(x, x); |
| 98 | |
| 99 | float16x4_t y = vdup_n_f16(c_cephes_log_p0); |
| 100 | y = vfma_f16(vdup_n_f16(c_cephes_log_p1), y, x); |
| 101 | y = vfma_f16(vdup_n_f16(c_cephes_log_p2), y, x); |
| 102 | y = vfma_f16(vdup_n_f16(c_cephes_log_p3), y, x); |
| 103 | y = vfma_f16(vdup_n_f16(c_cephes_log_p4), y, x); |
| 104 | y = vfma_f16(vdup_n_f16(c_cephes_log_p5), y, x); |
| 105 | y = vfma_f16(vdup_n_f16(c_cephes_log_p6), y, x); |
| 106 | y = vfma_f16(vdup_n_f16(c_cephes_log_p7), y, x); |
| 107 | y = vfma_f16(vdup_n_f16(c_cephes_log_p8), y, x); |
| 108 | y = vmul_f16(y, x); |
| 109 | |
| 110 | y = vmul_f16(y, z); |
| 111 | |
| 112 | y = vfma_f16(y, e, vdup_n_f16(c_cephes_log_q1)); |
| 113 | |
| 114 | y = vfms_f16(y, z, vdup_n_f16(0.5f)); |
| 115 | |
| 116 | x = vadd_f16(x, y); |
| 117 | x = vfma_f16(x, e, vdup_n_f16(c_cephes_log_q2)); |
| 118 | x = (float16x4_t)(vorr_u16((uint16x4_t)(x), invalid_mask)); // negative arg will be NAN |
| 119 | return x; |
| 120 | } |
| 121 | |
| 122 | static inline float16x8_t log_ps_f16(float16x8_t x) |
| 123 | { |
| 124 | float16x8_t one = vdupq_n_f16(1); |
| 125 | |
| 126 | x = vmaxq_f16(x, vdupq_n_f16(0)); /* force flush to zero on denormal values */ |
| 127 | uint16x8_t invalid_mask = vcleq_f16(x, vdupq_n_f16(0)); |
| 128 | |
| 129 | int16x8_t ux = vreinterpretq_s16_f16(x); |
| 130 | |
| 131 | int16x8_t emm0 = vshrq_n_s16(ux, 10); |
| 132 | |
| 133 | /* keep only the fractional part */ |
| 134 | ux = vandq_s16(ux, vdupq_n_s16(c_inv_mant_mask_f16)); |
| 135 | ux = vorrq_s16(ux, vreinterpretq_s16_f16(vdupq_n_f16(0.5f))); |
| 136 | x = vreinterpretq_f16_s16(ux); |
| 137 | |
| 138 | emm0 = vsubq_s16(emm0, vdupq_n_s16(0xf)); |
| 139 | float16x8_t e = vcvtq_f16_s16(emm0); |
| 140 | |
| 141 | e = vaddq_f16(e, one); |
| 142 | |
| 143 | /* part2: |
| 144 | * if( x < SQRTHF ) { |
| 145 | * e -= 1; |
| 146 | * x = x + x - 1.0; |
| 147 | * } else { x = x - 1.0; } |
| 148 | */ |
| 149 | uint16x8_t mask = vcltq_f16(x, vdupq_n_f16(c_cephes_SQRTHF)); |
| 150 | float16x8_t tmp = vreinterpretq_f16_u16(vandq_u16(vreinterpretq_u16_f16(x), mask)); |
| 151 | x = vsubq_f16(x, one); |
| 152 | e = vsubq_f16(e, vreinterpretq_f16_u16(vandq_u16(vreinterpretq_u16_f16(one), mask))); |
| 153 | x = vaddq_f16(x, tmp); |
| 154 | |
| 155 | float16x8_t z = vmulq_f16(x, x); |
| 156 | |
| 157 | float16x8_t y = vdupq_n_f16(c_cephes_log_p0); |
| 158 | y = vfmaq_f16(vdupq_n_f16(c_cephes_log_p1), y, x); |
| 159 | y = vfmaq_f16(vdupq_n_f16(c_cephes_log_p2), y, x); |
| 160 | y = vfmaq_f16(vdupq_n_f16(c_cephes_log_p3), y, x); |
| 161 | y = vfmaq_f16(vdupq_n_f16(c_cephes_log_p4), y, x); |
| 162 | y = vfmaq_f16(vdupq_n_f16(c_cephes_log_p5), y, x); |
| 163 | y = vfmaq_f16(vdupq_n_f16(c_cephes_log_p6), y, x); |
| 164 | y = vfmaq_f16(vdupq_n_f16(c_cephes_log_p7), y, x); |
| 165 | y = vfmaq_f16(vdupq_n_f16(c_cephes_log_p8), y, x); |
| 166 | y = vmulq_f16(y, x); |
| 167 | |
| 168 | y = vmulq_f16(y, z); |
| 169 | |
| 170 | y = vfmaq_f16(y, e, vdupq_n_f16(c_cephes_log_q1)); |
| 171 | |
| 172 | y = vfmsq_f16(y, z, vdupq_n_f16(0.5f)); |
| 173 | |
| 174 | x = vaddq_f16(x, y); |
| 175 | x = vfmaq_f16(x, e, vdupq_n_f16(c_cephes_log_q2)); |
| 176 | x = vreinterpretq_f16_u16(vorrq_u16(vreinterpretq_u16_f16(x), invalid_mask)); // negative arg will be NAN |
| 177 | return x; |
| 178 | } |
| 179 | |
| 180 | #define c_exp_hi_f16 10.7421875f |
| 181 | #define c_exp_lo_f16 -10.7421875f |
| 182 | |
| 183 | #define c_cephes_LOG2EF 1.44269504088896341 |
| 184 | #define c_cephes_exp_C1 0.693359375 |
| 185 | #define c_cephes_exp_C2 -2.12194440e-4 |
| 186 | |
| 187 | #define c_cephes_exp_p0 1.9875691500E-4 |
| 188 | #define c_cephes_exp_p1 1.3981999507E-3 |
| 189 | #define c_cephes_exp_p2 8.3334519073E-3 |
| 190 | #define c_cephes_exp_p3 4.1665795894E-2 |
| 191 | #define c_cephes_exp_p4 1.6666665459E-1 |
| 192 | #define c_cephes_exp_p5 5.0000001201E-1 |
| 193 | |
| 194 | /* exp() computed for 4 float at once */ |
| 195 | static inline float16x4_t exp_ps_f16(float16x4_t x) |
| 196 | { |
| 197 | float16x4_t tmp, fx; |
| 198 | |
| 199 | float16x4_t one = vdup_n_f16(1); |
| 200 | x = vmin_f16(x, vdup_n_f16(c_exp_hi_f16)); |
| 201 | x = vmax_f16(x, vdup_n_f16(c_exp_lo_f16)); |
| 202 | |
| 203 | /* express exp(x) as exp(g + n*log(2)) */ |
| 204 | #if defined(_MSC_VER) && !defined(__clang__) |
| 205 | fx = vfma_f16(vdup_n_f16(0.5f), x, vcvt_f16_f32(vdupq_n_f32(c_cephes_LOG2EF))); |
| 206 | #else |
| 207 | fx = vfma_f16(vdup_n_f16(0.5f), x, vdup_n_f16(c_cephes_LOG2EF)); |
| 208 | #endif |
| 209 | |
| 210 | /* perform a floorf */ |
| 211 | tmp = vcvt_f16_s16(vcvt_s16_f16(fx)); |
| 212 | |
| 213 | /* if greater, substract 1 */ |
| 214 | uint16x4_t mask = vcgt_f16(tmp, fx); |
| 215 | mask = vand_u16(mask, (uint16x4_t)(one)); |
| 216 | |
| 217 | fx = vsub_f16(tmp, (float16x4_t)(mask)); |
| 218 | |
| 219 | #if defined(_MSC_VER) && !defined(__clang__) |
| 220 | tmp = vmul_f16(fx, vcvt_f16_f32(vdupq_n_f32(c_cephes_exp_C1))); |
| 221 | float16x4_t z = vmul_f16(fx, vcvt_f16_f32(vdupq_n_f32(c_cephes_exp_C2))); |
| 222 | #else |
| 223 | tmp = vmul_f16(fx, vdup_n_f16(c_cephes_exp_C1)); |
| 224 | float16x4_t z = vmul_f16(fx, vdup_n_f16(c_cephes_exp_C2)); |
| 225 | #endif |
| 226 | x = vsub_f16(x, tmp); |
| 227 | x = vsub_f16(x, z); |
| 228 | |
| 229 | z = vmul_f16(x, x); |
| 230 | |
| 231 | float16x4_t y = vdup_n_f16(c_cephes_exp_p0); |
| 232 | y = vfma_f16(vdup_n_f16(c_cephes_exp_p1), y, x); |
| 233 | y = vfma_f16(vdup_n_f16(c_cephes_exp_p2), y, x); |
| 234 | y = vfma_f16(vdup_n_f16(c_cephes_exp_p3), y, x); |
| 235 | y = vfma_f16(vdup_n_f16(c_cephes_exp_p4), y, x); |
| 236 | y = vfma_f16(vdup_n_f16(c_cephes_exp_p5), y, x); |
| 237 | |
| 238 | y = vfma_f16(x, y, z); |
| 239 | y = vadd_f16(y, one); |
| 240 | |
| 241 | /* build 2^n */ |
| 242 | int16x4_t mm; |
| 243 | mm = vcvt_s16_f16(fx); |
| 244 | mm = vadd_s16(mm, vdup_n_s16(0xf)); |
| 245 | mm = vshl_n_s16(mm, 10); |
| 246 | float16x4_t pow2n = vreinterpret_f16_s16(mm); |
| 247 | |
| 248 | y = vmul_f16(y, pow2n); |
| 249 | return y; |
| 250 | } |
| 251 | |
| 252 | static inline float16x8_t exp_ps_f16(float16x8_t x) |
| 253 | { |
| 254 | float16x8_t tmp, fx; |
| 255 | |
| 256 | float16x8_t one = vdupq_n_f16(1); |
| 257 | x = vminq_f16(x, vdupq_n_f16(c_exp_hi_f16)); |
| 258 | x = vmaxq_f16(x, vdupq_n_f16(c_exp_lo_f16)); |
| 259 | |
| 260 | /* express exp(x) as exp(g + n*log(2)) */ |
| 261 | #if defined(_MSC_VER) && !defined(__clang__) |
| 262 | float16x4_t _c_cephes_LOG2EF = vcvt_f16_f32(vdupq_n_f32(c_cephes_LOG2EF)); |
| 263 | fx = vfmaq_f16(vdupq_n_f16(0.5f), x, vcombine_f16(_c_cephes_LOG2EF, _c_cephes_LOG2EF)); |
| 264 | #else |
| 265 | fx = vfmaq_f16(vdupq_n_f16(0.5f), x, vdupq_n_f16(c_cephes_LOG2EF)); |
| 266 | #endif |
| 267 | |
| 268 | /* perform a floorf */ |
| 269 | tmp = vcvtq_f16_s16(vcvtq_s16_f16(fx)); |
| 270 | |
| 271 | /* if greater, substract 1 */ |
| 272 | uint16x8_t mask = vcgtq_f16(tmp, fx); |
| 273 | mask = vandq_u16(mask, vreinterpretq_u16_f16(one)); |
| 274 | |
| 275 | fx = vsubq_f16(tmp, vreinterpretq_f16_u16(mask)); |
| 276 | |
| 277 | #if defined(_MSC_VER) && !defined(__clang__) |
| 278 | float16x4_t _c_cephes_exp_C1 = vcvt_f16_f32(vdupq_n_f32(c_cephes_exp_C1)); |
| 279 | tmp = vmulq_f16(fx, vcombine_f16(_c_cephes_exp_C1, _c_cephes_exp_C1)); |
| 280 | float16x4_t _c_cephes_exp_C2 = vcvt_f16_f32(vdupq_n_f32(c_cephes_exp_C2)); |
| 281 | float16x8_t z = vmulq_f16(fx, vcombine_f16(_c_cephes_exp_C2, _c_cephes_exp_C2)); |
| 282 | #else |
| 283 | tmp = vmulq_f16(fx, vdupq_n_f16(c_cephes_exp_C1)); |
| 284 | float16x8_t z = vmulq_f16(fx, vdupq_n_f16(c_cephes_exp_C2)); |
| 285 | #endif |
| 286 | x = vsubq_f16(x, tmp); |
| 287 | x = vsubq_f16(x, z); |
| 288 | |
| 289 | z = vmulq_f16(x, x); |
| 290 | |
| 291 | float16x8_t y = vdupq_n_f16(c_cephes_exp_p0); |
| 292 | y = vfmaq_f16(vdupq_n_f16(c_cephes_exp_p1), y, x); |
| 293 | y = vfmaq_f16(vdupq_n_f16(c_cephes_exp_p2), y, x); |
| 294 | y = vfmaq_f16(vdupq_n_f16(c_cephes_exp_p3), y, x); |
| 295 | y = vfmaq_f16(vdupq_n_f16(c_cephes_exp_p4), y, x); |
| 296 | y = vfmaq_f16(vdupq_n_f16(c_cephes_exp_p5), y, x); |
| 297 | |
| 298 | y = vfmaq_f16(x, y, z); |
| 299 | y = vaddq_f16(y, one); |
| 300 | |
| 301 | /* build 2^n */ |
| 302 | int16x8_t mm; |
| 303 | mm = vcvtq_s16_f16(fx); |
| 304 | mm = vaddq_s16(mm, vdupq_n_s16(0xf)); |
| 305 | mm = vshlq_n_s16(mm, 10); |
| 306 | float16x8_t pow2n = vreinterpretq_f16_s16(mm); |
| 307 | |
| 308 | y = vmulq_f16(y, pow2n); |
| 309 | return y; |
| 310 | } |
| 311 | |
| 312 | #define c_minus_cephes_DP1 -0.78515625 |
| 313 | #define c_minus_cephes_DP2 -2.4187564849853515625e-4 |
| 314 | #define c_minus_cephes_DP3 -3.77489497744594108e-8 |
| 315 | #define c_sincof_p0 -1.9515295891E-4 |
| 316 | #define c_sincof_p1 8.3321608736E-3 |
| 317 | #define c_sincof_p2 -1.6666654611E-1 |
| 318 | #define c_coscof_p0 2.443315711809948E-005 |
| 319 | #define c_coscof_p1 -1.388731625493765E-003 |
| 320 | #define c_coscof_p2 4.166664568298827E-002 |
| 321 | #define c_cephes_FOPI 1.27323954473516 // 4 / M_PI |
| 322 | |
| 323 | /* evaluation of 4 sines & cosines at once. |
| 324 | * |
| 325 | * The code is the exact rewriting of the cephes sinf function. |
| 326 | * Precision is excellent as long as x < 8192 (I did not bother to |
| 327 | * take into account the special handling they have for greater values |
| 328 | * -- it does not return garbage for arguments over 8192, though, but |
| 329 | * the extra precision is missing). |
| 330 | * |
| 331 | * Note that it is such that sinf((float)M_PI) = 8.74e-8, which is the |
| 332 | * surprising but correct result. |
| 333 | * |
| 334 | * Note also that when you compute sin(x), cos(x) is available at |
| 335 | * almost no extra price so both sin_ps and cos_ps make use of |
| 336 | * sincos_ps.. |
| 337 | */ |
| 338 | static inline void sincos_ps_f16(float16x4_t x, float16x4_t* ysin, float16x4_t* ycos) |
| 339 | { |
| 340 | // any x |
| 341 | float16x4_t y; |
| 342 | |
| 343 | uint16x4_t emm2; |
| 344 | |
| 345 | uint16x4_t sign_mask_sin, sign_mask_cos; |
| 346 | sign_mask_sin = vclt_f16(x, vdup_n_f16(0)); |
| 347 | x = vabs_f16(x); |
| 348 | |
| 349 | /* scale by 4/Pi */ |
| 350 | #if defined(_MSC_VER) && !defined(__clang__) |
| 351 | float16x4_t _c_cephes_FOPI = vcvt_f16_f32(vdupq_n_f32(c_cephes_FOPI)); |
| 352 | y = vmul_f16(x, _c_cephes_FOPI); |
| 353 | #else |
| 354 | y = vmul_f16(x, vdup_n_f16(c_cephes_FOPI)); |
| 355 | #endif |
| 356 | |
| 357 | /* store the integer part of y in mm0 */ |
| 358 | emm2 = vcvt_u16_f16(y); |
| 359 | /* j=(j+1) & (~1) (see the cephes sources) */ |
| 360 | emm2 = vadd_u16(emm2, vdup_n_u16(1)); |
| 361 | emm2 = vand_u16(emm2, vdup_n_u16(~1)); |
| 362 | y = vcvt_f16_u16(emm2); |
| 363 | |
| 364 | /* get the polynom selection mask |
| 365 | * there is one polynom for 0 <= x <= Pi/4 |
| 366 | * and another one for Pi/4<x<=Pi/2 |
| 367 | * |
| 368 | * Both branches will be computed. |
| 369 | */ |
| 370 | uint16x4_t poly_mask = vtst_u16(emm2, vdup_n_u16(2)); |
| 371 | |
| 372 | /* The magic pass: "Extended precision modular arithmetic" |
| 373 | * x = ((x - y * DP1) - y * DP2) - y * DP3; */ |
| 374 | #if defined(_MSC_VER) && !defined(__clang__) |
| 375 | float16x4_t _c_minus_cephes_DP1 = vcvt_f16_f32(vdupq_n_f32(c_minus_cephes_DP1)); |
| 376 | float16x4_t _c_minus_cephes_DP2 = vcvt_f16_f32(vdupq_n_f32(c_minus_cephes_DP2)); |
| 377 | float16x4_t _c_minus_cephes_DP3 = vcvt_f16_f32(vdupq_n_f32(c_minus_cephes_DP3)); |
| 378 | x = vfma_f16(x, y, _c_minus_cephes_DP1); |
| 379 | x = vfma_f16(x, y, _c_minus_cephes_DP2); |
| 380 | x = vfma_f16(x, y, _c_minus_cephes_DP3); |
| 381 | #else |
| 382 | x = vfma_f16(x, y, vdup_n_f16(c_minus_cephes_DP1)); |
| 383 | x = vfma_f16(x, y, vdup_n_f16(c_minus_cephes_DP2)); |
| 384 | x = vfma_f16(x, y, vdup_n_f16(c_minus_cephes_DP3)); |
| 385 | #endif |
| 386 | |
| 387 | sign_mask_sin = veor_u16(sign_mask_sin, vtst_u16(emm2, vdup_n_u16(4))); |
| 388 | sign_mask_cos = vtst_u16(vsub_u16(emm2, vdup_n_u16(2)), vdup_n_u16(4)); |
| 389 | |
| 390 | /* Evaluate the first polynom (0 <= x <= Pi/4) in y1, |
| 391 | * and the second polynom (Pi/4 <= x <= 0) in y2 */ |
| 392 | float16x4_t z = vmul_f16(x, x); |
| 393 | float16x4_t y1, y2; |
| 394 | |
| 395 | y1 = vfma_f16(vdup_n_f16(c_coscof_p1), z, vdup_n_f16(c_coscof_p0)); |
| 396 | y2 = vfma_f16(vdup_n_f16(c_sincof_p1), z, vdup_n_f16(c_sincof_p0)); |
| 397 | y1 = vfma_f16(vdup_n_f16(c_coscof_p2), y1, z); |
| 398 | y2 = vfma_f16(vdup_n_f16(c_sincof_p2), y2, z); |
| 399 | y1 = vmul_f16(y1, z); |
| 400 | y2 = vmul_f16(y2, z); |
| 401 | y1 = vmul_f16(y1, z); |
| 402 | y1 = vfms_f16(y1, z, vdup_n_f16(0.5f)); |
| 403 | y2 = vfma_f16(x, y2, x); |
| 404 | y1 = vadd_f16(y1, vdup_n_f16(1)); |
| 405 | |
| 406 | /* select the correct result from the two polynoms */ |
| 407 | float16x4_t ys = vbsl_f16(poly_mask, y1, y2); |
| 408 | float16x4_t yc = vbsl_f16(poly_mask, y2, y1); |
| 409 | *ysin = vbsl_f16(sign_mask_sin, vneg_f16(ys), ys); |
| 410 | *ycos = vbsl_f16(sign_mask_cos, yc, vneg_f16(yc)); |
| 411 | } |
| 412 | |
| 413 | static inline void sincos_ps_f16(float16x8_t x, float16x8_t* ysin, float16x8_t* ycos) |
| 414 | { |
| 415 | // any x |
| 416 | float16x8_t y; |
| 417 | |
| 418 | uint16x8_t emm2; |
| 419 | |
| 420 | uint16x8_t sign_mask_sin, sign_mask_cos; |
| 421 | sign_mask_sin = vcltq_f16(x, vdupq_n_f16(0)); |
| 422 | x = vabsq_f16(x); |
| 423 | |
| 424 | /* scale by 4/Pi */ |
| 425 | #if defined(_MSC_VER) && !defined(__clang__) |
| 426 | float16x4_t _c_cephes_FOPI = vcvt_f16_f32(vdupq_n_f32(c_cephes_FOPI)); |
| 427 | y = vmulq_f16(x, vcombine_f16(_c_cephes_FOPI, _c_cephes_FOPI)); |
| 428 | #else |
| 429 | y = vmulq_f16(x, vdupq_n_f16(c_cephes_FOPI)); |
| 430 | #endif |
| 431 | |
| 432 | /* store the integer part of y in mm0 */ |
| 433 | emm2 = vcvtq_u16_f16(y); |
| 434 | /* j=(j+1) & (~1) (see the cephes sources) */ |
| 435 | emm2 = vaddq_u16(emm2, vdupq_n_u16(1)); |
| 436 | emm2 = vandq_u16(emm2, vdupq_n_u16(~1)); |
| 437 | y = vcvtq_f16_u16(emm2); |
| 438 | |
| 439 | /* get the polynom selection mask |
| 440 | * there is one polynom for 0 <= x <= Pi/4 |
| 441 | * and another one for Pi/4<x<=Pi/2 |
| 442 | * |
| 443 | * Both branches will be computed. |
| 444 | */ |
| 445 | uint16x8_t poly_mask = vtstq_u16(emm2, vdupq_n_u16(2)); |
| 446 | |
| 447 | /* The magic pass: "Extended precision modular arithmetic" |
| 448 | * x = ((x - y * DP1) - y * DP2) - y * DP3; */ |
| 449 | #if defined(_MSC_VER) && !defined(__clang__) |
| 450 | float16x4_t _c_minus_cephes_DP1 = vcvt_f16_f32(vdupq_n_f32(c_minus_cephes_DP1)); |
| 451 | float16x4_t _c_minus_cephes_DP2 = vcvt_f16_f32(vdupq_n_f32(c_minus_cephes_DP2)); |
| 452 | float16x4_t _c_minus_cephes_DP3 = vcvt_f16_f32(vdupq_n_f32(c_minus_cephes_DP3)); |
| 453 | x = vfmaq_f16(x, y, vcombine_f16(_c_minus_cephes_DP1, _c_minus_cephes_DP1)); |
| 454 | x = vfmaq_f16(x, y, vcombine_f16(_c_minus_cephes_DP2, _c_minus_cephes_DP2)); |
| 455 | x = vfmaq_f16(x, y, vcombine_f16(_c_minus_cephes_DP3, _c_minus_cephes_DP3)); |
| 456 | #else |
| 457 | x = vfmaq_f16(x, y, vdupq_n_f16(c_minus_cephes_DP1)); |
| 458 | x = vfmaq_f16(x, y, vdupq_n_f16(c_minus_cephes_DP2)); |
| 459 | x = vfmaq_f16(x, y, vdupq_n_f16(c_minus_cephes_DP3)); |
| 460 | #endif |
| 461 | |
| 462 | sign_mask_sin = veorq_u16(sign_mask_sin, vtstq_u16(emm2, vdupq_n_u16(4))); |
| 463 | sign_mask_cos = vtstq_u16(vsubq_u16(emm2, vdupq_n_u16(2)), vdupq_n_u16(4)); |
| 464 | |
| 465 | /* Evaluate the first polynom (0 <= x <= Pi/4) in y1, |
| 466 | * and the second polynom (Pi/4 <= x <= 0) in y2 */ |
| 467 | float16x8_t z = vmulq_f16(x, x); |
| 468 | float16x8_t y1, y2; |
| 469 | |
| 470 | y1 = vfmaq_f16(vdupq_n_f16(c_coscof_p1), z, vdupq_n_f16(c_coscof_p0)); |
| 471 | y2 = vfmaq_f16(vdupq_n_f16(c_sincof_p1), z, vdupq_n_f16(c_sincof_p0)); |
| 472 | y1 = vfmaq_f16(vdupq_n_f16(c_coscof_p2), y1, z); |
| 473 | y2 = vfmaq_f16(vdupq_n_f16(c_sincof_p2), y2, z); |
| 474 | y1 = vmulq_f16(y1, z); |
| 475 | y2 = vmulq_f16(y2, z); |
| 476 | y1 = vmulq_f16(y1, z); |
| 477 | y1 = vfmsq_f16(y1, z, vdupq_n_f16(0.5f)); |
| 478 | y2 = vfmaq_f16(x, y2, x); |
| 479 | y1 = vaddq_f16(y1, vdupq_n_f16(1)); |
| 480 | |
| 481 | /* select the correct result from the two polynoms */ |
| 482 | float16x8_t ys = vbslq_f16(poly_mask, y1, y2); |
| 483 | float16x8_t yc = vbslq_f16(poly_mask, y2, y1); |
| 484 | *ysin = vbslq_f16(sign_mask_sin, vnegq_f16(ys), ys); |
| 485 | *ycos = vbslq_f16(sign_mask_cos, yc, vnegq_f16(yc)); |
| 486 | } |
| 487 | |
| 488 | static inline float16x4_t sin_ps_f16(float16x4_t x) |
| 489 | { |
| 490 | float16x4_t ysin, ycos; |
| 491 | sincos_ps_f16(x, &ysin, &ycos); |
| 492 | return ysin; |
| 493 | } |
| 494 | |
| 495 | static inline float16x8_t sin_ps_f16(float16x8_t x) |
| 496 | { |
| 497 | float16x8_t ysin, ycos; |
| 498 | sincos_ps_f16(x, &ysin, &ycos); |
| 499 | return ysin; |
| 500 | } |
| 501 | |
| 502 | static inline float16x4_t cos_ps_f16(float16x4_t x) |
| 503 | { |
| 504 | float16x4_t ysin, ycos; |
| 505 | sincos_ps_f16(x, &ysin, &ycos); |
| 506 | return ycos; |
| 507 | } |
| 508 | |
| 509 | static inline float16x8_t cos_ps_f16(float16x8_t x) |
| 510 | { |
| 511 | float16x8_t ysin, ycos; |
| 512 | sincos_ps_f16(x, &ysin, &ycos); |
| 513 | return ycos; |
| 514 | } |
| 515 | |
| 516 | #define c_tanh_tiny 1e-4f |
| 517 | #define c_tanh_hi 9.0f |
| 518 | // The monomial coefficients of the numerator polynomial (odd). |
| 519 | #define c_tanh_alpha_1 4.89352455891786e-3f |
| 520 | #define c_tanh_alpha_3 6.37261928875436e-4f |
| 521 | #define c_tanh_alpha_5 1.48572235717979e-5f |
| 522 | #define c_tanh_alpha_7 5.12229709037114e-8f |
| 523 | #define c_tanh_alpha_9 -8.60467152213735e-11f |
| 524 | #define c_tanh_alpha_11 2.00018790482477e-13f |
| 525 | #define c_tanh_alpha_13 -2.76076847742355e-16f |
| 526 | // The monomial coefficients of the denominator polynomial (even). |
| 527 | #define c_tanh_beta_0 4.89352518554385e-3f |
| 528 | #define c_tanh_beta_2 2.26843463243900e-3f |
| 529 | #define c_tanh_beta_4 1.18534705686654e-4f |
| 530 | #define c_tanh_beta_6 1.19825839466702e-6f |
| 531 | |
| 532 | /* Single precision hyperbolic tangent computed for 4 simultaneous float */ |
| 533 | static inline float16x4_t tanh_ps_f16(float16x4_t x) |
| 534 | { |
| 535 | float16x4_t x2 = vabs_f16(x); |
| 536 | |
| 537 | uint16x4_t tiny_mask = vcge_f16(x2, vdup_n_f16(c_tanh_tiny)); |
| 538 | |
| 539 | // clamp the inputs to the range [-9, 9] since anything outside |
| 540 | // this range is -/+1.0f in single-precision. |
| 541 | x2 = (float16x4_t)(vbsl_u16(vcge_f16(vdup_n_f16(c_tanh_hi), x2), (uint16x4_t)(x2), (uint16x4_t)(vdup_n_f16(c_tanh_hi)))); |
| 542 | |
| 543 | // since the polynomials are odd/even, we need x**2. |
| 544 | float16x4_t z = vmul_f16(x2, x2); |
| 545 | |
| 546 | // evaluate the numerator polynomial y. |
| 547 | float16x4_t y = vdup_n_f16(c_tanh_alpha_13); |
| 548 | y = vfma_f16(vdup_n_f16(c_tanh_alpha_11), y, z); |
| 549 | y = vfma_f16(vdup_n_f16(c_tanh_alpha_9), y, z); |
| 550 | y = vfma_f16(vdup_n_f16(c_tanh_alpha_7), y, z); |
| 551 | y = vfma_f16(vdup_n_f16(c_tanh_alpha_5), y, z); |
| 552 | y = vfma_f16(vdup_n_f16(c_tanh_alpha_3), y, z); |
| 553 | y = vfma_f16(vdup_n_f16(c_tanh_alpha_1), y, z); |
| 554 | y = vmul_f16(y, x2); |
| 555 | |
| 556 | // evaluate the denominator polynomial w. |
| 557 | float16x4_t w = vdup_n_f16(c_tanh_beta_6); |
| 558 | w = vfma_f16(vdup_n_f16(c_tanh_beta_4), w, z); |
| 559 | w = vfma_f16(vdup_n_f16(c_tanh_beta_2), w, z); |
| 560 | w = vfma_f16(vdup_n_f16(c_tanh_beta_0), w, z); |
| 561 | |
| 562 | // divide the numerator by the denominator. |
| 563 | y = vdiv_f16(y, w); |
| 564 | |
| 565 | // reinstate the sign. |
| 566 | y = (float16x4_t)(vbsl_u16(vdup_n_u16(1u << 15), (uint16x4_t)(x), (uint16x4_t)(y))); |
| 567 | |
| 568 | // when the argument is very small in magnitude it's more accurate to just return it. |
| 569 | y = (float16x4_t)(vbsl_u16(tiny_mask, (uint16x4_t)(y), (uint16x4_t)(x))); |
| 570 | |
| 571 | return y; |
| 572 | } |
| 573 | |
| 574 | static inline float16x8_t tanh_ps_f16(float16x8_t x) |
| 575 | { |
| 576 | float16x8_t x2 = vabsq_f16(x); |
| 577 | |
| 578 | uint16x8_t tiny_mask = vcgeq_f16(x2, vdupq_n_f16(c_tanh_tiny)); |
| 579 | |
| 580 | // clamp the inputs to the range [-9, 9] since anything outside |
| 581 | // this range is -/+1.0f in single-precision. |
| 582 | x2 = vreinterpretq_f16_u16(vbslq_u16(vcgeq_f16(vdupq_n_f16(c_tanh_hi), x2), vreinterpretq_u16_f16(x2), vreinterpretq_u16_f16(vdupq_n_f16(c_tanh_hi)))); |
| 583 | |
| 584 | // since the polynomials are odd/even, we need x**2. |
| 585 | float16x8_t z = vmulq_f16(x2, x2); |
| 586 | |
| 587 | // evaluate the numerator polynomial y. |
| 588 | float16x8_t y = vdupq_n_f16(c_tanh_alpha_13); |
| 589 | y = vfmaq_f16(vdupq_n_f16(c_tanh_alpha_11), y, z); |
| 590 | y = vfmaq_f16(vdupq_n_f16(c_tanh_alpha_9), y, z); |
| 591 | y = vfmaq_f16(vdupq_n_f16(c_tanh_alpha_7), y, z); |
| 592 | y = vfmaq_f16(vdupq_n_f16(c_tanh_alpha_5), y, z); |
| 593 | y = vfmaq_f16(vdupq_n_f16(c_tanh_alpha_3), y, z); |
| 594 | y = vfmaq_f16(vdupq_n_f16(c_tanh_alpha_1), y, z); |
| 595 | y = vmulq_f16(y, x2); |
| 596 | |
| 597 | // evaluate the denominator polynomial w. |
| 598 | float16x8_t w = vdupq_n_f16(c_tanh_beta_6); |
| 599 | w = vfmaq_f16(vdupq_n_f16(c_tanh_beta_4), w, z); |
| 600 | w = vfmaq_f16(vdupq_n_f16(c_tanh_beta_2), w, z); |
| 601 | w = vfmaq_f16(vdupq_n_f16(c_tanh_beta_0), w, z); |
| 602 | |
| 603 | // divide the numerator by the denominator. |
| 604 | y = vdivq_f16(y, w); |
| 605 | |
| 606 | // reinstate the sign. |
| 607 | y = vreinterpretq_f16_u16(vbslq_u16(vdupq_n_u16(1u << 15), vreinterpretq_u16_f16(x), vreinterpretq_u16_f16(y))); |
| 608 | |
| 609 | // when the argument is very small in magnitude it's more accurate to just return it. |
| 610 | y = vreinterpretq_f16_u16(vbslq_u16(tiny_mask, vreinterpretq_u16_f16(y), vreinterpretq_u16_f16(x))); |
| 611 | |
| 612 | return y; |
| 613 | } |
| 614 | |
| 615 | static inline float16x4_t sigmoid_ps_f16(float16x4_t _v) |
| 616 | { |
| 617 | float16x4_t _one = vdup_n_f16(1.f); |
| 618 | _v = vneg_f16(_v); |
| 619 | _v = exp_ps_f16(_v); |
| 620 | _v = vadd_f16(_v, _one); |
| 621 | return vdiv_f16(_one, _v); |
| 622 | } |
| 623 | |
| 624 | static inline float16x8_t sigmoid_ps_f16(float16x8_t _v) |
| 625 | { |
| 626 | float16x8_t _one = vdupq_n_f16(1.f); |
| 627 | _v = vnegq_f16(_v); |
| 628 | _v = exp_ps_f16(_v); |
| 629 | _v = vaddq_f16(_v, _one); |
| 630 | return vdivq_f16(_one, _v); |
| 631 | } |
| 632 | |
| 633 | #endif // NEON_MATHFUN_FP16S_H |