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