Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | |
Manuel Bottini | ed75326 | 2019-05-15 15:30:47 +0100 | [diff] [blame] | 25 | #include <cmath> |
| 26 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | namespace arm_compute |
| 28 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 29 | /** Exponent polynomial coefficients */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | const std::array<float32x4_t, 8> exp_tab = |
| 31 | { |
| 32 | { |
| 33 | vdupq_n_f32(1.f), |
| 34 | vdupq_n_f32(0.0416598916054f), |
| 35 | vdupq_n_f32(0.500000596046f), |
| 36 | vdupq_n_f32(0.0014122662833f), |
| 37 | vdupq_n_f32(1.00000011921f), |
| 38 | vdupq_n_f32(0.00833693705499f), |
| 39 | vdupq_n_f32(0.166665703058f), |
| 40 | vdupq_n_f32(0.000195780929062f), |
| 41 | } |
| 42 | }; |
| 43 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 44 | /** Logarithm polynomial coefficients */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 45 | const std::array<float32x4_t, 8> log_tab = |
| 46 | { |
| 47 | { |
| 48 | vdupq_n_f32(-2.29561495781f), |
| 49 | vdupq_n_f32(-2.47071170807f), |
| 50 | vdupq_n_f32(-5.68692588806f), |
| 51 | vdupq_n_f32(-0.165253549814f), |
| 52 | vdupq_n_f32(5.17591238022f), |
| 53 | vdupq_n_f32(0.844007015228f), |
| 54 | vdupq_n_f32(4.58445882797f), |
| 55 | vdupq_n_f32(0.0141278216615f), |
| 56 | } |
| 57 | }; |
| 58 | |
Manuel Bottini | ed75326 | 2019-05-15 15:30:47 +0100 | [diff] [blame] | 59 | /** Sin polynomial coefficients */ |
| 60 | constexpr float te_sin_coeff2 = 0.166666666666f; // 1/(2*3) |
| 61 | constexpr float te_sin_coeff3 = 0.05f; // 1/(4*5) |
| 62 | constexpr float te_sin_coeff4 = 0.023809523810f; // 1/(6*7) |
| 63 | constexpr float te_sin_coeff5 = 0.013888888889f; // 1/(8*9) |
| 64 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 65 | #ifndef DOXYGEN_SKIP_THIS |
Georgios Pinitas | d8e765b | 2017-08-02 13:44:33 +0100 | [diff] [blame] | 66 | inline float32x4_t vfloorq_f32(float32x4_t val) |
| 67 | { |
| 68 | static const float32x4_t CONST_1 = vdupq_n_f32(1.f); |
| 69 | |
| 70 | const int32x4_t z = vcvtq_s32_f32(val); |
| 71 | const float32x4_t r = vcvtq_f32_s32(z); |
| 72 | |
| 73 | return vbslq_f32(vcgtq_f32(r, val), vsubq_f32(r, CONST_1), r); |
| 74 | } |
| 75 | |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 76 | inline float32x4_t vroundq_rte_f32(float32x4_t val) |
| 77 | { |
| 78 | #ifdef __aarch64__ |
| 79 | return vrndnq_f32(val); |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 80 | #else // __aarch64__ |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 81 | static const float32x4_t CONST_HALF_FLOAT = vdupq_n_f32(0.5f); |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 82 | static const float32x4_t CONST_1_FLOAT = vdupq_n_f32(1.f); |
| 83 | static const int32x4_t CONST_1_INT = vdupq_n_s32(1); |
| 84 | const float32x4_t floor_val = vfloorq_f32(val); |
| 85 | const float32x4_t diff = vsubq_f32(val, floor_val); |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * Select the floor value when (diff<0.5 || (diff==0.5 && floor_val%2==0). |
| 89 | * This condition is checked by vorrq_u32(vcltq_f32(diff, CONST_HALF_FLOAT) ,vandq_u32(vceqq_f32(diff, CONST_HALF_FLOAT) , vmvnq_u32(vtstq_s32(vandq_s32(vcvtq_s32_f32(floor_val), CONST_1_INT),CONST_1_INT)))) |
| 90 | */ |
| 91 | |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 92 | return vbslq_f32(vorrq_u32(vcltq_f32(diff, CONST_HALF_FLOAT), vandq_u32(vceqq_f32(diff, CONST_HALF_FLOAT), vmvnq_u32(vtstq_s32(vandq_s32(vcvtq_s32_f32(floor_val), CONST_1_INT), CONST_1_INT)))), |
| 93 | floor_val, vaddq_f32(floor_val, CONST_1_FLOAT)); |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 94 | #endif // __aarch64__ |
| 95 | } |
| 96 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 97 | inline float32x2_t vinvsqrt_f32(float32x2_t x) |
| 98 | { |
| 99 | float32x2_t sqrt_reciprocal = vrsqrte_f32(x); |
| 100 | sqrt_reciprocal = vmul_f32(vrsqrts_f32(vmul_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 101 | sqrt_reciprocal = vmul_f32(vrsqrts_f32(vmul_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 102 | |
| 103 | return sqrt_reciprocal; |
| 104 | } |
| 105 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 106 | inline float32x4_t vinvsqrtq_f32(float32x4_t x) |
| 107 | { |
| 108 | float32x4_t sqrt_reciprocal = vrsqrteq_f32(x); |
| 109 | sqrt_reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 110 | sqrt_reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 111 | |
| 112 | return sqrt_reciprocal; |
| 113 | } |
| 114 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 115 | inline float32x2_t vinv_f32(float32x2_t x) |
| 116 | { |
| 117 | float32x2_t recip = vrecpe_f32(x); |
| 118 | recip = vmul_f32(vrecps_f32(x, recip), recip); |
| 119 | recip = vmul_f32(vrecps_f32(x, recip), recip); |
| 120 | return recip; |
| 121 | } |
| 122 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 123 | inline float32x4_t vinvq_f32(float32x4_t x) |
| 124 | { |
| 125 | float32x4_t recip = vrecpeq_f32(x); |
| 126 | recip = vmulq_f32(vrecpsq_f32(x, recip), recip); |
| 127 | recip = vmulq_f32(vrecpsq_f32(x, recip), recip); |
| 128 | return recip; |
| 129 | } |
| 130 | |
| 131 | inline float32x4_t vtaylor_polyq_f32(float32x4_t x, const std::array<float32x4_t, 8> &coeffs) |
| 132 | { |
| 133 | float32x4_t A = vmlaq_f32(coeffs[0], coeffs[4], x); |
| 134 | float32x4_t B = vmlaq_f32(coeffs[2], coeffs[6], x); |
| 135 | float32x4_t C = vmlaq_f32(coeffs[1], coeffs[5], x); |
| 136 | float32x4_t D = vmlaq_f32(coeffs[3], coeffs[7], x); |
| 137 | float32x4_t x2 = vmulq_f32(x, x); |
| 138 | float32x4_t x4 = vmulq_f32(x2, x2); |
| 139 | float32x4_t res = vmlaq_f32(vmlaq_f32(A, B, x2), vmlaq_f32(C, D, x2), x4); |
| 140 | return res; |
| 141 | } |
| 142 | |
| 143 | inline float32x4_t vexpq_f32(float32x4_t x) |
| 144 | { |
Georgios Pinitas | ee12254 | 2017-06-26 15:54:06 +0100 | [diff] [blame] | 145 | static const float32x4_t CONST_LN2 = vdupq_n_f32(0.6931471805f); // ln(2) |
| 146 | static const float32x4_t CONST_INV_LN2 = vdupq_n_f32(1.4426950408f); // 1/ln(2) |
| 147 | static const float32x4_t CONST_0 = vdupq_n_f32(0.f); |
| 148 | static const int32x4_t CONST_NEGATIVE_126 = vdupq_n_s32(-126); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 149 | |
| 150 | // Perform range reduction [-log(2),log(2)] |
| 151 | int32x4_t m = vcvtq_s32_f32(vmulq_f32(x, CONST_INV_LN2)); |
| 152 | float32x4_t val = vmlsq_f32(x, vcvtq_f32_s32(m), CONST_LN2); |
| 153 | |
| 154 | // Polynomial Approximation |
| 155 | float32x4_t poly = vtaylor_polyq_f32(val, exp_tab); |
| 156 | |
| 157 | // Reconstruct |
Georgios Pinitas | ee12254 | 2017-06-26 15:54:06 +0100 | [diff] [blame] | 158 | poly = vreinterpretq_f32_s32(vqaddq_s32(vreinterpretq_s32_f32(poly), vqshlq_n_s32(m, 23))); |
| 159 | poly = vbslq_f32(vcltq_s32(m, CONST_NEGATIVE_126), CONST_0, poly); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 160 | |
| 161 | return poly; |
| 162 | } |
| 163 | |
| 164 | inline float32x4_t vlogq_f32(float32x4_t x) |
| 165 | { |
| 166 | static const int32x4_t CONST_127 = vdupq_n_s32(127); // 127 |
| 167 | static const float32x4_t CONST_LN2 = vdupq_n_f32(0.6931471805f); // ln(2) |
| 168 | |
| 169 | // Extract exponent |
| 170 | int32x4_t m = vsubq_s32(vreinterpretq_s32_u32(vshrq_n_u32(vreinterpretq_u32_f32(x), 23)), CONST_127); |
| 171 | float32x4_t val = vreinterpretq_f32_s32(vsubq_s32(vreinterpretq_s32_f32(x), vshlq_n_s32(m, 23))); |
| 172 | |
| 173 | // Polynomial Approximation |
| 174 | float32x4_t poly = vtaylor_polyq_f32(val, log_tab); |
| 175 | |
| 176 | // Reconstruct |
| 177 | poly = vmlaq_f32(poly, vcvtq_f32_s32(m), CONST_LN2); |
| 178 | |
| 179 | return poly; |
| 180 | } |
| 181 | |
| 182 | inline float32x4_t vtanhq_f32(float32x4_t val) |
| 183 | { |
| 184 | static const float32x4_t CONST_1 = vdupq_n_f32(1.f); |
| 185 | static const float32x4_t CONST_2 = vdupq_n_f32(2.f); |
| 186 | static const float32x4_t CONST_MIN_TANH = vdupq_n_f32(-10.f); |
| 187 | static const float32x4_t CONST_MAX_TANH = vdupq_n_f32(10.f); |
| 188 | |
| 189 | float32x4_t x = vminq_f32(vmaxq_f32(val, CONST_MIN_TANH), CONST_MAX_TANH); |
| 190 | float32x4_t exp2x = vexpq_f32(vmulq_f32(CONST_2, x)); |
| 191 | float32x4_t num = vsubq_f32(exp2x, CONST_1); |
| 192 | float32x4_t den = vaddq_f32(exp2x, CONST_1); |
| 193 | float32x4_t tanh = vmulq_f32(num, vinvq_f32(den)); |
| 194 | return tanh; |
| 195 | } |
| 196 | |
| 197 | inline float32x4_t vpowq_f32(float32x4_t val, float32x4_t n) |
| 198 | { |
| 199 | return vexpq_f32(vmulq_f32(n, vlogq_f32(val))); |
| 200 | } |
Manuel Bottini | ed75326 | 2019-05-15 15:30:47 +0100 | [diff] [blame] | 201 | |
| 202 | inline float32x4_t vsinq_f32(float32x4_t val) |
| 203 | { |
| 204 | const float32x4_t pi_v = vdupq_n_f32(M_PI); |
| 205 | const float32x4_t pio2_v = vdupq_n_f32(M_PI / 2); |
| 206 | const float32x4_t ipi_v = vdupq_n_f32(1 / M_PI); |
| 207 | |
| 208 | //Find positive or negative |
| 209 | const int32x4_t c_v = vabsq_s32(vcvtq_s32_f32(vmulq_f32(val, ipi_v))); |
| 210 | const uint32x4_t sign_v = vcleq_f32(val, vdupq_n_f32(0)); |
| 211 | const uint32x4_t odd_v = vandq_u32(vreinterpretq_u32_s32(c_v), vdupq_n_u32(1)); |
| 212 | |
| 213 | uint32x4_t neg_v = veorq_u32(odd_v, sign_v); |
| 214 | |
| 215 | //Modulus a - (n * int(a*(1/n))) |
| 216 | float32x4_t ma = vsubq_f32(vabsq_f32(val), vmulq_f32(pi_v, vcvtq_f32_s32(c_v))); |
| 217 | const uint32x4_t reb_v = vcgeq_f32(ma, pio2_v); |
| 218 | |
| 219 | //Rebase a between 0 and pi/2 |
| 220 | ma = vbslq_f32(reb_v, vsubq_f32(pi_v, ma), ma); |
| 221 | |
| 222 | //Taylor series |
| 223 | const float32x4_t ma2 = vmulq_f32(ma, ma); |
| 224 | |
| 225 | //2nd elem: x^3 / 3! |
| 226 | float32x4_t elem = vmulq_f32(vmulq_f32(ma, ma2), vdupq_n_f32(te_sin_coeff2)); |
| 227 | float32x4_t res = vsubq_f32(ma, elem); |
| 228 | |
| 229 | //3rd elem: x^5 / 5! |
| 230 | elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff3)); |
| 231 | res = vaddq_f32(res, elem); |
| 232 | |
| 233 | //4th elem: x^7 / 7!float32x2_t vsin_f32(float32x2_t val) |
| 234 | elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff4)); |
| 235 | res = vsubq_f32(res, elem); |
| 236 | |
| 237 | //5th elem: x^9 / 9! |
| 238 | elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff5)); |
| 239 | res = vaddq_f32(res, elem); |
| 240 | |
| 241 | //Change of sign |
| 242 | neg_v = vshlq_n_u32(neg_v, 31); |
| 243 | res = vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(res), neg_v)); |
| 244 | return res; |
| 245 | } |
| 246 | |
| 247 | inline float32x2_t vsin_f32(float32x2_t val) |
| 248 | { |
| 249 | const float32x2_t pi_v = vdup_n_f32(M_PI); |
| 250 | const float32x2_t pio2_v = vdup_n_f32(M_PI / 2); |
| 251 | const float32x2_t ipi_v = vdup_n_f32(1 / M_PI); |
| 252 | |
| 253 | //Find positive or negative |
| 254 | const int32x2_t c_v = vabs_s32(vcvt_s32_f32(vmul_f32(val, ipi_v))); |
| 255 | const uint32x2_t sign_v = vcle_f32(val, vdup_n_f32(0)); |
| 256 | const uint32x2_t odd_v = vand_u32(vreinterpret_u32_s32(c_v), vdup_n_u32(1)); |
| 257 | |
| 258 | uint32x2_t neg_v = veor_u32(odd_v, sign_v); |
| 259 | |
| 260 | //Modulus a - (n * int(a*(1/n))) |
| 261 | float32x2_t ma = vsub_f32(vabs_f32(val), vmul_f32(pi_v, vcvt_f32_s32(c_v))); |
| 262 | const uint32x2_t reb_v = vcge_f32(ma, pio2_v); |
| 263 | |
| 264 | //Rebase a between 0 and pi/2 |
| 265 | ma = vbsl_f32(reb_v, vsub_f32(pi_v, ma), ma); |
| 266 | |
| 267 | //Taylor series |
| 268 | const float32x2_t ma2 = vmul_f32(ma, ma); |
| 269 | |
| 270 | //2nd elem: x^3 / 3! |
| 271 | float32x2_t elem = vmul_f32(vmul_f32(ma, ma2), vdup_n_f32(te_sin_coeff2)); |
| 272 | float32x2_t res = vsub_f32(ma, elem); |
| 273 | |
| 274 | //3rd elem: x^5 / 5! |
| 275 | elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff3)); |
| 276 | res = vadd_f32(res, elem); |
| 277 | |
| 278 | //4th elem: x^7 / 7!float32x2_t vsin_f32(float32x2_t val) |
| 279 | elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff4)); |
| 280 | res = vsub_f32(res, elem); |
| 281 | |
| 282 | //5th elem: x^9 / 9! |
| 283 | elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff5)); |
| 284 | res = vadd_f32(res, elem); |
| 285 | |
| 286 | //Change of sign |
| 287 | neg_v = vshl_n_u32(neg_v, 31); |
| 288 | res = vreinterpret_f32_u32(veor_u32(vreinterpret_u32_f32(res), neg_v)); |
| 289 | return res; |
| 290 | } |
| 291 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 292 | #endif /* DOXYGEN_SKIP_THIS */ |
| 293 | |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 294 | inline int32x4_t rounding_divide_by_pow2(int32x4_t x, int exponent) |
| 295 | { |
| 296 | const int32x4_t shift_vec = vdupq_n_s32(-exponent); |
| 297 | const int32x4_t fixup = vshrq_n_s32(vandq_s32(x, shift_vec), 31); |
| 298 | const int32x4_t fixed_up_x = vqaddq_s32(x, fixup); |
| 299 | return vrshlq_s32(fixed_up_x, shift_vec); |
| 300 | } |
| 301 | |
| 302 | inline int32_t rounding_divide_by_pow2(int32_t x, int exponent) |
| 303 | { |
| 304 | const int32_t mask = (1 << exponent) - 1; |
| 305 | const int32_t threshold = (mask >> 1) + (x < 0 ? 1 : 0); |
| 306 | return (x >> exponent) + ((x & mask) > threshold ? 1 : 0); |
| 307 | } |
| 308 | |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 309 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 310 | /** Exponent polynomial coefficients */ |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 311 | /** Logarithm polynomial coefficients */ |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 312 | #ifndef DOXYGEN_SKIP_THIS |
Georgios Pinitas | 565bf2d | 2018-08-31 11:46:49 +0100 | [diff] [blame] | 313 | inline float16x8_t vfloorq_f16(float16x8_t val) |
| 314 | { |
| 315 | static const float16x8_t CONST_1 = vdupq_n_f16(1.f); |
| 316 | |
| 317 | const int16x8_t z = vcvtq_s16_f16(val); |
| 318 | const float16x8_t r = vcvtq_f16_s16(z); |
| 319 | |
| 320 | return vbslq_f16(vcgtq_f16(r, val), vsubq_f16(r, CONST_1), r); |
| 321 | } |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 322 | |
| 323 | inline float16x8_t vroundq_rte_f16(float16x8_t val) |
| 324 | { |
| 325 | return vrndnq_f16(val); |
| 326 | } |
| 327 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 328 | inline float16x4_t vinvsqrt_f16(float16x4_t x) |
| 329 | { |
| 330 | float16x4_t sqrt_reciprocal = vrsqrte_f16(x); |
| 331 | sqrt_reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 332 | sqrt_reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 333 | return sqrt_reciprocal; |
| 334 | } |
| 335 | |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 336 | inline float16x8_t vinvsqrtq_f16(float16x8_t x) |
| 337 | { |
| 338 | float16x8_t sqrt_reciprocal = vrsqrteq_f16(x); |
| 339 | sqrt_reciprocal = vmulq_f16(vrsqrtsq_f16(vmulq_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 340 | sqrt_reciprocal = vmulq_f16(vrsqrtsq_f16(vmulq_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 341 | return sqrt_reciprocal; |
| 342 | } |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 343 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 344 | inline float16x4_t vinv_f16(float16x4_t x) |
| 345 | { |
| 346 | float16x4_t recip = vrecpe_f16(x); |
| 347 | recip = vmul_f16(vrecps_f16(x, recip), recip); |
| 348 | recip = vmul_f16(vrecps_f16(x, recip), recip); |
| 349 | return recip; |
| 350 | } |
| 351 | |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 352 | inline float16x8_t vinvq_f16(float16x8_t x) |
| 353 | { |
| 354 | float16x8_t recip = vrecpeq_f16(x); |
| 355 | recip = vmulq_f16(vrecpsq_f16(x, recip), recip); |
| 356 | recip = vmulq_f16(vrecpsq_f16(x, recip), recip); |
| 357 | return recip; |
| 358 | } |
| 359 | |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 360 | inline float16x8_t vtanhq_f16(float16x8_t val) |
| 361 | { |
| 362 | const float16x8_t CONST_1 = vdupq_n_f16(1.f); |
| 363 | const float16x8_t CONST_2 = vdupq_n_f16(2.f); |
| 364 | const float16x8_t CONST_MIN_TANH = vdupq_n_f16(-10.f); |
| 365 | const float16x8_t CONST_MAX_TANH = vdupq_n_f16(10.f); |
| 366 | |
| 367 | const float16x8_t x = vminq_f16(vmaxq_f16(val, CONST_MIN_TANH), CONST_MAX_TANH); |
| 368 | const float16x8_t exp2x = vexpq_f16(vmulq_f16(CONST_2, x)); |
| 369 | const float16x8_t num = vsubq_f16(exp2x, CONST_1); |
| 370 | const float16x8_t den = vaddq_f16(exp2x, CONST_1); |
| 371 | const float16x8_t tanh = vmulq_f16(num, vinvq_f16(den)); |
| 372 | return tanh; |
| 373 | } |
| 374 | |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 375 | inline float16x8_t vtaylor_polyq_f16(float16x8_t x, const std::array<float16x8_t, 8> &coeffs) |
| 376 | { |
| 377 | const float16x8_t A = vaddq_f16(coeffs[0], vmulq_f16(coeffs[4], x)); |
| 378 | const float16x8_t B = vaddq_f16(coeffs[2], vmulq_f16(coeffs[6], x)); |
| 379 | const float16x8_t C = vaddq_f16(coeffs[1], vmulq_f16(coeffs[5], x)); |
| 380 | const float16x8_t D = vaddq_f16(coeffs[3], vmulq_f16(coeffs[7], x)); |
| 381 | const float16x8_t x2 = vmulq_f16(x, x); |
| 382 | const float16x8_t x4 = vmulq_f16(x2, x2); |
| 383 | const float16x8_t res = vaddq_f16(vaddq_f16(A, vmulq_f16(B, x2)), vmulq_f16(vaddq_f16(C, vmulq_f16(D, x2)), x4)); |
| 384 | return res; |
| 385 | } |
| 386 | |
| 387 | inline float16x8_t vexpq_f16(float16x8_t x) |
| 388 | { |
Michele Di Giorgio | 1c948d4 | 2018-11-20 16:03:01 +0000 | [diff] [blame] | 389 | // TODO (COMPMID-1535) : Revisit FP16 approximations |
| 390 | const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x)); |
| 391 | const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x)); |
Anthony Barbier | 3a6163e | 2018-08-10 17:36:36 +0100 | [diff] [blame] | 392 | |
Michele Di Giorgio | 1c948d4 | 2018-11-20 16:03:01 +0000 | [diff] [blame] | 393 | const float16x8_t res = vcvt_high_f16_f32(vcvt_f16_f32(vexpq_f32(x_low)), vexpq_f32(x_high)); |
| 394 | return res; |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | inline float16x8_t vlogq_f16(float16x8_t x) |
| 398 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 399 | // TODO (COMPMID-1535) : Revisit FP16 approximations |
| 400 | const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x)); |
| 401 | const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x)); |
Anthony Barbier | 3a6163e | 2018-08-10 17:36:36 +0100 | [diff] [blame] | 402 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 403 | const float16x8_t res = vcvt_high_f16_f32(vcvt_f16_f32(vlogq_f32(x_low)), vlogq_f32(x_high)); |
| 404 | return res; |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | inline float16x8_t vpowq_f16(float16x8_t val, float16x8_t n) |
| 408 | { |
Gian Marco Iodice | f2cde9b | 2018-08-23 15:29:16 +0100 | [diff] [blame] | 409 | // TODO (giaiod01) - COMPMID-1535 |
| 410 | float32x4_t n0_f32 = vcvt_f32_f16(vget_low_f16(n)); |
| 411 | float32x4_t n1_f32 = vcvt_f32_f16(vget_high_f16(n)); |
| 412 | float32x4_t val0_f32 = vcvt_f32_f16(vget_low_f16(val)); |
| 413 | float32x4_t val1_f32 = vcvt_f32_f16(vget_high_f16(val)); |
| 414 | |
| 415 | float32x4_t res0_f32 = vexpq_f32(vmulq_f32(n0_f32, vlogq_f32(val0_f32))); |
| 416 | float32x4_t res1_f32 = vexpq_f32(vmulq_f32(n1_f32, vlogq_f32(val1_f32))); |
| 417 | |
| 418 | return vcombine_f16(vcvt_f16_f32(res0_f32), vcvt_f16_f32(res1_f32)); |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 419 | } |
Manuel Bottini | ed75326 | 2019-05-15 15:30:47 +0100 | [diff] [blame] | 420 | |
| 421 | inline float16x8_t vsinq_f16(float16x8_t val) |
| 422 | { |
| 423 | const float32x4_t val_high = vcvt_f32_f16(vget_high_f16(val)); |
| 424 | const float32x4_t val_low = vcvt_f32_f16(vget_low_f16(val)); |
| 425 | |
| 426 | const float32x4_t res_high = vsinq_f32(val_high); |
| 427 | const float32x4_t res_low = vsinq_f32(val_low); |
| 428 | |
| 429 | return vcombine_f16(vcvt_f16_f32(res_low), vcvt_f16_f32(res_high)); |
| 430 | } |
| 431 | |
| 432 | inline float16x4_t vsin_f16(float16x4_t val) |
| 433 | { |
| 434 | const float32x4_t val_f32 = vcvt_f32_f16(val); |
| 435 | const float32x2_t val_high = vget_high_f32(val_f32); |
| 436 | const float32x2_t val_low = vget_low_f32(val_f32); |
| 437 | |
| 438 | const float32x2_t res_high = vsin_f32(val_high); |
| 439 | const float32x2_t res_low = vsin_f32(val_low); |
| 440 | |
| 441 | return vcvt_f16_f32(vcombine_f32(res_low, res_high)); |
| 442 | } |
| 443 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 444 | #endif /* DOXYGEN_SKIP_THIS */ |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 445 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Gian Marco Iodice | 356f643 | 2017-09-22 11:32:21 +0100 | [diff] [blame] | 446 | } // namespace arm_compute |