Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Viet-Hoa Do | fd472f0 | 2023-03-15 14:05:06 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2023 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 | */ |
SiCongLi | 410e21e | 2020-12-11 15:07:53 +0000 | [diff] [blame] | 24 | #include "support/ToolchainSupport.h" |
| 25 | |
Manuel Bottini | ed75326 | 2019-05-15 15:30:47 +0100 | [diff] [blame] | 26 | #include <cmath> |
morgolock | 3155f77 | 2020-05-11 16:00:04 +0100 | [diff] [blame] | 27 | #include <limits> |
Manuel Bottini | ed75326 | 2019-05-15 15:30:47 +0100 | [diff] [blame] | 28 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | namespace arm_compute |
| 30 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 31 | /** Logarithm polynomial coefficients */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 32 | const std::array<float32x4_t, 8> log_tab = {{ |
| 33 | vdupq_n_f32(-2.29561495781f), |
| 34 | vdupq_n_f32(-2.47071170807f), |
| 35 | vdupq_n_f32(-5.68692588806f), |
| 36 | vdupq_n_f32(-0.165253549814f), |
| 37 | vdupq_n_f32(5.17591238022f), |
| 38 | vdupq_n_f32(0.844007015228f), |
| 39 | vdupq_n_f32(4.58445882797f), |
| 40 | vdupq_n_f32(0.0141278216615f), |
| 41 | }}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 42 | |
Manuel Bottini | ed75326 | 2019-05-15 15:30:47 +0100 | [diff] [blame] | 43 | /** Sin polynomial coefficients */ |
| 44 | constexpr float te_sin_coeff2 = 0.166666666666f; // 1/(2*3) |
| 45 | constexpr float te_sin_coeff3 = 0.05f; // 1/(4*5) |
| 46 | constexpr float te_sin_coeff4 = 0.023809523810f; // 1/(6*7) |
| 47 | constexpr float te_sin_coeff5 = 0.013888888889f; // 1/(8*9) |
| 48 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 49 | #ifndef DOXYGEN_SKIP_THIS |
Viet-Hoa Do | 86689cd | 2022-11-21 17:17:56 +0000 | [diff] [blame] | 50 | inline float32x4_t prefer_vfmaq_f32(float32x4_t a, float32x4_t b, float32x4_t c) |
| 51 | { |
Pablo Marquez Tello | 3f16c57 | 2023-06-20 13:39:10 +0100 | [diff] [blame] | 52 | #if __ARM_FEATURE_FMA |
Viet-Hoa Do | 86689cd | 2022-11-21 17:17:56 +0000 | [diff] [blame] | 53 | return vfmaq_f32(a, b, c); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 54 | #else // __ARM_FEATURE_FMA |
Viet-Hoa Do | 86689cd | 2022-11-21 17:17:56 +0000 | [diff] [blame] | 55 | return vmlaq_f32(a, b, c); |
Pablo Marquez Tello | 3f16c57 | 2023-06-20 13:39:10 +0100 | [diff] [blame] | 56 | #endif // __ARM_FEATURE_FMA |
Viet-Hoa Do | 86689cd | 2022-11-21 17:17:56 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Georgios Pinitas | d8e765b | 2017-08-02 13:44:33 +0100 | [diff] [blame] | 59 | inline float32x4_t vfloorq_f32(float32x4_t val) |
| 60 | { |
| 61 | static const float32x4_t CONST_1 = vdupq_n_f32(1.f); |
| 62 | |
| 63 | const int32x4_t z = vcvtq_s32_f32(val); |
| 64 | const float32x4_t r = vcvtq_f32_s32(z); |
| 65 | |
| 66 | return vbslq_f32(vcgtq_f32(r, val), vsubq_f32(r, CONST_1), r); |
| 67 | } |
| 68 | |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 69 | inline float32x4_t vroundq_rte_f32(float32x4_t val) |
| 70 | { |
| 71 | #ifdef __aarch64__ |
| 72 | return vrndnq_f32(val); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 73 | #else // __aarch64__ |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 74 | static const float32x4_t CONST_HALF_FLOAT = vdupq_n_f32(0.5f); |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 75 | static const float32x4_t CONST_1_FLOAT = vdupq_n_f32(1.f); |
| 76 | static const int32x4_t CONST_1_INT = vdupq_n_s32(1); |
| 77 | const float32x4_t floor_val = vfloorq_f32(val); |
| 78 | const float32x4_t diff = vsubq_f32(val, floor_val); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 79 | const float32x4_t fp32_upper_limit = |
| 80 | vreinterpretq_f32_u32(vdupq_n_u32(0x4B000000)); // 0x4B000000 = (23U + 127U) << 23U |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 81 | |
| 82 | /* |
Ramy Elgammal | 7fefac7 | 2023-04-20 12:32:03 +0100 | [diff] [blame] | 83 | * 1. Select the floor value when (diff<0.5 || (diff==0.5 && floor_val%2==0). |
| 84 | * 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)))) |
| 85 | * |
| 86 | * 2. In case the input value (val) is out of signed int32 range, then simple use the input value as the rounded value |
| 87 | * Because: |
| 88 | * in this case converting to int32 would saturate |
| 89 | * If the input float value is >= 2^23 * 1.00... 23 Zeros ..0 then the rounded value is exactly equal to the input value. |
| 90 | * Because: |
| 91 | * in IEEE single precision floating point representation the fraction part is 23 bit, so if exponent is 23 it means the fraction part = 0 as any digits after decimal point are truncated. |
| 92 | * Hence, rounding has no effect: |
| 93 | * Threshold upper limit with format |S|E(8bits)| Fraction(23bits) | = (23 + 127) << 23 (assuming positive sign): Adding 127, because 127 represents the actual zero in this format. |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 94 | */ |
| 95 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 96 | float32x4_t rounded_val = vbslq_f32( |
| 97 | vorrq_u32(vcltq_f32(diff, CONST_HALF_FLOAT), |
| 98 | vandq_u32(vceqq_f32(diff, CONST_HALF_FLOAT), |
| 99 | vmvnq_u32(vtstq_s32(vandq_s32(vcvtq_s32_f32(floor_val), CONST_1_INT), CONST_1_INT)))), |
| 100 | floor_val, vaddq_f32(floor_val, CONST_1_FLOAT)); |
Ramy Elgammal | 7fefac7 | 2023-04-20 12:32:03 +0100 | [diff] [blame] | 101 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 102 | float32x4_t result = vbslq_f32(vcgeq_f32(vabsq_f32(val), fp32_upper_limit), val, rounded_val); |
Ramy Elgammal | 7fefac7 | 2023-04-20 12:32:03 +0100 | [diff] [blame] | 103 | |
| 104 | return result; |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 105 | #endif // __aarch64__ |
| 106 | } |
| 107 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 108 | inline float32x2_t vinvsqrt_f32(float32x2_t x) |
| 109 | { |
| 110 | float32x2_t sqrt_reciprocal = vrsqrte_f32(x); |
| 111 | sqrt_reciprocal = vmul_f32(vrsqrts_f32(vmul_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 112 | sqrt_reciprocal = vmul_f32(vrsqrts_f32(vmul_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 113 | |
| 114 | return sqrt_reciprocal; |
| 115 | } |
| 116 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 117 | inline float32x4_t vinvsqrtq_f32(float32x4_t x) |
| 118 | { |
| 119 | float32x4_t sqrt_reciprocal = vrsqrteq_f32(x); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 120 | sqrt_reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 121 | sqrt_reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 122 | |
| 123 | return sqrt_reciprocal; |
| 124 | } |
| 125 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 126 | inline float32x2_t vinv_f32(float32x2_t x) |
| 127 | { |
| 128 | float32x2_t recip = vrecpe_f32(x); |
| 129 | recip = vmul_f32(vrecps_f32(x, recip), recip); |
| 130 | recip = vmul_f32(vrecps_f32(x, recip), recip); |
| 131 | return recip; |
| 132 | } |
| 133 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 134 | inline float32x4_t vinvq_f32(float32x4_t x) |
| 135 | { |
| 136 | float32x4_t recip = vrecpeq_f32(x); |
| 137 | recip = vmulq_f32(vrecpsq_f32(x, recip), recip); |
| 138 | recip = vmulq_f32(vrecpsq_f32(x, recip), recip); |
| 139 | return recip; |
| 140 | } |
| 141 | |
| 142 | inline float32x4_t vtaylor_polyq_f32(float32x4_t x, const std::array<float32x4_t, 8> &coeffs) |
| 143 | { |
| 144 | float32x4_t A = vmlaq_f32(coeffs[0], coeffs[4], x); |
| 145 | float32x4_t B = vmlaq_f32(coeffs[2], coeffs[6], x); |
| 146 | float32x4_t C = vmlaq_f32(coeffs[1], coeffs[5], x); |
| 147 | float32x4_t D = vmlaq_f32(coeffs[3], coeffs[7], x); |
| 148 | float32x4_t x2 = vmulq_f32(x, x); |
| 149 | float32x4_t x4 = vmulq_f32(x2, x2); |
| 150 | float32x4_t res = vmlaq_f32(vmlaq_f32(A, B, x2), vmlaq_f32(C, D, x2), x4); |
| 151 | return res; |
| 152 | } |
| 153 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 154 | static const uint32_t exp_f32_coeff[] = { |
Viet-Hoa Do | 86689cd | 2022-11-21 17:17:56 +0000 | [diff] [blame] | 155 | 0x3f7ffff6, // x^1: 0x1.ffffecp-1f |
| 156 | 0x3efffedb, // x^2: 0x1.fffdb6p-2f |
| 157 | 0x3e2aaf33, // x^3: 0x1.555e66p-3f |
| 158 | 0x3d2b9f17, // x^4: 0x1.573e2ep-5f |
| 159 | 0x3c072010, // x^5: 0x1.0e4020p-7f |
| 160 | }; |
| 161 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 162 | inline float32x4_t vexpq_f32(float32x4_t x) |
| 163 | { |
Viet-Hoa Do | 86689cd | 2022-11-21 17:17:56 +0000 | [diff] [blame] | 164 | const auto c1 = vreinterpretq_f32_u32(vdupq_n_u32(exp_f32_coeff[0])); |
| 165 | const auto c2 = vreinterpretq_f32_u32(vdupq_n_u32(exp_f32_coeff[1])); |
| 166 | const auto c3 = vreinterpretq_f32_u32(vdupq_n_u32(exp_f32_coeff[2])); |
| 167 | const auto c4 = vreinterpretq_f32_u32(vdupq_n_u32(exp_f32_coeff[3])); |
| 168 | const auto c5 = vreinterpretq_f32_u32(vdupq_n_u32(exp_f32_coeff[4])); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 169 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 170 | const auto shift = vreinterpretq_f32_u32(vdupq_n_u32(0x4b00007f)); // 2^23 + 127 = 0x1.0000fep23f |
| 171 | const auto inv_ln2 = vreinterpretq_f32_u32(vdupq_n_u32(0x3fb8aa3b)); // 1 / ln(2) = 0x1.715476p+0f |
| 172 | const auto neg_ln2_hi = |
| 173 | vreinterpretq_f32_u32(vdupq_n_u32(0xbf317200)); // -ln(2) from bits -1 to -19: -0x1.62e400p-1f |
| 174 | const auto neg_ln2_lo = |
| 175 | vreinterpretq_f32_u32(vdupq_n_u32(0xb5bfbe8e)); // -ln(2) from bits -20 to -42: -0x1.7f7d1cp-20f |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 176 | |
Viet-Hoa Do | 86689cd | 2022-11-21 17:17:56 +0000 | [diff] [blame] | 177 | const auto inf = vdupq_n_f32(std::numeric_limits<float>::infinity()); |
Ramy Elgammal | 7fefac7 | 2023-04-20 12:32:03 +0100 | [diff] [blame] | 178 | const auto max_input = vdupq_n_f32(88.37f); // Approximately ln(2^127.5) |
Viet-Hoa Do | 86689cd | 2022-11-21 17:17:56 +0000 | [diff] [blame] | 179 | const auto zero = vdupq_n_f32(0.f); |
Ramy Elgammal | 7fefac7 | 2023-04-20 12:32:03 +0100 | [diff] [blame] | 180 | const auto min_input = vdupq_n_f32(-86.64f); // Approximately ln(2^-125) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 181 | |
Viet-Hoa Do | 86689cd | 2022-11-21 17:17:56 +0000 | [diff] [blame] | 182 | // Range reduction: |
| 183 | // e^x = 2^n * e^r |
| 184 | // where: |
| 185 | // n = floor(x / ln(2)) |
| 186 | // r = x - n * ln(2) |
| 187 | // |
| 188 | // By adding x / ln(2) with 2^23 + 127 (shift): |
| 189 | // * As FP32 fraction part only has 23-bits, the addition of 2^23 + 127 forces decimal part |
| 190 | // of x / ln(2) out of the result. The integer part of x / ln(2) (i.e. n) + 127 will occupy |
| 191 | // the whole fraction part of z in FP32 format. |
| 192 | // Subtracting 2^23 + 127 (shift) from z will result in the integer part of x / ln(2) |
| 193 | // (i.e. n) because the decimal part has been pushed out and lost. |
| 194 | // * The addition of 127 makes the FP32 fraction part of z ready to be used as the exponent |
| 195 | // in FP32 format. Left shifting z by 23 bits will result in 2^n. |
Ramy Elgammal | 7fefac7 | 2023-04-20 12:32:03 +0100 | [diff] [blame] | 196 | const auto z = prefer_vfmaq_f32(shift, x, inv_ln2); |
| 197 | const auto n = z - shift; |
| 198 | const auto scale = vreinterpretq_f32_u32(vreinterpretq_u32_f32(z) << 23); // 2^n |
Viet-Hoa Do | 86689cd | 2022-11-21 17:17:56 +0000 | [diff] [blame] | 199 | |
| 200 | // The calculation of n * ln(2) is done using 2 steps to achieve accuracy beyond FP32. |
| 201 | // This outperforms longer Taylor series (3-4 tabs) both in term of accuracy and performance. |
| 202 | const auto r_hi = prefer_vfmaq_f32(x, n, neg_ln2_hi); |
Ramy Elgammal | 7fefac7 | 2023-04-20 12:32:03 +0100 | [diff] [blame] | 203 | const auto r = prefer_vfmaq_f32(r_hi, n, neg_ln2_lo); |
Viet-Hoa Do | 86689cd | 2022-11-21 17:17:56 +0000 | [diff] [blame] | 204 | |
| 205 | // Compute the truncated Taylor series of e^r. |
| 206 | // poly = scale * (1 + c1 * r + c2 * r^2 + c3 * r^3 + c4 * r^4 + c5 * r^5) |
| 207 | const auto r2 = r * r; |
| 208 | |
Ramy Elgammal | 7fefac7 | 2023-04-20 12:32:03 +0100 | [diff] [blame] | 209 | const auto p1 = c1 * r; |
| 210 | const auto p23 = prefer_vfmaq_f32(c2, c3, r); |
| 211 | const auto p45 = prefer_vfmaq_f32(c4, c5, r); |
| 212 | const auto p2345 = prefer_vfmaq_f32(p23, p45, r2); |
Viet-Hoa Do | 86689cd | 2022-11-21 17:17:56 +0000 | [diff] [blame] | 213 | const auto p12345 = prefer_vfmaq_f32(p1, p2345, r2); |
| 214 | |
| 215 | auto poly = prefer_vfmaq_f32(scale, p12345, scale); |
| 216 | |
| 217 | // Handle underflow and overflow. |
| 218 | poly = vbslq_f32(vcltq_f32(x, min_input), zero, poly); |
| 219 | poly = vbslq_f32(vcgtq_f32(x, max_input), inf, poly); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 220 | |
| 221 | return poly; |
| 222 | } |
| 223 | |
Murray Kornelsen | 926f502 | 2022-07-13 21:22:39 -0400 | [diff] [blame] | 224 | #ifdef __aarch64__ |
| 225 | inline float32x4_t verfq_f32(float32x4_t x) |
| 226 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 227 | static const float erffdata[4] = {0.278393f, 0.230389f, 0.000972f, 0.078108f}; |
Murray Kornelsen | 926f502 | 2022-07-13 21:22:39 -0400 | [diff] [blame] | 228 | static const float32x4_t coeffdata = vld1q_f32(erffdata); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 229 | static const float32x4_t onev{vdupq_n_f32(1.0f)}; |
Murray Kornelsen | 926f502 | 2022-07-13 21:22:39 -0400 | [diff] [blame] | 230 | |
| 231 | uint32x4_t selector = vcltzq_f32(x); |
| 232 | |
| 233 | float32x4_t absx = vabsq_f32(x); |
| 234 | float32x4_t absx2 = vmulq_f32(x, x); |
| 235 | float32x4_t absx3 = vmulq_f32(absx2, absx); |
| 236 | float32x4_t absx4 = vmulq_f32(absx2, absx2); |
| 237 | |
| 238 | float32x4_t denom = onev; |
| 239 | denom = vfmaq_laneq_f32(denom, absx, coeffdata, 0); |
| 240 | denom = vfmaq_laneq_f32(denom, absx2, coeffdata, 1); |
| 241 | denom = vfmaq_laneq_f32(denom, absx3, coeffdata, 2); |
| 242 | denom = vfmaq_laneq_f32(denom, absx4, coeffdata, 3); |
| 243 | |
| 244 | denom = vmulq_f32(denom, denom); |
| 245 | denom = vmulq_f32(denom, denom); |
| 246 | |
| 247 | float32x4_t fract = onev; |
| 248 | fract = vdivq_f32(fract, denom); |
| 249 | |
| 250 | float32x4_t result = onev; |
| 251 | result = vsubq_f32(result, fract); |
| 252 | |
| 253 | float32x4_t inverse = vnegq_f32(result); |
| 254 | |
| 255 | result = vbslq_f32(selector, inverse, result); |
| 256 | |
| 257 | return result; |
| 258 | } |
| 259 | #endif // #ifdef __aarch64__ |
| 260 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 261 | inline float32x4_t vlogq_f32(float32x4_t x) |
| 262 | { |
| 263 | static const int32x4_t CONST_127 = vdupq_n_s32(127); // 127 |
| 264 | static const float32x4_t CONST_LN2 = vdupq_n_f32(0.6931471805f); // ln(2) |
| 265 | |
| 266 | // Extract exponent |
| 267 | int32x4_t m = vsubq_s32(vreinterpretq_s32_u32(vshrq_n_u32(vreinterpretq_u32_f32(x), 23)), CONST_127); |
| 268 | float32x4_t val = vreinterpretq_f32_s32(vsubq_s32(vreinterpretq_s32_f32(x), vshlq_n_s32(m, 23))); |
| 269 | |
| 270 | // Polynomial Approximation |
| 271 | float32x4_t poly = vtaylor_polyq_f32(val, log_tab); |
| 272 | |
| 273 | // Reconstruct |
| 274 | poly = vmlaq_f32(poly, vcvtq_f32_s32(m), CONST_LN2); |
| 275 | |
| 276 | return poly; |
| 277 | } |
| 278 | |
| 279 | inline float32x4_t vtanhq_f32(float32x4_t val) |
| 280 | { |
| 281 | static const float32x4_t CONST_1 = vdupq_n_f32(1.f); |
| 282 | static const float32x4_t CONST_2 = vdupq_n_f32(2.f); |
| 283 | static const float32x4_t CONST_MIN_TANH = vdupq_n_f32(-10.f); |
| 284 | static const float32x4_t CONST_MAX_TANH = vdupq_n_f32(10.f); |
Aleksandr Nikolaev | 7e9f34d | 2021-05-04 16:46:27 +0100 | [diff] [blame] | 285 | static const float32x4_t CONST_THR = vdupq_n_f32(5.e-3); |
| 286 | static const float32x4_t CONST_1_3 = vdupq_n_f32(0.3333333f); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 287 | |
Sheri Zhang | 5dda217 | 2021-10-15 19:54:17 +0100 | [diff] [blame] | 288 | float32x4_t x = vminq_f32(vmaxq_f32(val, CONST_MIN_TANH), CONST_MAX_TANH); |
Aleksandr Nikolaev | 7e9f34d | 2021-05-04 16:46:27 +0100 | [diff] [blame] | 289 | // x * (1 - x^2/3) if |x| < 5.e-3 or (exp2x - 1) / (exp2x + 1) otherwise |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 290 | float32x4_t exp2x = |
| 291 | vbslq_f32(vcgtq_f32(vabsq_f32(x), CONST_THR), vexpq_f32(vmulq_f32(CONST_2, x)), vmulq_f32(x, x)); |
| 292 | float32x4_t num = |
| 293 | vbslq_f32(vcgtq_f32(vabsq_f32(x), CONST_THR), vsubq_f32(exp2x, CONST_1), vmulq_f32(CONST_1_3, exp2x)); |
| 294 | float32x4_t den = vbslq_f32(vcgtq_f32(vabsq_f32(x), CONST_THR), vaddq_f32(exp2x, CONST_1), vsubq_f32(CONST_1, num)); |
| 295 | float32x4_t tanh = vbslq_f32(vcgtq_f32(vabsq_f32(x), CONST_THR), vmulq_f32(num, vinvq_f32(den)), vmulq_f32(x, den)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 296 | return tanh; |
| 297 | } |
| 298 | |
| 299 | inline float32x4_t vpowq_f32(float32x4_t val, float32x4_t n) |
| 300 | { |
| 301 | return vexpq_f32(vmulq_f32(n, vlogq_f32(val))); |
| 302 | } |
Manuel Bottini | ed75326 | 2019-05-15 15:30:47 +0100 | [diff] [blame] | 303 | |
| 304 | inline float32x4_t vsinq_f32(float32x4_t val) |
| 305 | { |
| 306 | const float32x4_t pi_v = vdupq_n_f32(M_PI); |
| 307 | const float32x4_t pio2_v = vdupq_n_f32(M_PI / 2); |
| 308 | const float32x4_t ipi_v = vdupq_n_f32(1 / M_PI); |
| 309 | |
| 310 | //Find positive or negative |
| 311 | const int32x4_t c_v = vabsq_s32(vcvtq_s32_f32(vmulq_f32(val, ipi_v))); |
| 312 | const uint32x4_t sign_v = vcleq_f32(val, vdupq_n_f32(0)); |
| 313 | const uint32x4_t odd_v = vandq_u32(vreinterpretq_u32_s32(c_v), vdupq_n_u32(1)); |
| 314 | |
| 315 | uint32x4_t neg_v = veorq_u32(odd_v, sign_v); |
| 316 | |
| 317 | //Modulus a - (n * int(a*(1/n))) |
| 318 | float32x4_t ma = vsubq_f32(vabsq_f32(val), vmulq_f32(pi_v, vcvtq_f32_s32(c_v))); |
| 319 | const uint32x4_t reb_v = vcgeq_f32(ma, pio2_v); |
| 320 | |
| 321 | //Rebase a between 0 and pi/2 |
| 322 | ma = vbslq_f32(reb_v, vsubq_f32(pi_v, ma), ma); |
| 323 | |
| 324 | //Taylor series |
| 325 | const float32x4_t ma2 = vmulq_f32(ma, ma); |
| 326 | |
| 327 | //2nd elem: x^3 / 3! |
| 328 | float32x4_t elem = vmulq_f32(vmulq_f32(ma, ma2), vdupq_n_f32(te_sin_coeff2)); |
| 329 | float32x4_t res = vsubq_f32(ma, elem); |
| 330 | |
| 331 | //3rd elem: x^5 / 5! |
| 332 | elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff3)); |
| 333 | res = vaddq_f32(res, elem); |
| 334 | |
| 335 | //4th elem: x^7 / 7!float32x2_t vsin_f32(float32x2_t val) |
| 336 | elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff4)); |
| 337 | res = vsubq_f32(res, elem); |
| 338 | |
| 339 | //5th elem: x^9 / 9! |
| 340 | elem = vmulq_f32(vmulq_f32(elem, ma2), vdupq_n_f32(te_sin_coeff5)); |
| 341 | res = vaddq_f32(res, elem); |
| 342 | |
| 343 | //Change of sign |
| 344 | neg_v = vshlq_n_u32(neg_v, 31); |
| 345 | res = vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(res), neg_v)); |
| 346 | return res; |
| 347 | } |
| 348 | |
| 349 | inline float32x2_t vsin_f32(float32x2_t val) |
| 350 | { |
| 351 | const float32x2_t pi_v = vdup_n_f32(M_PI); |
| 352 | const float32x2_t pio2_v = vdup_n_f32(M_PI / 2); |
| 353 | const float32x2_t ipi_v = vdup_n_f32(1 / M_PI); |
| 354 | |
| 355 | //Find positive or negative |
| 356 | const int32x2_t c_v = vabs_s32(vcvt_s32_f32(vmul_f32(val, ipi_v))); |
| 357 | const uint32x2_t sign_v = vcle_f32(val, vdup_n_f32(0)); |
| 358 | const uint32x2_t odd_v = vand_u32(vreinterpret_u32_s32(c_v), vdup_n_u32(1)); |
| 359 | |
| 360 | uint32x2_t neg_v = veor_u32(odd_v, sign_v); |
| 361 | |
| 362 | //Modulus a - (n * int(a*(1/n))) |
| 363 | float32x2_t ma = vsub_f32(vabs_f32(val), vmul_f32(pi_v, vcvt_f32_s32(c_v))); |
| 364 | const uint32x2_t reb_v = vcge_f32(ma, pio2_v); |
| 365 | |
| 366 | //Rebase a between 0 and pi/2 |
| 367 | ma = vbsl_f32(reb_v, vsub_f32(pi_v, ma), ma); |
| 368 | |
| 369 | //Taylor series |
| 370 | const float32x2_t ma2 = vmul_f32(ma, ma); |
| 371 | |
| 372 | //2nd elem: x^3 / 3! |
| 373 | float32x2_t elem = vmul_f32(vmul_f32(ma, ma2), vdup_n_f32(te_sin_coeff2)); |
| 374 | float32x2_t res = vsub_f32(ma, elem); |
| 375 | |
| 376 | //3rd elem: x^5 / 5! |
| 377 | elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff3)); |
| 378 | res = vadd_f32(res, elem); |
| 379 | |
| 380 | //4th elem: x^7 / 7!float32x2_t vsin_f32(float32x2_t val) |
| 381 | elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff4)); |
| 382 | res = vsub_f32(res, elem); |
| 383 | |
| 384 | //5th elem: x^9 / 9! |
| 385 | elem = vmul_f32(vmul_f32(elem, ma2), vdup_n_f32(te_sin_coeff5)); |
| 386 | res = vadd_f32(res, elem); |
| 387 | |
| 388 | //Change of sign |
| 389 | neg_v = vshl_n_u32(neg_v, 31); |
| 390 | res = vreinterpret_f32_u32(veor_u32(vreinterpret_u32_f32(res), neg_v)); |
| 391 | return res; |
| 392 | } |
| 393 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 394 | #endif /* DOXYGEN_SKIP_THIS */ |
| 395 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 396 | inline int32x4_t rounding_divide_by_pow2(int32x4_t x, int32x4_t exponent) |
| 397 | { |
| 398 | const int32x4_t shift_vec = vnegq_s32(exponent); |
| 399 | const int32x4_t fixup = vshrq_n_s32(vandq_s32(x, shift_vec), 31); |
| 400 | const int32x4_t fixed_up_x = vqaddq_s32(x, fixup); |
| 401 | return vrshlq_s32(fixed_up_x, shift_vec); |
| 402 | } |
| 403 | |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 404 | inline int32x4_t rounding_divide_by_pow2(int32x4_t x, int exponent) |
| 405 | { |
| 406 | const int32x4_t shift_vec = vdupq_n_s32(-exponent); |
| 407 | const int32x4_t fixup = vshrq_n_s32(vandq_s32(x, shift_vec), 31); |
| 408 | const int32x4_t fixed_up_x = vqaddq_s32(x, fixup); |
| 409 | return vrshlq_s32(fixed_up_x, shift_vec); |
| 410 | } |
| 411 | |
| 412 | inline int32_t rounding_divide_by_pow2(int32_t x, int exponent) |
| 413 | { |
| 414 | const int32_t mask = (1 << exponent) - 1; |
| 415 | const int32_t threshold = (mask >> 1) + (x < 0 ? 1 : 0); |
| 416 | return (x >> exponent) + ((x & mask) > threshold ? 1 : 0); |
| 417 | } |
| 418 | |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 419 | inline float32x4x4_t convert_uint8x16_to_float32x4x4(const uint8x16_t &in) |
| 420 | { |
| 421 | float32x4x4_t out; |
| 422 | |
| 423 | const auto tmp1 = vmovl_u8(vget_low_u8(in)); |
| 424 | out.val[0] = vcvtq_f32_u32(vmovl_u16(vget_low_u16(tmp1))); |
| 425 | out.val[1] = vcvtq_f32_u32(vmovl_u16(vget_high_u16(tmp1))); |
| 426 | |
| 427 | const auto tmp2 = vmovl_u8(vget_high_u8(in)); |
| 428 | out.val[2] = vcvtq_f32_u32(vmovl_u16(vget_low_u16(tmp2))); |
| 429 | out.val[3] = vcvtq_f32_u32(vmovl_u16(vget_high_u16(tmp2))); |
| 430 | return out; |
| 431 | } |
| 432 | |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 433 | inline float32x4x4_t convert_int8x16_to_float32x4x4(const int8x16_t &in) |
| 434 | { |
| 435 | float32x4x4_t out; |
| 436 | |
| 437 | const auto tmp1 = vmovl_s8(vget_low_s8(in)); |
| 438 | out.val[0] = vcvtq_f32_s32(vmovl_s16(vget_low_s16(tmp1))); |
| 439 | out.val[1] = vcvtq_f32_s32(vmovl_s16(vget_high_s16(tmp1))); |
| 440 | |
| 441 | const auto tmp2 = vmovl_s8(vget_high_s8(in)); |
| 442 | out.val[2] = vcvtq_f32_s32(vmovl_s16(vget_low_s16(tmp2))); |
| 443 | out.val[3] = vcvtq_f32_s32(vmovl_s16(vget_high_s16(tmp2))); |
| 444 | return out; |
| 445 | } |
| 446 | |
Manuel Bottini | 4370cff | 2020-02-07 16:31:59 +0000 | [diff] [blame] | 447 | template <> |
| 448 | inline float32x4x4_t convert_to_float32x4x4(const uint8x16_t &in) |
| 449 | { |
| 450 | return convert_uint8x16_to_float32x4x4(in); |
| 451 | } |
| 452 | |
| 453 | template <> |
| 454 | inline float32x4x4_t convert_to_float32x4x4(const int8x16_t &in) |
| 455 | { |
| 456 | return convert_int8x16_to_float32x4x4(in); |
| 457 | } |
| 458 | |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 459 | inline void convert_float32x4x3_to_uint8x8x3(const float32x4x3_t &in1, const float32x4x3_t &in2, uint8x8x3_t &out) |
| 460 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 461 | out.val[0] = vqmovn_u16(vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in1.val[0])), vqmovn_u32(vcvtq_u32_f32(in2.val[0])))); |
| 462 | out.val[1] = vqmovn_u16(vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in1.val[1])), vqmovn_u32(vcvtq_u32_f32(in2.val[1])))); |
| 463 | out.val[2] = vqmovn_u16(vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in1.val[2])), vqmovn_u32(vcvtq_u32_f32(in2.val[2])))); |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 466 | 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] | 467 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 468 | const auto low = vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in.val[0])), vqmovn_u32(vcvtq_u32_f32(in.val[1]))); |
| 469 | const auto high = vcombine_u16(vqmovn_u32(vcvtq_u32_f32(in.val[2])), vqmovn_u32(vcvtq_u32_f32(in.val[3]))); |
| 470 | out = vcombine_u8(vqmovn_u16(low), vqmovn_u16(high)); |
Manuel Bottini | 21079dd | 2019-10-29 17:20:09 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 473 | inline void convert_float32x4x4_to_int8x16(const float32x4x4_t &in, int8x16_t &out) |
| 474 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 475 | const auto low = vcombine_s16(vqmovn_s32(vcvtq_s32_f32(in.val[0])), vqmovn_s32(vcvtq_s32_f32(in.val[1]))); |
| 476 | const auto high = vcombine_s16(vqmovn_s32(vcvtq_s32_f32(in.val[2])), vqmovn_s32(vcvtq_s32_f32(in.val[3]))); |
| 477 | out = vcombine_s8(vqmovn_s16(low), vqmovn_s16(high)); |
Sang-Hoon Park | c3a7420 | 2019-11-22 16:05:46 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Sang-Hoon Park | dcf3c7e | 2021-03-04 17:03:46 +0000 | [diff] [blame] | 480 | template <> |
| 481 | inline uint8x16_t convert_float_to_int<float32x4x4_t, uint8x16_t>(const float32x4x4_t &in) |
| 482 | { |
| 483 | uint8x16_t out; |
| 484 | convert_float32x4x4_to_uint8x16(in, out); |
| 485 | return out; |
| 486 | } |
| 487 | |
| 488 | template <> |
| 489 | inline float32x4x4_t convert_int_to_float<float32x4x4_t, uint8x16_t>(const uint8x16_t &in) |
| 490 | { |
| 491 | return convert_uint8x16_to_float32x4x4(in); |
| 492 | } |
| 493 | |
| 494 | template <> |
| 495 | inline int8x16_t convert_float_to_int<float32x4x4_t, int8x16_t>(const float32x4x4_t &in) |
| 496 | { |
| 497 | int8x16_t out; |
| 498 | convert_float32x4x4_to_int8x16(in, out); |
| 499 | return out; |
| 500 | } |
| 501 | |
| 502 | template <> |
| 503 | inline float32x4x4_t convert_int_to_float<float32x4x4_t, int8x16_t>(const int8x16_t &in) |
| 504 | { |
| 505 | return convert_int8x16_to_float32x4x4(in); |
| 506 | } |
| 507 | |
Sheri Zhang | 5dda217 | 2021-10-15 19:54:17 +0100 | [diff] [blame] | 508 | inline float vreduce(const float32x4_t &v) |
| 509 | { |
| 510 | const float32x2_t v0 = vget_high_f32(v); |
| 511 | const float32x2_t v1 = vget_low_f32(v); |
| 512 | const float32x2_t v_out = vadd_f32(v0, v1); |
| 513 | |
| 514 | const float a = vget_lane_f32(v_out, 0); |
| 515 | const float b = vget_lane_f32(v_out, 1); |
| 516 | |
| 517 | return a + b; |
| 518 | } |
| 519 | |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 520 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 521 | /** Exponent polynomial coefficients */ |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 522 | /** Logarithm polynomial coefficients */ |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 523 | #ifndef DOXYGEN_SKIP_THIS |
Georgios Pinitas | 565bf2d | 2018-08-31 11:46:49 +0100 | [diff] [blame] | 524 | inline float16x8_t vfloorq_f16(float16x8_t val) |
| 525 | { |
| 526 | static const float16x8_t CONST_1 = vdupq_n_f16(1.f); |
| 527 | |
| 528 | const int16x8_t z = vcvtq_s16_f16(val); |
| 529 | const float16x8_t r = vcvtq_f16_s16(z); |
| 530 | |
| 531 | return vbslq_f16(vcgtq_f16(r, val), vsubq_f16(r, CONST_1), r); |
| 532 | } |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 533 | |
| 534 | inline float16x8_t vroundq_rte_f16(float16x8_t val) |
| 535 | { |
| 536 | return vrndnq_f16(val); |
| 537 | } |
| 538 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 539 | inline float16x4_t vinvsqrt_f16(float16x4_t x) |
| 540 | { |
| 541 | float16x4_t sqrt_reciprocal = vrsqrte_f16(x); |
| 542 | sqrt_reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 543 | sqrt_reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 544 | return sqrt_reciprocal; |
| 545 | } |
| 546 | |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 547 | inline float16x8_t vinvsqrtq_f16(float16x8_t x) |
| 548 | { |
| 549 | float16x8_t sqrt_reciprocal = vrsqrteq_f16(x); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 550 | sqrt_reciprocal = vmulq_f16(vrsqrtsq_f16(vmulq_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 551 | 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] | 552 | return sqrt_reciprocal; |
| 553 | } |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 554 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 555 | inline float16x4_t vinv_f16(float16x4_t x) |
| 556 | { |
| 557 | float16x4_t recip = vrecpe_f16(x); |
| 558 | recip = vmul_f16(vrecps_f16(x, recip), recip); |
| 559 | recip = vmul_f16(vrecps_f16(x, recip), recip); |
| 560 | return recip; |
| 561 | } |
| 562 | |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 563 | inline float16x8_t vinvq_f16(float16x8_t x) |
| 564 | { |
| 565 | float16x8_t recip = vrecpeq_f16(x); |
| 566 | recip = vmulq_f16(vrecpsq_f16(x, recip), recip); |
| 567 | recip = vmulq_f16(vrecpsq_f16(x, recip), recip); |
| 568 | return recip; |
| 569 | } |
| 570 | |
Jonathan Deakin | 2bc8cfe | 2022-10-13 10:50:25 +0000 | [diff] [blame] | 571 | inline float16x4_t vtanh_rational_approx_f16(float16x4_t x16) |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 572 | { |
Jonathan Deakin | 2bc8cfe | 2022-10-13 10:50:25 +0000 | [diff] [blame] | 573 | // Calculate rational approximation part of tanh exactly on a half-register of F16 by using F32s |
| 574 | // Note: doesn't handle overflows, needs truncating at |x| = 4.508 |
| 575 | const float32x4_t x = vcvt_f32_f16(x16); |
| 576 | |
| 577 | const float32x4_t ONE = vdupq_n_f32(1.0f); |
Ramy Elgammal | 7fefac7 | 2023-04-20 12:32:03 +0100 | [diff] [blame] | 578 | const float32x4_t C1 = vdupq_n_f32(0.43760237f); |
| 579 | const float32x4_t C2 = vdupq_n_f32(0.104402f); |
| 580 | const float32x4_t C3 = vdupq_n_f32(0.013442706f); |
| 581 | const float32x4_t C4 = vdupq_n_f32(0.00073561433f); |
Jonathan Deakin | 2bc8cfe | 2022-10-13 10:50:25 +0000 | [diff] [blame] | 582 | |
Ramy Elgammal | 7fefac7 | 2023-04-20 12:32:03 +0100 | [diff] [blame] | 583 | const float32x4_t x2 = vmulq_f32(x, x); |
Jonathan Deakin | 2bc8cfe | 2022-10-13 10:50:25 +0000 | [diff] [blame] | 584 | |
| 585 | // Denominator polynomial 1 + C1*x^2 + C3*x^4 |
| 586 | float32x4_t denom = vfmaq_f32(C1, C3, x2); |
Ramy Elgammal | 7fefac7 | 2023-04-20 12:32:03 +0100 | [diff] [blame] | 587 | denom = vfmaq_f32(ONE, x2, denom); |
Jonathan Deakin | 2bc8cfe | 2022-10-13 10:50:25 +0000 | [diff] [blame] | 588 | |
| 589 | // Numerator polynomial x*(1 + C2*x^2 + C4*x^4) |
| 590 | float32x4_t numer = vfmaq_f32(C2, C4, x2); |
Ramy Elgammal | 7fefac7 | 2023-04-20 12:32:03 +0100 | [diff] [blame] | 591 | numer = vfmaq_f32(ONE, x2, numer); |
| 592 | numer = vmulq_f32(numer, x); |
Jonathan Deakin | 2bc8cfe | 2022-10-13 10:50:25 +0000 | [diff] [blame] | 593 | |
| 594 | return vcvt_f16_f32(vdivq_f32(numer, denom)); |
| 595 | } |
| 596 | |
| 597 | inline float16x8_t vtanhq_f16(float16x8_t x) |
| 598 | { |
| 599 | // Split into high/low and use rational approximation on both parts exactly |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 600 | const float16x8_t tanh = |
| 601 | vcombine_f16(vtanh_rational_approx_f16(vget_low_f16(x)), vtanh_rational_approx_f16(vget_high_f16(x))); |
Jonathan Deakin | 2bc8cfe | 2022-10-13 10:50:25 +0000 | [diff] [blame] | 602 | |
| 603 | // tanh(x) == sign(x) to F16 precision for |x| >= 4.508, use sign after this |
Ramy Elgammal | 7fefac7 | 2023-04-20 12:32:03 +0100 | [diff] [blame] | 604 | const float16x8_t ONE = vdupq_n_f16(1.0f); |
| 605 | const float16x8_t MAX_X = vdupq_n_f16(4.508f); |
| 606 | const auto at_limit = vcageq_f16(x, MAX_X); // |x| >= 4.508 |
| 607 | const float16x8_t sign_x = vbslq_f16(vclezq_f16(x), -ONE, ONE); |
Jonathan Deakin | 2bc8cfe | 2022-10-13 10:50:25 +0000 | [diff] [blame] | 608 | return vbslq_f16(at_limit, sign_x, tanh); |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 609 | } |
| 610 | |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 611 | inline float16x8_t vtaylor_polyq_f16(float16x8_t x, const std::array<float16x8_t, 8> &coeffs) |
| 612 | { |
| 613 | const float16x8_t A = vaddq_f16(coeffs[0], vmulq_f16(coeffs[4], x)); |
| 614 | const float16x8_t B = vaddq_f16(coeffs[2], vmulq_f16(coeffs[6], x)); |
| 615 | const float16x8_t C = vaddq_f16(coeffs[1], vmulq_f16(coeffs[5], x)); |
| 616 | const float16x8_t D = vaddq_f16(coeffs[3], vmulq_f16(coeffs[7], x)); |
| 617 | const float16x8_t x2 = vmulq_f16(x, x); |
| 618 | const float16x8_t x4 = vmulq_f16(x2, x2); |
| 619 | const float16x8_t res = vaddq_f16(vaddq_f16(A, vmulq_f16(B, x2)), vmulq_f16(vaddq_f16(C, vmulq_f16(D, x2)), x4)); |
| 620 | return res; |
| 621 | } |
| 622 | |
| 623 | inline float16x8_t vexpq_f16(float16x8_t x) |
| 624 | { |
Michele Di Giorgio | 1c948d4 | 2018-11-20 16:03:01 +0000 | [diff] [blame] | 625 | const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x)); |
| 626 | const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x)); |
Anthony Barbier | 3a6163e | 2018-08-10 17:36:36 +0100 | [diff] [blame] | 627 | |
Georgios Pinitas | f2cdce3 | 2019-12-09 18:35:57 +0000 | [diff] [blame] | 628 | 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] | 629 | return res; |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 630 | } |
| 631 | |
Murray Kornelsen | 926f502 | 2022-07-13 21:22:39 -0400 | [diff] [blame] | 632 | #ifdef __aarch64__ |
| 633 | inline float16x8_t verfq_f16(float16x8_t x) |
| 634 | { |
| 635 | const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x)); |
| 636 | const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x)); |
| 637 | |
| 638 | const float16x8_t res = vcombine_f16(vcvt_f16_f32(verfq_f32(x_low)), vcvt_f16_f32(verfq_f32(x_high))); |
| 639 | return res; |
| 640 | } |
| 641 | #endif // #ifdef __aarch64__ |
| 642 | |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 643 | inline float16x8_t vlogq_f16(float16x8_t x) |
| 644 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 645 | const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x)); |
| 646 | const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x)); |
Anthony Barbier | 3a6163e | 2018-08-10 17:36:36 +0100 | [diff] [blame] | 647 | |
Georgios Pinitas | f2cdce3 | 2019-12-09 18:35:57 +0000 | [diff] [blame] | 648 | 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] | 649 | return res; |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | inline float16x8_t vpowq_f16(float16x8_t val, float16x8_t n) |
| 653 | { |
Gian Marco Iodice | f2cde9b | 2018-08-23 15:29:16 +0100 | [diff] [blame] | 654 | float32x4_t n0_f32 = vcvt_f32_f16(vget_low_f16(n)); |
| 655 | float32x4_t n1_f32 = vcvt_f32_f16(vget_high_f16(n)); |
| 656 | float32x4_t val0_f32 = vcvt_f32_f16(vget_low_f16(val)); |
| 657 | float32x4_t val1_f32 = vcvt_f32_f16(vget_high_f16(val)); |
| 658 | |
| 659 | float32x4_t res0_f32 = vexpq_f32(vmulq_f32(n0_f32, vlogq_f32(val0_f32))); |
| 660 | float32x4_t res1_f32 = vexpq_f32(vmulq_f32(n1_f32, vlogq_f32(val1_f32))); |
| 661 | |
| 662 | 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] | 663 | } |
Manuel Bottini | ed75326 | 2019-05-15 15:30:47 +0100 | [diff] [blame] | 664 | |
| 665 | inline float16x8_t vsinq_f16(float16x8_t val) |
| 666 | { |
| 667 | const float32x4_t val_high = vcvt_f32_f16(vget_high_f16(val)); |
| 668 | const float32x4_t val_low = vcvt_f32_f16(vget_low_f16(val)); |
| 669 | |
| 670 | const float32x4_t res_high = vsinq_f32(val_high); |
| 671 | const float32x4_t res_low = vsinq_f32(val_low); |
| 672 | |
| 673 | return vcombine_f16(vcvt_f16_f32(res_low), vcvt_f16_f32(res_high)); |
| 674 | } |
| 675 | |
| 676 | inline float16x4_t vsin_f16(float16x4_t val) |
| 677 | { |
| 678 | const float32x4_t val_f32 = vcvt_f32_f16(val); |
| 679 | const float32x2_t val_high = vget_high_f32(val_f32); |
| 680 | const float32x2_t val_low = vget_low_f32(val_f32); |
| 681 | |
| 682 | const float32x2_t res_high = vsin_f32(val_high); |
| 683 | const float32x2_t res_low = vsin_f32(val_low); |
| 684 | |
| 685 | return vcvt_f16_f32(vcombine_f32(res_low, res_high)); |
| 686 | } |
| 687 | |
Sheri Zhang | 5dda217 | 2021-10-15 19:54:17 +0100 | [diff] [blame] | 688 | inline float16_t vreduce(const float16x8_t &v) |
| 689 | { |
| 690 | const float16x4_t v0 = vget_high_f16(v); |
| 691 | const float16x4_t v1 = vget_low_f16(v); |
| 692 | const float16x4_t v_out = vadd_f16(v0, v1); |
| 693 | |
| 694 | const float16_t a = vget_lane_f16(v_out, 0); |
| 695 | const float16_t b = vget_lane_f16(v_out, 1); |
| 696 | const float16_t c = vget_lane_f16(v_out, 2); |
| 697 | const float16_t d = vget_lane_f16(v_out, 3); |
| 698 | |
| 699 | return a + b + c + d; |
| 700 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 701 | #endif /* DOXYGEN_SKIP_THIS */ |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 702 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Gian Marco Iodice | 356f643 | 2017-09-22 11:32:21 +0100 | [diff] [blame] | 703 | } // namespace arm_compute |