Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, 2017 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | namespace arm_compute |
| 26 | { |
| 27 | /* Exponent polynomial coefficients */ |
| 28 | const std::array<float32x4_t, 8> exp_tab = |
| 29 | { |
| 30 | { |
| 31 | vdupq_n_f32(1.f), |
| 32 | vdupq_n_f32(0.0416598916054f), |
| 33 | vdupq_n_f32(0.500000596046f), |
| 34 | vdupq_n_f32(0.0014122662833f), |
| 35 | vdupq_n_f32(1.00000011921f), |
| 36 | vdupq_n_f32(0.00833693705499f), |
| 37 | vdupq_n_f32(0.166665703058f), |
| 38 | vdupq_n_f32(0.000195780929062f), |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | /* Logarithm polynomial coefficients */ |
| 43 | const std::array<float32x4_t, 8> log_tab = |
| 44 | { |
| 45 | { |
| 46 | vdupq_n_f32(-2.29561495781f), |
| 47 | vdupq_n_f32(-2.47071170807f), |
| 48 | vdupq_n_f32(-5.68692588806f), |
| 49 | vdupq_n_f32(-0.165253549814f), |
| 50 | vdupq_n_f32(5.17591238022f), |
| 51 | vdupq_n_f32(0.844007015228f), |
| 52 | vdupq_n_f32(4.58445882797f), |
| 53 | vdupq_n_f32(0.0141278216615f), |
| 54 | } |
| 55 | }; |
| 56 | |
Georgios Pinitas | d8e765b | 2017-08-02 13:44:33 +0100 | [diff] [blame] | 57 | inline float32x4_t vfloorq_f32(float32x4_t val) |
| 58 | { |
| 59 | static const float32x4_t CONST_1 = vdupq_n_f32(1.f); |
| 60 | |
| 61 | const int32x4_t z = vcvtq_s32_f32(val); |
| 62 | const float32x4_t r = vcvtq_f32_s32(z); |
| 63 | |
| 64 | return vbslq_f32(vcgtq_f32(r, val), vsubq_f32(r, CONST_1), r); |
| 65 | } |
| 66 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 67 | inline float32x2_t vinvsqrt_f32(float32x2_t x) |
| 68 | { |
| 69 | float32x2_t sqrt_reciprocal = vrsqrte_f32(x); |
| 70 | sqrt_reciprocal = vmul_f32(vrsqrts_f32(vmul_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 71 | sqrt_reciprocal = vmul_f32(vrsqrts_f32(vmul_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 72 | |
| 73 | return sqrt_reciprocal; |
| 74 | } |
| 75 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | inline float32x4_t vinvsqrtq_f32(float32x4_t x) |
| 77 | { |
| 78 | float32x4_t sqrt_reciprocal = vrsqrteq_f32(x); |
| 79 | sqrt_reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 80 | sqrt_reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 81 | |
| 82 | return sqrt_reciprocal; |
| 83 | } |
| 84 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 85 | inline float32x2_t vinv_f32(float32x2_t x) |
| 86 | { |
| 87 | float32x2_t recip = vrecpe_f32(x); |
| 88 | recip = vmul_f32(vrecps_f32(x, recip), recip); |
| 89 | recip = vmul_f32(vrecps_f32(x, recip), recip); |
| 90 | return recip; |
| 91 | } |
| 92 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | inline float32x4_t vinvq_f32(float32x4_t x) |
| 94 | { |
| 95 | float32x4_t recip = vrecpeq_f32(x); |
| 96 | recip = vmulq_f32(vrecpsq_f32(x, recip), recip); |
| 97 | recip = vmulq_f32(vrecpsq_f32(x, recip), recip); |
| 98 | return recip; |
| 99 | } |
| 100 | |
| 101 | inline float32x4_t vtaylor_polyq_f32(float32x4_t x, const std::array<float32x4_t, 8> &coeffs) |
| 102 | { |
| 103 | float32x4_t A = vmlaq_f32(coeffs[0], coeffs[4], x); |
| 104 | float32x4_t B = vmlaq_f32(coeffs[2], coeffs[6], x); |
| 105 | float32x4_t C = vmlaq_f32(coeffs[1], coeffs[5], x); |
| 106 | float32x4_t D = vmlaq_f32(coeffs[3], coeffs[7], x); |
| 107 | float32x4_t x2 = vmulq_f32(x, x); |
| 108 | float32x4_t x4 = vmulq_f32(x2, x2); |
| 109 | float32x4_t res = vmlaq_f32(vmlaq_f32(A, B, x2), vmlaq_f32(C, D, x2), x4); |
| 110 | return res; |
| 111 | } |
| 112 | |
| 113 | inline float32x4_t vexpq_f32(float32x4_t x) |
| 114 | { |
Georgios Pinitas | ee12254 | 2017-06-26 15:54:06 +0100 | [diff] [blame] | 115 | static const float32x4_t CONST_LN2 = vdupq_n_f32(0.6931471805f); // ln(2) |
| 116 | static const float32x4_t CONST_INV_LN2 = vdupq_n_f32(1.4426950408f); // 1/ln(2) |
| 117 | static const float32x4_t CONST_0 = vdupq_n_f32(0.f); |
| 118 | static const int32x4_t CONST_NEGATIVE_126 = vdupq_n_s32(-126); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 119 | |
| 120 | // Perform range reduction [-log(2),log(2)] |
| 121 | int32x4_t m = vcvtq_s32_f32(vmulq_f32(x, CONST_INV_LN2)); |
| 122 | float32x4_t val = vmlsq_f32(x, vcvtq_f32_s32(m), CONST_LN2); |
| 123 | |
| 124 | // Polynomial Approximation |
| 125 | float32x4_t poly = vtaylor_polyq_f32(val, exp_tab); |
| 126 | |
| 127 | // Reconstruct |
Georgios Pinitas | ee12254 | 2017-06-26 15:54:06 +0100 | [diff] [blame] | 128 | poly = vreinterpretq_f32_s32(vqaddq_s32(vreinterpretq_s32_f32(poly), vqshlq_n_s32(m, 23))); |
| 129 | poly = vbslq_f32(vcltq_s32(m, CONST_NEGATIVE_126), CONST_0, poly); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 130 | |
| 131 | return poly; |
| 132 | } |
| 133 | |
| 134 | inline float32x4_t vlogq_f32(float32x4_t x) |
| 135 | { |
| 136 | static const int32x4_t CONST_127 = vdupq_n_s32(127); // 127 |
| 137 | static const float32x4_t CONST_LN2 = vdupq_n_f32(0.6931471805f); // ln(2) |
| 138 | |
| 139 | // Extract exponent |
| 140 | int32x4_t m = vsubq_s32(vreinterpretq_s32_u32(vshrq_n_u32(vreinterpretq_u32_f32(x), 23)), CONST_127); |
| 141 | float32x4_t val = vreinterpretq_f32_s32(vsubq_s32(vreinterpretq_s32_f32(x), vshlq_n_s32(m, 23))); |
| 142 | |
| 143 | // Polynomial Approximation |
| 144 | float32x4_t poly = vtaylor_polyq_f32(val, log_tab); |
| 145 | |
| 146 | // Reconstruct |
| 147 | poly = vmlaq_f32(poly, vcvtq_f32_s32(m), CONST_LN2); |
| 148 | |
| 149 | return poly; |
| 150 | } |
| 151 | |
| 152 | inline float32x4_t vtanhq_f32(float32x4_t val) |
| 153 | { |
| 154 | static const float32x4_t CONST_1 = vdupq_n_f32(1.f); |
| 155 | static const float32x4_t CONST_2 = vdupq_n_f32(2.f); |
| 156 | static const float32x4_t CONST_MIN_TANH = vdupq_n_f32(-10.f); |
| 157 | static const float32x4_t CONST_MAX_TANH = vdupq_n_f32(10.f); |
| 158 | |
| 159 | float32x4_t x = vminq_f32(vmaxq_f32(val, CONST_MIN_TANH), CONST_MAX_TANH); |
| 160 | float32x4_t exp2x = vexpq_f32(vmulq_f32(CONST_2, x)); |
| 161 | float32x4_t num = vsubq_f32(exp2x, CONST_1); |
| 162 | float32x4_t den = vaddq_f32(exp2x, CONST_1); |
| 163 | float32x4_t tanh = vmulq_f32(num, vinvq_f32(den)); |
| 164 | return tanh; |
| 165 | } |
| 166 | |
| 167 | inline float32x4_t vpowq_f32(float32x4_t val, float32x4_t n) |
| 168 | { |
| 169 | return vexpq_f32(vmulq_f32(n, vlogq_f32(val))); |
| 170 | } |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 171 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 172 | /* Exponent polynomial coefficients */ |
| 173 | const std::array<float16x8_t, 8> exp_tab_f16 = |
| 174 | { |
| 175 | { |
| 176 | vdupq_n_f16(1.f), |
| 177 | vdupq_n_f16(0.0416598916054f), |
| 178 | vdupq_n_f16(0.500000596046f), |
| 179 | vdupq_n_f16(0.0014122662833f), |
| 180 | vdupq_n_f16(1.00000011921f), |
| 181 | vdupq_n_f16(0.00833693705499f), |
| 182 | vdupq_n_f16(0.166665703058f), |
| 183 | vdupq_n_f16(0.000195780929062f), |
| 184 | } |
| 185 | }; |
| 186 | |
| 187 | /* Logarithm polynomial coefficients */ |
| 188 | const std::array<float16x8_t, 8> log_tab_f16 = |
| 189 | { |
| 190 | { |
| 191 | vdupq_n_f16(-2.29561495781f), |
| 192 | vdupq_n_f16(-2.47071170807f), |
| 193 | vdupq_n_f16(-5.68692588806f), |
| 194 | vdupq_n_f16(-0.165253549814f), |
| 195 | vdupq_n_f16(5.17591238022f), |
| 196 | vdupq_n_f16(0.844007015228f), |
| 197 | vdupq_n_f16(4.58445882797f), |
| 198 | vdupq_n_f16(0.0141278216615f), |
| 199 | } |
| 200 | }; |
Pablo Tello | 8fda1cb | 2017-07-05 15:20:38 +0100 | [diff] [blame] | 201 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 202 | inline float16x4_t vinvsqrt_f16(float16x4_t x) |
| 203 | { |
| 204 | float16x4_t sqrt_reciprocal = vrsqrte_f16(x); |
| 205 | sqrt_reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 206 | sqrt_reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 207 | return sqrt_reciprocal; |
| 208 | } |
| 209 | |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 210 | inline float16x8_t vinvsqrtq_f16(float16x8_t x) |
| 211 | { |
| 212 | float16x8_t sqrt_reciprocal = vrsqrteq_f16(x); |
| 213 | sqrt_reciprocal = vmulq_f16(vrsqrtsq_f16(vmulq_f16(x, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal); |
| 214 | 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] | 215 | return sqrt_reciprocal; |
| 216 | } |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 217 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 218 | inline float16x4_t vinv_f16(float16x4_t x) |
| 219 | { |
| 220 | float16x4_t recip = vrecpe_f16(x); |
| 221 | recip = vmul_f16(vrecps_f16(x, recip), recip); |
| 222 | recip = vmul_f16(vrecps_f16(x, recip), recip); |
| 223 | return recip; |
| 224 | } |
| 225 | |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 226 | inline float16x8_t vinvq_f16(float16x8_t x) |
| 227 | { |
| 228 | float16x8_t recip = vrecpeq_f16(x); |
| 229 | recip = vmulq_f16(vrecpsq_f16(x, recip), recip); |
| 230 | recip = vmulq_f16(vrecpsq_f16(x, recip), recip); |
| 231 | return recip; |
| 232 | } |
| 233 | |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 234 | inline float16x8_t vtanhq_f16(float16x8_t val) |
| 235 | { |
| 236 | const float16x8_t CONST_1 = vdupq_n_f16(1.f); |
| 237 | const float16x8_t CONST_2 = vdupq_n_f16(2.f); |
| 238 | const float16x8_t CONST_MIN_TANH = vdupq_n_f16(-10.f); |
| 239 | const float16x8_t CONST_MAX_TANH = vdupq_n_f16(10.f); |
| 240 | |
| 241 | const float16x8_t x = vminq_f16(vmaxq_f16(val, CONST_MIN_TANH), CONST_MAX_TANH); |
| 242 | const float16x8_t exp2x = vexpq_f16(vmulq_f16(CONST_2, x)); |
| 243 | const float16x8_t num = vsubq_f16(exp2x, CONST_1); |
| 244 | const float16x8_t den = vaddq_f16(exp2x, CONST_1); |
| 245 | const float16x8_t tanh = vmulq_f16(num, vinvq_f16(den)); |
| 246 | return tanh; |
| 247 | } |
| 248 | |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 249 | inline float16x8_t vtaylor_polyq_f16(float16x8_t x, const std::array<float16x8_t, 8> &coeffs) |
| 250 | { |
| 251 | const float16x8_t A = vaddq_f16(coeffs[0], vmulq_f16(coeffs[4], x)); |
| 252 | const float16x8_t B = vaddq_f16(coeffs[2], vmulq_f16(coeffs[6], x)); |
| 253 | const float16x8_t C = vaddq_f16(coeffs[1], vmulq_f16(coeffs[5], x)); |
| 254 | const float16x8_t D = vaddq_f16(coeffs[3], vmulq_f16(coeffs[7], x)); |
| 255 | const float16x8_t x2 = vmulq_f16(x, x); |
| 256 | const float16x8_t x4 = vmulq_f16(x2, x2); |
| 257 | const float16x8_t res = vaddq_f16(vaddq_f16(A, vmulq_f16(B, x2)), vmulq_f16(vaddq_f16(C, vmulq_f16(D, x2)), x4)); |
| 258 | return res; |
| 259 | } |
| 260 | |
| 261 | inline float16x8_t vexpq_f16(float16x8_t x) |
| 262 | { |
| 263 | static const float16x8_t CONST_LN2 = vdupq_n_f16(0.6931471805f); // ln(2) |
| 264 | static const float16x8_t CONST_INV_LN2 = vdupq_n_f16(1.4426950408f); // 1/ln(2) |
| 265 | static const float16x8_t CONST_0 = vdupq_n_f16(0.f); |
| 266 | static const int16x8_t CONST_NEGATIVE_126 = vdupq_n_s16(-126); |
| 267 | |
| 268 | // Perform range reduction [-log(2),log(2)] |
| 269 | const int16x8_t m = vcvtq_s16_f16(vmulq_f16(x, CONST_INV_LN2)); |
| 270 | const float16x8_t val = vsubq_f16(x, vmulq_f16(vcvtq_f16_s16(m), CONST_LN2)); |
| 271 | |
| 272 | // Polynomial Approximation |
| 273 | float16x8_t poly = vtaylor_polyq_f16(val, exp_tab_f16); |
| 274 | |
| 275 | // Reconstruct |
| 276 | poly = vreinterpretq_f16_s16(vqaddq_s16(vreinterpretq_s16_f16(poly), vqshlq_n_s16(m, 9))); |
| 277 | poly = vbslq_f16(vcltq_s16(m, CONST_NEGATIVE_126), CONST_0, poly); |
| 278 | |
| 279 | return poly; |
| 280 | } |
| 281 | |
| 282 | inline float16x8_t vlogq_f16(float16x8_t x) |
| 283 | { |
| 284 | static const int16x8_t CONST_127 = vdupq_n_s16(127); // 127 |
| 285 | static const float16x8_t CONST_LN2 = vdupq_n_f16(0.6931471805f); // ln(2) |
| 286 | |
| 287 | // Extract exponent |
| 288 | const int16x8_t m = vsubq_s16(vreinterpretq_s16_u16(vshrq_n_u16(vreinterpretq_u16_f16(x), 9)), CONST_127); |
| 289 | const float16x8_t val = vreinterpretq_f16_s16(vsubq_s16(vreinterpretq_s16_f16(x), vshlq_n_s16(m, 9))); |
| 290 | |
| 291 | // Polynomial Approximation |
| 292 | float16x8_t poly = vtaylor_polyq_f16(val, log_tab_f16); |
| 293 | |
| 294 | // Reconstruct |
| 295 | poly = vaddq_f16(poly, vmulq_f16(vcvtq_f16_s16(m), CONST_LN2)); |
| 296 | |
| 297 | return poly; |
| 298 | } |
| 299 | |
| 300 | inline float16x8_t vpowq_f16(float16x8_t val, float16x8_t n) |
| 301 | { |
| 302 | return vexpq_f16(vmulq_f16(n, vlogq_f16(val))); |
| 303 | } |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 304 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Gian Marco Iodice | 356f643 | 2017-09-22 11:32:21 +0100 | [diff] [blame] | 305 | } // namespace arm_compute |