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 | */ |
Manuel Bottini | ed75326 | 2019-05-15 15:30:47 +0100 | [diff] [blame] | 24 | #include <cmath> |
| 25 | |
Georgios Pinitas | e874ef9 | 2019-09-09 17:40:33 +0100 | [diff] [blame] | 26 | #ifndef M_PI |
| 27 | #define M_PI (3.14159265358979323846) |
| 28 | #endif // M_PI |
| 29 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | namespace arm_compute |
| 31 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 32 | /** Exponent polynomial coefficients */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | const std::array<float32x4_t, 8> exp_tab = |
| 34 | { |
| 35 | { |
| 36 | vdupq_n_f32(1.f), |
| 37 | vdupq_n_f32(0.0416598916054f), |
| 38 | vdupq_n_f32(0.500000596046f), |
| 39 | vdupq_n_f32(0.0014122662833f), |
| 40 | vdupq_n_f32(1.00000011921f), |
| 41 | vdupq_n_f32(0.00833693705499f), |
| 42 | vdupq_n_f32(0.166665703058f), |
| 43 | vdupq_n_f32(0.000195780929062f), |
| 44 | } |
| 45 | }; |
| 46 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 47 | /** Logarithm polynomial coefficients */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 48 | const std::array<float32x4_t, 8> log_tab = |
| 49 | { |
| 50 | { |
| 51 | vdupq_n_f32(-2.29561495781f), |
| 52 | vdupq_n_f32(-2.47071170807f), |
| 53 | vdupq_n_f32(-5.68692588806f), |
| 54 | vdupq_n_f32(-0.165253549814f), |
| 55 | vdupq_n_f32(5.17591238022f), |
| 56 | vdupq_n_f32(0.844007015228f), |
| 57 | vdupq_n_f32(4.58445882797f), |
| 58 | vdupq_n_f32(0.0141278216615f), |
| 59 | } |
| 60 | }; |
| 61 | |
Manuel Bottini | ed75326 | 2019-05-15 15:30:47 +0100 | [diff] [blame] | 62 | /** Sin polynomial coefficients */ |
| 63 | constexpr float te_sin_coeff2 = 0.166666666666f; // 1/(2*3) |
| 64 | constexpr float te_sin_coeff3 = 0.05f; // 1/(4*5) |
| 65 | constexpr float te_sin_coeff4 = 0.023809523810f; // 1/(6*7) |
| 66 | constexpr float te_sin_coeff5 = 0.013888888889f; // 1/(8*9) |
| 67 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 68 | #ifndef DOXYGEN_SKIP_THIS |
Georgios Pinitas | d8e765b | 2017-08-02 13:44:33 +0100 | [diff] [blame] | 69 | inline float32x4_t vfloorq_f32(float32x4_t val) |
| 70 | { |
| 71 | static const float32x4_t CONST_1 = vdupq_n_f32(1.f); |
| 72 | |
| 73 | const int32x4_t z = vcvtq_s32_f32(val); |
| 74 | const float32x4_t r = vcvtq_f32_s32(z); |
| 75 | |
| 76 | return vbslq_f32(vcgtq_f32(r, val), vsubq_f32(r, CONST_1), r); |
| 77 | } |
| 78 | |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 79 | inline float32x4_t vroundq_rte_f32(float32x4_t val) |
| 80 | { |
| 81 | #ifdef __aarch64__ |
| 82 | return vrndnq_f32(val); |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 83 | #else // __aarch64__ |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 84 | static const float32x4_t CONST_HALF_FLOAT = vdupq_n_f32(0.5f); |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 85 | static const float32x4_t CONST_1_FLOAT = vdupq_n_f32(1.f); |
| 86 | static const int32x4_t CONST_1_INT = vdupq_n_s32(1); |
| 87 | const float32x4_t floor_val = vfloorq_f32(val); |
| 88 | const float32x4_t diff = vsubq_f32(val, floor_val); |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * Select the floor value when (diff<0.5 || (diff==0.5 && floor_val%2==0). |
| 92 | * 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)))) |
| 93 | */ |
| 94 | |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 95 | 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)))), |
| 96 | floor_val, vaddq_f32(floor_val, CONST_1_FLOAT)); |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 97 | #endif // __aarch64__ |
| 98 | } |
| 99 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 100 | inline float32x2_t vinvsqrt_f32(float32x2_t x) |
| 101 | { |
| 102 | float32x2_t sqrt_reciprocal = vrsqrte_f32(x); |
| 103 | sqrt_reciprocal = vmul_f32(vrsqrts_f32(vmul_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 104 | sqrt_reciprocal = vmul_f32(vrsqrts_f32(vmul_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 105 | |
| 106 | return sqrt_reciprocal; |
| 107 | } |
| 108 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 109 | inline float32x4_t vinvsqrtq_f32(float32x4_t x) |
| 110 | { |
| 111 | float32x4_t sqrt_reciprocal = vrsqrteq_f32(x); |
| 112 | sqrt_reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 113 | sqrt_reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 114 | |
| 115 | return sqrt_reciprocal; |
| 116 | } |
| 117 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 118 | inline float32x2_t vinv_f32(float32x2_t x) |
| 119 | { |
| 120 | float32x2_t recip = vrecpe_f32(x); |
| 121 | recip = vmul_f32(vrecps_f32(x, recip), recip); |
| 122 | recip = vmul_f32(vrecps_f32(x, recip), recip); |
| 123 | return recip; |
| 124 | } |
| 125 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 126 | inline float32x4_t vinvq_f32(float32x4_t x) |
| 127 | { |
| 128 | float32x4_t recip = vrecpeq_f32(x); |
| 129 | recip = vmulq_f32(vrecpsq_f32(x, recip), recip); |
| 130 | recip = vmulq_f32(vrecpsq_f32(x, recip), recip); |
| 131 | return recip; |
| 132 | } |
| 133 | |
| 134 | inline float32x4_t vtaylor_polyq_f32(float32x4_t x, const std::array<float32x4_t, 8> &coeffs) |
| 135 | { |
| 136 | float32x4_t A = vmlaq_f32(coeffs[0], coeffs[4], x); |
| 137 | float32x4_t B = vmlaq_f32(coeffs[2], coeffs[6], x); |
| 138 | float32x4_t C = vmlaq_f32(coeffs[1], coeffs[5], x); |
| 139 | float32x4_t D = vmlaq_f32(coeffs[3], coeffs[7], x); |
| 140 | float32x4_t x2 = vmulq_f32(x, x); |
| 141 | float32x4_t x4 = vmulq_f32(x2, x2); |
| 142 | float32x4_t res = vmlaq_f32(vmlaq_f32(A, B, x2), vmlaq_f32(C, D, x2), x4); |
| 143 | return res; |
| 144 | } |
| 145 | |
| 146 | inline float32x4_t vexpq_f32(float32x4_t x) |
| 147 | { |
Georgios Pinitas | ee12254 | 2017-06-26 15:54:06 +0100 | [diff] [blame] | 148 | static const float32x4_t CONST_LN2 = vdupq_n_f32(0.6931471805f); // ln(2) |
| 149 | static const float32x4_t CONST_INV_LN2 = vdupq_n_f32(1.4426950408f); // 1/ln(2) |
| 150 | static const float32x4_t CONST_0 = vdupq_n_f32(0.f); |
| 151 | static const int32x4_t CONST_NEGATIVE_126 = vdupq_n_s32(-126); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 152 | |
| 153 | // Perform range reduction [-log(2),log(2)] |
| 154 | int32x4_t m = vcvtq_s32_f32(vmulq_f32(x, CONST_INV_LN2)); |
| 155 | float32x4_t val = vmlsq_f32(x, vcvtq_f32_s32(m), CONST_LN2); |
| 156 | |
| 157 | // Polynomial Approximation |
| 158 | float32x4_t poly = vtaylor_polyq_f32(val, exp_tab); |
| 159 | |
| 160 | // Reconstruct |
Georgios Pinitas | ee12254 | 2017-06-26 15:54:06 +0100 | [diff] [blame] | 161 | poly = vreinterpretq_f32_s32(vqaddq_s32(vreinterpretq_s32_f32(poly), vqshlq_n_s32(m, 23))); |
| 162 | poly = vbslq_f32(vcltq_s32(m, CONST_NEGATIVE_126), CONST_0, poly); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 163 | |
| 164 | return poly; |
| 165 | } |
| 166 | |
| 167 | inline float32x4_t vlogq_f32(float32x4_t x) |
| 168 | { |
| 169 | static const int32x4_t CONST_127 = vdupq_n_s32(127); // 127 |
| 170 | static const float32x4_t CONST_LN2 = vdupq_n_f32(0.6931471805f); // ln(2) |
| 171 | |
| 172 | // Extract exponent |
| 173 | int32x4_t m = vsubq_s32(vreinterpretq_s32_u32(vshrq_n_u32(vreinterpretq_u32_f32(x), 23)), CONST_127); |
| 174 | float32x4_t val = vreinterpretq_f32_s32(vsubq_s32(vreinterpretq_s32_f32(x), vshlq_n_s32(m, 23))); |
| 175 | |
| 176 | // Polynomial Approximation |
| 177 | float32x4_t poly = vtaylor_polyq_f32(val, log_tab); |
| 178 | |
| 179 | // Reconstruct |
| 180 | poly = vmlaq_f32(poly, vcvtq_f32_s32(m), CONST_LN2); |
| 181 | |
| 182 | return poly; |
| 183 | } |
| 184 | |
| 185 | inline float32x4_t vtanhq_f32(float32x4_t val) |
| 186 | { |
| 187 | static const float32x4_t CONST_1 = vdupq_n_f32(1.f); |
| 188 | static const float32x4_t CONST_2 = vdupq_n_f32(2.f); |
| 189 | static const float32x4_t CONST_MIN_TANH = vdupq_n_f32(-10.f); |
| 190 | static const float32x4_t CONST_MAX_TANH = vdupq_n_f32(10.f); |
| 191 | |
| 192 | float32x4_t x = vminq_f32(vmaxq_f32(val, CONST_MIN_TANH), CONST_MAX_TANH); |
| 193 | float32x4_t exp2x = vexpq_f32(vmulq_f32(CONST_2, x)); |
| 194 | float32x4_t num = vsubq_f32(exp2x, CONST_1); |
| 195 | float32x4_t den = vaddq_f32(exp2x, CONST_1); |
| 196 | float32x4_t tanh = vmulq_f32(num, vinvq_f32(den)); |
| 197 | return tanh; |
| 198 | } |
| 199 | |
| 200 | inline float32x4_t vpowq_f32(float32x4_t val, float32x4_t n) |
| 201 | { |
| 202 | return vexpq_f32(vmulq_f32(n, vlogq_f32(val))); |
| 203 | } |
Manuel Bottini | ed75326 | 2019-05-15 15:30:47 +0100 | [diff] [blame] | 204 | |
| 205 | inline float32x4_t vsinq_f32(float32x4_t val) |
| 206 | { |
| 207 | const float32x4_t pi_v = vdupq_n_f32(M_PI); |
| 208 | const float32x4_t pio2_v = vdupq_n_f32(M_PI / 2); |
| 209 | const float32x4_t ipi_v = vdupq_n_f32(1 / M_PI); |
| 210 | |
| 211 | //Find positive or negative |
| 212 | const int32x4_t c_v = vabsq_s32(vcvtq_s32_f32(vmulq_f32(val, ipi_v))); |
| 213 | const uint32x4_t sign_v = vcleq_f32(val, vdupq_n_f32(0)); |
| 214 | const uint32x4_t odd_v = vandq_u32(vreinterpretq_u32_s32(c_v), vdupq_n_u32(1)); |
| 215 | |
| 216 | uint32x4_t neg_v = veorq_u32(odd_v, sign_v); |
| 217 | |
| 218 | //Modulus a - (n * int(a*(1/n))) |
| 219 | float32x4_t ma = vsubq_f32(vabsq_f32(val), vmulq_f32(pi_v, vcvtq_f32_s32(c_v))); |
| 220 | const uint32x4_t reb_v = vcgeq_f32(ma, pio2_v); |
| 221 | |
| 222 | //Rebase a between 0 and pi/2 |
| 223 | ma = vbslq_f32(reb_v, vsubq_f32(pi_v, ma), ma); |
| 224 | |
| 225 | //Taylor series |
| 226 | const float32x4_t ma2 = vmulq_f32(ma, ma); |
| 227 | |
| 228 | //2nd elem: x^3 / 3! |
| 229 | float32x4_t elem = vmulq_f32(vmulq_f32(ma, ma2), vdupq_n_f32(te_sin_coeff2)); |
| 230 | float32x4_t res = vsubq_f32(ma, elem); |
| 231 | |
| 232 | //3rd elem: x^5 / 5! |
| 233 | elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff3)); |
| 234 | res = vaddq_f32(res, elem); |
| 235 | |
| 236 | //4th elem: x^7 / 7!float32x2_t vsin_f32(float32x2_t val) |
| 237 | elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff4)); |
| 238 | res = vsubq_f32(res, elem); |
| 239 | |
| 240 | //5th elem: x^9 / 9! |
| 241 | elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff5)); |
| 242 | res = vaddq_f32(res, elem); |
| 243 | |
| 244 | //Change of sign |
| 245 | neg_v = vshlq_n_u32(neg_v, 31); |
| 246 | res = vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(res), neg_v)); |
| 247 | return res; |
| 248 | } |
| 249 | |
| 250 | inline float32x2_t vsin_f32(float32x2_t val) |
| 251 | { |
| 252 | const float32x2_t pi_v = vdup_n_f32(M_PI); |
| 253 | const float32x2_t pio2_v = vdup_n_f32(M_PI / 2); |
| 254 | const float32x2_t ipi_v = vdup_n_f32(1 / M_PI); |
| 255 | |
| 256 | //Find positive or negative |
| 257 | const int32x2_t c_v = vabs_s32(vcvt_s32_f32(vmul_f32(val, ipi_v))); |
| 258 | const uint32x2_t sign_v = vcle_f32(val, vdup_n_f32(0)); |
| 259 | const uint32x2_t odd_v = vand_u32(vreinterpret_u32_s32(c_v), vdup_n_u32(1)); |
| 260 | |
| 261 | uint32x2_t neg_v = veor_u32(odd_v, sign_v); |
| 262 | |
| 263 | //Modulus a - (n * int(a*(1/n))) |
| 264 | float32x2_t ma = vsub_f32(vabs_f32(val), vmul_f32(pi_v, vcvt_f32_s32(c_v))); |
| 265 | const uint32x2_t reb_v = vcge_f32(ma, pio2_v); |
| 266 | |
| 267 | //Rebase a between 0 and pi/2 |
| 268 | ma = vbsl_f32(reb_v, vsub_f32(pi_v, ma), ma); |
| 269 | |
| 270 | //Taylor series |
| 271 | const float32x2_t ma2 = vmul_f32(ma, ma); |
| 272 | |
| 273 | //2nd elem: x^3 / 3! |
| 274 | float32x2_t elem = vmul_f32(vmul_f32(ma, ma2), vdup_n_f32(te_sin_coeff2)); |
| 275 | float32x2_t res = vsub_f32(ma, elem); |
| 276 | |
| 277 | //3rd elem: x^5 / 5! |
| 278 | elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff3)); |
| 279 | res = vadd_f32(res, elem); |
| 280 | |
| 281 | //4th elem: x^7 / 7!float32x2_t vsin_f32(float32x2_t val) |
| 282 | elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff4)); |
| 283 | res = vsub_f32(res, elem); |
| 284 | |
| 285 | //5th elem: x^9 / 9! |
| 286 | elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff5)); |
| 287 | res = vadd_f32(res, elem); |
| 288 | |
| 289 | //Change of sign |
| 290 | neg_v = vshl_n_u32(neg_v, 31); |
| 291 | res = vreinterpret_f32_u32(veor_u32(vreinterpret_u32_f32(res), neg_v)); |
| 292 | return res; |
| 293 | } |
| 294 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 295 | #endif /* DOXYGEN_SKIP_THIS */ |
| 296 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 297 | inline int32x4_t rounding_divide_by_pow2(int32x4_t x, int32x4_t exponent) |
| 298 | { |
| 299 | const int32x4_t shift_vec = vnegq_s32(exponent); |
| 300 | const int32x4_t fixup = vshrq_n_s32(vandq_s32(x, shift_vec), 31); |
| 301 | const int32x4_t fixed_up_x = vqaddq_s32(x, fixup); |
| 302 | return vrshlq_s32(fixed_up_x, shift_vec); |
| 303 | } |
| 304 | |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 305 | inline int32x4_t rounding_divide_by_pow2(int32x4_t x, int exponent) |
| 306 | { |
| 307 | const int32x4_t shift_vec = vdupq_n_s32(-exponent); |
| 308 | const int32x4_t fixup = vshrq_n_s32(vandq_s32(x, shift_vec), 31); |
| 309 | const int32x4_t fixed_up_x = vqaddq_s32(x, fixup); |
| 310 | return vrshlq_s32(fixed_up_x, shift_vec); |
| 311 | } |
| 312 | |
| 313 | inline int32_t rounding_divide_by_pow2(int32_t x, int exponent) |
| 314 | { |
| 315 | const int32_t mask = (1 << exponent) - 1; |
| 316 | const int32_t threshold = (mask >> 1) + (x < 0 ? 1 : 0); |
| 317 | return (x >> exponent) + ((x & mask) > threshold ? 1 : 0); |
| 318 | } |
| 319 | |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 320 | inline float32x4x4_t convert_uint8x16_to_float32x4x4(const uint8x16_t &in) |
| 321 | { |
| 322 | float32x4x4_t out; |
| 323 | |
| 324 | const auto tmp1 = vmovl_u8(vget_low_u8(in)); |
| 325 | out.val[0] = vcvtq_f32_u32(vmovl_u16(vget_low_u16(tmp1))); |
| 326 | out.val[1] = vcvtq_f32_u32(vmovl_u16(vget_high_u16(tmp1))); |
| 327 | |
| 328 | const auto tmp2 = vmovl_u8(vget_high_u8(in)); |
| 329 | out.val[2] = vcvtq_f32_u32(vmovl_u16(vget_low_u16(tmp2))); |
| 330 | out.val[3] = vcvtq_f32_u32(vmovl_u16(vget_high_u16(tmp2))); |
| 331 | return out; |
| 332 | } |
| 333 | |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 334 | inline float32x4x4_t convert_int8x16_to_float32x4x4(const int8x16_t &in) |
| 335 | { |
| 336 | float32x4x4_t out; |
| 337 | |
| 338 | const auto tmp1 = vmovl_s8(vget_low_s8(in)); |
| 339 | out.val[0] = vcvtq_f32_s32(vmovl_s16(vget_low_s16(tmp1))); |
| 340 | out.val[1] = vcvtq_f32_s32(vmovl_s16(vget_high_s16(tmp1))); |
| 341 | |
| 342 | const auto tmp2 = vmovl_s8(vget_high_s8(in)); |
| 343 | out.val[2] = vcvtq_f32_s32(vmovl_s16(vget_low_s16(tmp2))); |
| 344 | out.val[3] = vcvtq_f32_s32(vmovl_s16(vget_high_s16(tmp2))); |
| 345 | return out; |
| 346 | } |
| 347 | |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 348 | inline void convert_float32x4x3_to_uint8x8x3(const float32x4x3_t &in1, const float32x4x3_t &in2, uint8x8x3_t &out) |
| 349 | { |
| 350 | out.val[0] = vqmovn_u16(vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in1.val[0])), |
| 351 | vqmovn_u32(vcvtq_u32_f32(in2.val[0])))); |
| 352 | out.val[1] = vqmovn_u16(vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in1.val[1])), |
| 353 | vqmovn_u32(vcvtq_u32_f32(in2.val[1])))); |
| 354 | out.val[2] = vqmovn_u16(vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in1.val[2])), |
| 355 | vqmovn_u32(vcvtq_u32_f32(in2.val[2])))); |
| 356 | } |
| 357 | |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 358 | inline void convert_float32x4x4_to_uint8x16(const float32x4x4_t &in, uint8x16_t &out) |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 359 | { |
| 360 | const auto low = vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in.val[0])), |
| 361 | vqmovn_u32(vcvtq_u32_f32(in.val[1]))); |
| 362 | const auto high = vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in.val[2])), |
| 363 | vqmovn_u32(vcvtq_u32_f32(in.val[3]))); |
| 364 | out = vcombine_u8(vqmovn_u16(low), vqmovn_u16(high)); |
| 365 | } |
| 366 | |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 367 | inline void convert_float32x4x4_to_int8x16(const float32x4x4_t &in, int8x16_t &out) |
| 368 | { |
| 369 | const auto low = vcombine_s16(vqmovn_s32(vcvtq_s32_f32(in.val[0])), |
| 370 | vqmovn_s32(vcvtq_s32_f32(in.val[1]))); |
| 371 | const auto high = vcombine_s16(vqmovn_s32(vcvtq_s32_f32(in.val[2])), |
| 372 | vqmovn_s32(vcvtq_s32_f32(in.val[3]))); |
| 373 | out = vcombine_s8(vqmovn_s16(low), vqmovn_s16(high)); |
| 374 | } |
| 375 | |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 376 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 377 | /** Exponent polynomial coefficients */ |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 378 | /** Logarithm polynomial coefficients */ |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 379 | #ifndef DOXYGEN_SKIP_THIS |
Georgios Pinitas | 565bf2d | 2018-08-31 11:46:49 +0100 | [diff] [blame] | 380 | inline float16x8_t vfloorq_f16(float16x8_t val) |
| 381 | { |
| 382 | static const float16x8_t CONST_1 = vdupq_n_f16(1.f); |
| 383 | |
| 384 | const int16x8_t z = vcvtq_s16_f16(val); |
| 385 | const float16x8_t r = vcvtq_f16_s16(z); |
| 386 | |
| 387 | return vbslq_f16(vcgtq_f16(r, val), vsubq_f16(r, CONST_1), r); |
| 388 | } |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 389 | |
| 390 | inline float16x8_t vroundq_rte_f16(float16x8_t val) |
| 391 | { |
| 392 | return vrndnq_f16(val); |
| 393 | } |
| 394 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 395 | inline float16x4_t vinvsqrt_f16(float16x4_t x) |
| 396 | { |
| 397 | float16x4_t sqrt_reciprocal = vrsqrte_f16(x); |
| 398 | sqrt_reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 399 | sqrt_reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 400 | return sqrt_reciprocal; |
| 401 | } |
| 402 | |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 403 | inline float16x8_t vinvsqrtq_f16(float16x8_t x) |
| 404 | { |
| 405 | float16x8_t sqrt_reciprocal = vrsqrteq_f16(x); |
| 406 | sqrt_reciprocal = vmulq_f16(vrsqrtsq_f16(vmulq_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 407 | 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] | 408 | return sqrt_reciprocal; |
| 409 | } |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 410 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 411 | inline float16x4_t vinv_f16(float16x4_t x) |
| 412 | { |
| 413 | float16x4_t recip = vrecpe_f16(x); |
| 414 | recip = vmul_f16(vrecps_f16(x, recip), recip); |
| 415 | recip = vmul_f16(vrecps_f16(x, recip), recip); |
| 416 | return recip; |
| 417 | } |
| 418 | |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 419 | inline float16x8_t vinvq_f16(float16x8_t x) |
| 420 | { |
| 421 | float16x8_t recip = vrecpeq_f16(x); |
| 422 | recip = vmulq_f16(vrecpsq_f16(x, recip), recip); |
| 423 | recip = vmulq_f16(vrecpsq_f16(x, recip), recip); |
| 424 | return recip; |
| 425 | } |
| 426 | |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 427 | inline float16x8_t vtanhq_f16(float16x8_t val) |
| 428 | { |
| 429 | const float16x8_t CONST_1 = vdupq_n_f16(1.f); |
| 430 | const float16x8_t CONST_2 = vdupq_n_f16(2.f); |
| 431 | const float16x8_t CONST_MIN_TANH = vdupq_n_f16(-10.f); |
| 432 | const float16x8_t CONST_MAX_TANH = vdupq_n_f16(10.f); |
| 433 | |
| 434 | const float16x8_t x = vminq_f16(vmaxq_f16(val, CONST_MIN_TANH), CONST_MAX_TANH); |
| 435 | const float16x8_t exp2x = vexpq_f16(vmulq_f16(CONST_2, x)); |
| 436 | const float16x8_t num = vsubq_f16(exp2x, CONST_1); |
| 437 | const float16x8_t den = vaddq_f16(exp2x, CONST_1); |
| 438 | const float16x8_t tanh = vmulq_f16(num, vinvq_f16(den)); |
| 439 | return tanh; |
| 440 | } |
| 441 | |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 442 | inline float16x8_t vtaylor_polyq_f16(float16x8_t x, const std::array<float16x8_t, 8> &coeffs) |
| 443 | { |
| 444 | const float16x8_t A = vaddq_f16(coeffs[0], vmulq_f16(coeffs[4], x)); |
| 445 | const float16x8_t B = vaddq_f16(coeffs[2], vmulq_f16(coeffs[6], x)); |
| 446 | const float16x8_t C = vaddq_f16(coeffs[1], vmulq_f16(coeffs[5], x)); |
| 447 | const float16x8_t D = vaddq_f16(coeffs[3], vmulq_f16(coeffs[7], x)); |
| 448 | const float16x8_t x2 = vmulq_f16(x, x); |
| 449 | const float16x8_t x4 = vmulq_f16(x2, x2); |
| 450 | const float16x8_t res = vaddq_f16(vaddq_f16(A, vmulq_f16(B, x2)), vmulq_f16(vaddq_f16(C, vmulq_f16(D, x2)), x4)); |
| 451 | return res; |
| 452 | } |
| 453 | |
| 454 | inline float16x8_t vexpq_f16(float16x8_t x) |
| 455 | { |
Michele Di Giorgio | 1c948d4 | 2018-11-20 16:03:01 +0000 | [diff] [blame] | 456 | // TODO (COMPMID-1535) : Revisit FP16 approximations |
| 457 | const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x)); |
| 458 | const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x)); |
Anthony Barbier | 3a6163e | 2018-08-10 17:36:36 +0100 | [diff] [blame] | 459 | |
Georgios Pinitas | f2cdce3 | 2019-12-09 18:35:57 +0000 | [diff] [blame] | 460 | const float16x8_t res = vcombine_f16(vcvt_f16_f32(vexpq_f32(x_low)), vcvt_f16_f32(vexpq_f32(x_high))); |
Michele Di Giorgio | 1c948d4 | 2018-11-20 16:03:01 +0000 | [diff] [blame] | 461 | return res; |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | inline float16x8_t vlogq_f16(float16x8_t x) |
| 465 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 466 | // TODO (COMPMID-1535) : Revisit FP16 approximations |
| 467 | const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x)); |
| 468 | const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x)); |
Anthony Barbier | 3a6163e | 2018-08-10 17:36:36 +0100 | [diff] [blame] | 469 | |
Georgios Pinitas | f2cdce3 | 2019-12-09 18:35:57 +0000 | [diff] [blame] | 470 | const float16x8_t res = vcombine_f16(vcvt_f16_f32(vlogq_f32(x_low)), vcvt_f16_f32(vlogq_f32(x_high))); |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 471 | return res; |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | inline float16x8_t vpowq_f16(float16x8_t val, float16x8_t n) |
| 475 | { |
Gian Marco Iodice | f2cde9b | 2018-08-23 15:29:16 +0100 | [diff] [blame] | 476 | // TODO (giaiod01) - COMPMID-1535 |
| 477 | float32x4_t n0_f32 = vcvt_f32_f16(vget_low_f16(n)); |
| 478 | float32x4_t n1_f32 = vcvt_f32_f16(vget_high_f16(n)); |
| 479 | float32x4_t val0_f32 = vcvt_f32_f16(vget_low_f16(val)); |
| 480 | float32x4_t val1_f32 = vcvt_f32_f16(vget_high_f16(val)); |
| 481 | |
| 482 | float32x4_t res0_f32 = vexpq_f32(vmulq_f32(n0_f32, vlogq_f32(val0_f32))); |
| 483 | float32x4_t res1_f32 = vexpq_f32(vmulq_f32(n1_f32, vlogq_f32(val1_f32))); |
| 484 | |
| 485 | 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] | 486 | } |
Manuel Bottini | ed75326 | 2019-05-15 15:30:47 +0100 | [diff] [blame] | 487 | |
| 488 | inline float16x8_t vsinq_f16(float16x8_t val) |
| 489 | { |
| 490 | const float32x4_t val_high = vcvt_f32_f16(vget_high_f16(val)); |
| 491 | const float32x4_t val_low = vcvt_f32_f16(vget_low_f16(val)); |
| 492 | |
| 493 | const float32x4_t res_high = vsinq_f32(val_high); |
| 494 | const float32x4_t res_low = vsinq_f32(val_low); |
| 495 | |
| 496 | return vcombine_f16(vcvt_f16_f32(res_low), vcvt_f16_f32(res_high)); |
| 497 | } |
| 498 | |
| 499 | inline float16x4_t vsin_f16(float16x4_t val) |
| 500 | { |
| 501 | const float32x4_t val_f32 = vcvt_f32_f16(val); |
| 502 | const float32x2_t val_high = vget_high_f32(val_f32); |
| 503 | const float32x2_t val_low = vget_low_f32(val_f32); |
| 504 | |
| 505 | const float32x2_t res_high = vsin_f32(val_high); |
| 506 | const float32x2_t res_low = vsin_f32(val_low); |
| 507 | |
| 508 | return vcvt_f16_f32(vcombine_f32(res_low, res_high)); |
| 509 | } |
| 510 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 511 | #endif /* DOXYGEN_SKIP_THIS */ |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 512 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Gian Marco Iodice | 356f643 | 2017-09-22 11:32:21 +0100 | [diff] [blame] | 513 | } // namespace arm_compute |